[BACK]Return to parse.y CVS log [TXT][DIR] Up to [local] / blind

Diff for /blind/parse.y between version 1.5 and 1.7

version 1.5, 2022/03/20 22:00:51 version 1.7, 2022/03/22 01:51:36
Line 21 
Line 21 
 #include <stdio.h>  #include <stdio.h>
 #include <stdarg.h>  #include <stdarg.h>
 #include <unistd.h>  #include <unistd.h>
   #include <ctype.h>
   
 #include "blind.h"  #include "blind.h"
 #include "log.h"  #include "log.h"
Line 38  static struct file {
Line 39  static struct file {
         int                      errors;          int                      errors;
 } *file, *top;  } *file, *top;
   
   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             yyparse(void);
 int             yylex(void);  int             yylex(void);
 int             yyerror(const char *, ...)  int             yyerror(const char *, ...)
Line 49  struct file    *config_push(const char *);
Line 56  struct file    *config_push(const char *);
 int             config_pop(void);  int             config_pop(void);
 int             config_perm(int, const char *);  int             config_perm(int, const char *);
   
   
   TAILQ_HEAD(symhead, sym)         symhead = TAILQ_HEAD_INITIALIZER(symhead);
   struct sym {
           TAILQ_ENTRY(sym)         entry;
           int                      used;
           int                      persist;
           char                    *nam;
           char                    *val;
   };
   char           *symget(const char *);
   
 struct blind   *env = NULL;  struct blind   *env = NULL;
 static int      errors = 0;  static int      errors = 0;
   
Line 62  typedef struct {
Line 80  typedef struct {
   
 %}  %}
   
 %token EXPIRE  %token ACTION ARROW DISABLE ENABLE EXPIRE ERROR SET
   
 %token <v.number> NUMBER  %token <v.number> NUMBER
 %token <v.string> STRING  %token <v.string> STRING
Line 70  typedef struct {
Line 88  typedef struct {
   
 grammar :  grammar :
         | grammar '\n'          | grammar '\n'
         | grammar expire '\n'          | grammar set '\n'
         | grammar error '\n'    { file->errors++; }          | grammar error '\n'    { file->errors++; }
         ;          ;
   
 expire  : EXPIRE NUMBER {  set     : SET EXPIRE NUMBER {
         log_debug("found EXPIRE");          env->bl_ttl = $3;
         env->bl_ttl = $2;  
 }  }
   | SET ACTION ENABLE {
           env->bl_opt |= BL_OPT_ACTION;
    }
   | SET ACTION DISABLE {
           env->bl_opt |= !BL_OPT_ACTION;
    }
 ;  ;
   
 %%  %%
   
   struct keywords {
           const char      *k_name;
           int              k_val;
   };
   
 int  int
 yyerror(const char *fmt, ...)  yyerror(const char *fmt, ...)
 {  {
           va_list          ap;
           char            *msg;
   
           file->errors++;
           va_start(ap, fmt);
           if (vasprintf(&msg, fmt, ap) == -1)
                   log_fatal("yyerror vasprintf");
           va_end(ap);
           log_info("%s:%d: %s", file->name, yylval.lineno, msg);
           free(msg);
         return (0);          return (0);
 }  }
   
 int  int
   kw_cmp(const void *k, const void *e)
   {
           return (strcmp(k, ((const struct keywords *)e)->k_name));
   }
   
   int
   lookup(char *s)
   {
           /* this has to be sorted always */
           static const struct keywords keywords[] = {
                   { "action",             ACTION  },
                   { "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);
   }
   
   #define START_EXPAND    1
   #define DONE_EXPAND     2
   
   static int      expanding;
   
   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 (file->ungetpos > 0)
                           c = file->ungetbuf[--file->ungetpos];
                   else
                           c = getc(file->stream);
   
                   if (c == START_EXPAND)
                           expanding = 1;
                   else if (c == DONE_EXPAND)
                           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 (file == top || config_pop() == EOF)
                                   return (EOF);
                           return (quotec);
                   }
                   return (c);
           }
   
           while ((c = igetc()) == '\\') {
                   next = igetc();
                   if (next != '\n') {
                           c = next;
                           break;
                   }
                   yylval.lineno = file->lineno;
                   file->lineno++;
           }
   
           if (c == EOF) {
                   /*
                    * Fake EOL when hit EOF for the first time. This gets line
                    * count right if last line in included file is syntactically
                    * invalid and has no newline.
                    */
                   if (file->eof_reached == 0) {
                           file->eof_reached = 1;
                           return ('\n');
                   }
                   while (c == EOF) {
                           if (file == top || config_pop() == EOF)
                                   return (EOF);
                           c = igetc();
                   }
           }
           return (c);
   }
   
   void
   lungetc(int c)
   {
           if (c == EOF)
                   return;
   
           if (file->ungetpos >= file->ungetsize) {
                   void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
                   if (p == NULL)
                           log_fatal("cannot reallocate memory");
                   file->ungetbuf = p;
                   file->ungetsize *= 2;
           }
           file->ungetbuf[file->ungetpos++] = c;
   }
   
   int
   findeol(void)
   {
           int     c;
   
           /* skip to either EOF or the first real EOL */
           while (1) {
                   c = lgetc(0);
                   if (c == '\n') {
                           file->lineno++;
                           break;
                   }
                   if (c == EOF)
                           break;
           }
           return (ERROR);
   }
   
   int
 yylex(void)  yylex(void)
 {  {
         return (0);          char     buf[8096];
           char    *p, *val;
           int      quotec, next, c;
           int      token;
   
   top:
           p = buf;
           while ((c = lgetc(0)) == ' ' || c == '\t')
                   ; /* nothing */
   
           yylval.lineno = file->lineno;
           if (c == '#')
                   while ((c = lgetc(0)) != '\n' && c != EOF)
                           ; /* nothing */
           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(DONE_EXPAND);
                   while (p >= val) {
                           lungetc((unsigned char)*p);
                           p--;
                   }
                   lungetc(START_EXPAND);
                   goto top;
           }
   
           switch (c) {
           case '\'':
           case '"':
                   quotec = c;
                   while (1) {
                           if ((c = lgetc(quotec)) == EOF)
                                   return (0);
                           if (c == '\n') {
                                   file->lineno++;
                                   continue;
                           } else if (c == '\\') {
                                   if ((next = lgetc(quotec)) == EOF)
                                           return (0);
                                   if (next == quotec || next == ' ' ||
                                       next == '\t')
                                           c = next;
                                   else if (next == '\n') {
                                           file->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 = file->lineno;
                   file->lineno++;
           }
           if (c == EOF)
                   return (0);
           return (c);
 }  }
   
 int  int
Line 112  config_load(struct blind *temp)
Line 453  config_load(struct blind *temp)
         config_pop();          config_pop();
   
         // setup          // setup
   
           if (errors) {
                   //      config_purge(PURGE_ALL);
                   return (-1);
           }
   
         return (0);          return (0);
 }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.7

https://cvs.kroczynski.net