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