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