]> mj.ucw.cz Git - umpf.git/blob - ham.c
use clist for headers
[umpf.git] / ham.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "brum.h"
6
7 #define BUFSIZE 1024 
8
9 int curbufsize;
10
11 static char* //TODO: rewrite
12 buf_double(char* buf, int size)
13 {
14         buf = realloc(buf, 2*size);
15         
16         if (!buf)
17                 die("Low memory");
18
19         return buf;
20
21 }
22
23 static void
24 new_header(char* buf, struct list* h)
25 {
26         char* p;
27         struct hlist* new;
28
29         new = xmalloc(sizeof(struct hlist));
30
31         p = strchr(buf, ':');
32         if (!p)
33                 new->value = xstrdup("");
34         else {
35                 *p = 0;
36                 new->value = xstrdup(p+1);
37         }
38         new->name = xstrdup(buf);
39
40         list_add_last(h, &new->car);
41 }
42
43 struct list*
44 make_hlist(void)
45 {
46         struct list* l = xmalloc(sizeof(struct list));
47         char* buf;
48         int i = 0; /* current position */
49         int c, last = 0;
50
51         list_init(l);
52         buf = xmalloc(BUFSIZE);
53         curbufsize = BUFSIZE;
54         
55         while ((c = getchar()) != EOF){
56                 if (c == '\r')
57                         continue;
58
59                 if (i >= curbufsize-2){
60                         buf = buf_double(buf, curbufsize);
61                         curbufsize *= 2;
62                 }
63
64                 buf[i++] = c; 
65                 if (c == '\n'){
66                         if (last == '\n')
67                                 break;
68                         if ((c = getchar())!=' ' && c!='\t'){
69                                 if (c != EOF)
70                                         ungetc(c, stdin);
71                                 buf[i] = 0;
72                                 new_header(buf, l);
73                                 i = 0;
74                         } else
75                                 buf[i++] = c;
76                 }
77                         last = c;
78         }
79         free(buf);
80         return l;
81 }
82
83 void
84 print_headers(struct list* l)
85 {
86         struct hlist* p;
87
88         LIST_FOREACH(p,l)
89                 printf("%s:%s",p->name,p->value);
90 }
91
92 void
93 do_action(struct action* a)
94 {
95         puts("--do action--");
96         if (a->l)
97                 puts(a->l);
98         printf("->");
99         if (a->r)
100                 puts(a->r);
101         putchar(' ');
102         if (a->s)
103                 puts(a->s);
104         puts("with email\n");
105         print_headers(a->e.headers);
106         puts("\n--Muhehehechlemst!--\n");
107 }