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