]> diplodocus.org Git - nmh/blob - uip/scansbr.c
Added l modifier to X printf conversions of wchar_t's.
[nmh] / uip / scansbr.c
1
2 /*
3 * scansbr.c -- routines to help scan along...
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
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/scansbr.h>
14 #include <h/tws.h>
15 #include <h/utils.h>
16
17 #define MAXSCANL 256 /* longest possible scan line */
18
19 /*
20 * Buffer size for content part of header fields. We want this
21 * to be large enough so that we don't do a lot of extra FLDPLUS
22 * calls on m_getfld but small enough so that we don't snarf
23 * the entire message body when we're only going to display 30
24 * characters of it.
25 */
26 #define SBUFSIZ 512
27
28 static struct format *fmt;
29 static struct comp *datecomp; /* pntr to "date" comp */
30 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
31 * (if referenced) */
32 static int ncomps = 0; /* # of interesting components */
33 static char **compbuffers = 0; /* buffers for component text */
34 static struct comp **used_buf = 0; /* stack for comp that use buffers */
35
36 static int dat[5]; /* aux. data for format routine */
37
38 char *scanl = 0; /* text of most recent scanline */
39 m_getfld_state_t gstate; /* for access by msh */
40
41 #define DIEWRERR() adios (scnmsg, "write error on")
42
43 #define FPUTS(buf) {\
44 if (mh_fputs(buf,scnout) == EOF)\
45 DIEWRERR();\
46 }
47
48 /*
49 * prototypes
50 */
51 static int mh_fputs(char *, FILE *);
52
53 #ifdef MULTIBYTE_SUPPORT
54 #define SCAN_CHARWIDTH MB_CUR_MAX
55 #else
56 #define SCAN_CHARWIDTH 1
57 #endif
58
59 int
60 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
61 int unseen, char *folder, long size, int noisy)
62 {
63 int i, compnum, encrypted, state;
64 unsigned char *cp, *tmpbuf, *startbody;
65 char **nxtbuf;
66 char *saved_c_text = NULL;
67 struct comp *cptr;
68 struct comp **savecomp;
69 char *scnmsg = NULL;
70 FILE *scnout = NULL;
71 char name[NAMESZ];
72 int bufsz;
73 static int rlwidth, slwidth;
74 static size_t scanl_size;
75
76 /* first-time only initialization */
77 if (!scanl) {
78 if (width == 0) {
79 if ((width = sc_width ()) < WIDTH/2)
80 width = WIDTH/2;
81 else if (width > MAXSCANL)
82 width = MAXSCANL;
83 }
84 dat[3] = slwidth = width;
85 /* Arbitrarily allocate 20 * slwidth to provide room for lots
86 of escape sequences. */
87 scanl_size = SCAN_CHARWIDTH * (20 * slwidth + 2);
88 scanl = (char *) mh_xmalloc (scanl_size);
89 if (outnum)
90 umask(~m_gmprot());
91
92 /* Compile format string */
93 ncomps = fmt_compile (nfs, &fmt, 1) + 2;
94
95 bodycomp = fmt_findcomp("body");
96 datecomp = fmt_findcomp("date");
97 cptr = fmt_findcomp("folder");
98 if (cptr && folder)
99 cptr->c_text = getcpy(folder);
100 if (fmt_addcompentry("encrypted")) {
101 ncomps++;
102 }
103 cptr = fmt_findcomp("dtimenow");
104 if (cptr)
105 cptr->c_text = getcpy(dtimenow (0));
106
107 /*
108 * In other programs I got rid of this complicated buffer switching,
109 * but since scan reads lots of messages at once and this complicated
110 * memory management, I decided to keep it; otherwise there was
111 * the potential for a lot of malloc() and free()s, and I could
112 * see the malloc() pool really getting fragmented. Maybe it
113 * wouldn't be an issue in practice; perhaps this will get
114 * revisited someday.
115 *
116 * So, some notes for what's going on:
117 *
118 * nxtbuf is an array of pointers that contains malloc()'d buffers
119 * to hold our component text. used_buf is an array of struct comp
120 * pointers that holds pointers to component structures we found while
121 * processing a message.
122 *
123 * We read in the message with m_getfld(), using "tmpbuf" as our
124 * input buffer. tmpbuf is set at the start of message processing
125 * to the first buffer in our buffer pool (nxtbuf).
126 *
127 * Every time we find a component we care about, we set that component's
128 * text buffer to the current value of tmpbuf, and then switch tmpbuf
129 * to the next buffer in our pool. We also add that component to
130 * our used_buf pool.
131 *
132 * When we're done, we go back and zero out all of the component
133 * text buffer pointers that we saved in used_buf.
134 *
135 * Note that this means c_text memory is NOT owned by the fmt_module
136 * and it's our responsibility to free it.
137 */
138
139 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
140 if (nxtbuf == NULL)
141 adios (NULL, "unable to allocate component buffers");
142 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
143 sizeof(struct comp *));
144 if (used_buf == NULL)
145 adios (NULL, "unable to allocate component buffer stack");
146 used_buf += ncomps+1; *--used_buf = 0;
147 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
148 for (i = ncomps; i--; )
149 *nxtbuf++ = mh_xmalloc(rlwidth);
150 }
151
152 /*
153 * each-message initialization
154 */
155 nxtbuf = compbuffers;
156 savecomp = used_buf;
157 tmpbuf = *nxtbuf++;
158 startbody = NULL;
159 dat[0] = innum ? innum : outnum;
160 dat[1] = curflg;
161 dat[4] = unseen;
162
163 /*
164 * Get the first field. If the message is non-empty
165 * and we're doing an "inc", open the output file.
166 */
167 bufsz = rlwidth;
168 m_getfld_state_reset (&gstate);
169 if ((state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) == FILEEOF) {
170 if (ferror(inb)) {
171 advise("read", "unable to"); /* "read error" */
172 return SCNFAT;
173 } else {
174 return SCNEOF;
175 }
176 }
177
178 if (outnum) {
179 if (outnum > 0) {
180 scnmsg = m_name (outnum);
181 if (*scnmsg == '?') /* msg num out of range */
182 return SCNNUM;
183 } else {
184 scnmsg = "/dev/null";
185 }
186 if ((scnout = fopen (scnmsg, "w")) == NULL)
187 adios (scnmsg, "unable to write");
188 }
189
190 /* scan - main loop */
191 for (compnum = 1; ;
192 bufsz = rlwidth, state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) {
193 switch (state) {
194 case FLD:
195 case FLDPLUS:
196 compnum++;
197 if (outnum) {
198 FPUTS (name);
199 if ( putc (':', scnout) == EOF) DIEWRERR();
200 FPUTS (tmpbuf);
201 }
202 /*
203 * if we're interested in this component, save a pointer
204 * to the component text, then start using our next free
205 * buffer as the component temp buffer (buffer switching
206 * saves an extra copy of the component text).
207 */
208 if ((cptr = fmt_findcasecomp(name))) {
209 if (! cptr->c_text) {
210 cptr->c_text = tmpbuf;
211 for (cp = tmpbuf + strlen (tmpbuf) - 1;
212 cp >= tmpbuf; cp--)
213 if (isspace (*cp))
214 *cp = 0;
215 else
216 break;
217 *--savecomp = cptr;
218 tmpbuf = *nxtbuf++;
219 }
220 }
221
222 while (state == FLDPLUS) {
223 bufsz = rlwidth;
224 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
225 if (outnum)
226 FPUTS (tmpbuf);
227 }
228 break;
229
230 case BODY:
231 compnum = -1;
232 /*
233 * A slight hack ... if we have less than rlwidth characters
234 * in the buffer, call m_getfld again.
235 */
236
237 if ((i = strlen(tmpbuf)) < rlwidth) {
238 bufsz = rlwidth - i;
239 state = m_getfld (&gstate, name, tmpbuf + i, &bufsz, inb);
240 }
241
242 if (! outnum) {
243 state = FILEEOF; /* stop now if scan cmd */
244 if (bodycomp && startbody == NULL)
245 startbody = tmpbuf;
246 goto finished;
247 }
248 if (putc ('\n', scnout) == EOF) DIEWRERR();
249 FPUTS (tmpbuf);
250 /*
251 * The previous code here used to call m_getfld() using
252 * pointers to the underlying output stdio buffers to
253 * avoid the extra copy. Tests by Markus Schnalke show
254 * no noticable performance loss on larger mailboxes
255 * if we incur an extra copy, and messing around with
256 * internal stdio buffers is becoming more and more
257 * unportable as times go on. So from now on just deal
258 * with the overhead of an extra copy.
259 *
260 * Subtle change - with the previous code tmpbuf wasn't
261 * used, so we could reuse it for the {body} component.
262 * Now since we're using tmpbuf as our read buffer we
263 * need to save the beginning of the body for later.
264 * See the above (and below) use of startbody.
265 */
266 body:;
267 if (bodycomp && startbody == NULL) {
268 startbody = tmpbuf;
269 tmpbuf = *nxtbuf++;
270 }
271
272 while (state == BODY) {
273 bufsz = rlwidth;
274 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
275 FPUTS(tmpbuf);
276 }
277 goto finished;
278
279 case LENERR:
280 case FMTERR:
281 fprintf (stderr,
282 innum ? "??Format error (message %d) in "
283 : "??Format error in ",
284 outnum ? outnum : innum);
285 fprintf (stderr, "component %d\n", compnum);
286
287 if (outnum) {
288 FPUTS ("\n\nBAD MSG:\n");
289 FPUTS (name);
290 if (putc ('\n', scnout) == EOF) DIEWRERR();
291 state = BODY;
292 goto body;
293 }
294 /* fall through */
295
296 case FILEEOF:
297 goto finished;
298
299 default:
300 adios (NULL, "getfld() returned %d", state);
301 }
302 }
303
304 /*
305 * format and output the scan line.
306 */
307 finished:
308 if (ferror(inb)) {
309 advise("read", "unable to"); /* "read error" */
310 return SCNFAT;
311 }
312
313 /* Save and restore buffer so we don't trash our dynamic pool! */
314 if (bodycomp) {
315 saved_c_text = bodycomp->c_text;
316 bodycomp->c_text = startbody;
317 }
318
319 if (size)
320 dat[2] = size;
321 else if (outnum > 0)
322 {
323 dat[2] = ftell(scnout);
324 if (dat[2] == EOF) DIEWRERR();
325 }
326
327 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
328 struct stat st;
329
330 fstat (fileno(inb), &st);
331 if (!size && !outnum)
332 dat[2] = st.st_size;
333 if (datecomp) {
334 if (! datecomp->c_text) {
335 if (datecomp->c_tws == NULL)
336 datecomp->c_tws = (struct tws *)
337 calloc((size_t) 1, sizeof(*datecomp->c_tws));
338 if (datecomp->c_tws == NULL)
339 adios (NULL, "unable to allocate tws buffer");
340 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
341 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
342 } else {
343 datecomp->c_flags &= ~CF_DATEFAB;
344 }
345 }
346 }
347
348 fmt_scan (fmt, scanl, scanl_size, slwidth, dat);
349
350 if (bodycomp)
351 bodycomp->c_text = saved_c_text;
352
353 if (noisy)
354 fputs (scanl, stdout);
355
356 cptr = fmt_findcomp ("encrypted");
357 encrypted = cptr && cptr->c_text;
358
359 /* return dynamically allocated buffers to pool */
360 while ((cptr = *savecomp++)) {
361 cptr->c_text = NULL;
362 }
363
364 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
365 DIEWRERR();
366
367 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
368 }
369
370
371 static int
372 mh_fputs(char *s, FILE *stream)
373 {
374 char c;
375
376 while ((c = *s++))
377 if (putc (c,stream) == EOF )
378 return(EOF);
379 return (0);
380 }
381
382 /* The following three functions allow access to the global gstate above. */
383 void
384 scan_finished () {
385 m_getfld_state_destroy (&gstate);
386 }
387
388 void
389 scan_detect_mbox_style (FILE *f) {
390 m_unknown (&gstate, f);
391 }
392
393 void
394 scan_eom_action (int (*action)()) {
395 m_eomsbr (gstate, action);
396 }
397
398 void
399 scan_reset_m_getfld_state () {
400 m_getfld_state_reset (&gstate);
401 }