]> diplodocus.org Git - nmh/blob - sbr/fmt_compile.c
* uip/new.c: cast folder_len to int to avoid warning on
[nmh] / sbr / fmt_compile.c
1
2 /*
3 * fmt_compile.c -- "compile" format strings for fmt_scan
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/addrsbr.h>
14 #include <h/tws.h>
15 #include <h/fmt_scan.h>
16 #include <h/fmt_compile.h>
17
18 #ifdef TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # ifdef TM_IN_SYS_TIME
23 # include <sys/time.h>
24 # else
25 # include <time.h>
26 # endif
27 #endif
28
29 /*
30 * hash table for deciding if a component is "interesting"
31 */
32 struct comp *wantcomp[128];
33
34 static struct format *formatvec; /* array to hold formats */
35 static struct format *next_fp; /* next free format slot */
36 static struct format *fp; /* current format slot */
37 static struct comp *cm; /* most recent comp ref */
38 static struct ftable *ftbl; /* most recent func ref */
39 static int ncomp;
40 static int infunction; /* function nesting cnt */
41
42 extern struct mailname fmt_mnull;
43
44 /* ftable->type (argument type) */
45 #define TF_COMP 0 /* component expected */
46 #define TF_NUM 1 /* number expected */
47 #define TF_STR 2 /* string expected */
48 #define TF_EXPR 3 /* component or func. expected */
49 #define TF_NONE 4 /* no argument */
50 #define TF_MYBOX 5 /* special - get current user's mbox */
51 #define TF_NOW 6 /* special - get current unix time */
52 #define TF_EXPR_SV 7 /* like expr but save current str reg */
53 #define TF_NOP 8 /* like expr but no result */
54
55 /* ftable->flags */
56 /* NB that TFL_PUTS is also used to decide whether the test
57 * in a "%<(function)..." should be a string or numeric one.
58 */
59 #define TFL_PUTS 1 /* implicit putstr if top level */
60 #define TFL_PUTN 2 /* implicit putnum if top level */
61
62 struct ftable {
63 char *name; /* function name */
64 char type; /* argument type */
65 char f_type; /* fmt type */
66 char extra; /* arg. type dependent extra info */
67 char flags;
68 };
69
70 static struct ftable functable[] = {
71 { "nonzero", TF_EXPR, FT_V_NE, FT_IF_V_NE, 0 },
72 { "zero", TF_EXPR, FT_V_EQ, FT_IF_V_EQ, 0 },
73 { "eq", TF_NUM, FT_V_EQ, FT_IF_V_EQ, 0 },
74 { "ne", TF_NUM, FT_V_NE, FT_IF_V_NE, 0 },
75 { "gt", TF_NUM, FT_V_GT, FT_IF_V_GT, 0 },
76 { "null", TF_EXPR, FT_S_NULL, FT_IF_S_NULL, 0 },
77 { "nonnull", TF_EXPR, FT_S_NONNULL, FT_IF_S, 0 },
78 { "match", TF_STR, FT_V_MATCH, FT_IF_MATCH, 0 },
79 { "amatch", TF_STR, FT_V_AMATCH, FT_IF_AMATCH, 0 },
80
81 { "putstr", TF_EXPR, FT_STR, 0, 0 },
82 { "putstrf", TF_EXPR, FT_STRF, 0, 0 },
83 { "putnum", TF_EXPR, FT_NUM, 0, 0 },
84 { "putnumf", TF_EXPR, FT_NUMF, 0, 0 },
85 { "putaddr", TF_STR, FT_PUTADDR, 0, 0 },
86 { "void", TF_NOP, 0, 0, 0 },
87
88 { "comp", TF_COMP, FT_LS_COMP, 0, TFL_PUTS },
89 { "lit", TF_STR, FT_LS_LIT, 0, TFL_PUTS },
90 { "getenv", TF_STR, FT_LS_GETENV, 0, TFL_PUTS },
91 { "profile", TF_STR, FT_LS_CFIND, 0, TFL_PUTS },
92 { "decodecomp", TF_COMP, FT_LS_DECODECOMP, 0, TFL_PUTS },
93 { "decode", TF_EXPR, FT_LS_DECODE, 0, TFL_PUTS },
94 { "trim", TF_EXPR, FT_LS_TRIM, 0, 0 },
95 { "compval", TF_COMP, FT_LV_COMP, 0, TFL_PUTN },
96 { "compflag", TF_COMP, FT_LV_COMPFLAG, 0, TFL_PUTN },
97 { "num", TF_NUM, FT_LV_LIT, 0, TFL_PUTN },
98 { "msg", TF_NONE, FT_LV_DAT, 0, TFL_PUTN },
99 { "cur", TF_NONE, FT_LV_DAT, 1, TFL_PUTN },
100 { "size", TF_NONE, FT_LV_DAT, 2, TFL_PUTN },
101 { "width", TF_NONE, FT_LV_DAT, 3, TFL_PUTN },
102 { "unseen", TF_NONE, FT_LV_DAT, 4, TFL_PUTN },
103 { "dat", TF_NUM, FT_LV_DAT, 0, TFL_PUTN },
104 { "strlen", TF_NONE, FT_LV_STRLEN, 0, TFL_PUTN },
105 { "me", TF_MYBOX, FT_LS_LIT, 0, TFL_PUTS },
106 { "plus", TF_NUM, FT_LV_PLUS_L, 0, TFL_PUTN },
107 { "minus", TF_NUM, FT_LV_MINUS_L, 0, TFL_PUTN },
108 { "divide", TF_NUM, FT_LV_DIVIDE_L, 0, TFL_PUTN },
109 { "modulo", TF_NUM, FT_LV_MODULO_L, 0, TFL_PUTN },
110 { "charleft", TF_NONE, FT_LV_CHAR_LEFT, 0, TFL_PUTN },
111 { "timenow", TF_NOW, FT_LV_LIT, 0, TFL_PUTN },
112
113 { "month", TF_COMP, FT_LS_MONTH, FT_PARSEDATE, TFL_PUTS },
114 { "lmonth", TF_COMP, FT_LS_LMONTH, FT_PARSEDATE, TFL_PUTS },
115 { "tzone", TF_COMP, FT_LS_ZONE, FT_PARSEDATE, TFL_PUTS },
116 { "day", TF_COMP, FT_LS_DAY, FT_PARSEDATE, TFL_PUTS },
117 { "weekday", TF_COMP, FT_LS_WEEKDAY, FT_PARSEDATE, TFL_PUTS },
118 { "tws", TF_COMP, FT_LS_822DATE, FT_PARSEDATE, TFL_PUTS },
119 { "sec", TF_COMP, FT_LV_SEC, FT_PARSEDATE, TFL_PUTN },
120 { "min", TF_COMP, FT_LV_MIN, FT_PARSEDATE, TFL_PUTN },
121 { "hour", TF_COMP, FT_LV_HOUR, FT_PARSEDATE, TFL_PUTN },
122 { "mday", TF_COMP, FT_LV_MDAY, FT_PARSEDATE, TFL_PUTN },
123 { "mon", TF_COMP, FT_LV_MON, FT_PARSEDATE, TFL_PUTN },
124 { "year", TF_COMP, FT_LV_YEAR, FT_PARSEDATE, TFL_PUTN },
125 { "yday", TF_COMP, FT_LV_YDAY, FT_PARSEDATE, TFL_PUTN },
126 { "wday", TF_COMP, FT_LV_WDAY, FT_PARSEDATE, TFL_PUTN },
127 { "zone", TF_COMP, FT_LV_ZONE, FT_PARSEDATE, TFL_PUTN },
128 { "clock", TF_COMP, FT_LV_CLOCK, FT_PARSEDATE, TFL_PUTN },
129 { "rclock", TF_COMP, FT_LV_RCLOCK, FT_PARSEDATE, TFL_PUTN },
130 { "sday", TF_COMP, FT_LV_DAYF, FT_PARSEDATE, TFL_PUTN },
131 { "szone", TF_COMP, FT_LV_ZONEF, FT_PARSEDATE, TFL_PUTN },
132 { "dst", TF_COMP, FT_LV_DST, FT_PARSEDATE, TFL_PUTN },
133 { "pretty", TF_COMP, FT_LS_PRETTY, FT_PARSEDATE, TFL_PUTS },
134 { "nodate", TF_COMP, FT_LV_COMPFLAG, FT_PARSEDATE, TFL_PUTN },
135 { "date2local", TF_COMP, FT_LOCALDATE, FT_PARSEDATE, 0 },
136 { "date2gmt", TF_COMP, FT_GMTDATE, FT_PARSEDATE, 0 },
137
138 { "pers", TF_COMP, FT_LS_PERS, FT_PARSEADDR, TFL_PUTS },
139 { "mbox", TF_COMP, FT_LS_MBOX, FT_PARSEADDR, TFL_PUTS },
140 { "host", TF_COMP, FT_LS_HOST, FT_PARSEADDR, TFL_PUTS },
141 { "path", TF_COMP, FT_LS_PATH, FT_PARSEADDR, TFL_PUTS },
142 { "gname", TF_COMP, FT_LS_GNAME, FT_PARSEADDR, TFL_PUTS },
143 { "note", TF_COMP, FT_LS_NOTE, FT_PARSEADDR, TFL_PUTS },
144 { "addr", TF_COMP, FT_LS_ADDR, FT_PARSEADDR, TFL_PUTS },
145 { "proper", TF_COMP, FT_LS_822ADDR, FT_PARSEADDR, TFL_PUTS },
146 { "type", TF_COMP, FT_LV_HOSTTYPE, FT_PARSEADDR, TFL_PUTN },
147 { "ingrp", TF_COMP, FT_LV_INGRPF, FT_PARSEADDR, TFL_PUTN },
148 { "nohost", TF_COMP, FT_LV_NOHOSTF, FT_PARSEADDR, TFL_PUTN },
149 { "formataddr", TF_EXPR_SV,FT_FORMATADDR, FT_FORMATADDR, 0 },
150 { "friendly", TF_COMP, FT_LS_FRIENDLY, FT_PARSEADDR, TFL_PUTS },
151
152 { "mymbox", TF_COMP, FT_LV_COMPFLAG, FT_MYMBOX, TFL_PUTN },
153 { "addtoseq", TF_STR, FT_ADDTOSEQ, 0, 0 },
154
155 { "unquote", TF_EXPR, FT_LS_UNQUOTE, 0, TFL_PUTS},
156
157 { NULL, 0, 0, 0, 0 }
158 };
159
160 /* Add new component to the hash table */
161 #define NEWCOMP(cm,name) do { \
162 cm = ((struct comp *) calloc(1, sizeof (struct comp)));\
163 cm->c_name = name;\
164 ncomp++;\
165 i = CHASH(name);\
166 cm->c_next = wantcomp[i];\
167 wantcomp[i] = cm; \
168 } while (0)
169
170 #define NEWFMT (next_fp++)
171 #define NEW(type,fill,wid) do {\
172 fp=NEWFMT; fp->f_type=(type); fp->f_fill=(fill); fp->f_width=(wid); \
173 } while (0)
174
175 /* Add (possibly new) component to the hash table */
176 #define ADDC(name) do { \
177 FINDCOMP(cm, name);\
178 if (!cm) {\
179 NEWCOMP(cm,name);\
180 }\
181 fp->f_comp = cm; \
182 } while (0)
183
184 #define LV(type, value) do { NEW(type,0,0); fp->f_value = (value); } while (0)
185 #define LS(type, str) do { NEW(type,0,0); fp->f_text = (str); } while (0)
186
187 #define PUTCOMP(comp) do { NEW(FT_COMP,0,0); ADDC(comp); } while (0)
188 #define PUTLIT(str) do { NEW(FT_LIT,0,0); fp->f_text = (str); } while (0)
189 #define PUTC(c) do { NEW(FT_CHAR,0,0); fp->f_char = (c); } while (0)
190
191 static char *format_string;
192 static unsigned char *usr_fstring; /* for CERROR */
193
194 #define CERROR(str) compile_error (str, cp)
195
196 /*
197 * external prototypes
198 */
199 extern char *getusername(void);
200
201 /*
202 * static prototypes
203 */
204 static struct ftable *lookup(char *);
205 static void compile_error(char *, char *);
206 static char *compile (char *);
207 static char *do_spec(char *);
208 static char *do_name(char *, int);
209 static char *do_func(char *);
210 static char *do_expr (char *, int);
211 static char *do_loop(char *);
212 static char *do_if(char *);
213
214
215 static struct ftable *
216 lookup(char *name)
217 {
218 register struct ftable *t = functable;
219 register char *nm;
220 register char c = *name;
221
222 while ((nm = t->name)) {
223 if (*nm == c && strcmp (nm, name) == 0)
224 return (ftbl = t);
225
226 t++;
227 }
228 return (struct ftable *) 0;
229 }
230
231
232 static void
233 compile_error(char *str, char *cp)
234 {
235 int i, errpos, errctx;
236
237 errpos = cp - format_string;
238 errctx = errpos > 20 ? 20 : errpos;
239 usr_fstring[errpos] = '\0';
240
241 for (i = errpos-errctx; i < errpos; i++) {
242 #ifdef LOCALE
243 if (iscntrl(usr_fstring[i]))
244 #else
245 if (usr_fstring[i] < 32)
246 #endif
247 usr_fstring[i] = '_';
248 }
249
250 advise(NULL, "\"%s\": format compile error - %s",
251 &usr_fstring[errpos-errctx], str);
252 adios (NULL, "%*s", errctx+1, "^");
253 }
254
255 /*
256 * Compile format string "fstring" into format list "fmt".
257 * Return the number of header components found in the format
258 * string.
259 */
260
261 int
262 fmt_compile(char *fstring, struct format **fmt)
263 {
264 register char *cp;
265 int i;
266
267 if (format_string)
268 free (format_string);
269 format_string = getcpy (fstring);
270 usr_fstring = fstring;
271
272 /* init the component hash table. */
273 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++)
274 wantcomp[i] = 0;
275
276 memset((char *) &fmt_mnull, 0, sizeof(fmt_mnull));
277
278 /* it takes at least 4 char to generate one format so we
279 * allocate a worst-case format array using 1/4 the length
280 * of the format string. We actually need twice this much
281 * to handle both pre-processing (e.g., address parsing) and
282 * normal processing.
283 */
284 i = strlen(fstring)/2 + 1;
285 if (i==1) i++;
286 next_fp = formatvec = (struct format *)calloc ((size_t) i,
287 sizeof(struct format));
288 if (next_fp == NULL)
289 adios (NULL, "unable to allocate format storage");
290
291 ncomp = 0;
292 infunction = 0;
293
294 cp = compile(format_string);
295 if (*cp) {
296 CERROR("extra '%>', '%|' or '%?'");
297 }
298 LV(FT_DONE, 0); /* really done */
299 *fmt = formatvec;
300
301 return (ncomp);
302 }
303
304 static char *
305 compile (char *sp)
306 {
307 register char *cp = sp;
308 register int c;
309
310 for (;;) {
311 sp = cp;
312 while ((c = *cp) && c != '%')
313 cp++;
314 *cp = 0;
315 switch (cp-sp) {
316 case 0:
317 break;
318 case 1:
319 PUTC(*sp);
320 break;
321 default:
322 PUTLIT(sp);
323 break;
324 }
325 if (c == 0)
326 return (cp);
327
328 switch (c = *++cp) {
329 case '%':
330 PUTC (*cp);
331 cp++;
332 break;
333
334 case '|':
335 case '>':
336 case '?':
337 case ']':
338 return (cp);
339
340 case '<':
341 cp = do_if(++cp);
342 break;
343
344 case '[': /* ] */
345 cp = do_loop(++cp);
346 break;
347
348 case ';': /* comment line */
349 cp++;
350 while ((c = *cp++) && c != '\n')
351 continue;
352 break;
353
354 default:
355 cp = do_spec(cp);
356 break;
357 }
358 }
359 }
360
361
362 static char *
363 do_spec(char *sp)
364 {
365 register char *cp = sp;
366 register int c;
367 #ifndef lint
368 register int ljust = 0;
369 #endif /* not lint */
370 register int wid = 0;
371 register char fill = ' ';
372
373 c = *cp++;
374 if (c == '-') {
375 ljust++;
376 c = *cp++;
377 }
378 if (c == '0') {
379 fill = c;
380 c = *cp++;
381 }
382 while (isdigit(c)) {
383 wid = wid*10 + (c - '0');
384 c = *cp++;
385 }
386 if (c == '{') {
387 cp = do_name(cp, 0);
388 if (! infunction)
389 fp->f_type = wid? FT_COMPF : FT_COMP;
390 }
391 else if (c == '(') {
392 cp = do_func(cp);
393 if (! infunction) {
394 if (ftbl->flags & TFL_PUTS) {
395 LV( wid? FT_STRF : FT_STR, ftbl->extra);
396 }
397 else if (ftbl->flags & TFL_PUTN) {
398 LV( wid? FT_NUMF : FT_NUM, ftbl->extra);
399 }
400 }
401 }
402 else {
403 CERROR("component or function name expected");
404 }
405 if (ljust)
406 wid = -wid;
407 fp->f_width = wid;
408 fp->f_fill = fill;
409
410 return (cp);
411 }
412
413 static char *
414 do_name(char *sp, int preprocess)
415 {
416 register char *cp = sp;
417 register int c;
418 register int i;
419 static int primed = 0;
420
421 while (isalnum(c = *cp++) || c == '-' || c == '_')
422 ;
423 if (c != '}') {
424 CERROR("'}' expected");
425 }
426 cp[-1] = '\0';
427 PUTCOMP(sp);
428 switch (preprocess) {
429
430 case FT_PARSEDATE:
431 if (cm->c_type & CT_ADDR) {
432 CERROR("component used as both date and address");
433 }
434 cm->c_tws = (struct tws *)
435 calloc((size_t) 1, sizeof(*cm->c_tws));
436 fp->f_type = preprocess;
437 PUTCOMP(sp);
438 cm->c_type |= CT_DATE;
439 break;
440
441 case FT_MYMBOX:
442 if (!primed) {
443 ismymbox ((struct mailname *) 0);
444 primed++;
445 }
446 /* fall through */
447 case FT_PARSEADDR:
448 if (cm->c_type & CT_DATE) {
449 CERROR("component used as both date and address");
450 }
451 cm->c_mn = &fmt_mnull;
452 fp->f_type = preprocess;
453 PUTCOMP(sp);
454 cm->c_type |= CT_ADDR;
455 break;
456
457 case FT_FORMATADDR:
458 if (cm->c_type & CT_DATE) {
459 CERROR("component used as both date and address");
460 }
461 cm->c_type |= CT_ADDR;
462 break;
463 }
464 return (cp);
465 }
466
467 static char *
468 do_func(char *sp)
469 {
470 register char *cp = sp;
471 register int c;
472 register struct ftable *t;
473 register int n;
474 int mflag; /* minus sign in NUM */
475
476 infunction++;
477
478 while (isalnum(c = *cp++))
479 ;
480 if (c != '(' && c != '{' && c != ' ' && c != ')') {
481 CERROR("'(', '{', ' ' or ')' expected");
482 }
483 cp[-1] = '\0';
484 if ((t = lookup (sp)) == 0) {
485 CERROR("unknown function");
486 }
487 if (isspace(c))
488 c = *cp++;
489
490 switch (t->type) {
491
492 case TF_COMP:
493 if (c != '{') {
494 CERROR("component name expected");
495 }
496 cp = do_name(cp, t->extra);
497 fp->f_type = t->f_type;
498 c = *cp++;
499 break;
500
501 case TF_NUM:
502 if ((mflag = (c == '-')))
503 c = *cp++;
504 n = 0;
505 while (isdigit(c)) {
506 n = n*10 + (c - '0');
507 c = *cp++;
508 }
509 if (mflag)
510 n = (-n);
511 LV(t->f_type,n);
512 break;
513
514 case TF_STR:
515 sp = cp - 1;
516 while (c && c != ')')
517 c = *cp++;
518 cp[-1] = '\0';
519 LS(t->f_type,sp);
520 break;
521
522 case TF_NONE:
523 LV(t->f_type,t->extra);
524 break;
525
526 case TF_MYBOX:
527 LS(t->f_type, getusername());
528 break;
529
530 case TF_NOW:
531 LV(t->f_type, time((time_t *) 0));
532 break;
533
534 case TF_EXPR_SV:
535 LV(FT_SAVESTR, 0);
536 /* fall through */
537 case TF_EXPR:
538 *--cp = c;
539 cp = do_expr(cp, t->extra);
540 LV(t->f_type, 0);
541 c = *cp++;
542 ftbl = t;
543 break;
544
545 case TF_NOP:
546 *--cp = c;
547 cp = do_expr(cp, t->extra);
548 c = *cp++;
549 ftbl = t;
550 break;
551 }
552 if (c != ')') {
553 CERROR("')' expected");
554 }
555 --infunction;
556 return (cp);
557 }
558
559 static char *
560 do_expr (char *sp, int preprocess)
561 {
562 register char *cp = sp;
563 register int c;
564
565 if ((c = *cp++) == '{') {
566 cp = do_name (cp, preprocess);
567 fp->f_type = FT_LS_COMP;
568 } else if (c == '(') {
569 cp = do_func (cp);
570 } else if (c == ')') {
571 return (--cp);
572 } else if (c == '%' && *cp == '<') {
573 cp = do_if (cp+1);
574 } else {
575 CERROR ("'(', '{', '%<' or ')' expected");
576 }
577 return (cp);
578 }
579
580 static char *
581 do_loop(char *sp)
582 {
583 register char *cp = sp;
584 struct format *floop;
585
586 floop = next_fp;
587 cp = compile (cp);
588 if (*cp++ != ']')
589 CERROR ("']' expected");
590
591 LV(FT_DONE, 1); /* not yet done */
592 LV(FT_GOTO, 0);
593 fp->f_skip = floop - fp; /* skip backwards */
594
595 return cp;
596 }
597
598 static char *
599 do_if(char *sp)
600 {
601 register char *cp = sp;
602 register struct format *fexpr,
603 *fif = (struct format *)NULL;
604 register int c = '<';
605
606 for (;;) {
607 if (c == '<') { /* doing an IF */
608 if ((c = *cp++) == '{') /*}*/{
609 cp = do_name(cp, 0);
610 fp->f_type = FT_LS_COMP;
611 LV (FT_IF_S, 0);
612 }
613 else if (c == '(') {
614 cp = do_func(cp);
615 /* see if we can merge the load and the "if" */
616 if (ftbl->f_type >= IF_FUNCS)
617 fp->f_type = ftbl->extra;
618 else {
619 /* Put out a string test or a value test depending
620 * on what this function's return type is.
621 */
622 if (ftbl->flags & TFL_PUTS) {
623 LV (FT_IF_S, 0);
624 } else {
625 LV (FT_IF_V_NE, 0);
626 }
627 }
628 }
629 else {
630 CERROR("'(' or '{' expected"); /*}*/
631 }
632 }
633
634 fexpr = fp; /* loc of [ELS]IF */
635 cp = compile (cp); /* compile IF TRUE stmts */
636 if (fif)
637 fif->f_skip = next_fp - fif;
638
639 if ((c = *cp++) == '|') { /* the last ELSE */
640 LV(FT_GOTO, 0);
641 fif = fp; /* loc of GOTO */
642 fexpr->f_skip = next_fp - fexpr;
643
644 fexpr = (struct format *)NULL;/* no extra ENDIF */
645
646 cp = compile (cp); /* compile ELSE stmts */
647 fif->f_skip = next_fp - fif;
648 c = *cp++;
649 }
650 else if (c == '?') { /* another ELSIF */
651 LV(FT_GOTO, 0);
652 fif = fp; /* loc of GOTO */
653 fexpr->f_skip = next_fp - fexpr;
654
655 c = '<'; /* impersonate an IF */
656 continue;
657 }
658 break;
659 }
660
661 if (c != '>') {
662 CERROR("'>' expected.");
663 }
664
665 if (fexpr) /* IF ... [ELSIF ...] ENDIF */
666 fexpr->f_skip = next_fp - fexpr;
667
668 return (cp);
669 }