From f7c92afe680379a53124eba4297c0eaf1579d132 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Tue, 31 Mar 2020 14:18:55 -0400 Subject: fixed some memory leaks and created typeset() for !brk --- badroff.c | 96 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 37 deletions(-) diff --git a/badroff.c b/badroff.c index ed84760..c2c6515 100644 --- a/badroff.c +++ b/badroff.c @@ -26,22 +26,41 @@ char* chrmult(char c, size_t ct){ FILE* in; buf* inbuf; +int addchr(void){ + int c = fgetc(in); + if (c != EOF) inschrbuf(inbuf, c); + return c; +} + size_t fillbuf(size_t len){ // returns real len int c; size_t set; - for (set = buflen(inbuf); set < len && (c = fgetc(in)) != EOF;set++) - inschrbuf(inbuf, c); + for (set = buflen(inbuf); set < len && (c = addchr()) != EOF;set++); return set; } size_t chrfill(char chr){ - size_t line; size_t len = buflen(inbuf); char* str = peekstrbuf(inbuf, 0, len); + size_t line; for (line = 0; line < len; line++) if (str[line] == chr) return line+1; - for (int c = 0; c != chr && (c = fgetc(in)) != EOF; line++) - inschrbuf(inbuf, c); + for (int c = 0; c != chr && (c = addchr()) != EOF; line++); + return line; +} + +#define min(a,b) a < b ? a : b + +size_t chrnfill(char chr, size_t sz){//fills to first of chr or sz + size_t len = buflen(inbuf); + size_t lim = min(len,sz); + char* str = peekstrbuf(inbuf, 0, lim); + size_t line; + for (line = 0; line < lim; line++) + if (str[line] == chr) return line+1; + free(str); + for (int c = 0; c != chr && (c = addchr()) != EOF + && line 0) low = mid + 1; - else return call[mid](dat+strlen(mval)); + else + return call[mid](dat+strlen(mval)); } + free(dat); return NULL; } // normal typesetting -char* cleanup(char* line){ // cleans up trailing whitespace && ^\n$ - for (ws = len-1; line[ws] == ' '; ws--, len--); // rethink - if (ws < len-1){ - line[ws] = '\n' - line[ws+1] = '\0'; - } else if (line[len-1] != '\n'){ - len++; - line = realloc(line, len); - } - if (line[len-1] != '\n'){ - line[len-1] = '\n'; - line[len] = '\0'; - } - return line; -} +bool cont_typst = false; // communicates with line() for broken lines. -char* fold(char* line){ // inserts \n to limit line length - +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