18 #define INT_TO_STRING_LEN ((sizeof(int)*4*CHAR_BIT)/10 + 6)
20 static void __attribute__ ((noreturn))
21 bye(int code, char* msg, ...)
25 if (current_body->tmpfile)
26 unlink(current_body->tmpfile);
30 vfprintf(stderr, msg, args);
47 if ((var_tab[var] != NULL))
48 free_string(var_tab[var]);
52 set_var(int var, char* value)
55 var_tab[var] = xstrdup(value);
62 return xstrdup(const_tab[-var]);
63 return xstrdup(var_tab[var]);
66 /* return var struct or NULL if not found */
67 static struct variable*
68 get_var_struct(char* name, struct list* hash)
73 n = get_bucket_number(name);
74 int nocase = isupper(*name);
75 LIST_FOREACH(p, hash + n)
76 if (!(nocase ? strcasecmp : strcmp)(p->name,name))
83 regex_cmp(char* s, char* r)
88 int ovector[OVECCOUNT];
90 brum = pcre_compile(r,0,&error,&erroroffset,NULL);
94 int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
100 #define UPPER(a) ((a) >> 8)
101 #define LOWER(a) ((a) & 0xFF)
104 xcat(char* left, char* right)
106 char* res = xmalloc(strlen(left) + strlen(right) + 1);
118 fold(const char* value, int taken)
122 int len = strlen(value);
123 int newlines[len / 78 + 5];
129 if (len + taken <= 78)
130 return xstrdup(value);
131 for(i = 0; i < len; i++) {
132 if (value[i] == ' ' || value[i]=='\t')
137 newlines[newl++] = curr_ws;
139 while(value[i] && (value[i] == '\t'
147 ret = xmalloc(2*newl + len + 1);
148 for (i = 0; i < len; i++) {
149 if (newl_done == newl) {
150 strcpy(ret + pos, value + i);
153 if(i != newlines[newl_done])
154 ret[pos++] = value[i];
165 unfold(const char* u)
171 new = xmalloc(strlen(u)+1);
174 #define IS_WHITE(c) ((c) == '\t' || (c)==' ' || c=='\n')
176 while (IS_WHITE(*pu))
181 while (IS_WHITE(*pu))
194 modify_headers(struct list* headers, struct list* hash)
202 LIST_FOREACH(p, headers){
203 pv = get_var_struct(p->name, hash);
206 u = unfold(p->value);
207 value = get_var(pv->varcode);
208 if (strcmp(u, value)){
210 free_string(p->value);
211 p->value = fold(value,
212 strlen(p->name) + 2);
219 for (i = 0; i < HASHSIZE; i++){
220 LIST_FOREACH(pv, hash + i){
221 if (isupper(pv->name[0]) && pv->modified){
224 p = xmalloc(sizeof(struct hlist));
225 p->name = xstrdup(pv->name);
226 p->value = get_var(pv->varcode);
228 list_add_last(headers,&p->car);
235 copy_headers(struct list* orig)
237 struct list* new = xmalloc(sizeof(struct list));
238 struct hlist* po, *pn;
242 LIST_FOREACH(po, orig){
243 pn = xmalloc(sizeof(struct hlist));
244 pn->name = xstrdup(po->name);
245 pn->value = xstrdup(po->value);
248 list_add_last(new, &pn->car);
255 prepare_email(struct list* hash)
259 modify_headers(current_headers, hash);
260 em.headers = copy_headers(current_headers);
261 em.body_len = current_body->body_len;
262 em.fd = current_body->fd;
263 if (current_body->body) {
264 em.body = xmalloc(em.body_len);
265 memcpy(em.body, current_body->body, em.body_len);
268 em.tmpfile = xstrdup(current_body->tmpfile);
276 destroy_email(struct email em)
279 free_string(em.body);
281 free_string(em.tmpfile);
285 do_string_ternary_op(struct code* p)
287 char* l = get_var(p->u.tpop.l);
288 char* r = get_var(p->u.tpop.r);
293 result = xmalloc(INT_TO_STRING_LEN);
294 sprintf(result, "%d", regex_cmp(l, r));
297 result = xmalloc(INT_TO_STRING_LEN);
298 sprintf(result, "%d", !regex_cmp(l, r));
307 set_var(p->u.tpop.res, result);
311 do_num_ternary_op(struct code* p)
314 char* result = xmalloc(INT_TO_STRING_LEN);
316 sscanf(get_var(p->u.tpop.l),"%d", &l);
317 sscanf(get_var(p->u.tpop.r),"%d", &r);
345 res = ((l || r) && !(l && r));
363 sprintf(result, "%d", res);
364 set_var(p->u.tpop.res, result);
370 char* val = get_var(var);
377 sscanf(val, "%d", &v);
383 deliver(char* where, int copy, struct list* hash)
386 struct email em = prepare_email(hash);
388 res = deliver_local_email(where, &em);
401 send_mail(char* where, int copy, struct list* hash)
406 struct email em = prepare_email(hash);
412 if ((pid = fork()) < 0)
419 res = execl("/usr/lib/sendmail", "sendmail", where, NULL);
422 write_email_to_fd(pd[1], &em);
436 interp(struct list* ins, struct list* hash)
442 LIST_FOREACH(p, ins) {
445 set_var(p->u.set.l, get_var(p->u.set.r));
448 p = p->u.jump.target;
452 if (eval_cond(p->u.jump_if.cond))
453 p = p->u.jump_if.target;
456 case OPC_JUMP_UNLESS:
457 if (!eval_cond(p->u.jump_unless.cond))
458 p = p->u.jump_unless.target;
477 do_num_ternary_op(p);
480 result = xmalloc(INT_TO_STRING_LEN);
481 sscanf(get_var(p->u.tpop.l),"%d", &v);
482 sprintf(result, "%d", !v);
483 set_var(p->u.tpop.res, result);
487 do_string_ternary_op(p);
492 send_mail(get_var(p->u.arrow.what),
493 p->u.arrow.copy, hash);
496 deliver(get_var(p->u.arrow.what),
497 p->u.arrow.copy, hash);
505 deliver(default_mailbox, 0, hash);
509 print_vars(struct list* hash)
514 for (i=0; i<HASHSIZE; i++){
515 LIST_FOREACH(p, hash + i)
516 printf("%s=%s\n",p->name, get_var(p->varcode));
521 save_current_headers(struct list* hash)
527 LIST_FOREACH(p, current_headers){
528 pv = get_var_struct(p->name, hash);
531 u = unfold(p->value);
532 set_var(pv->varcode, u);