diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-03-31 10:57:01 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-03-31 10:57:01 -0400 |
commit | 13af96cec2fb1461bc0680e4106d5132dce72b47 (patch) | |
tree | 5c0505267f435c6fcff9a91d222070593e3664e6 | |
parent | f995a0a66e665592b18cb053ee727093f0f5617b (diff) |
a partial attempt at typesetting
-rw-r--r-- | badroff.c | 41 |
1 files changed, 38 insertions, 3 deletions
@@ -15,6 +15,12 @@ FILE* getfile(int argc, char** argv){ return stdin; } +char* chrmult(char c, size_t ct){ + char* str = malloc(sizeof(char)*(ct+1)); str[ct] = 0; + memset(str, c, ct); + return str; +} + // file read & buffer fill helpers FILE* in; @@ -48,9 +54,9 @@ char* center(char* txt){ int max = width/2 + len/2; // excluding terminator 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] = ' '; + memset(str, ' ', min); memcpy(str+min, txt, len); + str[max] = 0; return str; } char* setwidth(char* txt){ @@ -80,8 +86,37 @@ char* cmd(void){ // 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; +} + +char* fold(char* line){ // inserts \n to limit line length + +} + char* typeset(void){ - return popstrbuf(inbuf, chrfill('\n')); + size_t len; size_t nl; + for (nl = 0; (len = chrfill('\n')) == 1; nl++) + popstrbuf(inbuf, len); + if (nl) return chrmult('\n', nl+1); + char* line = popstrbuf(inbuf, len); + size_t ws; + line = cleanup(line); + if (brk && line[len-1] == '\n') line[len-1] = ' '; + if (brk) for (; *line == ' '; line++); //remove preceding whitespace + return fold(line); } // a parser to choose when to typeset and when to run a command |