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