#include <stdarg.h>
#include "cond.tab.h"
-#include "brum.h"
-#define BUFSIZE 4096
+#include "umpf.h"
#define KLEN 10
struct keys {
{"else", KW_ELSE},
{"if", KW_IF},
{"mail", KW_MAIL},
- {"pipe", KW_PIPE}
+ {"pipe", KW_PIPE},
+ {"discard", KW_DISCARD},
+ {"filter", KW_FILTER}
};
+void
+read_conf(char* filename)
+{
+ conf = fopen(filename, "r");
+
+ if (! conf) {
+ fprintf(stderr, "Error reading config file: %m\nSaving to default mailbox %s\n", default_mailbox);
+ longjmp(env, 1);
+ }
+}
+
void __attribute__ ((noreturn))
die(char* msg, ...)
{
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, ...)
{
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;
static char*
get_string_out(int delim)
{
- int last = delim;
int i = 0;
int c;
char buf[BUFSIZE];
- while ((c = getchar()) != 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);
}
(c >= 'A' && c <= 'Z');
}
-int
-yylex(void)
+static int
+get_token_start(void)
{
int c;
-
- while ((c = getchar ()) == ' ' || 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;
- int d = getchar();
+ int d = getc(conf);
if (d >= 0) {
switch (CC(c,d)) {
case CC('!','='): yylval.n = CC('!','='); return NEQ;
case CC('~','~'): yylval.n = CC('~','~'); return RE;
case CC('-','>'): yylval.n = CC('-','>'); return ARROW;
}
- ungetc(d,stdin);
+ ungetc(d,conf);
}
switch (c) {
}
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);
+ if (c != EOF)
+ ungetc(c,conf);
buf[i] = 0;
yylval.str = xstrdup(buf);
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);
+ if (!i)
+ parse_err("Variable identifier must not be empty");
+ if (c > 0)
+ ungetc(c,conf);
buf[i] = 0;
yylval.str = xstrdup(buf);
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);
+ if (c > 0)
+ ungetc(c,conf);
n = (sizeof(kwds)/sizeof(struct keys));
for (i = 0; i < n; i++){