From cb9206b461e19ce3864b646d8f56f7bdea429f0f Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Thu, 28 May 2020 23:49:31 -0400 Subject: made more robust against user input --- badroff.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/badroff.c b/badroff.c index 66fce40..8d574b2 100644 --- a/badroff.c +++ b/badroff.c @@ -66,6 +66,12 @@ size_t chrnfill(char chr, size_t sz){//fills to first of chr or sz return len; } +static void consumews(char* str) { + char* pos = str; + while (*pos == ' ') pos++; + memmove(str, pos, strlen(pos)+1); +} + // typesetting config and commands to change. static int width = 80; static char nbsp = 0; @@ -89,7 +95,7 @@ static char* center(char* txt){ return str; } static char* setwidth(char* txt){ - sscanf(txt, "%d", &width); + if (isdigit(*txt)) sscanf(txt, "%d", &width); return NULL; } static char* fillline(char* txt){ @@ -100,7 +106,7 @@ static char* fillline(char* txt){ } static char* token(char* txt, char c) { char* end = strchr(txt, c); - *end = 0; + if (end) *end = 0; return end+1; } static char* leader(char* txt){ @@ -262,7 +268,7 @@ static char* merge(char* txt){ // merges until an endgroup } static char* cmds[] = // MUST be sorted alphabetically - {"CT ", "EG", "FIL ", "LD ", "LS", "MRG", "NBSP", "V ", "W "}; + {"CT", "EG", "FIL", "LD", "LS", "MRG", "NBSP", "V", "W"}; static char* (*call[])(char* txt) = {center, fingroup, fillline, leader, lineset, merge, nbrkspc, vert, setwidth}; @@ -282,15 +288,19 @@ static char* cmd(void){ else if (cmp > 0) low = mid + 1; else{ + int len = strlen(mval); found = true; - proc = call[mid](dat+strlen(mval)); + consumews(dat+len); + proc = call[mid](dat+len); break; } } - free(dat); - if (found) return proc; + if (found) { + free(dat); + return proc; + } else { - fprintf(stderr, "SYNTAX ERROR\n"); + fprintf(stderr, "INVALID COMMAND .%s\n",dat); exit(1); } } -- cgit