]> mj.ucw.cz Git - umpf.git/blob - ham.c
cleanup
[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*
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 struct hlist* 
24 new_header(char* buf, struct hlist* end)
25 {
26         char* p;
27         struct hlist* new;
28
29         new = xmalloc(sizeof(struct hlist));
30
31         if (end)
32                 end->next = new;
33
34         p = strchr(buf, ':');
35
36         if (!p)
37                 new->value = xstrdup("");
38         else {
39                 *p = 0;
40                 new->value = xstrdup(p+1);
41         }
42         new->name = xstrdup(buf);
43         new->next = NULL;
44
45         return new;
46 }
47
48 struct hlist*
49 make_hlist(void)
50 {
51         struct hlist* start = NULL, *end = NULL;
52         char* buf;
53         int i = 0; /* current position */
54         int c, last = 0;
55
56         buf = xmalloc(BUFSIZE);
57         curbufsize = BUFSIZE;
58         
59         while ((c = getchar()) != EOF){
60                 if (c == '\r')
61                         continue;
62
63                 if (i >= curbufsize-2){
64                         buf = buf_double(buf, curbufsize);
65                         curbufsize *= 2;
66                 }
67
68                 buf[i++] = c; 
69                 if (c == '\n'){
70                         if (last == '\n')
71                                 break;
72                         if ((c = getchar())!=' ' && c!='\t'){
73                                 if (c != EOF)
74                                         ungetc(c, stdin);
75                                 buf[i] = 0;
76                                 end = new_header(buf, end);
77                                 if (!start)
78                                         start = end;
79                                 i = 0;
80                         } else
81                                 buf[i++] = c;
82                 }
83                         last = c;
84         }
85         free(buf);
86         return start;
87 }
88
89 void
90 print_headers(struct hlist* h)
91 {
92         struct hlist* p = h;
93
94         while (p){
95                 printf("%s:%s",p->name,p->value);
96                 p = p->next;
97         }
98 }
99
100 void
101 do_action(struct action* a)
102 {
103         puts("--do action--");
104         if (a->l)
105                 puts(a->l);
106         printf("->");
107         if (a->r)
108                 puts(a->r);
109         putchar(' ');
110         if (a->s)
111                 puts(a->s);
112         puts("with email\n");
113         print_headers(a->e.headers);
114         puts("\n--Muhehehechlemst!--\n");
115 }