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