14 #define INT_TO_STRING_LEN ((sizeof(int)*4*CHAR_BIT)/10 + 6)
24 set_var(int var, char* value)
27 var_tab[var] = xstrdup(value);
35 return const_tab[-var];
39 /* return var struct or NULL if not found */
40 static struct variable*
41 get_var_struct(char* name, struct list* hash)
46 n = get_bucket_number(name);
47 int nocase = isupper(*name);
48 LIST_FOREACH(p, hash + n)
49 if (!(nocase ? strcasecmp : strcmp)(p->name,name))
56 regex_cmp(char* s, char* r)
61 int ovector[OVECCOUNT];
63 brum = pcre_compile(r,0,&error,&erroroffset,NULL);
67 int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
73 #define UPPER(a) ((a) >> 8)
74 #define LOWER(a) ((a) & 0xFF)
77 xcat(char* left, char* right)
79 char* res = xmalloc(strlen(left) + strlen(right) + 1);
91 modify_headers(struct list* headers, struct list* hash)
98 LIST_FOREACH(p, headers){
99 pv = get_var_struct(p->name, hash);
105 p->value = xstrdup(get_var(pv->varcode)); //FIXME: fold it
110 for (i = 0; i < HASHSIZE; i++){
111 LIST_FOREACH(pv, hash + i){
112 if (isupper(pv->name[0]) && pv->modified){
115 p = xmalloc(sizeof(struct hlist));
116 p->name = xstrdup(pv->name);
117 p->value = xstrdup(get_var(pv->varcode));
119 list_add_last(headers,&p->car);
126 copy_headers(struct list* orig)
128 struct list* new = xmalloc(sizeof(struct list));
129 struct hlist* po, *pn;
133 LIST_FOREACH(po, orig){
134 pn = xmalloc(sizeof(struct hlist));
135 pn->name = xstrdup(po->name);
136 pn->value = xstrdup(po->value);
139 list_add_last(new, &pn->car);
146 prepare_email(struct list* hash)
150 em = xmalloc(sizeof(struct email));
152 modify_headers(current_headers, hash);
153 em->headers = copy_headers(current_headers);
154 em->body_len = current_body->body_len;
155 em->body = xmalloc(em->body_len);
156 memcpy(em->body, current_body->body, em->body_len);
162 do_string_ternary_op(struct code* p)
164 char* l = get_var(p->u.tpop.l);
165 char* r = get_var(p->u.tpop.r);
170 result = xmalloc(INT_TO_STRING_LEN);
171 sprintf(result, "%d", regex_cmp(l, r));
173 result = xmalloc(INT_TO_STRING_LEN);
174 sprintf(result, "%d", !regex_cmp(l, r));
181 set_var(p->u.tpop.res, result);
185 do_num_ternary_op(struct code* p)
188 char* result = xmalloc(INT_TO_STRING_LEN);
190 sscanf(get_var(p->u.tpop.l),"%d", &l);
191 sscanf(get_var(p->u.tpop.r),"%d", &r);
219 res = ((l || r) && !(l && r));
237 sprintf(result, "%d", res);
238 set_var(p->u.tpop.res, result);
244 char* val = get_var(var);
251 sscanf(val, "%d", &v);
257 interp(struct list* ins)
263 LIST_FOREACH(p, ins) {
266 set_var(p->u.set.l, get_var(p->u.set.r));
269 p = p->u.jump.target;
273 if (eval_cond(p->u.jump_if.cond))
274 p = p->u.jump_if.target;
277 case OPC_JUMP_UNLESS:
278 if (!eval_cond(p->u.jump_unless.cond))
279 p = p->u.jump_unless.target;
298 do_num_ternary_op(p);
301 result = xmalloc(INT_TO_STRING_LEN);
302 sscanf(get_var(p->u.tpop.l),"%d", &v);
303 sprintf(result, "%d", !v);
304 set_var(p->u.tpop.res, result);
308 do_string_ternary_op(p);
324 print_vars(struct list* hash)
329 for (i=0; i<HASHSIZE; i++){
330 LIST_FOREACH(p, hash + i)
331 printf("%s=%s\n",p->name, get_var(p->varcode));
342 new = xmalloc(strlen(u)+1);
345 #define IS_WHITE(c) ((c) == '\t' || (c)==' ' || c=='\n')
347 while (IS_WHITE(*pu))
352 while (IS_WHITE(*pu))
365 save_current_headers(struct list* hash)
371 LIST_FOREACH(p, current_headers){
372 pv = get_var_struct(p->name, hash);
375 u = unfold(p->value);
376 set_var(pv->varcode, u);
384 get_default_mailbox(char* mb)
386 default_mailbox = mb;