#include #include "sb.h" #include "buf.h" typedef enum { false, true } bool; FILE* getfile(int argc, char** argv){ if (argc >= 2) return fopen(argv[1], "r"); else return stdin; } 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; } char* out; while ( (out = line())[0] != '\0'){ printf("%s",out); } fclose(in); }