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