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