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