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