]> mj.ucw.cz Git - umpf.git/commitdiff
clean up a bit
authorAnicka Bernathova <anicka@anicka.net>
Thu, 3 Jul 2008 11:05:58 +0000 (13:05 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Thu, 3 Jul 2008 11:05:58 +0000 (13:05 +0200)
cond.y

diff --git a/cond.y b/cond.y
index 090cfec5e964746ba3fa845cd78a7f36998797ad..1dc42c379982c252051b215516cdb8d328b20462 100644 (file)
--- 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 == '='){