=================================================================== RCS file: /cvs/cvs/blind/parse.y,v retrieving revision 1.13 retrieving revision 1.17 diff -u -p -r1.13 -r1.17 --- blind/parse.y 2022/03/29 21:23:50 1.13 +++ blind/parse.y 2022/04/10 21:04:06 1.17 @@ -25,6 +25,11 @@ #include "config.h" #include "log.h" +#define START_EXPAND 1 +#define DONE_EXPAND 2 + +static struct file *f; + int lookup(char *); int igetc(void); int lgetc(int); @@ -39,8 +44,12 @@ int yyerror(const char *, ...) int config_load(struct blind *); struct file *config_push(const char *); -int config_pop(void); +int config_close(void); +static int expanding; +static int errors = 0; +struct blind *env = NULL; + TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); struct sym { TAILQ_ENTRY(sym) entry; @@ -49,11 +58,9 @@ struct sym { char *nam; char *val; }; + char *symget(const char *); -struct blind *env = NULL; -static int errors = 0; - typedef struct { union { int64_t number; @@ -138,11 +145,6 @@ lookup(char *s) return (STRING); } -#define START_EXPAND 1 -#define DONE_EXPAND 2 - -static int expanding; - char * symget(const char *nam) { @@ -163,8 +165,8 @@ 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); @@ -187,7 +189,7 @@ lgetc(int quotec) if ((c = igetc()) == EOF) { yyerror("reached end of file while parsing " "quoted string"); - if (config_pop() == EOF) + if (config_close() == EOF) return (EOF); return (quotec); } @@ -210,12 +212,12 @@ 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) { - if (config_pop() == EOF) + if (config_close() == EOF) return (EOF); c = igetc(); } @@ -229,14 +231,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 @@ -352,7 +354,7 @@ top: if (c == '-' || isdigit(c)) { do { *p++ = c; - if ((size_t)(p-buf) >= sizeof(buf)) { + if ((size_t)(p-buf) >= sizeof(buf)) { yyerror("string too long"); return (findeol()); } @@ -422,73 +424,56 @@ nodigits: int config_load(struct blind *temp) { - env = temp; - errors = 0; + env = temp; + errors = 0; - if ((f = config_push(env->bl_conf)) == NULL) { - // config_purge(PURGE_ALL); - return (-1); + 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(); + yyparse(); + // setup - // setup + if (errors) + return (-1); - if (errors) { - // config_purge(PURGE_ALL); - return (-1); - } - - return (0); + return (0); } -struct file * -config_push(const char *name) -{ - struct file *nf; - - if ((nf = calloc(1, sizeof(struct file))) == NULL) { - log_debug("cannot allocate memory"); - return (NULL); - } - if ((nf->name = strdup(name)) == NULL) { - log_debug("cannot duplicate name"); - free(nf); - return (NULL); - } - if ((nf->stream = fopen(nf->name, "r")) == NULL) { - log_debug("cannot open config file"); - free(nf->name); - free(nf); - return (NULL); - } - if (config_perm(fileno(nf->stream), nf->name)) { - fclose(nf->stream); - free(nf->name); - free(nf); - return (NULL); - } - nf->lineno = 1; - nf->ungetsize = 16; - nf->ungetbuf = malloc(nf->ungetsize); - if (nf->ungetbuf == NULL) { - log_debug("cannot allocate buffer"); - fclose(nf->stream); - free(nf->name); - free(nf); - return (NULL); - } - - return (nf); -} - int -config_pop(void) +config_close(void) { - fclose(f->stream); - free(f->name); - free(f->ungetbuf); - free(f); - - return (EOF); + fclose(f->stream); + free(f->name); + free(f->unbuf); + free(f); + + return (EOF); }