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