From 969e678287771bab0a34ca44396a721ade715720 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 30 Mar 2020 18:26:23 -0400 Subject: general fn call interface --- badroff.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/badroff.c b/badroff.c index 3d715fb..522b305 100644 --- a/badroff.c +++ b/badroff.c @@ -51,10 +51,22 @@ char* center(char* text){ return str; } +char* cmds[] = {"CT "}; +char* (*call[])(char* txt) = {center}; + char* cmd(void){ char* dat = popstrbuf(inbuf, chrfill('\n')); - if (!strncmp(dat, "CT ", 3)){ - return center(dat+3); + size_t low = 0; size_t high = sizeof(cmds)/sizeof(*cmds); //len + while (high >= 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 + low = mid + 1; + else if (cmp > 0) + high = mid - 1; + else return call[mid](dat+strlen(mval)); } return NULL; } -- cgit