=================================================================== RCS file: /cvs/cvs/blind/parse.y,v retrieving revision 1.16 retrieving revision 1.19 diff -u -p -r1.16 -r1.19 --- blind/parse.y 2022/04/10 20:03:38 1.16 +++ blind/parse.y 2022/04/17 21:23:34 1.19 @@ -25,15 +25,8 @@ #include "config.h" #include "log.h" -static struct file { - FILE *stream; - char *name; - size_t ungetpos; - size_t ungetsize; - u_char *ungetbuf; - int eof_reached; - int lineno; -} *f; +#define EXPAND_ON 1 +#define EXPAND_OFF 2 int lookup(char *); int igetc(void); @@ -47,30 +40,30 @@ 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 *); -struct file *config_push(const char *); int config_close(void); -#define START_EXPAND 1 -#define DONE_EXPAND 2 -static int expanding; -TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); +static int expanding; +static int errors = 0; +struct blind *env = NULL; +static struct file *f = NULL; + +TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); struct sym { - TAILQ_ENTRY(sym) entry; - int used; - int persist; - char *nam; - char *val; + TAILQ_ENTRY(sym) entry; + int used; + int persist; + char *nam; + char *val; }; -char *symget(const char *); -struct blind *env = NULL; -static int errors = 0; - typedef struct { union { - int64_t number; - char *string; + int64_t number; + char *string; } v; int lineno; } YYSTYPE; @@ -85,26 +78,44 @@ typedef struct { grammar : | grammar '\n' - | grammar set '\n' - | grammar error '\n' { errors++; } - ; + | 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; + env->bl_ttl = $3; } | SET ACTION ENABLE { - env->bl_opt |= BL_OPT_ACTION; + env->bl_opt |= BL_OPT_ACTION; } | SET ACTION DISABLE { - env->bl_opt |= !BL_OPT_ACTION; + env->bl_opt |= !BL_OPT_ACTION; } ; %% struct keywords { - const char *k_name; - int k_val; + const char *k_name; + int k_val; }; int @@ -118,7 +129,7 @@ yyerror(const char *fmt, ...) if (vasprintf(&msg, fmt, ap) == -1) log_fatal("yyerror vasprintf"); va_end(ap); - log_info("%s:%d: %s", f->name, yylval.lineno, msg); + log_info("%s:%d: %s", f->name, yylval.lineno, msg); free(msg); return (0); } @@ -151,6 +162,46 @@ lookup(char *s) 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) { @@ -171,14 +222,14 @@ igetc(void) int c; while (1) { - if (f->ungetpos > 0) - c = f->ungetbuf[--f->ungetpos]; + if (f->unpos > 0) + c = f->unbuf[--f->unpos]; else c = getc(f->stream); - if (c == START_EXPAND) + if (c == EXPAND_ON) expanding = 1; - else if (c == DONE_EXPAND) + else if (c == EXPAND_OFF) expanding = 0; else break; @@ -218,8 +269,8 @@ lgetc(int quotec) * count right if last line in included file is syntactically * invalid and has no newline. */ - if (f->eof_reached == 0) { - f->eof_reached = 1; + if (f->eof == 0) { + f->eof = 1; return ('\n'); } while (c == EOF) { @@ -237,14 +288,14 @@ lungetc(int c) if (c == EOF) return; - if (f->ungetpos >= f->ungetsize) { - void *p = reallocarray(f->ungetbuf, f->ungetsize, 2); + if (f->unpos >= f->unsize) { + void *p = reallocarray(f->unbuf, f->unsize, 2); if (p == NULL) log_fatal("cannot reallocate memory"); - f->ungetbuf = p; - f->ungetsize *= 2; + f->unbuf = p; + f->unsize *= 2; } - f->ungetbuf[f->ungetpos++] = c; + f->unbuf[f->unpos++] = c; } int @@ -305,12 +356,12 @@ top: return (findeol()); } p = val + strlen(val) - 1; - lungetc(DONE_EXPAND); + lungetc(EXPAND_OFF); while (p >= val) { lungetc((unsigned char)*p); p--; } - lungetc(START_EXPAND); + lungetc(EXPAND_ON); goto top; } @@ -455,9 +506,8 @@ config_load(struct blind *temp) return (-1); } f->lineno = 1; - f->ungetsize = 16; - f->ungetbuf = malloc(f->ungetsize); - if (f->ungetbuf == NULL) { + f->unsize = 16; + if ((f->unbuf = malloc(f->unsize)) == NULL) { log_debug("cannot allocate buffer"); fclose(f->stream); free(f->name); @@ -479,7 +529,7 @@ config_close(void) { fclose(f->stream); free(f->name); - free(f->ungetbuf); + free(f->unbuf); free(f); return (EOF);