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