chiark / gitweb /
tun: add hard routes even if they are currently down.
[secnet.git] / conffile.fl
index d191ffc..7228c9e 100644 (file)
@@ -1,7 +1,13 @@
 /* the "incl" state is used for picking up the name of an include file */
 %x incl
 
+%option nounput
+%option noinput
+%option never-interactive
+
 %{
+#include <assert.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "conffile.tab.h"
 #include "util.h"
 
+#define YY_NO_UNPUT
+
+#define YY_INPUT(buf,result,max_size)                                  \
+do{                                                                    \
+       (result)= fread((buf),1,(max_size),yyin);                       \
+       if (ferror(yyin))                                               \
+               fatal_perror("Error reading configuration file (%s)",   \
+                            config_file);                              \
+}while(0)
+
 #define MAX_INCLUDE_DEPTH 10
-YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
+struct include_stack_item {
+       YY_BUFFER_STATE bst;
+       int lineno;
+       cstring_t file;
+};
+struct include_stack_item include_stack[MAX_INCLUDE_DEPTH];
 int include_stack_ptr=0;
 
-uint32_t config_lineno=0;
-string_t config_file="xxx";
+int config_lineno=0;
+cstring_t config_file="xxx";
 
 static struct p_node *leafnode(uint32_t type)
 {
@@ -50,8 +71,25 @@ static struct p_node *stringnode(string_t string)
 static struct p_node *numnode(string_t number)
 {
        struct p_node *r;
+       unsigned long n;
        r=leafnode(T_NUMBER);
-       r->data.number=atoi(number);
+       errno = 0;
+       n = strtoul(number, NULL, 10);
+       /* The caller is expected to only give us [0-9]+,
+        * so we skip some of the usual syntax checking. */
+       r->data.number=n;
+       /* Give a consistent error message for any kind of
+        * out-of-range condition */
+       if(errno == ERANGE || n != r->data.number) {
+           Message(M_FATAL,"config file %s line %d: '%s' is too big\n",
+                   config_file, config_lineno, number);
+           exit(1);
+       }
+       if(errno) {
+           Message(M_FATAL,"config file %s line %d: '%s': %s\n",
+                   config_file, config_lineno, number, strerror(errno));
+           exit(1);
+       }
        return r;
 }
 
@@ -64,15 +102,28 @@ include                    BEGIN(incl);
        if (include_stack_ptr >= MAX_INCLUDE_DEPTH) {
                fatal("Configuration file includes nested too deeply");
        }
-       include_stack[include_stack_ptr++]=
-               YY_CURRENT_BUFFER;
+       include_stack[include_stack_ptr].bst=YY_CURRENT_BUFFER;
+       include_stack[include_stack_ptr].lineno=config_lineno;
+       include_stack[include_stack_ptr].file=config_file;
+       include_stack_ptr++;
        yyin=fopen(yytext,"r");
        if (!yyin) {
                fatal("Can't open included file %s",yytext);
        }
+       config_lineno=1;
+       config_file=safe_strdup(yytext,"conffile.fl/include");
        yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
        BEGIN(INITIAL);
        }
+<incl>\n               { /* include with no filename */
+       Message(M_FATAL,"config file %s line %d: %s\n",config_file,
+               config_lineno,"``include'' requires a filename");
+       BEGIN(INITIAL);
+       assert(config_lineno < INT_MAX);
+       ++config_lineno;
+       ++yynerrs;
+}
+
 <<EOF>>                {
        if (--include_stack_ptr < 0) {
                yyterminate();
@@ -80,7 +131,9 @@ include                      BEGIN(incl);
        else {
                fclose(yyin);
                yy_delete_buffer(YY_CURRENT_BUFFER);
-               yy_switch_to_buffer(include_stack[include_stack_ptr]);
+               yy_switch_to_buffer(include_stack[include_stack_ptr].bst);
+               config_lineno=include_stack[include_stack_ptr].lineno;
+               config_file=include_stack[include_stack_ptr].file;
        }
        }
 \"[^\"]*\"             yylval=stringnode(yytext); return TOK_STRING;