3 * fmt_compile.c -- "compile" format strings for fmt_scan
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.
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.
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.
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 fmtdump utility.
21 * A list of format instructions can be found in fmt_compile.h.
23 * If you wish to add a new function, you will need to do the following
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.
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).
36 * - Add the code in fmt_scan.c to handle your new function.
38 * - Add code to fmtdump.c to display your new function.
40 * - Document the new function in the mh-format(5) man page.
45 #include <h/addrsbr.h>
47 #include <h/fmt_scan.h>
48 #include <h/fmt_compile.h>
52 #ifdef HAVE_SYS_TIME_H
53 # include <sys/time.h>
58 * hash table for deciding if a component is "interesting"
60 static struct comp *wantcomp[128];
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 */
68 static int infunction; /* function nesting cnt */
70 extern struct mailname fmt_mnull;
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 */
87 /* NB that TFL_PUTS is also used to decide whether the test
88 * in a "%<(function)..." should be a string or numeric one.
90 #define TFL_PUTS 1 /* implicit putstr if top level */
91 #define TFL_PUTN 2 /* implicit putnum if top level */
94 * The functable array maps between the text names of format functions and
95 * the format instructions interpreted by the engine in fmt_scan.c.
97 * The elements of this structure are as follows:
99 * name - The name of the function as seen in the format string. This is
100 * what maps a particular function name into a format instruction.
101 * type - The type of argument this function expects. Those types are
102 * listed above (with the TF_ prefix). This affects what gets
103 * placed in the format instruction (the f_un union).
104 * f_type - The instruction corresponding to this function (from the list
106 * extra - Used by some functions to provide extra data to the compiler.
108 * - Providing an alternate instruction to combine a load
109 * and test operation (see do_if()).
110 * - Passed in f_value in the format instruction to provide
111 * extra information for the engine (see FT_LV_DAT handling
113 * - Provide a hint as to preprocessing that is required for
114 * this instruction (see do_name()).
115 * flags - See the definitions for TFL_PUTS & TFL_PUTN above.
119 char *name; /* function name */
120 char type; /* argument type */
121 char f_type; /* fmt type */
122 char extra; /* arg. type dependent extra info */
126 static struct ftable functable[] = {
127 { "nonzero", TF_EXPR, FT_V_NE, FT_IF_V_NE, 0 },
128 { "zero", TF_EXPR, FT_V_EQ, FT_IF_V_EQ, 0 },
129 { "eq", TF_NUM, FT_V_EQ, FT_IF_V_EQ, 0 },
130 { "ne", TF_NUM, FT_V_NE, FT_IF_V_NE, 0 },
131 { "gt", TF_NUM, FT_V_GT, FT_IF_V_GT, 0 },
132 { "null", TF_EXPR, FT_S_NULL, FT_IF_S_NULL, 0 },
133 { "nonnull", TF_EXPR, FT_S_NONNULL, FT_IF_S, 0 },
134 { "match", TF_STR, FT_V_MATCH, FT_IF_MATCH, 0 },
135 { "amatch", TF_STR, FT_V_AMATCH, FT_IF_AMATCH, 0 },
137 { "putstr", TF_EXPR, FT_STR, 0, 0 },
138 { "putstrf", TF_EXPR, FT_STRF, 0, 0 },
139 { "putnum", TF_EXPR, FT_NUM, 0, 0 },
140 { "putnumf", TF_EXPR, FT_NUMF, 0, 0 },
141 { "putaddr", TF_STR, FT_PUTADDR, 0, 0 },
142 { "putlit", TF_EXPR, FT_STRLIT, 0, 0 },
143 { "zputlit", TF_EXPR, FT_STRLITZ, 0, 0 },
144 { "void", TF_NOP, 0, 0, 0 },
146 { "comp", TF_COMP, FT_LS_COMP, 0, TFL_PUTS },
147 { "lit", TF_STR, FT_LS_LIT, 0, TFL_PUTS },
148 { "getenv", TF_STR, FT_LS_GETENV, 0, TFL_PUTS },
149 { "profile", TF_STR, FT_LS_CFIND, 0, TFL_PUTS },
150 { "decodecomp", TF_COMP, FT_LS_DECODECOMP, 0, TFL_PUTS },
151 { "decode", TF_EXPR, FT_LS_DECODE, 0, TFL_PUTS },
152 { "trim", TF_EXPR, FT_LS_TRIM, 0, 0 },
153 { "compval", TF_COMP, FT_LV_COMP, 0, TFL_PUTN },
154 { "compflag", TF_COMP, FT_LV_COMPFLAG, 0, TFL_PUTN },
155 { "num", TF_NUM, FT_LV_LIT, 0, TFL_PUTN },
156 { "msg", TF_NONE, FT_LV_DAT, 0, TFL_PUTN },
157 { "cur", TF_NONE, FT_LV_DAT, 1, TFL_PUTN },
158 { "size", TF_NONE, FT_LV_DAT, 2, TFL_PUTN },
159 { "width", TF_NONE, FT_LV_DAT, 3, TFL_PUTN },
160 { "unseen", TF_NONE, FT_LV_DAT, 4, TFL_PUTN },
161 { "dat", TF_NUM, FT_LV_DAT, 0, TFL_PUTN },
162 { "strlen", TF_NONE, FT_LV_STRLEN, 0, TFL_PUTN },
163 { "me", TF_MYBOX, FT_LS_LIT, 0, TFL_PUTS },
164 { "myname", TF_MYNAME, FT_LS_LIT, 0, TFL_PUTS },
165 { "myhost", TF_MYHOST, FT_LS_LIT, 0, TFL_PUTS },
166 { "localmbox", TF_LMBOX, FT_LS_LIT, 0, TFL_PUTS },
167 { "plus", TF_NUM, FT_LV_PLUS_L, 0, TFL_PUTN },
168 { "minus", TF_NUM, FT_LV_MINUS_L, 0, TFL_PUTN },
169 { "divide", TF_NUM, FT_LV_DIVIDE_L, 0, TFL_PUTN },
170 { "modulo", TF_NUM, FT_LV_MODULO_L, 0, TFL_PUTN },
171 { "charleft", TF_NONE, FT_LV_CHAR_LEFT, 0, TFL_PUTN },
172 { "timenow", TF_NOW, FT_LV_LIT, 0, TFL_PUTN },
174 { "month", TF_COMP, FT_LS_MONTH, FT_PARSEDATE, TFL_PUTS },
175 { "lmonth", TF_COMP, FT_LS_LMONTH, FT_PARSEDATE, TFL_PUTS },
176 { "tzone", TF_COMP, FT_LS_ZONE, FT_PARSEDATE, TFL_PUTS },
177 { "day", TF_COMP, FT_LS_DAY, FT_PARSEDATE, TFL_PUTS },
178 { "weekday", TF_COMP, FT_LS_WEEKDAY, FT_PARSEDATE, TFL_PUTS },
179 { "tws", TF_COMP, FT_LS_822DATE, FT_PARSEDATE, TFL_PUTS },
180 { "sec", TF_COMP, FT_LV_SEC, FT_PARSEDATE, TFL_PUTN },
181 { "min", TF_COMP, FT_LV_MIN, FT_PARSEDATE, TFL_PUTN },
182 { "hour", TF_COMP, FT_LV_HOUR, FT_PARSEDATE, TFL_PUTN },
183 { "mday", TF_COMP, FT_LV_MDAY, FT_PARSEDATE, TFL_PUTN },
184 { "mon", TF_COMP, FT_LV_MON, FT_PARSEDATE, TFL_PUTN },
185 { "year", TF_COMP, FT_LV_YEAR, FT_PARSEDATE, TFL_PUTN },
186 { "yday", TF_COMP, FT_LV_YDAY, FT_PARSEDATE, TFL_PUTN },
187 { "wday", TF_COMP, FT_LV_WDAY, FT_PARSEDATE, TFL_PUTN },
188 { "zone", TF_COMP, FT_LV_ZONE, FT_PARSEDATE, TFL_PUTN },
189 { "clock", TF_COMP, FT_LV_CLOCK, FT_PARSEDATE, TFL_PUTN },
190 { "rclock", TF_COMP, FT_LV_RCLOCK, FT_PARSEDATE, TFL_PUTN },
191 { "sday", TF_COMP, FT_LV_DAYF, FT_PARSEDATE, TFL_PUTN },
192 { "szone", TF_COMP, FT_LV_ZONEF, FT_PARSEDATE, TFL_PUTN },
193 { "dst", TF_COMP, FT_LV_DST, FT_PARSEDATE, TFL_PUTN },
194 { "pretty", TF_COMP, FT_LS_PRETTY, FT_PARSEDATE, TFL_PUTS },
195 { "nodate", TF_COMP, FT_LV_COMPFLAG, FT_PARSEDATE, TFL_PUTN },
196 { "date2local", TF_COMP, FT_LOCALDATE, FT_PARSEDATE, 0 },
197 { "date2gmt", TF_COMP, FT_GMTDATE, FT_PARSEDATE, 0 },
199 { "pers", TF_COMP, FT_LS_PERS, FT_PARSEADDR, TFL_PUTS },
200 { "mbox", TF_COMP, FT_LS_MBOX, FT_PARSEADDR, TFL_PUTS },
201 { "host", TF_COMP, FT_LS_HOST, FT_PARSEADDR, TFL_PUTS },
202 { "path", TF_COMP, FT_LS_PATH, FT_PARSEADDR, TFL_PUTS },
203 { "gname", TF_COMP, FT_LS_GNAME, FT_PARSEADDR, TFL_PUTS },
204 { "note", TF_COMP, FT_LS_NOTE, FT_PARSEADDR, TFL_PUTS },
205 { "addr", TF_COMP, FT_LS_ADDR, FT_PARSEADDR, TFL_PUTS },
206 { "proper", TF_COMP, FT_LS_822ADDR, FT_PARSEADDR, TFL_PUTS },
207 { "type", TF_COMP, FT_LV_HOSTTYPE, FT_PARSEADDR, TFL_PUTN },
208 { "ingrp", TF_COMP, FT_LV_INGRPF, FT_PARSEADDR, TFL_PUTN },
209 { "nohost", TF_COMP, FT_LV_NOHOSTF, FT_PARSEADDR, TFL_PUTN },
210 { "formataddr", TF_EXPR_SV,FT_FORMATADDR, FT_FORMATADDR, 0 },
211 { "concataddr", TF_EXPR_SV,FT_CONCATADDR, FT_FORMATADDR, 0 },
212 { "friendly", TF_COMP, FT_LS_FRIENDLY, FT_PARSEADDR, TFL_PUTS },
214 { "mymbox", TF_COMP, FT_LV_COMPFLAG, FT_MYMBOX, TFL_PUTN },
215 { "addtoseq", TF_STR, FT_ADDTOSEQ, 0, 0 },
217 { "unquote", TF_EXPR, FT_LS_UNQUOTE, 0, TFL_PUTS},
223 * Hash function for component name. The function should be
224 * case independent and probably shouldn't involve a routine
225 * call. This function is pretty good but will not work on
226 * single character component names.
228 #define CHASH(nm) (((((nm)[0]) - ((nm)[1])) & 0x1f) + (((nm)[2]) & 0x5f))
231 * Find a component in the hash table.
233 #define FINDCOMP(comp,name) \
234 for (comp = wantcomp[CHASH(name)]; \
235 comp && strcmp(comp->c_name,name); \
236 comp = comp->c_next) \
239 /* Add new component to the hash table */
240 #define NEWCOMP(cm,name) do { \
241 cm = ((struct comp *) calloc(1, sizeof (struct comp)));\
242 cm->c_name = getcpy(name);\
246 cm->c_next = wantcomp[i];\
250 #define NEWFMT (next_fp++)
251 #define NEW(type,fill,wid) do {\
252 fp=NEWFMT; fp->f_type=(type); fp->f_fill=(fill); fp->f_width=(wid); \
255 /* Add (possibly new) component to the hash table */
256 #define ADDC(name) do { \
262 fp->f_flags |= FF_COMPREF; \
266 #define LV(type, value) do { NEW(type,0,0); fp->f_value = (value); } while (0)
267 #define LS(type, str) do { NEW(type,0,0); fp->f_text = (str); fp->f_flags |= FF_STRALLOC; } while (0)
269 #define PUTCOMP(comp) do { NEW(FT_COMP,0,0); ADDC(comp); } while (0)
270 #define PUTLIT(str) do { NEW(FT_LIT,0,0); fp->f_text = getcpy(str); fp->f_flags |= FF_STRALLOC; } while (0)
271 #define PUTC(c) do { NEW(FT_CHAR,0,0); fp->f_char = (c); } while (0)
273 static char *format_string;
274 static unsigned char *usr_fstring; /* for CERROR */
276 #define CERROR(str) compile_error (str, cp)
281 static struct ftable *lookup(char *);
282 static void compile_error(char *, char *);
283 static char *compile (char *);
284 static char *do_spec(char *);
285 static char *do_name(char *, int);
286 static char *do_func(char *);
287 static char *do_expr (char *, int);
288 static char *do_loop(char *);
289 static char *do_if(char *);
290 static void free_component(struct comp *);
291 static void free_comptable(void);
295 * Lookup a function name in the functable
297 static struct ftable *
300 register struct ftable *t = functable;
302 register char c = *name;
304 while ((nm = t->name)) {
305 if (*nm == c && strcmp (nm, name) == 0)
310 return (struct ftable *) 0;
315 compile_error(char *str, char *cp)
317 int i, errpos, errctx;
319 errpos = cp - format_string;
320 errctx = errpos > 20 ? 20 : errpos;
321 usr_fstring[errpos] = '\0';
323 for (i = errpos-errctx; i < errpos; i++) {
325 if (iscntrl(usr_fstring[i]))
327 if (usr_fstring[i] < 32)
329 usr_fstring[i] = '_';
332 advise(NULL, "\"%s\": format compile error - %s",
333 &usr_fstring[errpos-errctx], str);
334 adios (NULL, "%*s", errctx+1, "^");
338 * Compile format string "fstring" into format list "fmt".
339 * Return the number of header components found in the format
344 fmt_compile(char *fstring, struct format **fmt, int reset_comptable)
348 static int comptable_initialized = 0;
350 format_string = getcpy (fstring);
351 usr_fstring = fstring;
353 if (reset_comptable || !comptable_initialized) {
355 comptable_initialized = 1;
358 memset((char *) &fmt_mnull, 0, sizeof(fmt_mnull));
360 /* it takes at least 4 char to generate one format so we
361 * allocate a worst-case format array using 1/4 the length
362 * of the format string. We actually need twice this much
363 * to handle both pre-processing (e.g., address parsing) and
366 i = strlen(fstring)/2 + 1;
368 next_fp = formatvec = (struct format *)calloc ((size_t) i,
369 sizeof(struct format));
371 adios (NULL, "unable to allocate format storage");
375 cp = compile(format_string);
377 CERROR("extra '%>', '%|' or '%?'");
379 LV(FT_DONE, 0); /* really done */
389 register char *cp = sp;
394 while ((c = *cp) && c != '%')
430 case ';': /* comment line */
432 while ((c = *cp++) && c != '\n')
445 * Process functions & components (handle field width here as well
450 register char *cp = sp;
453 register int ljust = 0;
454 #endif /* not lint */
455 register int wid = 0;
456 register char fill = ' ';
468 wid = wid*10 + (c - '0');
474 fp->f_type = wid? FT_COMPF : FT_COMP;
479 if (ftbl->flags & TFL_PUTS) {
480 LV( wid? FT_STRF : FT_STR, ftbl->extra);
482 else if (ftbl->flags & TFL_PUTN) {
483 LV( wid? FT_NUMF : FT_NUM, ftbl->extra);
488 CERROR("component or function name expected");
499 * Process a component name. Normally this involves generating an FT_COMP
500 * instruction for the specified component. If preprocess is set, then we
501 * do some extra processing.
504 do_name(char *sp, int preprocess)
506 register char *cp = sp;
509 static int primed = 0;
511 while (isalnum(c = *cp++) || c == '-' || c == '_')
514 CERROR("'}' expected");
518 switch (preprocess) {
521 if (cm->c_type & CT_ADDR) {
522 CERROR("component used as both date and address");
524 cm->c_tws = (struct tws *)
525 calloc((size_t) 1, sizeof(*cm->c_tws));
526 fp->f_type = preprocess;
528 cm->c_type |= CT_DATE;
533 ismymbox ((struct mailname *) 0);
538 if (cm->c_type & CT_DATE) {
539 CERROR("component used as both date and address");
541 cm->c_mn = &fmt_mnull;
542 fp->f_type = preprocess;
544 cm->c_type |= CT_ADDR;
548 if (cm->c_type & CT_DATE) {
549 CERROR("component used as both date and address");
551 cm->c_type |= CT_ADDR;
558 * Generate one or more instructions corresponding to the named function.
559 * The different type of function arguments are handled here.
564 register char *cp = sp;
566 register struct ftable *t;
568 int mflag; /* minus sign in NUM */
572 while (isalnum(c = *cp++))
574 if (c != '(' && c != '{' && c != ' ' && c != ')') {
575 CERROR("'(', '{', ' ' or ')' expected");
578 if ((t = lookup (sp)) == 0) {
579 CERROR("unknown function");
588 CERROR("component name expected");
590 cp = do_name(cp, t->extra);
591 fp->f_type = t->f_type;
596 if ((mflag = (c == '-')))
600 n = n*10 + (c - '0');
610 while (c && c != ')')
617 LV(t->f_type,t->extra);
621 LS(t->f_type, getusername());
625 LS(t->f_type, getfullname());
629 LS(t->f_type, LocalName(0));
633 LS(t->f_type, getlocalmbox());
637 LV(t->f_type, time((time_t *) 0));
645 cp = do_expr(cp, t->extra);
653 cp = do_expr(cp, t->extra);
659 CERROR("')' expected");
666 * Handle an expression as an argument. Basically we call one of do_name(),
667 * do_func(), or do_if()
670 do_expr (char *sp, int preprocess)
672 register char *cp = sp;
675 if ((c = *cp++) == '{') {
676 cp = do_name (cp, preprocess);
677 fp->f_type = FT_LS_COMP;
678 } else if (c == '(') {
680 } else if (c == ')') {
682 } else if (c == '%' && *cp == '<') {
685 CERROR ("'(', '{', '%<' or ')' expected");
691 * I am guessing this was for some kind of loop statement, which would have
692 * looked like %[ .... %]. It looks like the way this would have worked
693 * is that the format engine would have seen that FT_DONE had a 1 in the
694 * f_un.f_un_value and then decided whether or not to continue the loop.
695 * There is no support for this in the format engine, so right now if
696 * you try using it you will reach the FT_DONE and simply stop. I'm leaving
697 * this here in case someone wants to continue the work.
699 * Okay, got some more information on this from John L. Romine! From an
700 * email he sent to the nmh-workers mailing list on December 2, 2010, he
701 * explains it thusly:
703 * In this case (scan, formatsbr) it has to do with an extension to
704 * the mh-format syntax to allow for looping.
706 * The scan format is processed once for each message. Those #ifdef
707 * JLR changes allowed for the top part of the format file to be
708 * processed once, then a second, looping part to be processed
709 * once per message. As I recall, there were new mh-format escape
710 * sequences to delimit the loop. This would have allowed for things
711 * like per-format column headings in the scan output.
713 * Since existing format files didn't include the scan listing
714 * header (it was hard-coded in scan.c) it would not have been
715 * backward-compatible. All existing format files (including any
716 * local ones) would have needed to be changed to include the format
717 * codes for a header. The practice at the time was not to introduce
718 * incompatible changes in a minor release, and I never managed to
719 * put out a newer major release.
721 * I can see how this would work, and I suspect part of the motivation was
722 * because the format compiler routines (at the time) couldn't really be
723 * called multiple times on the same message because the memory management
724 * was so lousy. That's been reworked and things are now a lot cleaner,
725 * so I suspect if we're going to allow a format string to be used for the
726 * scan header it might be simpler to have a separate format string just
727 * for the header. But I'll leave this code in for now just in case we
728 * decide that we want some kind of looping support.
733 register char *cp = sp;
734 struct format *floop;
739 CERROR ("']' expected");
741 LV(FT_DONE, 1); /* not yet done */
743 fp->f_skip = floop - fp; /* skip backwards */
749 * Handle an if-elsif-endif statement. Note here that the branching
750 * is handled by the f_skip member of the struct format (which is really
751 * just f_width overloaded). This number controls how far to move forward
752 * (or back) in the format instruction array.
757 register char *cp = sp;
758 register struct format *fexpr,
759 *fif = (struct format *)NULL;
760 register int c = '<';
763 if (c == '<') { /* doing an IF */
764 if ((c = *cp++) == '{') /*}*/{
766 fp->f_type = FT_LS_COMP;
771 /* see if we can merge the load and the "if" */
772 if (ftbl->f_type >= IF_FUNCS)
773 fp->f_type = ftbl->extra;
775 /* Put out a string test or a value test depending
776 * on what this function's return type is.
778 if (ftbl->flags & TFL_PUTS) {
786 CERROR("'(' or '{' expected"); /*}*/
790 fexpr = fp; /* loc of [ELS]IF */
791 cp = compile (cp); /* compile IF TRUE stmts */
793 fif->f_skip = next_fp - fif;
795 if ((c = *cp++) == '|') { /* the last ELSE */
797 fif = fp; /* loc of GOTO */
798 fexpr->f_skip = next_fp - fexpr;
800 fexpr = (struct format *)NULL;/* no extra ENDIF */
802 cp = compile (cp); /* compile ELSE stmts */
803 fif->f_skip = next_fp - fif;
806 else if (c == '?') { /* another ELSIF */
808 fif = fp; /* loc of GOTO */
809 fexpr->f_skip = next_fp - fexpr;
811 c = '<'; /* impersonate an IF */
818 CERROR("'>' expected.");
821 if (fexpr) /* IF ... [ELSIF ...] ENDIF */
822 fexpr->f_skip = next_fp - fexpr;
828 * Free a set of format instructions.
830 * What we do here is:
832 * - Iterate through the list of format instructions, freeing any references
833 * to allocated memory in each instruction.
834 * - Free component references.
835 * - If requested, reset the component hash table; that will also free any
836 * references to components stored there.
841 fmt_free(struct format *fmt, int reset_comptable)
843 struct format *fp = fmt;
846 while (! (fp->f_type == FT_DONE && fp->f_value == 0)) {
847 if (fp->f_flags & FF_STRALLOC)
849 if (fp->f_flags & FF_COMPREF)
850 free_component(fp->f_comp);
861 * Find a component in our hash table. This is just a public interface to
862 * the FINDCOMP macro, so we don't have to expose our hash table.
866 fmt_findcomp(char *component)
870 FINDCOMP(cm, component);
876 * Like fmt_findcomp, but case-insensitive.
880 fmt_findcasecmp(char *component)
884 for (cm = wantcomp[CHASH(component)]; cm; cm = cm->c_next)
885 if (mh_strcasecmp(component, cm->c_name) == 0)
892 * Add an entry to the component hash table
894 * Returns true if the component was added, 0 if it already existed.
899 fmt_addcompentry(char *component)
904 FINDCOMP(cm, component);
909 NEWCOMP(cm, component);
912 * ncomp is really meant for fmt_compile() and this function is
913 * meant to be used outside of it. So decrement it just to be safe
914 * (internal callers should be using NEWCOMP()).
923 * Add a string to a component hash table entry.
925 * Note the special handling for components marked with CT_ADDR. The comments
926 * in fmt_scan.h explain this in more detail.
930 fmt_addcomptext(char *component, char *text)
932 int i, found = 0, bucket = CHASH(component);
933 struct comp *cptr = wantcomp[bucket];
937 if (mh_strcasecmp(component, cptr->c_name) == 0) {
939 if (! cptr->c_text) {
940 cptr->c_text = getcpy(text);
942 i = strlen(cp = cptr->c_text) - 1;
944 if (cptr->c_type & CT_ADDR) {
946 cp = add(",\n\t", cp);
951 cptr->c_text = add(text, cp);
957 return found ? bucket : -1;
961 * Append text to a component we've already found. See notes in fmt_scan.h
962 * for more information.
966 fmt_appendcomp(int bucket, char *component, char *text)
971 for (cptr = wantcomp[bucket]; cptr; cptr = cptr->c_next)
972 if (mh_strcasecmp(component, cptr->c_name) == 0)
973 cptr->c_text = add(text, cptr->c_text);
978 * Free and reset our component hash table
985 struct comp *cm, *cm2;
987 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
1001 * Decrement the reference count of a component structure. If it reaches
1006 free_component(struct comp *cm)
1008 if (--cm->c_refcount <= 0) {
1009 /* Shouldn't ever be NULL, but just in case ... */
1014 if (cm->c_type & CT_DATE)
1016 if (cm->c_type & CT_ADDR && cm->c_mn && cm->c_mn != &fmt_mnull)