From 539e0bac89012848047e9d69aeee3d2138714928 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Wed, 1 Apr 2020 13:51:38 -0400 Subject: migrated typeset typeset() now relies on a one line at a time approach; the current version does have some bugs, though (poor handling of large spaces, EOL whitespace) --- badroff.c | 58 +++++++++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/badroff.c b/badroff.c index c28e153..2a961b5 100644 --- a/badroff.c +++ b/badroff.c @@ -1,8 +1,9 @@ #include -#include "buf.h" #include #include +#include "buf.h" + typedef enum { false, true @@ -130,44 +131,27 @@ char* cmd(void){ // normal typesetting -bool cont_typst = false; // communicates with line() for broken lines. - -size_t nextws(size_t start){//searches for \n or [^ \n]-1 in buffer - //starts at `start` (if not whitespace, returns start-1) - size_t len = buflen(inbuf); - char* str = peekstrbuf(inbuf, start, len-start); - size_t i; - for (i = 0; i width){ + txt += ws; + txt[0] = '\n'; + len -= ws; + ws = width; + i = 0; + } } - free(str); - for (int c = 0; c != '\n' && ( c = addchr() ) != EOF && c == ' '; - len++); // if this is a newline, exit late (len is last, !(len-1)). - return len; + + return orig; } char* typeset(void){ - size_t len = chrnfill('\n', width+1); //+1 for \n on 81-char lines - char* next = peekstrbuf(inbuf, 0, len); - size_t wsend = 0; - if (next[--len] == '\n'){ - wsend = len; - } else if (next[len] == ' '){ - wsend = nextws(len); - } - else for (wsend = len; next[wsend] != ' '; wsend--); - // wsend is last index with a piece of whitespace - for (len = min(wsend,len-1); next[len] == ' '; len--); - // check, within the string, behind the whitespace for a letter. - if (next[len] == '\n') len--; // "\n" shouldn't become "\n\n" - // len should be *index of* last letter - if (peekchrbuf(inbuf, wsend) == '\n') cont_typst = false; - else cont_typst = true; - free(next); - char* ln = popstrbuf(inbuf, wsend+1); - ln[len+1] = '\n'; ln[len+2] = 0; - return ln; + size_t len = chrfill('\n'); + return wordset(popstrbuf(inbuf, len)); } // a parser to choose when to typeset and when to run a command @@ -177,7 +161,7 @@ char* line(void){ if ( (sz = fillbuf(2)) == 0) return ""; char* twobytes = peekstrbuf(inbuf, 0, 2); // .., .\n, or ^.? - if (sz == 1 || twobytes[1] == '\n' || cont_typst) return typeset(); + if (sz == 1 || twobytes[1] == '\n') return typeset(); if (twobytes[0] == '.') popchrbuf(inbuf); if (twobytes[0] == '.' && twobytes[1] != '.'){ char* data = cmd(); @@ -201,7 +185,7 @@ int main(int argc, char** argv){ char* out; while ( (out = line())[0] != '\0'){ printf("%s",out); - free(out); + // free(out); } fclose(in); return 0; -- cgit