From 500d4ee1346a6a783e7a52f2fc703b742aa53079 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 30 Mar 2020 18:39:12 -0400 Subject: slight reorganization of badroff.c --- badroff.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/badroff.c b/badroff.c index 084fa53..adf3e41 100644 --- a/badroff.c +++ b/badroff.c @@ -15,12 +15,11 @@ FILE* getfile(int argc, char** argv){ return stdin; } +// file read & buffer fill helpers + FILE* in; buf* inbuf; -int width = 80; -bool brk = false; // not implemented - size_t fillbuf(size_t len){ // returns real len int c; size_t set; @@ -40,14 +39,18 @@ size_t chrfill(char chr){ return line; } -char* center(char* text){ - size_t len = strlen(text); +// typesetting config and commands to change. +int width = 80; +bool brk = false; // not implemented + +char* center(char* txt){ + size_t len = strlen(txt); int max = width/2 + len/2; // excluding terminator - int min = max-len+1; // first index of *text + int min = max-len+1; // first index of *txt char* str = malloc(sizeof(char)*(max + 1)); str[max] = 0; for (int i=0; i < min; i++) str[i] = ' '; - memcpy(str+min, text, len); + memcpy(str+min, txt, len); return str; } char* setwidth(char* txt){ @@ -67,18 +70,22 @@ char* cmd(void){ int cmp = strncmp(dat, mval, strlen(mval)); if (cmp < 0) // dat < mval - low = mid + 1; - else if (cmp > 0) high = mid - 1; + else if (cmp > 0) + low = mid + 1; else return call[mid](dat+strlen(mval)); } return NULL; } +// normal typesetting + char* typeset(void){ return popstrbuf(inbuf, chrfill('\n')); } +// a parser to choose when to typeset and when to run a command + char* line(void){ size_t sz; if ( (sz = fillbuf(2)) == 0) @@ -95,6 +102,8 @@ char* line(void){ } } +// orchestration + int main(int argc, char** argv){ int c; in = getfile(argc, argv); -- cgit