diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 14:08:36 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 14:08:36 -0400 |
commit | 778338e39c6abf799237b3fd56fe1f20c8d3eaad (patch) | |
tree | d448cc497a9d7bf383b2d2c9be53e2b87dd6a9fc | |
parent | 4ab00506b3840d073b127733ab871e6a8bcbaec6 (diff) |
cmd and typeset framework added
-rw-r--r-- | badroff.c | 62 |
1 files changed, 60 insertions, 2 deletions
@@ -1,5 +1,11 @@ #include <stdio.h> #include "sb.h" +#include "buf.h" + +typedef enum { + false, + true +} bool; FILE* getfile(int argc, char** argv){ if (argc >= 2) @@ -9,15 +15,67 @@ FILE* getfile(int argc, char** argv){ } FILE* in; +buf* inbuf; + +int width; +bool brk; + + +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); + return set; +} + +size_t nlfill(){ + size_t line; + size_t len = buflen(inbuf); + char* str = peekstrbuf(inbuf, 0, len); + for (line = 0; line < len; line++) + if (str[line] == '\n') return line+1; + for (int c = 0; c != '\n' && (c = fgetc(in)) != EOF; line++) + inschrbuf(inbuf, c); + return line; +} + +void cmd(void){ + int n = nlfill(); + popstrbuf(inbuf, n); +} + +char* typeset(void){ + return popstrbuf(inbuf, nlfill()); +} + +char* line(void){ + size_t sz; + if ( (sz = fillbuf(2)) == 0) + return ""; + char* twobytes = peekstrbuf(inbuf, 0, 2); // .., .\n, or ^.? + if (sz == 1) return typeset(); + if (twobytes[0] == '.' && twobytes[1] == '\n') return typeset(); + if (twobytes[0] == '.') popchrbuf(inbuf); + if (twobytes[0] == '.' && twobytes[1] != '.'){ + cmd(); + return line(); + } else { + return typeset(); + } +} int main(int argc, char** argv){ int c; in = getfile(argc, argv); + inbuf = newbuf(256); if (in == NULL){ perror(argv[1]); return 1; } - while ( (c = fgetc(in)) != EOF){ - + char* out; + while ( (out = line())[0] != '\0'){ + printf("%s",out); } + fclose(in); } |