Beginnings of the changes to fmt_compile for memory rework.
[mmh] / sbr / fmt_compile.c
1
2 /*
3  * fmt_compile.c -- "compile" format strings for fmt_scan
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  *
9  * This code compiles the format strings (documented in mh-format(5)) into
10  * an internal form to be later processed by fmt_scan.c.
11  *
12  * What happens here is that the format strings are parsed and an array
13  * of struct format structures are returned.  Each format structure is
14  * a single operation interpreted by the the routines in fmt_scan.c.
15  *
16  * There is a NOT a one-to-one correspondence between format strings and
17  * format instructions; some functions have side effects that can result
18  * in multiple instructions being generated.  The exact list of instructions
19  * generated by a format string can be seem with the nmh fmtdump utility.
20  *
21  * A list of format instructions can be found in fmt_compile.h.
22  *
23  * If you wish to add a new function, you will need to do the following
24  * things:
25  *
26  * - Add a new instruction to the list of instructions in fmt_compile.h.
27  *   Note that test instructions (starting with FT_IF_S_NULL) have special
28  *   handling, so if you are NOT writing a test function then you need
29  *   to insert it into the list before that _and_ bump all of the
30  *   following instruction numbers.
31  *
32  * - Add the function name to the functable[] array below, and write any
33  *   special code that your function may require in terms of parsing
34  *   (it very well may not need anything).
35  *
36  * - Add the code in fmt_scan.c to handle your new function.
37  *
38  * - Add code to fmtdump.c to display your new function.
39  *
40  * - Document the new function in the mh-format(5) man page.
41  *
42  */
43
44 #include <h/mh.h>
45 #include <h/addrsbr.h>
46 #include <h/tws.h>
47 #include <h/fmt_scan.h>
48 #include <h/fmt_compile.h>
49 #include <h/mts.h>
50
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54 #include <time.h>
55
56 /*
57  * hash table for deciding if a component is "interesting"
58  */
59 static struct comp *wantcomp[128];
60
61 static struct format *formatvec;        /* array to hold formats */
62 static struct format *next_fp;          /* next free format slot */
63 static struct format *fp;               /* current format slot   */
64 static struct comp *cm;                 /* most recent comp ref  */
65 static struct ftable *ftbl;             /* most recent func ref  */
66 static int ncomp;
67 static int infunction;                  /* function nesting cnt  */
68
69 extern struct mailname fmt_mnull;
70
71 /* ftable->type (argument type) */
72 #define TF_COMP    0        /* component expected                 */
73 #define TF_NUM     1        /* number expected                    */
74 #define TF_STR     2        /* string expected                    */
75 #define TF_EXPR    3        /* component or func. expected        */
76 #define TF_NONE    4        /* no argument                        */
77 #define TF_MYBOX   5        /* special - get current user's mbox  */
78 #define TF_NOW     6        /* special - get current unix time    */
79 #define TF_EXPR_SV 7        /* like expr but save current str reg */
80 #define TF_NOP     8        /* like expr but no result            */
81 #define TF_MYNAME  9        /* special - get current name of user */
82 #define TF_MYHOST  10       /* special - get "local" hostname     */
83 #define TF_LMBOX   11       /* special - get full local mailbox   */
84
85 /* ftable->flags */
86 /* NB that TFL_PUTS is also used to decide whether the test
87  * in a "%<(function)..." should be a string or numeric one.
88  */
89 #define TFL_PUTS   1        /* implicit putstr if top level */
90 #define TFL_PUTN   2        /* implicit putnum if top level */
91
92 /*
93  * The functable array maps between the text names of format functions and
94  * the format instructions interpreted by the engine in fmt_scan.c.
95  *
96  * The elements of this structure are as follows:
97  *
98  * name -   The name of the function as seen in the format string.  This is
99  *          what maps a particular function name into a format instruction.
100  * type -   The type of argument this function expects.  Those types are
101  *          listed above (with the TF_ prefix).  This affects what gets
102  *          placed in the format instruction (the f_un union).
103  * f_type - The instruction corresponding to this function (from the list
104  *          in fmt_compile.h).
105  * extra  - Used by some functions to provide extra data to the compiler.
106  *          Uses include:
107  *              - Providing an alternate instruction to combine a load
108  *                and test operation (see do_if()).
109  *              - Passed in f_value in the format instruction to provide
110  *                extra information for the engine (see FT_LV_DAT handling
111  *                in fmt_scan.c).
112  *              - Provide a hint as to preprocessing that is required for
113  *                this instruction (see do_name()).
114  * flags  - See the definitions for TFL_PUTS & TFL_PUTN above.
115  */
116
117 struct ftable {
118     char *name;         /* function name                  */
119     char type;          /* argument type                  */
120     char f_type;        /* fmt type                       */
121     char extra;         /* arg. type dependent extra info */
122     char flags;
123 };
124
125 static struct ftable functable[] = {
126      { "nonzero",    TF_EXPR,   FT_V_NE,        FT_IF_V_NE,     0 },
127      { "zero",       TF_EXPR,   FT_V_EQ,        FT_IF_V_EQ,     0 },
128      { "eq",         TF_NUM,    FT_V_EQ,        FT_IF_V_EQ,     0 },
129      { "ne",         TF_NUM,    FT_V_NE,        FT_IF_V_NE,     0 },
130      { "gt",         TF_NUM,    FT_V_GT,        FT_IF_V_GT,     0 },
131      { "null",       TF_EXPR,   FT_S_NULL,      FT_IF_S_NULL,   0 },
132      { "nonnull",    TF_EXPR,   FT_S_NONNULL,   FT_IF_S,        0 },
133      { "match",      TF_STR,    FT_V_MATCH,     FT_IF_MATCH,    0 },
134      { "amatch",     TF_STR,    FT_V_AMATCH,    FT_IF_AMATCH,   0 },
135
136      { "putstr",     TF_EXPR,   FT_STR,         0,              0 },
137      { "putstrf",    TF_EXPR,   FT_STRF,        0,              0 },
138      { "putnum",     TF_EXPR,   FT_NUM,         0,              0 },
139      { "putnumf",    TF_EXPR,   FT_NUMF,        0,              0 },
140      { "putaddr",    TF_STR,    FT_PUTADDR,     0,              0 },
141      { "putlit",     TF_EXPR,   FT_STRLIT,      0,              0 },
142      { "zputlit",    TF_EXPR,   FT_STRLITZ,     0,              0 },
143      { "void",       TF_NOP,    0,              0,              0 },
144
145      { "comp",       TF_COMP,   FT_LS_COMP,     0,              TFL_PUTS },
146      { "lit",        TF_STR,    FT_LS_LIT,      0,              TFL_PUTS },
147      { "getenv",     TF_STR,    FT_LS_GETENV,   0,              TFL_PUTS },
148      { "profile",    TF_STR,    FT_LS_CFIND,    0,              TFL_PUTS },
149      { "decodecomp", TF_COMP,   FT_LS_DECODECOMP, 0,            TFL_PUTS },
150      { "decode",     TF_EXPR,   FT_LS_DECODE,   0,              TFL_PUTS },
151      { "trim",       TF_EXPR,   FT_LS_TRIM,     0,              0 },
152      { "compval",    TF_COMP,   FT_LV_COMP,     0,              TFL_PUTN },
153      { "compflag",   TF_COMP,   FT_LV_COMPFLAG, 0,              TFL_PUTN },
154      { "num",        TF_NUM,    FT_LV_LIT,      0,              TFL_PUTN },
155      { "msg",        TF_NONE,   FT_LV_DAT,      0,              TFL_PUTN },
156      { "cur",        TF_NONE,   FT_LV_DAT,      1,              TFL_PUTN },
157      { "size",       TF_NONE,   FT_LV_DAT,      2,              TFL_PUTN },
158      { "width",      TF_NONE,   FT_LV_DAT,      3,              TFL_PUTN },
159      { "unseen",     TF_NONE,   FT_LV_DAT,      4,              TFL_PUTN },
160      { "dat",        TF_NUM,    FT_LV_DAT,      0,              TFL_PUTN },
161      { "strlen",     TF_NONE,   FT_LV_STRLEN,   0,              TFL_PUTN },
162      { "me",         TF_MYBOX,  FT_LS_LIT,      0,              TFL_PUTS },
163      { "myname",     TF_MYNAME, FT_LS_LIT,      0,              TFL_PUTS },
164      { "myhost",     TF_MYHOST, FT_LS_LIT,      0,              TFL_PUTS },
165      { "localmbox",  TF_LMBOX,  FT_LS_LIT,      0,              TFL_PUTS },
166      { "plus",       TF_NUM,    FT_LV_PLUS_L,   0,              TFL_PUTN },
167      { "minus",      TF_NUM,    FT_LV_MINUS_L,  0,              TFL_PUTN },
168      { "divide",     TF_NUM,    FT_LV_DIVIDE_L, 0,              TFL_PUTN },
169      { "modulo",     TF_NUM,    FT_LV_MODULO_L, 0,              TFL_PUTN },
170      { "charleft",   TF_NONE,   FT_LV_CHAR_LEFT, 0,             TFL_PUTN },
171      { "timenow",    TF_NOW,    FT_LV_LIT,      0,              TFL_PUTN },
172
173      { "month",      TF_COMP,   FT_LS_MONTH,    FT_PARSEDATE,   TFL_PUTS },
174      { "lmonth",     TF_COMP,   FT_LS_LMONTH,   FT_PARSEDATE,   TFL_PUTS },
175      { "tzone",      TF_COMP,   FT_LS_ZONE,     FT_PARSEDATE,   TFL_PUTS },
176      { "day",        TF_COMP,   FT_LS_DAY,      FT_PARSEDATE,   TFL_PUTS },
177      { "weekday",    TF_COMP,   FT_LS_WEEKDAY,  FT_PARSEDATE,   TFL_PUTS },
178      { "tws",        TF_COMP,   FT_LS_822DATE,  FT_PARSEDATE,   TFL_PUTS },
179      { "sec",        TF_COMP,   FT_LV_SEC,      FT_PARSEDATE,   TFL_PUTN },
180      { "min",        TF_COMP,   FT_LV_MIN,      FT_PARSEDATE,   TFL_PUTN },
181      { "hour",       TF_COMP,   FT_LV_HOUR,     FT_PARSEDATE,   TFL_PUTN },
182      { "mday",       TF_COMP,   FT_LV_MDAY,     FT_PARSEDATE,   TFL_PUTN },
183      { "mon",        TF_COMP,   FT_LV_MON,      FT_PARSEDATE,   TFL_PUTN },
184      { "year",       TF_COMP,   FT_LV_YEAR,     FT_PARSEDATE,   TFL_PUTN },
185      { "yday",       TF_COMP,   FT_LV_YDAY,     FT_PARSEDATE,   TFL_PUTN },
186      { "wday",       TF_COMP,   FT_LV_WDAY,     FT_PARSEDATE,   TFL_PUTN },
187      { "zone",       TF_COMP,   FT_LV_ZONE,     FT_PARSEDATE,   TFL_PUTN },
188      { "clock",      TF_COMP,   FT_LV_CLOCK,    FT_PARSEDATE,   TFL_PUTN },
189      { "rclock",     TF_COMP,   FT_LV_RCLOCK,   FT_PARSEDATE,   TFL_PUTN },
190      { "sday",       TF_COMP,   FT_LV_DAYF,     FT_PARSEDATE,   TFL_PUTN },
191      { "szone",      TF_COMP,   FT_LV_ZONEF,    FT_PARSEDATE,   TFL_PUTN },
192      { "dst",        TF_COMP,   FT_LV_DST,      FT_PARSEDATE,   TFL_PUTN },
193      { "pretty",     TF_COMP,   FT_LS_PRETTY,   FT_PARSEDATE,   TFL_PUTS },
194      { "nodate",     TF_COMP,   FT_LV_COMPFLAG, FT_PARSEDATE,   TFL_PUTN },
195      { "date2local", TF_COMP,   FT_LOCALDATE,   FT_PARSEDATE,   0 },
196      { "date2gmt",   TF_COMP,   FT_GMTDATE,     FT_PARSEDATE,   0 },
197
198      { "pers",       TF_COMP,   FT_LS_PERS,     FT_PARSEADDR,   TFL_PUTS },
199      { "mbox",       TF_COMP,   FT_LS_MBOX,     FT_PARSEADDR,   TFL_PUTS },
200      { "host",       TF_COMP,   FT_LS_HOST,     FT_PARSEADDR,   TFL_PUTS },
201      { "path",       TF_COMP,   FT_LS_PATH,     FT_PARSEADDR,   TFL_PUTS },
202      { "gname",      TF_COMP,   FT_LS_GNAME,    FT_PARSEADDR,   TFL_PUTS },
203      { "note",       TF_COMP,   FT_LS_NOTE,     FT_PARSEADDR,   TFL_PUTS },
204      { "addr",       TF_COMP,   FT_LS_ADDR,     FT_PARSEADDR,   TFL_PUTS },
205      { "proper",     TF_COMP,   FT_LS_822ADDR,  FT_PARSEADDR,   TFL_PUTS },
206      { "type",       TF_COMP,   FT_LV_HOSTTYPE, FT_PARSEADDR,   TFL_PUTN },
207      { "ingrp",      TF_COMP,   FT_LV_INGRPF,   FT_PARSEADDR,   TFL_PUTN },
208      { "nohost",     TF_COMP,   FT_LV_NOHOSTF,  FT_PARSEADDR,   TFL_PUTN },
209      { "formataddr", TF_EXPR_SV,FT_FORMATADDR,  FT_FORMATADDR,  0 },
210      { "concataddr", TF_EXPR_SV,FT_CONCATADDR,  FT_FORMATADDR,  0 },
211      { "friendly",   TF_COMP,   FT_LS_FRIENDLY, FT_PARSEADDR,   TFL_PUTS },
212
213      { "mymbox",     TF_COMP,   FT_LV_COMPFLAG, FT_MYMBOX,      TFL_PUTN },
214      { "addtoseq",   TF_STR,    FT_ADDTOSEQ,    0,              0 },
215
216      { "unquote",   TF_EXPR,    FT_LS_UNQUOTE,  0,              TFL_PUTS},
217
218      { NULL,         0,         0,              0,              0 }
219 };
220
221 /* Add new component to the hash table */
222 #define NEWCOMP(cm,name) do { \
223         cm = ((struct comp *) calloc(1, sizeof (struct comp)));\
224         cm->c_name = name;\
225         ncomp++;\
226         i = CHASH(name);\
227         cm->c_next = wantcomp[i];\
228         wantcomp[i] = cm; \
229         } while (0)
230
231 #define NEWFMT (next_fp++)
232 #define NEW(type,fill,wid) do {\
233         fp=NEWFMT; fp->f_type=(type); fp->f_fill=(fill); fp->f_width=(wid); \
234         } while (0)
235
236 /* Add (possibly new) component to the hash table */
237 #define ADDC(name) do { \
238         FINDCOMP(cm, name);\
239         if (!cm) {\
240             NEWCOMP(cm,name);\
241         }\
242         fp->f_comp = cm; \
243         } while (0)
244
245 #define LV(type, value)         do { NEW(type,0,0); fp->f_value = (value); } while (0)
246 #define LS(type, str)           do { NEW(type,0,0); fp->f_text = (str); } while (0)
247
248 #define PUTCOMP(comp)           do { NEW(FT_COMP,0,0); ADDC(comp); } while (0)
249 #define PUTLIT(str)             do { NEW(FT_LIT,0,0); fp->f_text = getcpy(str); } while (0)
250 #define PUTC(c)                 do { NEW(FT_CHAR,0,0); fp->f_char = (c); } while (0)
251
252 static char *format_string;
253 static unsigned char *usr_fstring;      /* for CERROR */
254
255 #define CERROR(str) compile_error (str, cp)
256
257 /*
258  * static prototypes
259  */
260 static struct ftable *lookup(char *);
261 static void compile_error(char *, char *);
262 static char *compile (char *);
263 static char *do_spec(char *);
264 static char *do_name(char *, int);
265 static char *do_func(char *);
266 static char *do_expr (char *, int);
267 static char *do_loop(char *);
268 static char *do_if(char *);
269 static void free_component(struct comp *);
270 static void free_comptable(void);
271
272
273 /*
274  * Lookup a function name in the functable
275  */
276 static struct ftable *
277 lookup(char *name)
278 {
279     register struct ftable *t = functable;
280     register char *nm;
281     register char c = *name;
282
283     while ((nm = t->name)) {
284         if (*nm == c && strcmp (nm, name) == 0)
285             return (ftbl = t);
286
287         t++;
288     }
289     return (struct ftable *) 0;
290 }
291
292
293 static void
294 compile_error(char *str, char *cp)
295 {
296     int i, errpos, errctx;
297
298     errpos = cp - format_string;
299     errctx = errpos > 20 ? 20 : errpos;
300     usr_fstring[errpos] = '\0';
301
302     for (i = errpos-errctx; i < errpos; i++) {
303 #ifdef LOCALE
304         if (iscntrl(usr_fstring[i]))
305 #else
306         if (usr_fstring[i] < 32)
307 #endif
308             usr_fstring[i] = '_';
309     }
310
311     advise(NULL, "\"%s\": format compile error - %s",
312            &usr_fstring[errpos-errctx], str);
313     adios (NULL, "%*s", errctx+1, "^");
314 }
315
316 /*
317  * Compile format string "fstring" into format list "fmt".
318  * Return the number of header components found in the format
319  * string.
320  */
321
322 int
323 fmt_compile(char *fstring, struct format **fmt, int reset_comptable)
324 {
325     register char *cp;
326     size_t i;
327
328     format_string = getcpy (fstring);
329     usr_fstring = fstring;
330
331     if (reset_comptable)
332         free_comptable();
333
334     /* init the component hash table. */
335     for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++)
336         wantcomp[i] = 0;
337
338     memset((char *) &fmt_mnull, 0, sizeof(fmt_mnull));
339
340     /* it takes at least 4 char to generate one format so we
341      * allocate a worst-case format array using 1/4 the length
342      * of the format string.  We actually need twice this much
343      * to handle both pre-processing (e.g., address parsing) and
344      * normal processing.
345      */
346     i = strlen(fstring)/2 + 1;
347                 if (i==1) i++;
348     next_fp = formatvec = (struct format *)calloc ((size_t) i,
349                                                    sizeof(struct format));
350     if (next_fp == NULL)
351         adios (NULL, "unable to allocate format storage");
352
353     ncomp = 0;
354     infunction = 0;
355
356     cp = compile(format_string);
357     if (*cp) {
358         CERROR("extra '%>', '%|' or '%?'");
359     }
360     LV(FT_DONE, 0);             /* really done */
361     *fmt = formatvec;
362
363     free(format_string);
364     return (ncomp);
365 }
366
367 static char *
368 compile (char *sp)
369 {
370     register char *cp = sp;
371     register int  c;
372
373     for (;;) {
374         sp = cp;
375         while ((c = *cp) && c != '%')
376             cp++;
377         *cp = 0;
378         switch (cp-sp) {
379         case 0:
380             break;
381         case 1:
382             PUTC(*sp);
383             break;
384         default:
385             PUTLIT(sp);
386             break;
387         }
388         if (c == 0)
389             return (cp);
390
391         switch (c = *++cp) {
392         case '%':
393             PUTC (*cp);
394             cp++;
395             break;
396
397         case '|':
398         case '>':
399         case '?':
400         case ']':
401             return (cp);
402
403         case '<':
404             cp = do_if(++cp);
405             break;
406
407         case '[':       /* ] */
408             cp = do_loop(++cp);
409             break;
410
411         case ';':       /* comment line */
412             cp++;
413             while ((c = *cp++) && c != '\n')
414                 continue;
415             break;
416
417         default:
418             cp = do_spec(cp);
419             break;
420         }
421     }
422 }
423
424
425 /*
426  * Process functions & components (handle field width here as well
427  */
428 static char *
429 do_spec(char *sp)
430 {
431     register char *cp = sp;
432     register int c;
433 #ifndef lint
434     register int ljust = 0;
435 #endif  /* not lint */
436     register int wid = 0;
437     register char fill = ' ';
438
439     c = *cp++;
440     if (c == '-') {
441         ljust++;
442         c = *cp++;
443     }
444     if (c == '0') {
445         fill = c;
446         c = *cp++;
447     }
448     while (isdigit(c)) {
449         wid = wid*10 + (c - '0');
450         c = *cp++;
451     }
452     if (c == '{') {
453         cp = do_name(cp, 0);
454         if (! infunction)
455             fp->f_type = wid? FT_COMPF : FT_COMP;
456     }
457     else if (c == '(') {
458         cp = do_func(cp);
459         if (! infunction) {
460             if (ftbl->flags & TFL_PUTS) {
461                 LV( wid? FT_STRF : FT_STR, ftbl->extra);
462             }
463             else if (ftbl->flags & TFL_PUTN) {
464                 LV( wid? FT_NUMF : FT_NUM, ftbl->extra);
465             }
466         }
467     }
468     else {
469         CERROR("component or function name expected");
470     }
471     if (ljust)
472         wid = -wid;
473     fp->f_width = wid;
474     fp->f_fill = fill;
475
476     return (cp);
477 }
478
479 /*
480  * Process a component name.  Normally this involves generating an FT_COMP
481  * instruction for the specified component.  If preprocess is set, then we
482  * do some extra processing.
483  */
484 static char *
485 do_name(char *sp, int preprocess)
486 {
487     register char *cp = sp;
488     register int c;
489     register int i;
490     static int primed = 0;
491
492     while (isalnum(c = *cp++) || c == '-' || c == '_')
493         ;
494     if (c != '}') {
495         CERROR("'}' expected");
496     }
497     cp[-1] = '\0';
498     PUTCOMP(sp);
499     switch (preprocess) {
500
501     case FT_PARSEDATE:
502         if (cm->c_type & CT_ADDR) {
503             CERROR("component used as both date and address");
504         }
505         cm->c_tws = (struct tws *)
506             calloc((size_t) 1, sizeof(*cm->c_tws));
507         fp->f_type = preprocess;
508         PUTCOMP(sp);
509         cm->c_type |= CT_DATE;
510         break;
511
512     case FT_MYMBOX:
513         if (!primed) {
514             ismymbox ((struct mailname *) 0);
515             primed++;
516         }
517         /* fall through */
518     case FT_PARSEADDR:
519         if (cm->c_type & CT_DATE) {
520             CERROR("component used as both date and address");
521         }
522         cm->c_mn = &fmt_mnull;
523         fp->f_type = preprocess;
524         PUTCOMP(sp);
525         cm->c_type |= CT_ADDR;
526         break;
527
528     case FT_FORMATADDR:
529         if (cm->c_type & CT_DATE) {
530             CERROR("component used as both date and address");
531         }
532         cm->c_type |= CT_ADDR;
533         break;
534     }
535     return (cp);
536 }
537
538 /*
539  * Generate one or more instructions corresponding to the named function.
540  * The different type of function arguments are handled here.
541  */
542 static char *
543 do_func(char *sp)
544 {
545     register char *cp = sp;
546     register int c;
547     register struct ftable *t;
548     register int n;
549     int mflag;          /* minus sign in NUM */
550
551     infunction++;
552
553     while (isalnum(c = *cp++)) 
554         ;
555     if (c != '(' && c != '{' && c != ' ' && c != ')') {
556         CERROR("'(', '{', ' ' or ')' expected");
557     }
558     cp[-1] = '\0';
559     if ((t = lookup (sp)) == 0) {
560         CERROR("unknown function");
561     }
562     if (isspace(c))
563         c = *cp++;
564
565     switch (t->type) {
566
567     case TF_COMP:
568         if (c != '{') {
569             CERROR("component name expected");
570         }
571         cp = do_name(cp, t->extra);
572         fp->f_type = t->f_type;
573         c = *cp++;
574         break;
575
576     case TF_NUM:
577         if ((mflag = (c == '-')))
578             c = *cp++;
579         n = 0;
580         while (isdigit(c)) {
581             n = n*10 + (c - '0');
582             c = *cp++;
583         }
584         if (mflag)
585             n = (-n);
586         LV(t->f_type,n);
587         break;
588
589     case TF_STR:
590         sp = cp - 1;
591         while (c && c != ')')
592             c = *cp++;
593         cp[-1] = '\0';
594         LS(t->f_type,sp);
595         break;
596
597     case TF_NONE:
598         LV(t->f_type,t->extra);
599         break;
600
601     case TF_MYBOX:
602         LS(t->f_type, getusername());
603         break;
604
605     case TF_MYNAME:
606         LS(t->f_type, getfullname());
607         break;
608
609     case TF_MYHOST:
610         LS(t->f_type, LocalName(0));
611         break;
612
613     case TF_LMBOX:
614         LS(t->f_type, getlocalmbox());
615         break;
616
617     case TF_NOW:
618         LV(t->f_type, time((time_t *) 0));
619         break;
620
621     case TF_EXPR_SV:
622         LV(FT_SAVESTR, 0);
623         /* fall through */
624     case TF_EXPR:
625         *--cp = c;
626         cp = do_expr(cp, t->extra);
627         LV(t->f_type, 0);
628         c = *cp++;
629         ftbl = t;
630         break;
631
632     case TF_NOP:
633         *--cp = c;
634         cp = do_expr(cp, t->extra);
635         c = *cp++;
636         ftbl = t;
637         break;
638     }
639     if (c != ')') {
640         CERROR("')' expected");
641     }
642     --infunction;
643     return (cp);
644 }
645
646 /*
647  * Handle an expression as an argument.  Basically we call one of do_name(),
648  * do_func(), or do_if()
649  */
650 static char *
651 do_expr (char *sp, int preprocess)
652 {
653     register char *cp = sp;
654     register int  c;
655
656     if ((c = *cp++) == '{') {
657         cp = do_name (cp, preprocess);
658         fp->f_type = FT_LS_COMP;
659     } else if (c == '(') {
660         cp = do_func (cp);
661     } else if (c == ')') {
662         return (--cp);
663     } else if (c == '%' && *cp == '<') {
664         cp = do_if (cp+1);
665     } else {
666         CERROR ("'(', '{', '%<' or ')' expected");
667     }
668     return (cp);
669 }
670
671 /*
672  * I am guessing this was for some kind of loop statement, which would have
673  * looked like %[ .... %].  It looks like the way this would have worked
674  * is that the format engine would have seen that FT_DONE had a 1 in the
675  * f_un.f_un_value and then decided whether or not to continue the loop.
676  * There is no support for this in the format engine, so right now if
677  * you try using it you will reach the FT_DONE and simply stop.  I'm leaving
678  * this here in case someone wants to continue the work.
679  */
680 static char *
681 do_loop(char *sp)
682 {
683     register char *cp = sp;
684     struct format *floop;
685
686     floop = next_fp;
687     cp = compile (cp);
688     if (*cp++ != ']')
689         CERROR ("']' expected");
690
691     LV(FT_DONE, 1);             /* not yet done */
692     LV(FT_GOTO, 0);
693     fp->f_skip = floop - fp;    /* skip backwards */
694
695     return cp;
696 }
697
698 /*
699  * Handle an if-elsif-endif statement.  Note here that the branching
700  * is handled by the f_skip member of the struct format (which is really
701  * just f_width overloaded).  This number controls how far to move forward
702  * (or back) in the format instruction array.
703  */
704 static char *
705 do_if(char *sp)
706 {
707     register char *cp = sp;
708     register struct format *fexpr,
709                            *fif = (struct format *)NULL;
710     register int c = '<';
711
712     for (;;) {
713         if (c == '<') {                 /* doing an IF */
714             if ((c = *cp++) == '{') /*}*/{
715                 cp = do_name(cp, 0);
716                 fp->f_type = FT_LS_COMP;
717                 LV (FT_IF_S, 0);
718             }
719             else if (c == '(') {
720                 cp = do_func(cp);
721                 /* see if we can merge the load and the "if" */
722                 if (ftbl->f_type >= IF_FUNCS)
723                     fp->f_type = ftbl->extra;
724                 else {
725                     /* Put out a string test or a value test depending
726                      * on what this function's return type is.
727                      */
728                     if (ftbl->flags & TFL_PUTS) {
729                         LV (FT_IF_S, 0);
730                     } else {
731                         LV (FT_IF_V_NE, 0);
732                     }
733                 }
734             }
735             else {
736                 CERROR("'(' or '{' expected");  /*}*/
737             }
738         }
739
740         fexpr = fp;                     /* loc of [ELS]IF */
741         cp = compile (cp);              /* compile IF TRUE stmts */
742         if (fif)
743             fif->f_skip = next_fp - fif;
744
745         if ((c = *cp++) == '|') {       /* the last ELSE */
746             LV(FT_GOTO, 0);
747             fif = fp;                   /* loc of GOTO */
748             fexpr->f_skip = next_fp - fexpr;
749
750             fexpr = (struct format *)NULL;/* no extra ENDIF */
751
752             cp = compile (cp);          /* compile ELSE stmts */
753             fif->f_skip = next_fp - fif;
754             c = *cp++;
755         }
756         else if (c == '?') {            /* another ELSIF */
757             LV(FT_GOTO, 0);
758             fif = fp;                   /* loc of GOTO */
759             fexpr->f_skip = next_fp - fexpr;
760
761             c = '<';                    /* impersonate an IF */
762             continue;
763         }
764         break;
765     }
766
767     if (c != '>') {
768         CERROR("'>' expected.");
769     }
770
771     if (fexpr)                          /* IF ... [ELSIF ...] ENDIF */
772         fexpr->f_skip = next_fp - fexpr;
773
774     return (cp);
775 }
776
777 /*
778  * Free and reset our component hash table
779  */
780
781 static void
782 free_comptable(void)
783 {
784     int i;
785     struct comp *cm, *cm2;
786
787     for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
788         cm = wantcomp[i];
789         while (cm != NULL) {
790             cm2 = cm->c_next;
791             free_component(cm);
792             cm = cm2;
793         }
794         wantcomp[i] = 0;
795     }
796 }
797
798 /*
799  * Decrement the reference count of a component structure.  If it reaches
800  * zero, free it
801  */
802
803 static void
804 free_component(struct comp *cm)
805 {
806     if (--cm->c_refcount <= 0) {
807         free(cm);
808     }
809 }