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