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