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