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