aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-05-28 23:49:31 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-05-28 23:49:31 -0400
commitcb9206b461e19ce3864b646d8f56f7bdea429f0f (patch)
treec2683e9ea51b27945c1ba14ae6a49108f16bfe26
parent67fc24e800180b83398724d65821aa37268d3bd7 (diff)
made more robust against user input
-rw-r--r--badroff.c24
1 files 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);
}
}