From b153a789af8d99f4065fc2c2bfa384daaa6fed7e Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Thu, 3 Jul 2008 13:05:58 +0200 Subject: [PATCH] clean up a bit --- cond.y | 153 +++++++++++++++++++++++---------------------------------- 1 file changed, 62 insertions(+), 91 deletions(-) diff --git a/cond.y b/cond.y index 090cfec..1dc42c3 100644 --- a/cond.y +++ b/cond.y @@ -9,6 +9,8 @@ int yylex (void); void yyerror (char const *); +int regex_cmp(char* s, char* r); + %} %union { @@ -37,40 +39,8 @@ line: '\n' boo: CONST EQ CONST { $$ = ! strcmp($1, $3); } | CONST NEQ CONST { $$ = !! strcmp($1, $3); } - | CONST RE REGEX { - pcre *brum; - int erroroffset; - const char* error; - int ovector[OVECCOUNT]; - - brum=pcre_compile($3,0,&error,&erroroffset,NULL); - if (!brum){ - puts("Mnau"); - return 1; - } - - int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT); - - $$ = res >= 0; - pcre_free(brum); - } - | CONST NRE REGEX { - pcre *brum; - int erroroffset; - const char* error; - int ovector[OVECCOUNT]; - - brum=pcre_compile($3,0,&error,&erroroffset,NULL); - if (!brum){ - puts("Mnau"); - return 1; - } - - int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT); - - $$ = res < 0; - pcre_free(brum); - } + | CONST RE REGEX { $$ = regex_cmp($1,$3) >= 0 } + | CONST NRE REGEX { $$ = regex_cmp($1,$3) < 0 } | NUM EQ NUM { $$ = $1 == $3 } | NUM NEQ NUM { $$ = $1 != $3 } | NUM GE NUM { $$ = $1 >= $3 } @@ -91,80 +61,77 @@ boo: CONST EQ CONST { $$ = ! strcmp($1, $3); } #define BUFSIZE 4096 +int +regex_cmp(char* s, char* r) +{ + pcre *brum; + int erroroffset; + const char* error; + int ovector[OVECCOUNT]; + + brum=pcre_compile(r,0,&error,&erroroffset,NULL); + if (!brum) + return -1; + + int res=pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT); + + pcre_free(brum); + + return res; +} + +char* +get_string_out(char delim) +{ + int last = delim; + int i = 0; + char* s; + int c; + + if (!(s = malloc(BUFSIZE))){ + puts("Low memory"); + exit(0); + } + + while ((c = getchar()) != delim || last == '\\'){ + if (last=='\\' && c != delim) + s[i-1] = c; + else { + s[i] = c; + i++; + } + last = c; + if (i >= BUFSIZE-1) + break; + } + s[i] = '\0'; + + return s; +} + int yylex(void) { - int i, c, last; + int c, last; while ((c = getchar ()) == ' ' || c == '\t'); if (c == '"'){ last = '"'; - i = 0; - if (!(yylval.str = malloc(BUFSIZE))){ - puts("Low memory"); - exit(0); - } - while ((c = getchar()) != '"' || last == '\\'){ - if (last=='\\' && c != '"') - yylval.str[i-1] = c; - else { - yylval.str[i] = c; - i++; - } - last = c; - if (i >= BUFSIZE-1) - break; - } - yylval.str[i] = '\0'; - + yylval.str = get_string_out('"'); return CONST; } if (c == '\''){ last = '\''; - i = 0; - if (!(yylval.str = malloc(BUFSIZE))){ - puts("Low memory"); - exit(0); - } - while ((c = getchar()) != '\'' || last == '\\'){ - if (last=='\\' && c != '\'') - yylval.str[i-1] = c; - else { - yylval.str[i] = c; - i++; - } - last = c; - if (i >= BUFSIZE-1) - break; - } - yylval.str[i] = '\0'; - + yylval.str = get_string_out('\''); return CONST; } if (c == '/'){ last = '/'; - i = 0; - if (!(yylval.str = malloc(BUFSIZE))){ - puts("Low memory"); - exit(0); - } - while ((c = getchar()) != '/' || last == '\\'){ - if (last=='\\' && c != '/') - yylval.str[i-1] = c; - else { - yylval.str[i] = c; - i++; - } - last = c; - if (i >= BUFSIZE-1) - break; - } - yylval.str[i] = '\0'; - + yylval.str = get_string_out('/'); return REGEX; } @@ -186,15 +153,19 @@ yylex(void) if (c == '<'){ if ((c = getchar ()) == '=') return LE; - else + else { + ungetc(c,stdin); return LT; + } } if (c == '>'){ if ((c = getchar ()) == '=') return GE; - else + else { + ungetc(c,stdin); return GT; + } } if (c == '='){ -- 2.39.2