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