From: Anicka Bernathova Date: Mon, 21 Jul 2008 15:08:54 +0000 (+0200) Subject: xrealloc X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=eeee3addfa039494842a173322163e72ef1538a1;p=umpf.git xrealloc --- diff --git a/brum.c b/brum.c index 171cb07..b6fa451 100644 --- a/brum.c +++ b/brum.c @@ -22,6 +22,7 @@ main(int argc, char** argv) var_hash = new_var_hash(); current_headers = make_hlist(); + current_body = get_body(); // print_headers(current_headers); save_current_headers(var_hash); diff --git a/brum.h b/brum.h index 3692182..f5e2372 100644 --- a/brum.h +++ b/brum.h @@ -70,6 +70,7 @@ struct tree* input_tree; /* lex.c */ #define CC(a,b) ((a<<8)|b) void* xmalloc(size_t size); +void* xrealloc(void* buf, size_t size); char* xstrdup(char* s); void __attribute__ ((noreturn)) die(char* msg, ...); void read_conf(char* filename); @@ -92,6 +93,7 @@ struct hlist { struct email { struct list* headers; + char* body; }; struct action { @@ -111,6 +113,8 @@ void save_current_headers(struct list* hash); /* ham.c */ struct list* current_headers; +char* current_body; struct list* make_hlist(void); void print_headers(struct list* l); void do_action(struct action* a); +char* get_body(void); diff --git a/ham.c b/ham.c index 04153c5..16d7b36 100644 --- a/ham.c +++ b/ham.c @@ -8,18 +8,6 @@ int curbufsize; -static char* //TODO: rewrite -buf_double(char* buf, int size) -{ - buf = realloc(buf, 2*size); - - if (!buf) - die("Low memory"); - - return buf; - -} - static void new_header(char* buf, struct list* h) { @@ -56,16 +44,14 @@ make_hlist(void) if (c == '\r') continue; - if (i >= curbufsize-2){ - buf = buf_double(buf, curbufsize); - curbufsize *= 2; - } - + if (i >= curbufsize-2) + buf = xrealloc(buf, curbufsize *= 2); + buf[i++] = c; if (c == '\n'){ if (last == '\n') break; - if ((c = getchar())!=' ' && c!='\t'){ + if ((c = getchar()) != ' ' && c != '\t'){ if (c != EOF) ungetc(c, stdin); buf[i] = 0; @@ -80,6 +66,27 @@ make_hlist(void) return l; } +char* +get_body(void) +{ + char* buf; + int c; + int i = 0; + int curbufsize = BUFSIZE; + + buf = xmalloc(BUFSIZE); + while ((c = getchar()) != EOF){ + buf[i++] = c; + + if (i >= curbufsize - 1) + buf = xrealloc(buf, curbufsize *= 2); + } + + buf[i] = 0; + + return buf; +} + void print_headers(struct list* l) { @@ -92,16 +99,12 @@ print_headers(struct list* l) void do_action(struct action* a) { - puts("--do action--"); - if (a->l) - puts(a->l); - printf("->"); - if (a->r) - puts(a->r); - putchar(' '); - if (a->s) - puts(a->s); - puts("with email\n"); - print_headers(a->e.headers); - puts("\n--Muhehehechlemst!--\n"); + //just deliver e-mail, do not look at left side now +/* if (! a->r ){ + + } else if (!strcmp(a->r,"pipe")){ + 1; + } +*/ + } diff --git a/int.c b/int.c index 8da089b..795b843 100644 --- a/int.c +++ b/int.c @@ -46,7 +46,7 @@ find_var(char* name, char* value, struct list* hash) struct variable *p; n = get_bucket_number(name); - int nocase = !isupper(*name); + int nocase = isupper(*name); LIST_FOREACH(p, hash + n) if (!(nocase ? strcasecmp : strcmp)(p->name,name)){ if (value){ @@ -292,6 +292,7 @@ new_action(char* l, char* r, char* s, struct list* hash) modify_headers(current_headers, hash); a->e.headers = copy_headers(current_headers); + a->e.body = xstrdup(current_body); a->l = l; a->r = r; a->s = s; diff --git a/lex.c b/lex.c index 25e3a8a..4beb8bb 100644 --- a/lex.c +++ b/lex.c @@ -55,6 +55,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, ...) {