]>
diplodocus.org Git - nmh/blob - sbr/fmt_compile.c
1 /* fmt_compile.c -- "compile" format strings for fmt_scan
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.
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.
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.
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.
19 * A list of format instructions can be found in fmt_compile.h.
21 * If you wish to add a new function, you will need to do the following
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.
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).
34 * - Add the code in fmt_scan.c to handle your new function.
36 * - Add code to fmttest.c to display your new function.
38 * - Document the new function in mh-format(5).
43 #include <h/addrsbr.h>
45 #include <h/fmt_scan.h>
46 #include <h/fmt_compile.h>
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
57 * hash table for deciding if a component is "interesting"
59 static struct comp
*wantcomp
[128];
61 static struct format
*formatvec
; /* array to hold formats */
62 static struct format
*next_fp
; /* next free format slot */
63 static struct format
*fp
; /* current format slot */
64 static struct comp
*cm
; /* most recent comp ref */
65 static struct ftable
*ftbl
; /* most recent func ref */
67 static int infunction
; /* function nesting cnt */
69 extern struct mailname fmt_mnull
;
71 /* ftable->type (argument type) */
72 #define TF_COMP 0 /* component expected */
73 #define TF_NUM 1 /* number expected */
74 #define TF_STR 2 /* string expected */
75 #define TF_EXPR 3 /* component or func. expected */
76 #define TF_NONE 4 /* no argument */
77 #define TF_MYBOX 5 /* special - get current user's mbox */
78 #define TF_NOW 6 /* special - get current unix time */
79 #define TF_EXPR_SV 7 /* like expr but save current str reg */
80 #define TF_NOP 8 /* like expr but no result */
81 #define TF_MYNAME 9 /* special - get current name of user */
82 #define TF_MYHOST 10 /* special - get "local" hostname */
83 #define TF_LMBOX 11 /* special - get full local mailbox */
84 #define TF_BOLD 12 /* special - enter terminal bold mode */
85 #define TF_UNDERLN 13 /* special - enter underline mode */
86 #define TF_STNDOUT 14 /* special - enter underline mode */
87 #define TF_RESET 15 /* special - reset terminal modes */
88 #define TF_HASCLR 16 /* special - terminal have color? */
89 #define TF_FGCOLR 17 /* special - foreground term color */
90 #define TF_BGCOLR 18 /* special - background term color */
93 /* NB that TFL_PUTS is also used to decide whether the test
94 * in a "%<(function)..." should be a string or numeric one.
96 #define TFL_PUTS 1 /* implicit putstr if top level */
97 #define TFL_PUTN 2 /* implicit putnum if top level */
100 * The functable array maps between the text names of format functions and
101 * the format instructions interpreted by the engine in fmt_scan.c.
103 * The elements of this structure are as follows:
105 * name - The name of the function as seen in the format string. This is
106 * what maps a particular function name into a format instruction.
107 * type - The type of argument this function expects. Those types are
108 * listed above (with the TF_ prefix). This affects what gets
109 * placed in the format instruction (the f_un union). Also,
110 * instructions that require special handling are distinguished
111 * here (TF_MYMBOX is one example).
112 * f_type - The instruction corresponding to this function (from the list
114 * extra - Used by some functions to provide extra data to the compiler.
116 * - Providing an alternate instruction to combine a load
117 * and test operation (see do_if()).
118 * - Passed in f_value in the format instruction to provide
119 * extra information for the engine (see FT_LV_DAT handling
121 * - Provide a hint as to preprocessing that is required for
122 * this instruction (see do_name()).
123 * flags - See the definitions for TFL_PUTS & TFL_PUTN above.
127 char *name
; /* function name */
128 char type
; /* argument type */
129 char f_type
; /* fmt type */
130 char extra
; /* arg. type dependent extra info */
134 static struct ftable functable
[] = {
135 { "nonzero", TF_EXPR
, FT_V_NE
, FT_IF_V_NE
, 0 },
136 { "zero", TF_EXPR
, FT_V_EQ
, FT_IF_V_EQ
, 0 },
137 { "eq", TF_NUM
, FT_V_EQ
, FT_IF_V_EQ
, 0 },
138 { "ne", TF_NUM
, FT_V_NE
, FT_IF_V_NE
, 0 },
139 { "gt", TF_NUM
, FT_V_GT
, FT_IF_V_GT
, 0 },
140 { "null", TF_EXPR
, FT_S_NULL
, FT_IF_S_NULL
, 0 },
141 { "nonnull", TF_EXPR
, FT_S_NONNULL
, FT_IF_S
, 0 },
142 { "match", TF_STR
, FT_V_MATCH
, FT_IF_MATCH
, 0 },
143 { "amatch", TF_STR
, FT_V_AMATCH
, FT_IF_AMATCH
, 0 },
145 { "putstr", TF_EXPR
, FT_STR
, 0, 0 },
146 { "putstrf", TF_EXPR
, FT_STRF
, 0, 0 },
147 { "putnum", TF_EXPR
, FT_NUM
, 0, 0 },
148 { "putnumf", TF_EXPR
, FT_NUMF
, 0, 0 },
149 { "putaddr", TF_STR
, FT_PUTADDR
, 0, 0 },
150 { "putlit", TF_EXPR
, FT_STRLIT
, 0, 0 },
151 { "zputlit", TF_EXPR
, FT_STRLITZ
, 0, 0 },
152 { "void", TF_NOP
, 0, 0, 0 },
154 { "comp", TF_COMP
, FT_LS_COMP
, 0, TFL_PUTS
},
155 { "lit", TF_STR
, FT_LS_LIT
, 0, TFL_PUTS
},
156 { "getenv", TF_STR
, FT_LS_GETENV
, 0, TFL_PUTS
},
157 { "profile", TF_STR
, FT_LS_CFIND
, 0, TFL_PUTS
},
158 { "decodecomp", TF_COMP
, FT_LS_DECODECOMP
, 0, TFL_PUTS
},
159 { "decode", TF_EXPR
, FT_LS_DECODE
, 0, TFL_PUTS
},
160 { "trim", TF_EXPR
, FT_LS_TRIM
, 0, 0 },
161 { "kilo", TF_EXPR
, FT_LS_KILO
, 0, TFL_PUTS
},
162 { "kibi", TF_EXPR
, FT_LS_KIBI
, 0, TFL_PUTS
},
163 { "compval", TF_COMP
, FT_LV_COMP
, 0, TFL_PUTN
},
164 { "compflag", TF_COMP
, FT_LV_COMPFLAG
, 0, TFL_PUTN
},
165 { "num", TF_NUM
, FT_LV_LIT
, 0, TFL_PUTN
},
166 { "msg", TF_NONE
, FT_LV_DAT
, 0, TFL_PUTN
},
167 { "cur", TF_NONE
, FT_LV_DAT
, 1, TFL_PUTN
},
168 { "size", TF_NONE
, FT_LV_DAT
, 2, TFL_PUTN
},
169 { "width", TF_NONE
, FT_LV_DAT
, 3, TFL_PUTN
},
170 { "unseen", TF_NONE
, FT_LV_DAT
, 4, TFL_PUTN
},
171 { "dat", TF_NUM
, FT_LV_DAT
, 0, TFL_PUTN
},
172 { "strlen", TF_NONE
, FT_LV_STRLEN
, 0, TFL_PUTN
},
173 { "me", TF_MYBOX
, FT_LS_LIT
, 0, TFL_PUTS
},
174 { "myname", TF_MYNAME
, FT_LS_LIT
, 0, TFL_PUTS
},
175 { "myhost", TF_MYHOST
, FT_LS_LIT
, 0, TFL_PUTS
},
176 { "localmbox", TF_LMBOX
, FT_LS_LIT
, 0, TFL_PUTS
},
177 { "plus", TF_NUM
, FT_LV_PLUS_L
, 0, TFL_PUTN
},
178 { "minus", TF_NUM
, FT_LV_MINUS_L
, 0, TFL_PUTN
},
179 { "multiply", TF_NUM
, FT_LV_MULTIPLY_L
, 0, TFL_PUTN
},
180 { "divide", TF_NUM
, FT_LV_DIVIDE_L
, 0, TFL_PUTN
},
181 { "modulo", TF_NUM
, FT_LV_MODULO_L
, 0, TFL_PUTN
},
182 { "charleft", TF_NONE
, FT_LV_CHAR_LEFT
, 0, TFL_PUTN
},
183 { "timenow", TF_NOW
, FT_LV_LIT
, 0, TFL_PUTN
},
185 { "month", TF_COMP
, FT_LS_MONTH
, FT_PARSEDATE
, TFL_PUTS
},
186 { "lmonth", TF_COMP
, FT_LS_LMONTH
, FT_PARSEDATE
, TFL_PUTS
},
187 { "tzone", TF_COMP
, FT_LS_ZONE
, FT_PARSEDATE
, TFL_PUTS
},
188 { "day", TF_COMP
, FT_LS_DAY
, FT_PARSEDATE
, TFL_PUTS
},
189 { "weekday", TF_COMP
, FT_LS_WEEKDAY
, FT_PARSEDATE
, TFL_PUTS
},
190 { "tws", TF_COMP
, FT_LS_822DATE
, FT_PARSEDATE
, TFL_PUTS
},
191 { "sec", TF_COMP
, FT_LV_SEC
, FT_PARSEDATE
, TFL_PUTN
},
192 { "min", TF_COMP
, FT_LV_MIN
, FT_PARSEDATE
, TFL_PUTN
},
193 { "hour", TF_COMP
, FT_LV_HOUR
, FT_PARSEDATE
, TFL_PUTN
},
194 { "mday", TF_COMP
, FT_LV_MDAY
, FT_PARSEDATE
, TFL_PUTN
},
195 { "mon", TF_COMP
, FT_LV_MON
, FT_PARSEDATE
, TFL_PUTN
},
196 { "year", TF_COMP
, FT_LV_YEAR
, FT_PARSEDATE
, TFL_PUTN
},
197 { "yday", TF_COMP
, FT_LV_YDAY
, FT_PARSEDATE
, TFL_PUTN
},
198 { "wday", TF_COMP
, FT_LV_WDAY
, FT_PARSEDATE
, TFL_PUTN
},
199 { "zone", TF_COMP
, FT_LV_ZONE
, FT_PARSEDATE
, TFL_PUTN
},
200 { "clock", TF_COMP
, FT_LV_CLOCK
, FT_PARSEDATE
, TFL_PUTN
},
201 { "rclock", TF_COMP
, FT_LV_RCLOCK
, FT_PARSEDATE
, TFL_PUTN
},
202 { "sday", TF_COMP
, FT_LV_DAYF
, FT_PARSEDATE
, TFL_PUTN
},
203 { "szone", TF_COMP
, FT_LV_ZONEF
, FT_PARSEDATE
, TFL_PUTN
},
204 { "dst", TF_COMP
, FT_LV_DST
, FT_PARSEDATE
, TFL_PUTN
},
205 { "pretty", TF_COMP
, FT_LS_PRETTY
, FT_PARSEDATE
, TFL_PUTS
},
206 { "nodate", TF_COMP
, FT_LV_COMPFLAG
, FT_PARSEDATE
, TFL_PUTN
},
207 { "date2local", TF_COMP
, FT_LOCALDATE
, FT_PARSEDATE
, 0 },
208 { "date2gmt", TF_COMP
, FT_GMTDATE
, FT_PARSEDATE
, 0 },
210 { "pers", TF_COMP
, FT_LS_PERS
, FT_PARSEADDR
, TFL_PUTS
},
211 { "mbox", TF_COMP
, FT_LS_MBOX
, FT_PARSEADDR
, TFL_PUTS
},
212 { "host", TF_COMP
, FT_LS_HOST
, FT_PARSEADDR
, TFL_PUTS
},
213 { "path", TF_COMP
, FT_LS_PATH
, FT_PARSEADDR
, TFL_PUTS
},
214 { "gname", TF_COMP
, FT_LS_GNAME
, FT_PARSEADDR
, TFL_PUTS
},
215 { "note", TF_COMP
, FT_LS_NOTE
, FT_PARSEADDR
, TFL_PUTS
},
216 { "addr", TF_COMP
, FT_LS_ADDR
, FT_PARSEADDR
, TFL_PUTS
},
217 { "proper", TF_COMP
, FT_LS_822ADDR
, FT_PARSEADDR
, TFL_PUTS
},
218 { "type", TF_COMP
, FT_LV_HOSTTYPE
, FT_PARSEADDR
, TFL_PUTN
},
219 { "ingrp", TF_COMP
, FT_LV_INGRPF
, FT_PARSEADDR
, TFL_PUTN
},
220 { "nohost", TF_COMP
, FT_LV_NOHOSTF
, FT_PARSEADDR
, TFL_PUTN
},
221 { "formataddr", TF_EXPR_SV
,FT_FORMATADDR
, FT_FORMATADDR
, 0 },
222 { "concataddr", TF_EXPR_SV
,FT_CONCATADDR
, FT_FORMATADDR
, 0 },
223 { "friendly", TF_COMP
, FT_LS_FRIENDLY
, FT_PARSEADDR
, TFL_PUTS
},
225 { "mymbox", TF_COMP
, FT_LV_COMPFLAG
, FT_MYMBOX
, TFL_PUTN
},
226 { "getmymbox", TF_COMP
, FT_STR
, FT_GETMYMBOX
, 0 },
227 { "getmyaddr", TF_COMP
, FT_LS_ADDR
, FT_GETMYADDR
, TFL_PUTS
},
229 { "unquote", TF_EXPR
, FT_LS_UNQUOTE
, 0, TFL_PUTS
},
231 { "bold", TF_BOLD
, FT_LS_LIT
, 0, TFL_PUTS
},
232 { "underline", TF_UNDERLN
,FT_LS_LIT
, 0, TFL_PUTS
},
233 { "standout", TF_STNDOUT
,FT_LS_LIT
, 0, TFL_PUTS
},
234 { "resetterm", TF_RESET
, FT_LS_LIT
, 0, TFL_PUTS
},
235 { "hascolor", TF_HASCLR
, FT_LV_LIT
, 0, 0 },
236 { "fgcolor", TF_FGCOLR
, FT_LS_LIT
, 0, TFL_PUTS
},
237 { "bgcolor", TF_BGCOLR
, FT_LS_LIT
, 0, TFL_PUTS
},
243 * A mapping of color names to terminfo color numbers.
245 * There are two sets of terminal-setting codes: 'setaf/setab' (ANSI) and
246 * 'setf/setb'. Different terminals support different capabilities, so
247 * we provide a mapping for both. I'm not crazy about putting numbers
248 * directly in here, but it seems these are well defined by terminfo
249 * so it should be okay.
253 char *colorname
; /* Name of color */
254 int ansinum
; /* The ANSI escape color number */
255 int nonansinum
; /* The non-ANSI escape color number */
258 static struct colormap colortable
[] = {
270 /* Hash function for component name. Deliberately avoids a function
271 * call. Is case independent. Covers interval [0, 126] so never uses
272 * the last element of wantcomp[]. This function is "pretty good". */
273 #define CHASH(nm) ( \
275 ((nm)[0]) - ((nm)[0] ? ((nm)[1]) : 0) \
277 ((nm[1]) ? (((nm)[2]) & 0x5f) : 0) \
281 * Find a component in the hash table.
283 #define FINDCOMP(comp,name) do { \
284 for (comp = wantcomp[CHASH(name)]; \
285 comp && strcmp(comp->c_name,name); \
286 comp = comp->c_next) \
290 /* Add new component to the hash table */
291 #define NEWCOMP(cm,name) do { \
293 cm->c_name = mh_xstrdup(name);\
297 cm->c_next = wantcomp[i];\
301 #define NEW_FP(type,fill,wid) do {\
302 fp=next_fp++; fp->f_type=(type); fp->f_fill=(fill); fp->f_width=(wid); \
305 /* Add (possibly new) component to the hash table */
306 #define ADDC(name) do { \
312 fp->f_flags |= FF_COMPREF; \
316 #define LV(type, value) do { NEW_FP(type,0,0); fp->f_value = (value); } while (0)
317 #define LS(type, str) do { NEW_FP(type,0,0); fp->f_text = getcpy(str); fp->f_flags |= FF_STRALLOC; } while (0)
319 #define PUTCOMP(comp) do { NEW_FP(FT_COMP,0,0); ADDC(comp); } while (0)
320 #define PUTLIT(str) do { NEW_FP(FT_LIT,0,0); fp->f_text = getcpy(str); fp->f_flags |= FF_STRALLOC; } while (0)
321 #define PUTC(c) do { NEW_FP(FT_CHAR,0,0); fp->f_char = (c); } while (0)
323 static char *format_string
;
324 static char *usr_fstring
; /* for CERROR */
326 #define CERROR(str) compile_error (str, cp)
331 static struct ftable
*lookup(char *);
332 static void compile_error(char *, char *);
333 static char *compile (char *);
334 static char *do_spec(char *);
335 static char *do_name(char *, int);
336 static char *do_func(char *);
337 static char *do_expr (char *, int);
338 static char *do_loop(char *);
339 static char *do_if(char *);
340 static void free_component(struct comp
*);
341 static void free_comptable(void);
344 * Lookup a function name in the functable
346 static struct ftable
*
349 struct ftable
*t
= functable
;
353 while ((nm
= t
->name
)) {
354 if (*nm
== c
&& strcmp (nm
, name
) == 0)
364 compile_error(char *str
, char *cp
)
366 int i
, errpos
, errctx
;
368 errpos
= cp
- format_string
;
369 errctx
= min(errpos
, 20);
370 usr_fstring
[errpos
] = '\0';
372 for (i
= errpos
-errctx
; i
< errpos
; i
++) {
373 if (iscntrl((unsigned char) usr_fstring
[i
]))
374 usr_fstring
[i
] = '_';
377 inform("\"%s\": format compile error - %s",
378 &usr_fstring
[errpos
-errctx
], str
);
379 adios (NULL
, "%*s", errctx
+1, "^");
383 * Compile format string "fstring" into format list "fmt".
384 * Return the number of header components found in the format
389 fmt_compile(char *fstring
, struct format
**fmt
, int reset_comptable
)
393 static int comptable_initialized
= 0;
395 format_string
= mh_xstrdup(fstring
);
396 usr_fstring
= fstring
;
398 if (reset_comptable
|| !comptable_initialized
) {
400 comptable_initialized
= 1;
403 /* it takes at least 4 char to generate one format so we
404 * allocate a worst-case format array using 1/4 the length
405 * of the format string. We actually need twice this much
406 * to handle both pre-processing (e.g., address parsing) and
409 i
= strlen(fstring
)/2 + 1;
411 next_fp
= formatvec
= mh_xcalloc(i
, sizeof *next_fp
);
414 cp
= compile(format_string
);
416 CERROR("extra '%>', '%|' or '%?'");
418 LV(FT_DONE
, 0); /* really done */
433 while ((c
= *cp
) && c
!= '%')
469 case ';': /* comment line */
471 while ((c
= *cp
++) && c
!= '\n')
484 * Process functions & components (handle field width here as well
493 #endif /* not lint */
507 wid
= wid
*10 + (c
- '0');
513 fp
->f_type
= wid
? FT_COMPF
: FT_COMP
;
518 if (ftbl
->flags
& TFL_PUTS
) {
519 LV( wid
? FT_STRF
: FT_STR
, ftbl
->extra
);
521 else if (ftbl
->flags
& TFL_PUTN
) {
522 LV( wid
? FT_NUMF
: FT_NUM
, ftbl
->extra
);
527 CERROR("component or function name expected");
538 * Process a component name. Normally this involves generating an FT_COMP
539 * instruction for the specified component. If preprocess is set, then we
540 * do some extra processing.
543 do_name(char *sp
, int preprocess
)
548 static int primed
= 0;
550 while (isalnum(c
= *cp
++) || c
== '-' || c
== '_')
553 CERROR("'}' expected");
557 switch (preprocess
) {
560 if (cm
->c_type
& CT_ADDR
) {
561 CERROR("component used as both date and address");
568 fp
->f_type
= preprocess
;
570 cm
->c_type
|= CT_DATE
;
582 if (cm
->c_type
& CT_DATE
) {
583 CERROR("component used as both date and address");
585 cm
->c_mn
= &fmt_mnull
;
586 fp
->f_type
= preprocess
;
588 cm
->c_type
|= CT_ADDR
;
592 if (cm
->c_type
& CT_DATE
) {
593 CERROR("component used as both date and address");
595 cm
->c_type
|= CT_ADDR
;
602 * Generate one or more instructions corresponding to the named function.
603 * The different type of function arguments are handled here.
612 int mflag
; /* minus sign in NUM */
616 while (isalnum(c
= *cp
++))
618 if (c
!= '(' && c
!= '{' && c
!= ' ' && c
!= ')') {
619 CERROR("'(', '{', ' ' or ')' expected");
622 if ((t
= lookup (sp
)) == 0) {
623 CERROR("unknown function");
632 CERROR("component name expected");
634 cp
= do_name(cp
, t
->extra
);
635 fp
->f_type
= t
->f_type
;
640 if ((mflag
= (c
== '-')))
644 n
= n
*10 + (c
- '0');
654 while (c
&& c
!= ')')
661 LV(t
->f_type
,t
->extra
);
665 LS(t
->f_type
, getusername());
669 LS(t
->f_type
, getfullname());
673 LS(t
->f_type
, LocalName(0));
677 LS(t
->f_type
, getlocalmbox());
681 LS(t
->f_type
, get_term_stringcap("bold"));
685 LS(t
->f_type
, get_term_stringcap("smul"));
689 LS(t
->f_type
, get_term_stringcap("smso"));
693 LS(t
->f_type
, get_term_stringcap("sgr0"));
697 LV(t
->f_type
, get_term_numcap("colors") > 1);
702 struct colormap
*cmap
= colortable
;
706 while (c
&& c
!= ')')
710 while (cmap
->colorname
!= NULL
) {
711 if (strcasecmp(sp
, cmap
->colorname
) == 0)
716 if (cmap
->colorname
== NULL
) {
717 CERROR("Unknown color name");
721 code
= get_term_stringparm(t
->type
== TF_FGCOLR
? "setaf" : "setab",
725 * If this doesn't have anything, try falling back to setf/setb
729 code
= get_term_stringparm(t
->type
== TF_FGCOLR
? "setf" : "setb",
730 cmap
->nonansinum
, 0);
737 LV(t
->f_type
, time(NULL
));
745 cp
= do_expr(cp
, t
->extra
);
753 cp
= do_expr(cp
, t
->extra
);
759 CERROR("')' expected");
766 * Handle an expression as an argument. Basically we call one of do_name(),
767 * do_func(), or do_if()
770 do_expr (char *sp
, int preprocess
)
775 if ((c
= *cp
++) == '{') {
776 cp
= do_name (cp
, preprocess
);
777 fp
->f_type
= FT_LS_COMP
;
778 } else if (c
== '(') {
780 } else if (c
== ')') {
782 } else if (c
== '%' && *cp
== '<') {
785 CERROR ("'(', '{', '%<' or ')' expected");
791 * I am guessing this was for some kind of loop statement, which would have
792 * looked like %[ .... %]. It looks like the way this would have worked
793 * is that the format engine would have seen that FT_DONE had a 1 in the
794 * f_un.f_un_value and then decided whether or not to continue the loop.
795 * There is no support for this in the format engine, so right now if
796 * you try using it you will reach the FT_DONE and simply stop. I'm leaving
797 * this here in case someone wants to continue the work.
799 * Okay, got some more information on this from John L. Romine! From an
800 * email he sent to the nmh-workers mailing list on December 2, 2010, he
803 * In this case (scan, formatsbr) it has to do with an extension to
804 * the mh-format syntax to allow for looping.
806 * The scan format is processed once for each message. Those #ifdef
807 * JLR changes allowed for the top part of the format file to be
808 * processed once, then a second, looping part to be processed
809 * once per message. As I recall, there were new mh-format escape
810 * sequences to delimit the loop. This would have allowed for things
811 * like per-format column headings in the scan output.
813 * Since existing format files didn't include the scan listing
814 * header (it was hard-coded in scan.c) it would not have been
815 * backward-compatible. All existing format files (including any
816 * local ones) would have needed to be changed to include the format
817 * codes for a header. The practice at the time was not to introduce
818 * incompatible changes in a minor release, and I never managed to
819 * put out a newer major release.
821 * I can see how this would work, and I suspect part of the motivation was
822 * because the format compiler routines (at the time) couldn't really be
823 * called multiple times on the same message because the memory management
824 * was so lousy. That's been reworked and things are now a lot cleaner,
825 * so I suspect if we're going to allow a format string to be used for the
826 * scan header it might be simpler to have a separate format string just
827 * for the header. But I'll leave this code in for now just in case we
828 * decide that we want some kind of looping support.
834 struct format
*floop
;
839 CERROR ("']' expected");
841 LV(FT_DONE
, 1); /* not yet done */
843 fp
->f_skip
= floop
- fp
; /* skip backwards */
849 * Handle an if-elsif-endif statement. Note here that the branching
850 * is handled by the f_skip member of the struct format (which is really
851 * just f_width overloaded). This number controls how far to move forward
852 * (or back) in the format instruction array.
858 struct format
*fexpr
,
863 if (c
== '<') { /* doing an IF */
864 if ((c
= *cp
++) == '{') /*}*/{
866 fp
->f_type
= FT_LS_COMP
;
871 /* see if we can merge the load and the "if" */
872 if (ftbl
->f_type
>= IF_FUNCS
)
873 fp
->f_type
= ftbl
->extra
;
875 /* Put out a string test or a value test depending
876 * on what this function's return type is.
878 if (ftbl
->flags
& TFL_PUTS
) {
886 CERROR("'(' or '{' expected"); /*}*/
890 fexpr
= fp
; /* loc of [ELS]IF */
891 cp
= compile (cp
); /* compile IF TRUE stmts */
893 fif
->f_skip
= next_fp
- fif
;
895 if ((c
= *cp
++) == '|') { /* the last ELSE */
897 fif
= fp
; /* loc of GOTO */
898 fexpr
->f_skip
= next_fp
- fexpr
;
900 fexpr
= NULL
; /* no extra ENDIF */
902 cp
= compile (cp
); /* compile ELSE stmts */
903 fif
->f_skip
= next_fp
- fif
;
906 else if (c
== '?') { /* another ELSIF */
908 fif
= fp
; /* loc of GOTO */
909 fexpr
->f_skip
= next_fp
- fexpr
;
911 c
= '<'; /* impersonate an IF */
918 CERROR("'>' expected.");
921 if (fexpr
) /* IF ... [ELSIF ...] ENDIF */
922 fexpr
->f_skip
= next_fp
- fexpr
;
928 * Free a set of format instructions.
930 * What we do here is:
932 * - Iterate through the list of format instructions, freeing any references
933 * to allocated memory in each instruction.
934 * - Free component references.
935 * - If requested, reset the component hash table; that will also free any
936 * references to components stored there.
941 fmt_free(struct format
*fmt
, int reset_comptable
)
943 struct format
*fp
= fmt
;
946 while (! (fp
->f_type
== FT_DONE
&& fp
->f_value
== 0)) {
947 if (fp
->f_flags
& FF_STRALLOC
)
949 if (fp
->f_flags
& FF_COMPREF
)
950 free_component(fp
->f_comp
);
961 * Free just the text strings from all of the component hash table entries
965 fmt_freecomptext(void)
970 for (i
= 0; i
< DIM(wantcomp
); i
++)
971 for (cm
= wantcomp
[i
]; cm
; cm
= cm
->c_next
) {
978 * Find a component in our hash table. This is just a public interface to
979 * the FINDCOMP macro, so we don't have to expose our hash table.
983 fmt_findcomp(char *component
)
987 FINDCOMP(cm
, component
);
993 * Like fmt_findcomp, but case-insensitive.
997 fmt_findcasecomp(char *component
)
1001 for (cm
= wantcomp
[CHASH(component
)]; cm
; cm
= cm
->c_next
)
1002 if (strcasecmp(component
, FENDNULL(cm
->c_name
)) == 0)
1009 * Add an entry to the component hash table
1011 * Returns true if the component was added, 0 if it already existed.
1016 fmt_addcompentry(char *component
)
1021 FINDCOMP(cm
, component
);
1026 NEWCOMP(cm
, component
);
1029 * ncomp is really meant for fmt_compile() and this function is
1030 * meant to be used outside of it. So decrement it just to be safe
1031 * (internal callers should be using NEWCOMP()).
1040 * Add a string to a component hash table entry.
1042 * Note the special handling for components marked with CT_ADDR. The comments
1043 * in fmt_scan.h explain this in more detail.
1047 fmt_addcomptext(char *component
, char *text
)
1049 int i
, found
= 0, bucket
= CHASH(component
);
1050 struct comp
*cptr
= wantcomp
[bucket
];
1054 if (strcasecmp(component
, FENDNULL(cptr
->c_name
)) == 0) {
1056 if (! cptr
->c_text
) {
1057 cptr
->c_text
= getcpy(text
);
1059 i
= strlen(cp
= cptr
->c_text
) - 1;
1060 if (cp
[i
] == '\n') {
1061 if (cptr
->c_type
& CT_ADDR
) {
1063 cp
= add(",\n\t", cp
);
1068 cptr
->c_text
= add(text
, cp
);
1071 cptr
= cptr
->c_next
;
1074 return found
? bucket
: -1;
1078 * Append text to a component we've already found. See notes in fmt_scan.h
1079 * for more information.
1083 fmt_appendcomp(int bucket
, char *component
, char *text
)
1088 for (cptr
= wantcomp
[bucket
]; cptr
; cptr
= cptr
->c_next
)
1089 if (strcasecmp(component
, FENDNULL(cptr
->c_name
)) == 0)
1090 cptr
->c_text
= add(text
, cptr
->c_text
);
1095 * Iterate over our component hash table
1099 fmt_nextcomp(struct comp
*comp
, unsigned int *bucket
)
1104 comp
= comp
->c_next
;
1106 while (comp
== NULL
&& *bucket
< DIM(wantcomp
)) {
1107 comp
= wantcomp
[(*bucket
)++];
1114 * Free and reset our component hash table
1118 free_comptable(void)
1121 struct comp
*cm
, *cm2
;
1123 for (i
= 0; i
< DIM(wantcomp
); i
++) {
1125 while (cm
!= NULL
) {
1137 * Decrement the reference count of a component structure. If it reaches
1142 free_component(struct comp
*cm
)
1144 if (--cm
->c_refcount
<= 0) {
1145 /* Shouldn't ever be NULL, but just in case ... */
1148 if (cm
->c_type
& CT_DATE
)
1150 if (cm
->c_type
& CT_ADDR
&& cm
->c_mn
&& cm
->c_mn
!= &fmt_mnull
)