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