/* * Copyright (c) 2022 Daniel Kroczynski * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ %{ #include #include #include #include #include "blind.h" #include "config.h" #include "log.h" #define EXPAND_ON 1 #define EXPAND_OFF 2 int lookup(char *); int igetc(void); int lgetc(int); int findeol(void); void lungetc(int); int kw_cmp(const void *, const void *); int yyparse(void); int yylex(void); int yyerror(const char *, ...) __attribute__((__format__ (printf, 1, 2))) __attribute__((__nonnull__ (1))); int symset(const char *, const char *, int); char *symget(const char *); int config_load(struct blind *); int config_close(void); TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); struct sym { TAILQ_ENTRY(sym) entry; int used; int persist; char *nam; char *val; }; typedef struct { union { int64_t number; char *string; } v; int lineno; } YYSTYPE; static int expanding; static int errors = 0; struct blind *env = NULL; static struct file *f = NULL; %} %token ACTION ARROW COMPRESSION DISABLE ENABLE EXPIRE ERROR SET %token NUMBER %token STRING %% grammar : | grammar '\n' | grammar varset '\n' | grammar set '\n' | grammar error '\n' { errors++; } ; varset : STRING '=' STRING { char *s = $1; while (*s++) { if (isspace((unsigned char)*s)) { yyerror("macro contain whitespace"); free($1); free($3); YYERROR; } } if (symset($1, $3, 0) == -1) log_fatal("cannot store variable"); free($1); free($3); } ; set : SET EXPIRE NUMBER { env->bl_ttl = $3; } | SET ACTION ENABLE { env->bl_opt |= BL_OPT_ACTION; } | SET ACTION DISABLE { env->bl_opt |= !BL_OPT_ACTION; } | SET COMPRESSION ENABLE { env->bl_opt |= BL_OPT_COMPRESS; } | SET COMPRESSION DISABLE { env->bl_opt |= !BL_OPT_COMPRESS; } ; %% struct keywords { const char *k_name; int k_val; }; int yyerror(const char *fmt, ...) { va_list ap; char *msg; errors++; va_start(ap, fmt); if (vasprintf(&msg, fmt, ap) == -1) log_fatal("yyerror vasprintf"); va_end(ap); log_info("%s:%d: %s", f->name, yylval.lineno, msg); free(msg); return (0); } int kw_cmp(const void *k, const void *e) { return (strcmp(k, ((const struct keywords *)e)->k_name)); } int lookup(char *s) { static const struct keywords keywords[] = { { "action", ACTION }, { "compression", COMPRESSION }, { "disable", DISABLE }, { "enable", ENABLE }, { "expire", EXPIRE }, { "set", SET }, }; const struct keywords *p; p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]), sizeof(keywords[0]), kw_cmp); if (p) return (p->k_val); else return (STRING); } int symset(const char *nam, const char *val, int persist) { struct sym *sym; TAILQ_FOREACH(sym, &symhead, entry) { if (strcmp(nam, sym->nam) == 0) break; } if (sym != NULL) { if (sym->persist == 1) return (0); else { free(sym->nam); free(sym->val); TAILQ_REMOVE(&symhead, sym, entry); free(sym); } } if ((sym = calloc(1, sizeof(*sym))) == NULL) return (-1); sym->nam = strdup(nam); if (sym->nam == NULL) { free(sym); return (-1); } sym->val = strdup(val); if (sym->val == NULL) { free(sym->nam); free(sym); return (-1); } sym->used = 0; sym->persist = persist; TAILQ_INSERT_TAIL(&symhead, sym, entry); return (0); } char * symget(const char *nam) { struct sym *sym; TAILQ_FOREACH(sym, &symhead, entry) { if (strcmp(nam, sym->nam) == 0) { sym->used = 1; return (sym->val); } } return (NULL); } int igetc(void) { int c; while (1) { if (f->unpos > 0) c = f->unbuf[--f->unpos]; else c = getc(f->stream); if (c == EXPAND_ON) expanding = 1; else if (c == EXPAND_OFF) expanding = 0; else break; } return (c); } int lgetc(int quotec) { int c, next; if (quotec) { if ((c = igetc()) == EOF) { yyerror("reached end of file while parsing " "quoted string"); if (config_close() == EOF) return (EOF); return (quotec); } return (c); } while ((c = igetc()) == '\\') { next = igetc(); if (next != '\n') { c = next; break; } yylval.lineno = f->lineno; f->lineno++; } if (c == EOF) { if (config_close() == EOF) return (EOF); c = igetc(); } return (c); } void lungetc(int c) { if (c == EOF) return; if (f->unpos >= f->unsize) { void *p = reallocarray(f->unbuf, f->unsize, 2); if (p == NULL) log_fatal("cannot reallocate memory"); f->unbuf = p; f->unsize *= 2; } f->unbuf[f->unpos++] = c; } int findeol(void) { int c; while (1) { c = lgetc(0); if (c == '\n') { f->lineno++; break; } if (c == EOF) break; } return (ERROR); } int yylex(void) { char buf[8096]; char *p, *val; int quotec, next, c; int token; top: p = buf; while ((c = lgetc(0)) == ' ' || c == '\t') ; yylval.lineno = f->lineno; if (c == '#') while ((c = lgetc(0)) != '\n' && c != EOF) ; if (c == '$' && !expanding) { while (1) { if ((c = lgetc(0)) == EOF) return (0); if (p + 1 >= buf + sizeof(buf) - 1) { yyerror("string too long"); return (findeol()); } if (isalnum(c) || c == '_') { *p++ = c; continue; } *p = '\0'; lungetc(c); break; } val = symget(buf); if (val == NULL) { yyerror("macro '%s' not defined", buf); return (findeol()); } p = val + strlen(val) - 1; lungetc(EXPAND_OFF); while (p >= val) { lungetc((unsigned char)*p); p--; } lungetc(EXPAND_ON); goto top; } switch (c) { case '\'': case '"': quotec = c; while (1) { if ((c = lgetc(quotec)) == EOF) return (0); if (c == '\n') { f->lineno++; continue; } else if (c == '\\') { if ((next = lgetc(quotec)) == EOF) return (0); if (next == quotec || next == ' ' || next == '\t') c = next; else if (next == '\n') { f->lineno++; continue; } else lungetc(next); } else if (c == quotec) { *p = '\0'; break; } else if (c == '\0') { yyerror("syntax error"); return (findeol()); } if (p + 1 >= buf + sizeof(buf) - 1) { yyerror("string too long"); return (findeol()); } *p++ = c; } yylval.v.string = strdup(buf); if (yylval.v.string == NULL) log_fatal("cannot duplicate buffer"); return (STRING); } #define allowed_to_end_number(x) \ (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=') if (c == '-' || isdigit(c)) { do { *p++ = c; if ((size_t)(p-buf) >= sizeof(buf)) { yyerror("string too long"); return (findeol()); } } while ((c = lgetc(0)) != EOF && isdigit(c)); lungetc(c); if (p == buf + 1 && buf[0] == '-') goto nodigits; if (c == EOF || allowed_to_end_number(c)) { const char *errstr = NULL; *p = '\0'; yylval.v.number = strtonum(buf, LLONG_MIN, LLONG_MAX, &errstr); if (errstr) { yyerror("\"%s\" invalid number: %s", buf, errstr); return (findeol()); } return (NUMBER); } else { nodigits: while (p > buf + 1) lungetc((unsigned char)*--p); c = (unsigned char)*--p; if (c == '-') return (c); } } if (c == '=') { if ((c = lgetc(0)) != EOF && c == '>') return (ARROW); lungetc(c); c = '='; } #define allowed_in_string(x) \ (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \ x != '{' && x != '}' && x != '<' && x != '>' && \ x != '!' && x != '=' && x != '#' && \ x != ',')) if (isalnum(c) || c == ':' || c == '_') { do { *p++ = c; if ((size_t)(p-buf) >= sizeof(buf)) { yyerror("string too long"); return (findeol()); } } while ((c = lgetc(0)) != EOF && (allowed_in_string(c))); lungetc(c); *p = '\0'; if ((token = lookup(buf)) == STRING) if ((yylval.v.string = strdup(buf)) == NULL) log_fatal("cannot duplicate buffer"); return (token); } if (c == '\n') { yylval.lineno = f->lineno; f->lineno++; } if (c == EOF) return (0); return (c); } int config_load(struct blind *temp) { env = temp; errors = 0; if ((f = calloc(1, sizeof(struct file))) == NULL) { log_debug("cannot allocate memory"); return (-1); } if ((f->name = strdup(env->bl_conf)) == NULL) { log_debug("cannot duplicate name"); free(f); return (-1); } if ((f->stream = fopen(f->name, "r")) == NULL) { log_debug("cannot open config file"); free(f->name); free(f); return (-1); } if (config_perm(fileno(f->stream), f->name)) { fclose(f->stream); free(f->name); free(f); return (-1); } f->lineno = 1; f->unsize = 16; if ((f->unbuf = malloc(f->unsize)) == NULL) { log_debug("cannot allocate buffer"); fclose(f->stream); free(f->name); free(f); return (-1); } yyparse(); // setup if (errors) return (-1); return (0); } int config_close(void) { fclose(f->stream); free(f->name); free(f->unbuf); free(f); return (EOF); }