X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lex.c;h=89fb71b2a8d15f0c35df1eefa2bb9ca4d41e7051;hb=HEAD;hp=de4169321743f7ab65616d47d0bda6e1a3125431;hpb=865a98095c15091917080c2aaad8a286b809d21b;p=umpf.git diff --git a/lex.c b/lex.c index de41693..89fb71b 100644 --- a/lex.c +++ b/lex.c @@ -19,7 +19,8 @@ static struct keys kwds[] = {"if", KW_IF}, {"mail", KW_MAIL}, {"pipe", KW_PIPE}, - {"discard", KW_DISCARD} + {"discard", KW_DISCARD}, + {"filter", KW_FILTER} }; void @@ -27,8 +28,10 @@ read_conf(char* filename) { conf = fopen(filename, "r"); - if (! conf) - die("read_conf: %m"); + if (! conf) { + fprintf(stderr, "Error reading config file: %m\nSaving to default mailbox %s\n", default_mailbox); + longjmp(env, 1); + } } void __attribute__ ((noreturn)) @@ -74,12 +77,14 @@ parse_err(char* msg, ...) fprintf(stderr, "Line %d: ", line); vfprintf(stderr, msg, args); fputc('\n', stderr); + fprintf(stderr, "Saving the email to default mailbox %s\n", + default_mailbox); va_end(args); - exit(1); + longjmp(env, 1); } char* -xstrdup(char* s) +xstrdup(const char* s) { void* ret; @@ -92,19 +97,21 @@ xstrdup(char* s) static char* get_string_out(int delim) { - int last = delim; int i = 0; int c; char buf[BUFSIZE]; - while ((c = getc(conf)) != delim || last == '\\'){ - if (last=='\\' && c != delim) - buf[i-1] = c; - else { - buf[i] = c; - i++; - } - last = c; + while ((c = getc(conf)) != delim){ + if (c == '\\') { + c = getc(conf); + if (c == EOF) + parse_err("Unbounded string, %c missing", delim); + buf[i++] = c; + } else if (c == '\n' || c == EOF) + parse_err("Unbounded string, %c missing", delim); + else + buf[i++] = c; + if (i >= BUFSIZE-1) parse_err("Too long string, max allowed length is %d",BUFSIZE-1); } @@ -130,16 +137,38 @@ is_alpha(int c) (c >= 'A' && c <= 'Z'); } -int -yylex(void) +static int +get_token_start(void) { int c; - - while ((c = getc(conf)) == ' ' || c == '\t' || c =='\n'){ - if (c == '\n') + int want_newline = 0; + + for(;;) { + c = getc(conf); + if (c == EOF) + return c; + if (c == '#') { + want_newline = 1; + continue; + } + if (c == '\n') { line++; + want_newline = 0; + continue; + } + if (c != '\n' && want_newline) + continue; + if (c != ' ' && c != '\t') + return c; } +} + +int +yylex(void) +{ + int c; + c = get_token_start(); if (c == EOF) return 0; @@ -195,7 +224,8 @@ yylex(void) if (i >= BUFSIZE-1) parse_err("Too long number"); } - ungetc(c,conf); + if (c != EOF) + ungetc(c,conf); buf[i] = 0; yylval.str = xstrdup(buf); @@ -212,7 +242,10 @@ yylex(void) if (i >= BUFSIZE-1) parse_err("Too long identifier, max allowed length is %d",BUFSIZE-1); } - ungetc(c,conf); + if (!i) + parse_err("Variable identifier must not be empty"); + if (c > 0) + ungetc(c,conf); buf[i] = 0; yylval.str = xstrdup(buf); @@ -230,7 +263,8 @@ yylex(void) parse_err("Keyword too long"); } buf[i] = 0; - ungetc(c,conf); + if (c > 0) + ungetc(c,conf); n = (sizeof(kwds)/sizeof(struct keys)); for (i = 0; i < n; i++){