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