#include #include "buf.h" #include #include typedef enum { false, true } bool; FILE* getfile(int argc, char** argv){ if (argc >= 2) return fopen(argv[1], "r"); else 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; 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 = addchr()) != EOF;set++); return set; } size_t chrfill(char chr){ 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 = 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= low){ int mid = ((unsigned int)low + (unsigned int)high) >> 1; char* mval = cmds[mid]; int cmp = strncmp(dat, mval, strlen(mval)); if (cmp < 0) // dat < mval high = mid - 1; else if (cmp > 0) low = mid + 1; else return call[mid](dat+strlen(mval)); } free(dat); return NULL; } // normal typesetting bool cont_typst = false; // communicates with line() for broken lines. 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