diff options
-rw-r--r-- | badroff.c | 58 |
1 files changed, 21 insertions, 37 deletions
@@ -1,8 +1,9 @@ #include <stdio.h> -#include "buf.h" #include <string.h> #include <stdlib.h> +#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<len-start; i++){ - if (str[i] == '\n') return start+i; - else if (str[i] != ' ') return start+i-1; +char* wordset(char* txt){ + char* orig = txt; + size_t len = strlen(txt); + size_t ws = width; + for (size_t i=0; i<len; i++){ + if (txt[i] == ' ') ws = i; + if (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; |