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