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