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