]> mj.ucw.cz Git - umpf.git/blobdiff - lex.c
fix saving bodies
[umpf.git] / lex.c
diff --git a/lex.c b/lex.c
index 8710763bd9417da6dbd4907b84a7ad335707d273..9f0201319dfa47818beeef380a1786f45e8992a5 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -5,8 +5,7 @@
 #include <stdarg.h>
 
 #include "cond.tab.h"
-#include "brum.h"
-#define BUFSIZE 4096
+#include "umpf.h"
 #define KLEN 10 
 
 struct keys {
@@ -19,9 +18,19 @@ static struct keys kwds[] =
                {"else", KW_ELSE},
                {"if", KW_IF}, 
                {"mail", KW_MAIL}, 
-               {"pipe", KW_PIPE}
+               {"pipe", KW_PIPE},
+               {"discard", KW_DISCARD}
        };
 
+void
+read_conf(char* filename)
+{
+       conf = fopen(filename, "r");
+
+       if (! conf)
+               die("read_conf: %m"); 
+}
+
 void __attribute__ ((noreturn)) 
 die(char* msg, ...)
 {
@@ -45,6 +54,17 @@ xmalloc(size_t size)
         return ret;
 }
 
+void*
+xrealloc(void* buf, size_t size)
+{
+       buf = realloc(buf, size);
+       
+       if (!buf)
+               die("Low memory");
+
+       return buf;
+}
+
 static void __attribute__ ((noreturn)) 
 parse_err(char* msg, ...)
 {
@@ -59,7 +79,7 @@ parse_err(char* msg, ...)
 }
 
 char*
-xstrdup(char* s)
+xstrdup(const char* s)
 {
         void* ret;
 
@@ -77,7 +97,7 @@ get_string_out(int delim)
        int c;
        char buf[BUFSIZE];
 
-       while ((c = getchar()) != delim || last == '\\'){
+       while ((c = getc(conf)) != delim || last == '\\'){
                if (last=='\\' && c != delim)
                        buf[i-1] = c;
                else {          
@@ -115,7 +135,7 @@ yylex(void)
 {
        int c;
        
-       while ((c = getchar ()) == ' ' || c == '\t' || c =='\n'){
+       while ((c = getc(conf)) == ' ' || c == '\t' || c =='\n'){
                if (c == '\n')
                        line++;
        }
@@ -123,8 +143,7 @@ yylex(void)
        if (c == EOF)
                return 0;
        
-#define CC(a,b) ((a<<8)|b)
-       int d = getchar();
+       int d = getc(conf);
        if (d >= 0) {
                switch (CC(c,d)) {
                case CC('!','='): yylval.n = CC('!','='); return NEQ;
@@ -135,7 +154,7 @@ yylex(void)
                case CC('~','~'): yylval.n = CC('~','~'); return RE;
                case CC('-','>'): yylval.n = CC('-','>'); return ARROW;
                }
-               ungetc(d,stdin);
+               ungetc(d,conf);
        }
 
        switch (c) {
@@ -153,6 +172,9 @@ yylex(void)
                case '=':
                case ';':
                case '.':
+               case '|':
+               case '&':
+               case '^':
                        yylval.n = c;
                        return c;
                
@@ -163,17 +185,17 @@ yylex(void)
        }
 
        if (c >= '0' && c <= '9'){
-               ungetc(c,stdin);
+               ungetc(c,conf);
                int i = 0;
                char buf[BUFSIZE];
        
-               while ((c = getchar())>= '0' && c<= '9'){
+               while ((c = getc(conf))>= '0' && c<= '9'){
                        buf[i] = c;
                        i++;
                        if (i >= BUFSIZE-1)
                                parse_err("Too long number");   
                }
-               ungetc(c,stdin);
+               ungetc(c,conf);
                buf[i] = 0;
                yylval.str = xstrdup(buf);
 
@@ -184,13 +206,13 @@ yylex(void)
                int i = 0;
                char buf[BUFSIZE];
        
-               while (is_var_id(c = getchar())){
+               while (is_var_id(c = getc(conf))){
                        buf[i]=c;
                        i++;
                        if (i >= BUFSIZE-1)
                                parse_err("Too long identifier, max allowed length is %d",BUFSIZE-1);   
                }
-               ungetc(c,stdin);
+               ungetc(c,conf);
                buf[i] = 0;
                yylval.str = xstrdup(buf);
 
@@ -201,14 +223,14 @@ yylex(void)
                char buf[KLEN]; 
                int n, i = 0;
 
-               ungetc(c,stdin);
-               while (is_alpha(c = getchar())){
+               ungetc(c,conf);
+               while (is_alpha(c = getc(conf))){
                        buf[i++] = c;
                        if (i >= KLEN)
                                parse_err("Keyword too long");
                }
                buf[i] = 0;
-               ungetc(c,stdin);
+               ungetc(c,conf);
 
                n = (sizeof(kwds)/sizeof(struct keys));
                for (i = 0; i < n; i++){