]>
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 the mh-format(5) man page.
43 #include <h/addrsbr.h>
45 #include <h/fmt_scan.h>
46 #include <h/fmt_compile.h>
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
56 * hash table for deciding if a component is "interesting"
58 static struct comp
*wantcomp
[128];
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 */
66 static int infunction
; /* function nesting cnt */
68 extern struct mailname fmt_mnull
;
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 */
92 /* NB that TFL_PUTS is also used to decide whether the test
93 * in a "%<(function)..." should be a string or numeric one.
95 #define TFL_PUTS 1 /* implicit putstr if top level */
96 #define TFL_PUTN 2 /* implicit putnum if top level */
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.
102 * The elements of this structure are as follows:
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
113 * extra - Used by some functions to provide extra data to the compiler.
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
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.
126 char *name
; /* function name */
127 char type
; /* argument type */
128 char f_type
; /* fmt type */
129 char extra
; /* arg. type dependent extra info */
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 },
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 },
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
},
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 },
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
},
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
},
228 { "unquote", TF_EXPR
, FT_LS_UNQUOTE
, 0, TFL_PUTS
},
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
},
242 * A mapping of color names to terminfo color numbers.
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.
252 char *colorname
; /* Name of color */
253 int ansinum
; /* The ANSI escape color number */
254 int nonansinum
; /* The non-ANSI escape color number */
257 static struct colormap colortable
[] = {
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) ( \
274 ((nm)[0]) - ((nm)[0] ? ((nm)[1]) : 0) \
276 ((nm[1]) ? (((nm)[2]) & 0x5f) : 0) \
280 * Find a component in the hash table.
282 #define FINDCOMP(comp,name) \
283 for (comp = wantcomp[CHASH(name)]; \
284 comp && strcmp(comp->c_name,name); \
285 comp = comp->c_next) \
288 /* Add new component to the hash table */
289 #define NEWCOMP(cm,name) do { \
291 cm->c_name = mh_xstrdup(name);\
295 cm->c_next = wantcomp[i];\
299 #define NEWFMT (next_fp++)
300 #define NEW_FP(type,fill,wid) do {\
301 fp=NEWFMT; fp->f_type=(type); fp->f_fill=(fill); fp->f_width=(wid); \
304 /* Add (possibly new) component to the hash table */
305 #define ADDC(name) do { \
311 fp->f_flags |= FF_COMPREF; \
315 #define LV(type, value) do { NEW_FP(type,0,0); fp->f_value = (value); } while (0)
316 #define LS(type, str) do { NEW_FP(type,0,0); fp->f_text = getcpy(str); fp->f_flags |= FF_STRALLOC; } while (0)
318 #define PUTCOMP(comp) do { NEW_FP(FT_COMP,0,0); ADDC(comp); } while (0)
319 #define PUTLIT(str) do { NEW_FP(FT_LIT,0,0); fp->f_text = getcpy(str); fp->f_flags |= FF_STRALLOC; } while (0)
320 #define PUTC(c) do { NEW_FP(FT_CHAR,0,0); fp->f_char = (c); } while (0)
322 static char *format_string
;
323 static char *usr_fstring
; /* for CERROR */
325 #define CERROR(str) compile_error (str, cp)
330 static struct ftable
*lookup(char *);
331 static void compile_error(char *, char *);
332 static char *compile (char *);
333 static char *do_spec(char *);
334 static char *do_name(char *, int);
335 static char *do_func(char *);
336 static char *do_expr (char *, int);
337 static char *do_loop(char *);
338 static char *do_if(char *);
339 static void free_component(struct comp
*);
340 static void free_comptable(void);
343 * Lookup a function name in the functable
345 static struct ftable
*
348 struct ftable
*t
= functable
;
352 while ((nm
= t
->name
)) {
353 if (*nm
== c
&& strcmp (nm
, name
) == 0)
358 return (struct ftable
*) 0;
363 compile_error(char *str
, char *cp
)
365 int i
, errpos
, errctx
;
367 errpos
= cp
- format_string
;
368 errctx
= min(errpos
, 20);
369 usr_fstring
[errpos
] = '\0';
371 for (i
= errpos
-errctx
; i
< errpos
; i
++) {
372 if (iscntrl((unsigned char) usr_fstring
[i
]))
373 usr_fstring
[i
] = '_';
376 inform("\"%s\": format compile error - %s",
377 &usr_fstring
[errpos
-errctx
], str
);
378 adios (NULL
, "%*s", errctx
+1, "^");
382 * Compile format string "fstring" into format list "fmt".
383 * Return the number of header components found in the format
388 fmt_compile(char *fstring
, struct format
**fmt
, int reset_comptable
)
392 static int comptable_initialized
= 0;
394 format_string
= mh_xstrdup(fstring
);
395 usr_fstring
= fstring
;
397 if (reset_comptable
|| !comptable_initialized
) {
399 comptable_initialized
= 1;
402 /* it takes at least 4 char to generate one format so we
403 * allocate a worst-case format array using 1/4 the length
404 * of the format string. We actually need twice this much
405 * to handle both pre-processing (e.g., address parsing) and
408 i
= strlen(fstring
)/2 + 1;
410 next_fp
= formatvec
= mh_xcalloc(i
, sizeof *next_fp
);
413 cp
= compile(format_string
);
415 CERROR("extra '%>', '%|' or '%?'");
417 LV(FT_DONE
, 0); /* really done */
432 while ((c
= *cp
) && c
!= '%')
468 case ';': /* comment line */
470 while ((c
= *cp
++) && c
!= '\n')
483 * Process functions & components (handle field width here as well
492 #endif /* not lint */
506 wid
= wid
*10 + (c
- '0');
512 fp
->f_type
= wid
? FT_COMPF
: FT_COMP
;
517 if (ftbl
->flags
& TFL_PUTS
) {
518 LV( wid
? FT_STRF
: FT_STR
, ftbl
->extra
);
520 else if (ftbl
->flags
& TFL_PUTN
) {
521 LV( wid
? FT_NUMF
: FT_NUM
, ftbl
->extra
);
526 CERROR("component or function name expected");
537 * Process a component name. Normally this involves generating an FT_COMP
538 * instruction for the specified component. If preprocess is set, then we
539 * do some extra processing.
542 do_name(char *sp
, int preprocess
)
547 static int primed
= 0;
549 while (isalnum(c
= *cp
++) || c
== '-' || c
== '_')
552 CERROR("'}' expected");
556 switch (preprocess
) {
559 if (cm
->c_type
& CT_ADDR
) {
560 CERROR("component used as both date and address");
563 memset (cm
->c_tws
, 0, sizeof *cm
->c_tws
);
567 fp
->f_type
= preprocess
;
569 cm
->c_type
|= CT_DATE
;
576 ismymbox ((struct mailname
*) 0);
581 if (cm
->c_type
& CT_DATE
) {
582 CERROR("component used as both date and address");
584 cm
->c_mn
= &fmt_mnull
;
585 fp
->f_type
= preprocess
;
587 cm
->c_type
|= CT_ADDR
;
591 if (cm
->c_type
& CT_DATE
) {
592 CERROR("component used as both date and address");
594 cm
->c_type
|= CT_ADDR
;
601 * Generate one or more instructions corresponding to the named function.
602 * The different type of function arguments are handled here.
611 int mflag
; /* minus sign in NUM */
615 while (isalnum(c
= *cp
++))
617 if (c
!= '(' && c
!= '{' && c
!= ' ' && c
!= ')') {
618 CERROR("'(', '{', ' ' or ')' expected");
621 if ((t
= lookup (sp
)) == 0) {
622 CERROR("unknown function");
631 CERROR("component name expected");
633 cp
= do_name(cp
, t
->extra
);
634 fp
->f_type
= t
->f_type
;
639 if ((mflag
= (c
== '-')))
643 n
= n
*10 + (c
- '0');
653 while (c
&& c
!= ')')
660 LV(t
->f_type
,t
->extra
);
664 LS(t
->f_type
, getusername());
668 LS(t
->f_type
, getfullname());
672 LS(t
->f_type
, LocalName(0));
676 LS(t
->f_type
, getlocalmbox());
680 LS(t
->f_type
, get_term_stringcap("bold"));
684 LS(t
->f_type
, get_term_stringcap("smul"));
688 LS(t
->f_type
, get_term_stringcap("smso"));
692 LS(t
->f_type
, get_term_stringcap("sgr0"));
696 LV(t
->f_type
, get_term_numcap("colors") > 1);
701 struct colormap
*cmap
= colortable
;
705 while (c
&& c
!= ')')
709 while (cmap
->colorname
!= NULL
) {
710 if (strcasecmp(sp
, cmap
->colorname
) == 0)
715 if (cmap
->colorname
== NULL
) {
716 CERROR("Unknown color name");
720 code
= get_term_stringparm(t
->type
== TF_FGCOLR
? "setaf" : "setab",
724 * If this doesn't have anything, try falling back to setf/setb
728 code
= get_term_stringparm(t
->type
== TF_FGCOLR
? "setf" : "setb",
729 cmap
->nonansinum
, 0);
736 LV(t
->f_type
, time((time_t *) 0));
744 cp
= do_expr(cp
, t
->extra
);
752 cp
= do_expr(cp
, t
->extra
);
758 CERROR("')' expected");
765 * Handle an expression as an argument. Basically we call one of do_name(),
766 * do_func(), or do_if()
769 do_expr (char *sp
, int preprocess
)
774 if ((c
= *cp
++) == '{') {
775 cp
= do_name (cp
, preprocess
);
776 fp
->f_type
= FT_LS_COMP
;
777 } else if (c
== '(') {
779 } else if (c
== ')') {
781 } else if (c
== '%' && *cp
== '<') {
784 CERROR ("'(', '{', '%<' or ')' expected");
790 * I am guessing this was for some kind of loop statement, which would have
791 * looked like %[ .... %]. It looks like the way this would have worked
792 * is that the format engine would have seen that FT_DONE had a 1 in the
793 * f_un.f_un_value and then decided whether or not to continue the loop.
794 * There is no support for this in the format engine, so right now if
795 * you try using it you will reach the FT_DONE and simply stop. I'm leaving
796 * this here in case someone wants to continue the work.
798 * Okay, got some more information on this from John L. Romine! From an
799 * email he sent to the nmh-workers mailing list on December 2, 2010, he
802 * In this case (scan, formatsbr) it has to do with an extension to
803 * the mh-format syntax to allow for looping.
805 * The scan format is processed once for each message. Those #ifdef
806 * JLR changes allowed for the top part of the format file to be
807 * processed once, then a second, looping part to be processed
808 * once per message. As I recall, there were new mh-format escape
809 * sequences to delimit the loop. This would have allowed for things
810 * like per-format column headings in the scan output.
812 * Since existing format files didn't include the scan listing
813 * header (it was hard-coded in scan.c) it would not have been
814 * backward-compatible. All existing format files (including any
815 * local ones) would have needed to be changed to include the format
816 * codes for a header. The practice at the time was not to introduce
817 * incompatible changes in a minor release, and I never managed to
818 * put out a newer major release.
820 * I can see how this would work, and I suspect part of the motivation was
821 * because the format compiler routines (at the time) couldn't really be
822 * called multiple times on the same message because the memory management
823 * was so lousy. That's been reworked and things are now a lot cleaner,
824 * so I suspect if we're going to allow a format string to be used for the
825 * scan header it might be simpler to have a separate format string just
826 * for the header. But I'll leave this code in for now just in case we
827 * decide that we want some kind of looping support.
833 struct format
*floop
;
838 CERROR ("']' expected");
840 LV(FT_DONE
, 1); /* not yet done */
842 fp
->f_skip
= floop
- fp
; /* skip backwards */
848 * Handle an if-elsif-endif statement. Note here that the branching
849 * is handled by the f_skip member of the struct format (which is really
850 * just f_width overloaded). This number controls how far to move forward
851 * (or back) in the format instruction array.
857 struct format
*fexpr
,
858 *fif
= (struct format
*)NULL
;
862 if (c
== '<') { /* doing an IF */
863 if ((c
= *cp
++) == '{') /*}*/{
865 fp
->f_type
= FT_LS_COMP
;
870 /* see if we can merge the load and the "if" */
871 if (ftbl
->f_type
>= IF_FUNCS
)
872 fp
->f_type
= ftbl
->extra
;
874 /* Put out a string test or a value test depending
875 * on what this function's return type is.
877 if (ftbl
->flags
& TFL_PUTS
) {
885 CERROR("'(' or '{' expected"); /*}*/
889 fexpr
= fp
; /* loc of [ELS]IF */
890 cp
= compile (cp
); /* compile IF TRUE stmts */
892 fif
->f_skip
= next_fp
- fif
;
894 if ((c
= *cp
++) == '|') { /* the last ELSE */
896 fif
= fp
; /* loc of GOTO */
897 fexpr
->f_skip
= next_fp
- fexpr
;
899 fexpr
= (struct format
*)NULL
;/* no extra ENDIF */
901 cp
= compile (cp
); /* compile ELSE stmts */
902 fif
->f_skip
= next_fp
- fif
;
905 else if (c
== '?') { /* another ELSIF */
907 fif
= fp
; /* loc of GOTO */
908 fexpr
->f_skip
= next_fp
- fexpr
;
910 c
= '<'; /* impersonate an IF */
917 CERROR("'>' expected.");
920 if (fexpr
) /* IF ... [ELSIF ...] ENDIF */
921 fexpr
->f_skip
= next_fp
- fexpr
;
927 * Free a set of format instructions.
929 * What we do here is:
931 * - Iterate through the list of format instructions, freeing any references
932 * to allocated memory in each instruction.
933 * - Free component references.
934 * - If requested, reset the component hash table; that will also free any
935 * references to components stored there.
940 fmt_free(struct format
*fmt
, int reset_comptable
)
942 struct format
*fp
= fmt
;
945 while (! (fp
->f_type
== FT_DONE
&& fp
->f_value
== 0)) {
946 if (fp
->f_flags
& FF_STRALLOC
)
948 if (fp
->f_flags
& FF_COMPREF
)
949 free_component(fp
->f_comp
);
960 * Free just the text strings from all of the component hash table entries
964 fmt_freecomptext(void)
969 for (i
= 0; i
< sizeof(wantcomp
)/sizeof(wantcomp
[0]); i
++)
970 for (cm
= wantcomp
[i
]; cm
; cm
= cm
->c_next
) {
971 mh_xfree(cm
->c_text
);
977 * Find a component in our hash table. This is just a public interface to
978 * the FINDCOMP macro, so we don't have to expose our hash table.
982 fmt_findcomp(char *component
)
986 FINDCOMP(cm
, component
);
992 * Like fmt_findcomp, but case-insensitive.
996 fmt_findcasecomp(char *component
)
1000 for (cm
= wantcomp
[CHASH(component
)]; cm
; cm
= cm
->c_next
)
1001 if (strcasecmp(component
, FENDNULL(cm
->c_name
)) == 0)
1008 * Add an entry to the component hash table
1010 * Returns true if the component was added, 0 if it already existed.
1015 fmt_addcompentry(char *component
)
1020 FINDCOMP(cm
, component
);
1025 NEWCOMP(cm
, component
);
1028 * ncomp is really meant for fmt_compile() and this function is
1029 * meant to be used outside of it. So decrement it just to be safe
1030 * (internal callers should be using NEWCOMP()).
1039 * Add a string to a component hash table entry.
1041 * Note the special handling for components marked with CT_ADDR. The comments
1042 * in fmt_scan.h explain this in more detail.
1046 fmt_addcomptext(char *component
, char *text
)
1048 int i
, found
= 0, bucket
= CHASH(component
);
1049 struct comp
*cptr
= wantcomp
[bucket
];
1053 if (strcasecmp(component
, FENDNULL(cptr
->c_name
)) == 0) {
1055 if (! cptr
->c_text
) {
1056 cptr
->c_text
= getcpy(text
);
1058 i
= strlen(cp
= cptr
->c_text
) - 1;
1059 if (cp
[i
] == '\n') {
1060 if (cptr
->c_type
& CT_ADDR
) {
1062 cp
= add(",\n\t", cp
);
1067 cptr
->c_text
= add(text
, cp
);
1070 cptr
= cptr
->c_next
;
1073 return found
? bucket
: -1;
1077 * Append text to a component we've already found. See notes in fmt_scan.h
1078 * for more information.
1082 fmt_appendcomp(int bucket
, char *component
, char *text
)
1087 for (cptr
= wantcomp
[bucket
]; cptr
; cptr
= cptr
->c_next
)
1088 if (strcasecmp(component
, FENDNULL(cptr
->c_name
)) == 0)
1089 cptr
->c_text
= add(text
, cptr
->c_text
);
1094 * Iterate over our component hash table
1098 fmt_nextcomp(struct comp
*comp
, unsigned int *bucket
)
1103 comp
= comp
->c_next
;
1105 while (comp
== NULL
&& *bucket
< sizeof(wantcomp
)/sizeof(wantcomp
[0])) {
1106 comp
= wantcomp
[(*bucket
)++];
1113 * Free and reset our component hash table
1117 free_comptable(void)
1120 struct comp
*cm
, *cm2
;
1122 for (i
= 0; i
< sizeof(wantcomp
)/sizeof(wantcomp
[0]); i
++) {
1124 while (cm
!= NULL
) {
1136 * Decrement the reference count of a component structure. If it reaches
1141 free_component(struct comp
*cm
)
1143 if (--cm
->c_refcount
<= 0) {
1144 /* Shouldn't ever be NULL, but just in case ... */
1145 mh_xfree(cm
->c_name
);
1146 mh_xfree(cm
->c_text
);
1147 if (cm
->c_type
& CT_DATE
)
1149 if (cm
->c_type
& CT_ADDR
&& cm
->c_mn
&& cm
->c_mn
!= &fmt_mnull
)