]> diplodocus.org Git - nmh/blob - sbr/fmt_compile.c
Removed temporary probes added in commit
[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 * - Add code to fmtdump.c to display your new function.
39 *
40 * - Document the new function in the mh-format(5) man page.
41 *
42 */
43
44 #include <h/mh.h>
45 #include <h/addrsbr.h>
46 #include <h/tws.h>
47 #include <h/fmt_scan.h>
48 #include <h/fmt_compile.h>
49 #include <h/mts.h>
50 #include <h/utils.h>
51
52 #ifdef HAVE_SYS_TIME_H
53 # include <sys/time.h>
54 #endif
55 #include <time.h>
56
57 /*
58 * hash table for deciding if a component is "interesting"
59 */
60 static struct comp *wantcomp[128];
61
62 static struct format *formatvec; /* array to hold formats */
63 static struct format *next_fp; /* next free format slot */
64 static struct format *fp; /* current format slot */
65 static struct comp *cm; /* most recent comp ref */
66 static struct ftable *ftbl; /* most recent func ref */
67 static int ncomp;
68 static int infunction; /* function nesting cnt */
69
70 extern struct mailname fmt_mnull;
71
72 /* ftable->type (argument type) */
73 #define TF_COMP 0 /* component expected */
74 #define TF_NUM 1 /* number expected */
75 #define TF_STR 2 /* string expected */
76 #define TF_EXPR 3 /* component or func. expected */
77 #define TF_NONE 4 /* no argument */
78 #define TF_MYBOX 5 /* special - get current user's mbox */
79 #define TF_NOW 6 /* special - get current unix time */
80 #define TF_EXPR_SV 7 /* like expr but save current str reg */
81 #define TF_NOP 8 /* like expr but no result */
82 #define TF_MYNAME 9 /* special - get current name of user */
83 #define TF_MYHOST 10 /* special - get "local" hostname */
84 #define TF_LMBOX 11 /* special - get full local mailbox */
85
86 /* ftable->flags */
87 /* NB that TFL_PUTS is also used to decide whether the test
88 * in a "%<(function)..." should be a string or numeric one.
89 */
90 #define TFL_PUTS 1 /* implicit putstr if top level */
91 #define TFL_PUTN 2 /* implicit putnum if top level */
92
93 /*
94 * The functable array maps between the text names of format functions and
95 * the format instructions interpreted by the engine in fmt_scan.c.
96 *
97 * The elements of this structure are as follows:
98 *
99 * name - The name of the function as seen in the format string. This is
100 * what maps a particular function name into a format instruction.
101 * type - The type of argument this function expects. Those types are
102 * listed above (with the TF_ prefix). This affects what gets
103 * placed in the format instruction (the f_un union).
104 * f_type - The instruction corresponding to this function (from the list
105 * in fmt_compile.h).
106 * extra - Used by some functions to provide extra data to the compiler.
107 * Uses include:
108 * - Providing an alternate instruction to combine a load
109 * and test operation (see do_if()).
110 * - Passed in f_value in the format instruction to provide
111 * extra information for the engine (see FT_LV_DAT handling
112 * in fmt_scan.c).
113 * - Provide a hint as to preprocessing that is required for
114 * this instruction (see do_name()).
115 * flags - See the definitions for TFL_PUTS & TFL_PUTN above.
116 */
117
118 struct ftable {
119 char *name; /* function name */
120 char type; /* argument type */
121 char f_type; /* fmt type */
122 char extra; /* arg. type dependent extra info */
123 char flags;
124 };
125
126 static struct ftable functable[] = {
127 { "nonzero", TF_EXPR, FT_V_NE, FT_IF_V_NE, 0 },
128 { "zero", TF_EXPR, FT_V_EQ, FT_IF_V_EQ, 0 },
129 { "eq", TF_NUM, FT_V_EQ, FT_IF_V_EQ, 0 },
130 { "ne", TF_NUM, FT_V_NE, FT_IF_V_NE, 0 },
131 { "gt", TF_NUM, FT_V_GT, FT_IF_V_GT, 0 },
132 { "null", TF_EXPR, FT_S_NULL, FT_IF_S_NULL, 0 },
133 { "nonnull", TF_EXPR, FT_S_NONNULL, FT_IF_S, 0 },
134 { "match", TF_STR, FT_V_MATCH, FT_IF_MATCH, 0 },
135 { "amatch", TF_STR, FT_V_AMATCH, FT_IF_AMATCH, 0 },
136
137 { "putstr", TF_EXPR, FT_STR, 0, 0 },
138 { "putstrf", TF_EXPR, FT_STRF, 0, 0 },
139 { "putnum", TF_EXPR, FT_NUM, 0, 0 },
140 { "putnumf", TF_EXPR, FT_NUMF, 0, 0 },
141 { "putaddr", TF_STR, FT_PUTADDR, 0, 0 },
142 { "putlit", TF_EXPR, FT_STRLIT, 0, 0 },
143 { "zputlit", TF_EXPR, FT_STRLITZ, 0, 0 },
144 { "void", TF_NOP, 0, 0, 0 },
145
146 { "comp", TF_COMP, FT_LS_COMP, 0, TFL_PUTS },
147 { "lit", TF_STR, FT_LS_LIT, 0, TFL_PUTS },
148 { "getenv", TF_STR, FT_LS_GETENV, 0, TFL_PUTS },
149 { "profile", TF_STR, FT_LS_CFIND, 0, TFL_PUTS },
150 { "decodecomp", TF_COMP, FT_LS_DECODECOMP, 0, TFL_PUTS },
151 { "decode", TF_EXPR, FT_LS_DECODE, 0, TFL_PUTS },
152 { "trim", TF_EXPR, FT_LS_TRIM, 0, 0 },
153 { "compval", TF_COMP, FT_LV_COMP, 0, TFL_PUTN },
154 { "compflag", TF_COMP, FT_LV_COMPFLAG, 0, TFL_PUTN },
155 { "num", TF_NUM, FT_LV_LIT, 0, TFL_PUTN },
156 { "msg", TF_NONE, FT_LV_DAT, 0, TFL_PUTN },
157 { "cur", TF_NONE, FT_LV_DAT, 1, TFL_PUTN },
158 { "size", TF_NONE, FT_LV_DAT, 2, TFL_PUTN },
159 { "width", TF_NONE, FT_LV_DAT, 3, TFL_PUTN },
160 { "unseen", TF_NONE, FT_LV_DAT, 4, TFL_PUTN },
161 { "dat", TF_NUM, FT_LV_DAT, 0, TFL_PUTN },
162 { "strlen", TF_NONE, FT_LV_STRLEN, 0, TFL_PUTN },
163 { "me", TF_MYBOX, FT_LS_LIT, 0, TFL_PUTS },
164 { "myname", TF_MYNAME, FT_LS_LIT, 0, TFL_PUTS },
165 { "myhost", TF_MYHOST, FT_LS_LIT, 0, TFL_PUTS },
166 { "localmbox", TF_LMBOX, FT_LS_LIT, 0, TFL_PUTS },
167 { "plus", TF_NUM, FT_LV_PLUS_L, 0, TFL_PUTN },
168 { "minus", TF_NUM, FT_LV_MINUS_L, 0, TFL_PUTN },
169 { "divide", TF_NUM, FT_LV_DIVIDE_L, 0, TFL_PUTN },
170 { "modulo", TF_NUM, FT_LV_MODULO_L, 0, TFL_PUTN },
171 { "charleft", TF_NONE, FT_LV_CHAR_LEFT, 0, TFL_PUTN },
172 { "timenow", TF_NOW, FT_LV_LIT, 0, TFL_PUTN },
173
174 { "month", TF_COMP, FT_LS_MONTH, FT_PARSEDATE, TFL_PUTS },
175 { "lmonth", TF_COMP, FT_LS_LMONTH, FT_PARSEDATE, TFL_PUTS },
176 { "tzone", TF_COMP, FT_LS_ZONE, FT_PARSEDATE, TFL_PUTS },
177 { "day", TF_COMP, FT_LS_DAY, FT_PARSEDATE, TFL_PUTS },
178 { "weekday", TF_COMP, FT_LS_WEEKDAY, FT_PARSEDATE, TFL_PUTS },
179 { "tws", TF_COMP, FT_LS_822DATE, FT_PARSEDATE, TFL_PUTS },
180 { "sec", TF_COMP, FT_LV_SEC, FT_PARSEDATE, TFL_PUTN },
181 { "min", TF_COMP, FT_LV_MIN, FT_PARSEDATE, TFL_PUTN },
182 { "hour", TF_COMP, FT_LV_HOUR, FT_PARSEDATE, TFL_PUTN },
183 { "mday", TF_COMP, FT_LV_MDAY, FT_PARSEDATE, TFL_PUTN },
184 { "mon", TF_COMP, FT_LV_MON, FT_PARSEDATE, TFL_PUTN },
185 { "year", TF_COMP, FT_LV_YEAR, FT_PARSEDATE, TFL_PUTN },
186 { "yday", TF_COMP, FT_LV_YDAY, FT_PARSEDATE, TFL_PUTN },
187 { "wday", TF_COMP, FT_LV_WDAY, FT_PARSEDATE, TFL_PUTN },
188 { "zone", TF_COMP, FT_LV_ZONE, FT_PARSEDATE, TFL_PUTN },
189 { "clock", TF_COMP, FT_LV_CLOCK, FT_PARSEDATE, TFL_PUTN },
190 { "rclock", TF_COMP, FT_LV_RCLOCK, FT_PARSEDATE, TFL_PUTN },
191 { "sday", TF_COMP, FT_LV_DAYF, FT_PARSEDATE, TFL_PUTN },
192 { "szone", TF_COMP, FT_LV_ZONEF, FT_PARSEDATE, TFL_PUTN },
193 { "dst", TF_COMP, FT_LV_DST, FT_PARSEDATE, TFL_PUTN },
194 { "pretty", TF_COMP, FT_LS_PRETTY, FT_PARSEDATE, TFL_PUTS },
195 { "nodate", TF_COMP, FT_LV_COMPFLAG, FT_PARSEDATE, TFL_PUTN },
196 { "date2local", TF_COMP, FT_LOCALDATE, FT_PARSEDATE, 0 },
197 { "date2gmt", TF_COMP, FT_GMTDATE, FT_PARSEDATE, 0 },
198
199 { "pers", TF_COMP, FT_LS_PERS, FT_PARSEADDR, TFL_PUTS },
200 { "mbox", TF_COMP, FT_LS_MBOX, FT_PARSEADDR, TFL_PUTS },
201 { "host", TF_COMP, FT_LS_HOST, FT_PARSEADDR, TFL_PUTS },
202 { "path", TF_COMP, FT_LS_PATH, FT_PARSEADDR, TFL_PUTS },
203 { "gname", TF_COMP, FT_LS_GNAME, FT_PARSEADDR, TFL_PUTS },
204 { "note", TF_COMP, FT_LS_NOTE, FT_PARSEADDR, TFL_PUTS },
205 { "addr", TF_COMP, FT_LS_ADDR, FT_PARSEADDR, TFL_PUTS },
206 { "proper", TF_COMP, FT_LS_822ADDR, FT_PARSEADDR, TFL_PUTS },
207 { "type", TF_COMP, FT_LV_HOSTTYPE, FT_PARSEADDR, TFL_PUTN },
208 { "ingrp", TF_COMP, FT_LV_INGRPF, FT_PARSEADDR, TFL_PUTN },
209 { "nohost", TF_COMP, FT_LV_NOHOSTF, FT_PARSEADDR, TFL_PUTN },
210 { "formataddr", TF_EXPR_SV,FT_FORMATADDR, FT_FORMATADDR, 0 },
211 { "concataddr", TF_EXPR_SV,FT_CONCATADDR, FT_FORMATADDR, 0 },
212 { "friendly", TF_COMP, FT_LS_FRIENDLY, FT_PARSEADDR, TFL_PUTS },
213
214 { "mymbox", TF_COMP, FT_LV_COMPFLAG, FT_MYMBOX, TFL_PUTN },
215
216 { "unquote", TF_EXPR, FT_LS_UNQUOTE, 0, TFL_PUTS},
217
218 { NULL, 0, 0, 0, 0 }
219 };
220
221 /*
222 * Hash function for component name. The function should be
223 * case independent and probably shouldn't involve a routine
224 * call. This function is pretty good but will not work on
225 * single character component names.
226 */
227 #define CHASH(nm) (((((nm)[0]) - ((nm)[1])) & 0x1f) + (((nm)[2]) & 0x5f))
228
229 /*
230 * Find a component in the hash table.
231 */
232 #define FINDCOMP(comp,name) \
233 for (comp = wantcomp[CHASH(name)]; \
234 comp && strcmp(comp->c_name,name); \
235 comp = comp->c_next) \
236 ;
237
238 /* Add new component to the hash table */
239 #define NEWCOMP(cm,name) do { \
240 cm = ((struct comp *) calloc(1, sizeof (struct comp)));\
241 cm->c_name = getcpy(name);\
242 cm->c_refcount++;\
243 ncomp++;\
244 i = CHASH(name);\
245 cm->c_next = wantcomp[i];\
246 wantcomp[i] = cm; \
247 } while (0)
248
249 #define NEWFMT (next_fp++)
250 #define NEW(type,fill,wid) do {\
251 fp=NEWFMT; fp->f_type=(type); fp->f_fill=(fill); fp->f_width=(wid); \
252 } while (0)
253
254 /* Add (possibly new) component to the hash table */
255 #define ADDC(name) do { \
256 FINDCOMP(cm, name);\
257 if (!cm) {\
258 NEWCOMP(cm,name);\
259 }\
260 fp->f_comp = cm; \
261 fp->f_flags |= FF_COMPREF; \
262 cm->c_refcount++; \
263 } while (0)
264
265 #define LV(type, value) do { NEW(type,0,0); fp->f_value = (value); } while (0)
266 #define LS(type, str) do { NEW(type,0,0); fp->f_text = getcpy(str); fp->f_flags |= FF_STRALLOC; } while (0)
267
268 #define PUTCOMP(comp) do { NEW(FT_COMP,0,0); ADDC(comp); } while (0)
269 #define PUTLIT(str) do { NEW(FT_LIT,0,0); fp->f_text = getcpy(str); fp->f_flags |= FF_STRALLOC; } while (0)
270 #define PUTC(c) do { NEW(FT_CHAR,0,0); fp->f_char = (c); } while (0)
271
272 static char *format_string;
273 static char *usr_fstring; /* for CERROR */
274
275 #define CERROR(str) compile_error (str, cp)
276
277 /*
278 * static prototypes
279 */
280 static struct ftable *lookup(char *);
281 static void compile_error(char *, char *);
282 static char *compile (char *);
283 static char *do_spec(char *);
284 static char *do_name(char *, int);
285 static char *do_func(char *);
286 static char *do_expr (char *, int);
287 static char *do_loop(char *);
288 static char *do_if(char *);
289 static void free_component(struct comp *);
290 static void free_comptable(void);
291
292
293 /*
294 * Lookup a function name in the functable
295 */
296 static struct ftable *
297 lookup(char *name)
298 {
299 register struct ftable *t = functable;
300 register char *nm;
301 register char c = *name;
302
303 while ((nm = t->name)) {
304 if (*nm == c && strcmp (nm, name) == 0)
305 return (ftbl = t);
306
307 t++;
308 }
309 return (struct ftable *) 0;
310 }
311
312
313 static void
314 compile_error(char *str, char *cp)
315 {
316 int i, errpos, errctx;
317
318 errpos = cp - format_string;
319 errctx = errpos > 20 ? 20 : errpos;
320 usr_fstring[errpos] = '\0';
321
322 for (i = errpos-errctx; i < errpos; i++) {
323 #ifdef LOCALE
324 if (iscntrl((unsigned char) usr_fstring[i]))
325 #else
326 if (usr_fstring[i] < 32)
327 #endif
328 usr_fstring[i] = '_';
329 }
330
331 advise(NULL, "\"%s\": format compile error - %s",
332 &usr_fstring[errpos-errctx], str);
333 adios (NULL, "%*s", errctx+1, "^");
334 }
335
336 /*
337 * Compile format string "fstring" into format list "fmt".
338 * Return the number of header components found in the format
339 * string.
340 */
341
342 int
343 fmt_compile(char *fstring, struct format **fmt, int reset_comptable)
344 {
345 register char *cp;
346 size_t i;
347 static int comptable_initialized = 0;
348
349 format_string = getcpy (fstring);
350 usr_fstring = fstring;
351
352 if (reset_comptable || !comptable_initialized) {
353 free_comptable();
354 comptable_initialized = 1;
355 }
356
357 memset((char *) &fmt_mnull, 0, sizeof(fmt_mnull));
358
359 /* it takes at least 4 char to generate one format so we
360 * allocate a worst-case format array using 1/4 the length
361 * of the format string. We actually need twice this much
362 * to handle both pre-processing (e.g., address parsing) and
363 * normal processing.
364 */
365 i = strlen(fstring)/2 + 1;
366 if (i==1) i++;
367 next_fp = formatvec = (struct format *)calloc ((size_t) i,
368 sizeof(struct format));
369 if (next_fp == NULL)
370 adios (NULL, "unable to allocate format storage");
371
372 infunction = 0;
373
374 cp = compile(format_string);
375 if (*cp) {
376 CERROR("extra '%>', '%|' or '%?'");
377 }
378 LV(FT_DONE, 0); /* really done */
379 *fmt = formatvec;
380
381 free(format_string);
382 return (ncomp);
383 }
384
385 static char *
386 compile (char *sp)
387 {
388 register char *cp = sp;
389 register int c;
390
391 for (;;) {
392 sp = cp;
393 while ((c = *cp) && c != '%')
394 cp++;
395 *cp = 0;
396 switch (cp-sp) {
397 case 0:
398 break;
399 case 1:
400 PUTC(*sp);
401 break;
402 default:
403 PUTLIT(sp);
404 break;
405 }
406 if (c == 0)
407 return (cp);
408
409 switch (c = *++cp) {
410 case '%':
411 PUTC (*cp);
412 cp++;
413 break;
414
415 case '|':
416 case '>':
417 case '?':
418 case ']':
419 return (cp);
420
421 case '<':
422 cp = do_if(++cp);
423 break;
424
425 case '[': /* ] */
426 cp = do_loop(++cp);
427 break;
428
429 case ';': /* comment line */
430 cp++;
431 while ((c = *cp++) && c != '\n')
432 continue;
433 break;
434
435 default:
436 cp = do_spec(cp);
437 break;
438 }
439 }
440 }
441
442
443 /*
444 * Process functions & components (handle field width here as well
445 */
446 static char *
447 do_spec(char *sp)
448 {
449 register char *cp = sp;
450 register int c;
451 #ifndef lint
452 register int ljust = 0;
453 #endif /* not lint */
454 register int wid = 0;
455 register char fill = ' ';
456
457 c = *cp++;
458 if (c == '-') {
459 ljust++;
460 c = *cp++;
461 }
462 if (c == '0') {
463 fill = c;
464 c = *cp++;
465 }
466 while (isdigit(c)) {
467 wid = wid*10 + (c - '0');
468 c = *cp++;
469 }
470 if (c == '{') {
471 cp = do_name(cp, 0);
472 if (! infunction)
473 fp->f_type = wid? FT_COMPF : FT_COMP;
474 }
475 else if (c == '(') {
476 cp = do_func(cp);
477 if (! infunction) {
478 if (ftbl->flags & TFL_PUTS) {
479 LV( wid? FT_STRF : FT_STR, ftbl->extra);
480 }
481 else if (ftbl->flags & TFL_PUTN) {
482 LV( wid? FT_NUMF : FT_NUM, ftbl->extra);
483 }
484 }
485 }
486 else {
487 CERROR("component or function name expected");
488 }
489 if (ljust)
490 wid = -wid;
491 fp->f_width = wid;
492 fp->f_fill = fill;
493
494 return (cp);
495 }
496
497 /*
498 * Process a component name. Normally this involves generating an FT_COMP
499 * instruction for the specified component. If preprocess is set, then we
500 * do some extra processing.
501 */
502 static char *
503 do_name(char *sp, int preprocess)
504 {
505 register char *cp = sp;
506 register int c;
507 register int i;
508 static int primed = 0;
509
510 while (isalnum(c = *cp++) || c == '-' || c == '_')
511 ;
512 if (c != '}') {
513 CERROR("'}' expected");
514 }
515 cp[-1] = '\0';
516 PUTCOMP(sp);
517 switch (preprocess) {
518
519 case FT_PARSEDATE:
520 if (cm->c_type & CT_ADDR) {
521 CERROR("component used as both date and address");
522 }
523 cm->c_tws = (struct tws *)
524 calloc((size_t) 1, sizeof(*cm->c_tws));
525 fp->f_type = preprocess;
526 PUTCOMP(sp);
527 cm->c_type |= CT_DATE;
528 break;
529
530 case FT_MYMBOX:
531 if (!primed) {
532 ismymbox ((struct mailname *) 0);
533 primed++;
534 }
535 /* fall through */
536 case FT_PARSEADDR:
537 if (cm->c_type & CT_DATE) {
538 CERROR("component used as both date and address");
539 }
540 cm->c_mn = &fmt_mnull;
541 fp->f_type = preprocess;
542 PUTCOMP(sp);
543 cm->c_type |= CT_ADDR;
544 break;
545
546 case FT_FORMATADDR:
547 if (cm->c_type & CT_DATE) {
548 CERROR("component used as both date and address");
549 }
550 cm->c_type |= CT_ADDR;
551 break;
552 }
553 return (cp);
554 }
555
556 /*
557 * Generate one or more instructions corresponding to the named function.
558 * The different type of function arguments are handled here.
559 */
560 static char *
561 do_func(char *sp)
562 {
563 register char *cp = sp;
564 register int c;
565 register struct ftable *t;
566 register int n;
567 int mflag; /* minus sign in NUM */
568
569 infunction++;
570
571 while (isalnum(c = *cp++))
572 ;
573 if (c != '(' && c != '{' && c != ' ' && c != ')') {
574 CERROR("'(', '{', ' ' or ')' expected");
575 }
576 cp[-1] = '\0';
577 if ((t = lookup (sp)) == 0) {
578 CERROR("unknown function");
579 }
580 if (isspace(c))
581 c = *cp++;
582
583 switch (t->type) {
584
585 case TF_COMP:
586 if (c != '{') {
587 CERROR("component name expected");
588 }
589 cp = do_name(cp, t->extra);
590 fp->f_type = t->f_type;
591 c = *cp++;
592 break;
593
594 case TF_NUM:
595 if ((mflag = (c == '-')))
596 c = *cp++;
597 n = 0;
598 while (isdigit(c)) {
599 n = n*10 + (c - '0');
600 c = *cp++;
601 }
602 if (mflag)
603 n = (-n);
604 LV(t->f_type,n);
605 break;
606
607 case TF_STR:
608 sp = cp - 1;
609 while (c && c != ')')
610 c = *cp++;
611 cp[-1] = '\0';
612 LS(t->f_type,sp);
613 break;
614
615 case TF_NONE:
616 LV(t->f_type,t->extra);
617 break;
618
619 case TF_MYBOX:
620 LS(t->f_type, getusername());
621 break;
622
623 case TF_MYNAME:
624 LS(t->f_type, getfullname());
625 break;
626
627 case TF_MYHOST:
628 LS(t->f_type, LocalName(0));
629 break;
630
631 case TF_LMBOX:
632 LS(t->f_type, getlocalmbox());
633 break;
634
635 case TF_NOW:
636 LV(t->f_type, time((time_t *) 0));
637 break;
638
639 case TF_EXPR_SV:
640 LV(FT_SAVESTR, 0);
641 /* fall through */
642 case TF_EXPR:
643 *--cp = c;
644 cp = do_expr(cp, t->extra);
645 LV(t->f_type, 0);
646 c = *cp++;
647 ftbl = t;
648 break;
649
650 case TF_NOP:
651 *--cp = c;
652 cp = do_expr(cp, t->extra);
653 c = *cp++;
654 ftbl = t;
655 break;
656 }
657 if (c != ')') {
658 CERROR("')' expected");
659 }
660 --infunction;
661 return (cp);
662 }
663
664 /*
665 * Handle an expression as an argument. Basically we call one of do_name(),
666 * do_func(), or do_if()
667 */
668 static char *
669 do_expr (char *sp, int preprocess)
670 {
671 register char *cp = sp;
672 register int c;
673
674 if ((c = *cp++) == '{') {
675 cp = do_name (cp, preprocess);
676 fp->f_type = FT_LS_COMP;
677 } else if (c == '(') {
678 cp = do_func (cp);
679 } else if (c == ')') {
680 return (--cp);
681 } else if (c == '%' && *cp == '<') {
682 cp = do_if (cp+1);
683 } else {
684 CERROR ("'(', '{', '%<' or ')' expected");
685 }
686 return (cp);
687 }
688
689 /*
690 * I am guessing this was for some kind of loop statement, which would have
691 * looked like %[ .... %]. It looks like the way this would have worked
692 * is that the format engine would have seen that FT_DONE had a 1 in the
693 * f_un.f_un_value and then decided whether or not to continue the loop.
694 * There is no support for this in the format engine, so right now if
695 * you try using it you will reach the FT_DONE and simply stop. I'm leaving
696 * this here in case someone wants to continue the work.
697 *
698 * Okay, got some more information on this from John L. Romine! From an
699 * email he sent to the nmh-workers mailing list on December 2, 2010, he
700 * explains it thusly:
701 *
702 * In this case (scan, formatsbr) it has to do with an extension to
703 * the mh-format syntax to allow for looping.
704 *
705 * The scan format is processed once for each message. Those #ifdef
706 * JLR changes allowed for the top part of the format file to be
707 * processed once, then a second, looping part to be processed
708 * once per message. As I recall, there were new mh-format escape
709 * sequences to delimit the loop. This would have allowed for things
710 * like per-format column headings in the scan output.
711 *
712 * Since existing format files didn't include the scan listing
713 * header (it was hard-coded in scan.c) it would not have been
714 * backward-compatible. All existing format files (including any
715 * local ones) would have needed to be changed to include the format
716 * codes for a header. The practice at the time was not to introduce
717 * incompatible changes in a minor release, and I never managed to
718 * put out a newer major release.
719 *
720 * I can see how this would work, and I suspect part of the motivation was
721 * because the format compiler routines (at the time) couldn't really be
722 * called multiple times on the same message because the memory management
723 * was so lousy. That's been reworked and things are now a lot cleaner,
724 * so I suspect if we're going to allow a format string to be used for the
725 * scan header it might be simpler to have a separate format string just
726 * for the header. But I'll leave this code in for now just in case we
727 * decide that we want some kind of looping support.
728 */
729 static char *
730 do_loop(char *sp)
731 {
732 register char *cp = sp;
733 struct format *floop;
734
735 floop = next_fp;
736 cp = compile (cp);
737 if (*cp++ != ']')
738 CERROR ("']' expected");
739
740 LV(FT_DONE, 1); /* not yet done */
741 LV(FT_GOTO, 0);
742 fp->f_skip = floop - fp; /* skip backwards */
743
744 return cp;
745 }
746
747 /*
748 * Handle an if-elsif-endif statement. Note here that the branching
749 * is handled by the f_skip member of the struct format (which is really
750 * just f_width overloaded). This number controls how far to move forward
751 * (or back) in the format instruction array.
752 */
753 static char *
754 do_if(char *sp)
755 {
756 register char *cp = sp;
757 register struct format *fexpr,
758 *fif = (struct format *)NULL;
759 register int c = '<';
760
761 for (;;) {
762 if (c == '<') { /* doing an IF */
763 if ((c = *cp++) == '{') /*}*/{
764 cp = do_name(cp, 0);
765 fp->f_type = FT_LS_COMP;
766 LV (FT_IF_S, 0);
767 }
768 else if (c == '(') {
769 cp = do_func(cp);
770 /* see if we can merge the load and the "if" */
771 if (ftbl->f_type >= IF_FUNCS)
772 fp->f_type = ftbl->extra;
773 else {
774 /* Put out a string test or a value test depending
775 * on what this function's return type is.
776 */
777 if (ftbl->flags & TFL_PUTS) {
778 LV (FT_IF_S, 0);
779 } else {
780 LV (FT_IF_V_NE, 0);
781 }
782 }
783 }
784 else {
785 CERROR("'(' or '{' expected"); /*}*/
786 }
787 }
788
789 fexpr = fp; /* loc of [ELS]IF */
790 cp = compile (cp); /* compile IF TRUE stmts */
791 if (fif)
792 fif->f_skip = next_fp - fif;
793
794 if ((c = *cp++) == '|') { /* the last ELSE */
795 LV(FT_GOTO, 0);
796 fif = fp; /* loc of GOTO */
797 fexpr->f_skip = next_fp - fexpr;
798
799 fexpr = (struct format *)NULL;/* no extra ENDIF */
800
801 cp = compile (cp); /* compile ELSE stmts */
802 fif->f_skip = next_fp - fif;
803 c = *cp++;
804 }
805 else if (c == '?') { /* another ELSIF */
806 LV(FT_GOTO, 0);
807 fif = fp; /* loc of GOTO */
808 fexpr->f_skip = next_fp - fexpr;
809
810 c = '<'; /* impersonate an IF */
811 continue;
812 }
813 break;
814 }
815
816 if (c != '>') {
817 CERROR("'>' expected.");
818 }
819
820 if (fexpr) /* IF ... [ELSIF ...] ENDIF */
821 fexpr->f_skip = next_fp - fexpr;
822
823 return (cp);
824 }
825
826 /*
827 * Free a set of format instructions.
828 *
829 * What we do here is:
830 *
831 * - Iterate through the list of format instructions, freeing any references
832 * to allocated memory in each instruction.
833 * - Free component references.
834 * - If requested, reset the component hash table; that will also free any
835 * references to components stored there.
836 *
837 */
838
839 void
840 fmt_free(struct format *fmt, int reset_comptable)
841 {
842 struct format *fp = fmt;
843
844 if (fp) {
845 while (! (fp->f_type == FT_DONE && fp->f_value == 0)) {
846 if (fp->f_flags & FF_STRALLOC)
847 free(fp->f_text);
848 if (fp->f_flags & FF_COMPREF)
849 free_component(fp->f_comp);
850 fp++;
851 }
852 free(fmt);
853 }
854
855 if (reset_comptable)
856 free_comptable();
857 }
858
859 /*
860 * Free just the text strings from all of the component hash table entries
861 */
862
863 void
864 fmt_freecomptext(void)
865 {
866 unsigned int i;
867 struct comp *cm;
868
869 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++)
870 for (cm = wantcomp[i]; cm; cm = cm->c_next)
871 if (cm->c_text) {
872 free(cm->c_text);
873 cm->c_text = NULL;
874 }
875 }
876
877 /*
878 * Find a component in our hash table. This is just a public interface to
879 * the FINDCOMP macro, so we don't have to expose our hash table.
880 */
881
882 struct comp *
883 fmt_findcomp(char *component)
884 {
885 struct comp *cm;
886
887 FINDCOMP(cm, component);
888
889 return cm;
890 }
891
892 /*
893 * Like fmt_findcomp, but case-insensitive.
894 */
895
896 struct comp *
897 fmt_findcasecomp(char *component)
898 {
899 struct comp *cm;
900
901 for (cm = wantcomp[CHASH(component)]; cm; cm = cm->c_next)
902 if (strcasecmp(component, cm->c_name ? cm->c_name : "") == 0)
903 break;
904
905 return cm;
906 }
907
908 /*
909 * Add an entry to the component hash table
910 *
911 * Returns true if the component was added, 0 if it already existed.
912 *
913 */
914
915 int
916 fmt_addcompentry(char *component)
917 {
918 struct comp *cm;
919 int i;
920
921 FINDCOMP(cm, component);
922
923 if (cm)
924 return 0;
925
926 NEWCOMP(cm, component);
927
928 /*
929 * ncomp is really meant for fmt_compile() and this function is
930 * meant to be used outside of it. So decrement it just to be safe
931 * (internal callers should be using NEWCOMP()).
932 */
933
934 ncomp--;
935
936 return 1;
937 }
938
939 /*
940 * Add a string to a component hash table entry.
941 *
942 * Note the special handling for components marked with CT_ADDR. The comments
943 * in fmt_scan.h explain this in more detail.
944 */
945
946 int
947 fmt_addcomptext(char *component, char *text)
948 {
949 int i, found = 0, bucket = CHASH(component);
950 struct comp *cptr = wantcomp[bucket];
951 char *cp;
952
953 while (cptr) {
954 if (strcasecmp(component, cptr->c_name ? cptr->c_name : "") == 0) {
955 found++;
956 if (! cptr->c_text) {
957 cptr->c_text = getcpy(text);
958 } else {
959 i = strlen(cp = cptr->c_text) - 1;
960 if (cp[i] == '\n') {
961 if (cptr->c_type & CT_ADDR) {
962 cp[i] = '\0';
963 cp = add(",\n\t", cp);
964 } else {
965 cp = add("\t", cp);
966 }
967 }
968 cptr->c_text = add(text, cp);
969 }
970 }
971 cptr = cptr->c_next;
972 }
973
974 return found ? bucket : -1;
975 }
976
977 /*
978 * Append text to a component we've already found. See notes in fmt_scan.h
979 * for more information.
980 */
981
982 void
983 fmt_appendcomp(int bucket, char *component, char *text)
984 {
985 struct comp *cptr;
986
987 if (bucket != -1) {
988 for (cptr = wantcomp[bucket]; cptr; cptr = cptr->c_next)
989 if (strcasecmp(component, cptr->c_name ? cptr->c_name : "") == 0)
990 cptr->c_text = add(text, cptr->c_text);
991 }
992 }
993
994 /*
995 * Free and reset our component hash table
996 */
997
998 static void
999 free_comptable(void)
1000 {
1001 unsigned int i;
1002 struct comp *cm, *cm2;
1003
1004 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
1005 cm = wantcomp[i];
1006 while (cm != NULL) {
1007 cm2 = cm->c_next;
1008 free_component(cm);
1009 cm = cm2;
1010 }
1011 wantcomp[i] = 0;
1012 }
1013
1014 ncomp = 0;
1015 }
1016
1017 /*
1018 * Decrement the reference count of a component structure. If it reaches
1019 * zero, free it
1020 */
1021
1022 static void
1023 free_component(struct comp *cm)
1024 {
1025 if (--cm->c_refcount <= 0) {
1026 /* Shouldn't ever be NULL, but just in case ... */
1027 if (cm->c_name)
1028 free(cm->c_name);
1029 if (cm->c_text)
1030 free(cm->c_text);
1031 if (cm->c_type & CT_DATE)
1032 free(cm->c_tws);
1033 if (cm->c_type & CT_ADDR && cm->c_mn && cm->c_mn != &fmt_mnull)
1034 mnfree(cm->c_mn);
1035 free(cm);
1036 }
1037 }