use bsearch to match the keywords

not that it's a bottleneck, but it's fancier this way.
This commit is contained in:
Omar Polo 2021-07-08 18:30:19 +00:00
parent bffa7daab8
commit d93c819182
1 changed files with 14 additions and 5 deletions

19
parse.y
View File

@ -63,6 +63,7 @@ static struct vhost *new_vhost(void);
static struct location *new_location(void); static struct location *new_location(void);
void yyerror(const char*, ...); void yyerror(const char*, ...);
int kw_cmp(const void *, const void *);
static int yylex(void); static int yylex(void);
int parse_portno(const char*); int parse_portno(const char*);
void parse_conf(const char*); void parse_conf(const char*);
@ -340,6 +341,7 @@ static struct keyword {
const char *word; const char *word;
int token; int token;
} keywords[] = { } keywords[] = {
/* these MUST be sorted */
{"alias", TALIAS}, {"alias", TALIAS},
{"auto", TAUTO}, {"auto", TAUTO},
{"block", TBLOCK}, {"block", TBLOCK},
@ -374,15 +376,22 @@ static struct keyword {
{"user", TUSER}, {"user", TUSER},
}; };
int
kw_cmp(const void *k, const void *e)
{
return strcmp(k, ((struct keyword *)e)->word);
}
/* /*
* Taken an adapted from doas' parse.y * Taken an adapted from doas' parse.y
*/ */
static int static int
yylex(void) yylex(void)
{ {
struct keyword *kw;
char buf[8096], *ebuf, *p, *str, *v, *val; char buf[8096], *ebuf, *p, *str, *v, *val;
int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0; int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
size_t i, len; size_t len;
p = buf; p = buf;
ebuf = buf + sizeof(buf); ebuf = buf + sizeof(buf);
@ -529,10 +538,10 @@ eow:
goto repeat; goto repeat;
} }
if (!nonkw) { if (!nonkw) {
for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); ++i) { kw = bsearch(buf, keywords, sizeof(keywords)/sizeof(keywords[0]),
if (!strcmp(buf, keywords[i].word)) sizeof(keywords[0]), kw_cmp);
return keywords[i].token; if (kw != NULL)
} return kw->token;
} }
c = *buf; c = *buf;
if (!nonkw && (c == '-' || isdigit(c))) { if (!nonkw && (c == '-' || isdigit(c))) {