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