]> diplodocus.org Git - nmh/blob - h/fmt_scan.h
Alter mh-chart(7)'s NAME to be lowercase.
[nmh] / h / fmt_scan.h
1
2 /*
3 * fmt_scan.h -- definitions for fmt_scan()
4 */
5
6 /*
7 * This structure describes an "interesting" component. It holds
8 * the name & text from the component (if found) and one piece of
9 * auxiliary info. The structure for a particular component is located
10 * by (open) hashing the name and using it as an index into the ptr array
11 * "wantcomp". All format entries that reference a particular component
12 * point to its comp struct (so we only have to do component specific
13 * processing once. e.g., parse an address.).
14 *
15 * In previous implementations "wantcomp" was made available to other
16 * functions, but now it's private and is accessed via functions.
17 */
18 struct comp {
19 char *c_name; /* component name (in lower case) */
20 char *c_text; /* component text (if found) */
21 struct comp *c_next; /* hash chain linkage */
22 short c_flags; /* misc. flags (from fmt_scan) */
23 short c_type; /* type info (from fmt_compile) */
24 union {
25 struct tws *c_u_tws;
26 struct mailname *c_u_mn;
27 } c_un;
28 int c_refcount; /* Reference count */
29 };
30
31 #define c_tws c_un.c_u_tws
32 #define c_mn c_un.c_u_mn
33
34 /*
35 * c_type bits
36 */
37 #define CT_ADDR (1<<0) /* referenced as address */
38 #define CT_DATE (1<<1) /* referenced as date */
39
40 #define CT_BITS "\020\01ADDR\02DATE"
41
42 /*
43 * c_flags bits
44 */
45 #define CF_TRUE (1<<0) /* usually means component is present */
46 #define CF_PARSED (1<<1) /* address/date has been parsed */
47 #define CF_DATEFAB (1<<2) /* datefield fabricated */
48 #define CF_TRIMMED (1<<3) /* Component has been trimmed */
49
50 #define CF_BITS "\020\01TRUE\02PARSED\03CF_DATEFAB\04TRIMMED"
51
52 /*
53 * This structure defines one formatting instruction.
54 */
55 struct format {
56 unsigned char f_type;
57 char f_fill;
58 short f_width; /* output field width */
59 union {
60 struct comp *f_u_comp; /* associated component */
61 char *f_u_text; /* literal text */
62 char f_u_char; /* literal character */
63 int f_u_value; /* literal value */
64 } f_un;
65 short f_flags; /* misc. flags */
66 };
67
68 #define f_skip f_width /* instr to skip (false "if") */
69
70 #define f_comp f_un.f_u_comp
71 #define f_text f_un.f_u_text
72 #define f_char f_un.f_u_char
73 #define f_value f_un.f_u_value
74
75 /*
76 * f_flags bits
77 */
78
79 #define FF_STRALLOC (1<<0) /* String has been allocated */
80 #define FF_COMPREF (1<<1) /* Component reference */
81
82 /*
83 * prototypes used by the format engine
84 */
85
86 /*
87 * These are the definitions used by the callbacks for fmt_scan()
88 */
89
90 typedef char * (*formataddr_cb)(char *, char *);
91 typedef char * (*concataddr_cb)(char *, char *);
92 typedef void (*trace_cb)(void *, struct format *, int, char *, const char *);
93
94 struct fmt_callbacks {
95 formataddr_cb formataddr;
96 concataddr_cb concataddr;
97 trace_cb trace_func;
98 void * trace_context;
99 };
100
101 /*
102 * Create a new format string. Arguments are:
103 *
104 * form - Name of format file. Will be searched by etcpath(), see that
105 * function for details.
106 * format - The format string to be used if no format file is given
107 * default_fs - The default format string to be used if neither form nor
108 * format is given
109 *
110 * This function also takes care of processing \ escapes like \n, \t, etc.
111 *
112 * Returns an allocated format string.
113 */
114
115 char *new_fs (char *form, char *format, char *default_fs);
116
117 /*
118 * Free memory allocated by new_fs(). It allocates to a static so
119 * no argument is necessary.
120 */
121 void free_fs ();
122
123 /*
124 * Compile a format string into a set of format instructions. Arguments are:
125 *
126 * fstring - The format string (the "source code").
127 * fmt - Returns an allocated array of "struct fmt" elements. Each
128 * struct fmt is one format instruction interpreted by the
129 * format engine.
130 * reset - If set to true, the format compiler will reset the
131 * component hash table. The component hash table contains
132 * all of the references to message components referred to in
133 * the format instructions. If you have multiple format
134 * strings that you want to compile and operate on the
135 * same message, this should be set to false.
136 *
137 * Returns the total number of components referenced by all format instructions
138 * since the last reset of the hash table.
139 */
140
141 int fmt_compile (char *fstring, struct format **fmt, int reset);
142
143 /*
144 * Interpret a sequence of compiled format instructions. Arguments are:
145 *
146 * format - Array of format instructions generated by fmt_compile()
147 * scanl - Passed-in charstring_t object (created with
148 * charstring_create() and later destroyed with
149 * charstring_free()) that will contain the output of the
150 * format instructions. Is always terminated with a
151 * newline (\n).
152 * width - Maximum number of displayed characters. Does not include
153 * characters marked as nonprinting or (depending on the
154 * encoding) bytes in a multibyte encoding that exceed the
155 * character's column width.
156 * dat - An integer array that contains data used by certain format
157 * functions. Currently the following instructions use
158 * dat[]:
159 *
160 * dat[0] - %(msg), %(dat)
161 * dat[1] - %(cur)
162 * dat[2] - %(size)
163 * dat[3] - %(width)
164 * dat[4] - %(unseen)
165 *
166 * callbacks - A set of a callback functions used by the format engine.
167 * Can be NULL. If structure elements are NULL, a default
168 * function will be used. Callback structure elements are:
169 *
170 * formataddr - A callback for the %(formataddr) instruction
171 * concataddr - A callback for the %(concataddr) instruction
172 * trace - Called for every format instruction executed
173 *
174 * The return value is a pointer to the next format instruction to
175 * execute, which is currently always NULL.
176 */
177
178 struct format *fmt_scan (struct format *format, charstring_t scanl, int width,
179 int *dat, struct fmt_callbacks *callbacks);
180
181 /*
182 * Free a format structure and/or component hash table. Arguments are:
183 *
184 * format - An array of format structures allocated by fmt_compile,
185 * or NULL.
186 * reset - If true, reset and remove all references in the component
187 * hash table.
188 */
189
190 void fmt_free (struct format *fmt, int reset);
191
192 /*
193 * Free all of the component text structures in the component hash table
194 */
195
196 void fmt_freecomptext(void);
197
198 /*
199 * Search for a component structure in the component hash table. Arguments are:
200 *
201 * component - The name of the component to search for. By convention
202 * all component names used in format strings are lower case,
203 * but for backwards compatibility this search is done in
204 * a case-SENSITIVE manner.
205 *
206 * This function returns a "struct comp" corresponding to the named component,
207 * or NULL if the component is not found in the hash table.
208 */
209
210 struct comp *fmt_findcomp(char *component);
211
212 /*
213 * Search for a component structure in the component hash table.
214 *
215 * Identical to fmd_findcomp(), but is case-INSENSITIVE.
216 */
217
218 struct comp *fmt_findcasecomp(char *component);
219
220 /*
221 * Add a component entry to the component hash table
222 *
223 * component - The name of the component to add to the hash table.
224 *
225 * If the component is already in the hash table, this function will do
226 * nothing. Returns 1 if a component was added, 0 if it already existed.
227 */
228
229 int fmt_addcompentry(char *component);
230
231 /*
232 * Add a string to a component hash table entry. Arguments are:
233 *
234 * component - The name of the component to add text to. The component
235 * is searched for in a case-INSENSITIVE manner (note that
236 * this is different than fmt_findcomp()). If the component
237 * is not found in the hash table, this function will silently
238 * return.
239 * text - The text to add to a component hash table entry. Note that
240 * if the last character of the existing component
241 * text is a newline AND it is marked as an address
242 * component (the the CT_ADDR flag is set) existing
243 * component buffer is a newline, it will be separated
244 * from previous text by ",\n\t"; otherwise if the last
245 * character of the previous text is a newline it will
246 * simply be separated by a "\t". This unusual processing
247 * is designed to handle the case where you have multiple
248 * headers with the same name (e.g.: multiple "cc:" headers,
249 * even though that isn't technically allowed in the RFCs).
250 *
251 * This function is designed to be called when you start processing a new
252 * component. The function returns the integer value of the hash table
253 * bucket corresponding to this component. If there was no entry found
254 * in the component hash table, this function will return -1.
255 */
256
257 int fmt_addcomptext(char *component, char *text);
258
259 /*
260 * Append to an existing component. Arguments are:
261 *
262 * bucket - The hash table bucket corresponding to this component,
263 * as returned by fmt_addcomp(). If -1, this function will
264 * return with no actions performed.
265 * component - The component to append text to. Like fmt_addcomp, the
266 * component is searched case-INSENSITIVELY.
267 * text - The text to append to the component. No special processing
268 * is done.
269 *
270 * This function is designed to be called when you are processing continuation
271 * lines on the same header (state == FLDPLUS).
272 */
273
274 void fmt_appendcomp(int bucket, char *component, char *text);
275
276 /*
277 * Iterate over the complete hash table of component structures.
278 *
279 * Arguments are:
280 *
281 * comp - Pointer to the current component structure. The next
282 * component in the hash table after this component. To
283 * start (or restart) the iteration of the hash table
284 * this argument should be NULL.
285 * bucket - Pointer to hash bucket. Will be managed by this function,
286 * the caller should not modify this value.
287 *
288 * Returns the next component in the hash table. This value should be
289 * passed into the next call to fmt_nextcomp(). Returns NULL at the end
290 * of the hash table.
291 */
292
293 struct comp *fmt_nextcomp(struct comp *comp, unsigned int *bucket);
294
295 /*
296 * The implementation of the %(formataddr) function. This is available for
297 * programs to provide their own local implementation if they wish to do
298 * special processing (see uip/replsbr.c for an example). Arguments are:
299 *
300 * orig - Existing list of addresses
301 * str - New address(es) to append to list.
302 *
303 * This function returns an allocated string containing the new list of
304 * addresses.
305 */
306
307 char *formataddr(char *orig, char *str);
308
309 /*
310 * The implementation of the %(concataddr) function. Arguments and behavior
311 * are the same as %(formataddr). Again, see uip/replsbr.c to see how you
312 * can override this behavior.
313 */
314
315 char *concataddr(char *orig, char *str);