]> diplodocus.org Git - nmh/blob - uip/scansbr.c
Simplified m_strn() per Ralph's suggestions.
[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 int i, compnum, encrypted, state;
52 char *cp, *tmpbuf, *startbody, **nxtbuf;
53 char *saved_c_text = NULL;
54 struct comp *cptr;
55 struct comp **savecomp;
56 char *scnmsg = NULL;
57 FILE *scnout = NULL;
58 char name[NAMESZ];
59 int bufsz;
60 static int rlwidth, slwidth;
61
62 /* first-time only initialization, which will always happen the
63 way the code is now, with callers initializing *scanl to NULL.
64 scanl used to be a global. */
65 if (! *scanl) {
66 if (width == -1) {
67 /* Default: width of the terminal, but at least WIDTH/2. */
68 if ((width = sc_width ()) < WIDTH/2)
69 width = WIDTH/2;
70 } else if (width == 0) {
71 /* Unlimited width. */
72 width = INT_MAX;
73 }
74 dat[3] = slwidth = width;
75 *scanl = charstring_create (min(width, NMH_BUFSIZ));
76 if (outnum)
77 umask(~m_gmprot());
78
79 /* Compile format string */
80 ncomps = fmt_compile (nfs, &fmt, 1) + 2;
81
82 bodycomp = fmt_findcomp("body");
83 datecomp = fmt_findcomp("date");
84 cptr = fmt_findcomp("folder");
85 if (cptr && folder)
86 cptr->c_text = mh_xstrdup(folder);
87 if (fmt_addcompentry("encrypted")) {
88 ncomps++;
89 }
90 cptr = fmt_findcomp("dtimenow");
91 if (cptr)
92 cptr->c_text = getcpy(dtimenow (0));
93
94 /*
95 * In other programs I got rid of this complicated buffer switching,
96 * but since scan reads lots of messages at once and this complicated
97 * memory management, I decided to keep it; otherwise there was
98 * the potential for a lot of malloc() and free()s, and I could
99 * see the malloc() pool really getting fragmented. Maybe it
100 * wouldn't be an issue in practice; perhaps this will get
101 * revisited someday.
102 *
103 * So, some notes for what's going on:
104 *
105 * nxtbuf is an array of pointers that contains malloc()'d buffers
106 * to hold our component text. used_buf is an array of struct comp
107 * pointers that holds pointers to component structures we found while
108 * processing a message.
109 *
110 * We read in the message with m_getfld(), using "tmpbuf" as our
111 * input buffer. tmpbuf is set at the start of message processing
112 * to the first buffer in our buffer pool (nxtbuf).
113 *
114 * Every time we find a component we care about, we set that component's
115 * text buffer to the current value of tmpbuf, and then switch tmpbuf
116 * to the next buffer in our pool. We also add that component to
117 * our used_buf pool.
118 *
119 * When we're done, we go back and zero out all of the component
120 * text buffer pointers that we saved in used_buf.
121 *
122 * Note that this means c_text memory is NOT owned by the fmt_module
123 * and it's our responsibility to free it.
124 */
125
126 nxtbuf = compbuffers = mh_xcalloc(ncomps, sizeof *nxtbuf);
127 used_buf = mh_xcalloc(ncomps + 1, sizeof *used_buf);
128 used_buf += ncomps+1; *--used_buf = 0;
129 rlwidth = NMH_BUFSIZ;
130 for (i = ncomps; i--; )
131 *nxtbuf++ = mh_xmalloc(rlwidth);
132 }
133
134 /*
135 * each-message initialization
136 */
137 nxtbuf = compbuffers;
138 savecomp = used_buf;
139 tmpbuf = *nxtbuf++;
140 startbody = NULL;
141 dat[0] = innum ? innum : outnum;
142 dat[1] = curflg;
143 dat[4] = unseen;
144
145 /*
146 * Get the first field. If the message is non-empty
147 * and we're doing an "inc", open the output file.
148 */
149 bufsz = rlwidth;
150 m_getfld_state_reset (&gstate);
151 if ((state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) == FILEEOF) {
152 if (ferror(inb)) {
153 advise("read", "unable to"); /* "read error" */
154 return SCNFAT;
155 }
156 return SCNEOF;
157 }
158
159 if (outnum > 0) {
160 scnmsg = m_name (outnum);
161 if (*scnmsg == '?') /* msg num out of range */
162 return SCNNUM;
163 if ((scnout = fopen (scnmsg, "w")) == NULL)
164 adios (scnmsg, "unable to write");
165 }
166
167 /* scan - main loop */
168 for (compnum = 1; ;
169 bufsz = rlwidth, state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) {
170 switch (state) {
171 case FLD:
172 case FLDPLUS:
173 compnum++;
174 if (scnout) {
175 FPUTS (name);
176 PUTC(':');
177 FPUTS (tmpbuf);
178 }
179 /*
180 * if we're interested in this component, save a pointer
181 * to the component text, then start using our next free
182 * buffer as the component temp buffer (buffer switching
183 * saves an extra copy of the component text).
184 */
185 if ((cptr = fmt_findcasecomp(name))) {
186 if (! cptr->c_text) {
187 cptr->c_text = tmpbuf;
188 for (cp = tmpbuf + strlen (tmpbuf) - 1;
189 cp >= tmpbuf; cp--)
190 if (isspace ((unsigned char) *cp))
191 *cp = 0;
192 else
193 break;
194 *--savecomp = cptr;
195 tmpbuf = *nxtbuf++;
196 }
197 }
198
199 while (state == FLDPLUS) {
200 bufsz = rlwidth;
201 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
202 if (scnout)
203 FPUTS (tmpbuf);
204 }
205 break;
206
207 case BODY:
208 /*
209 * A slight hack ... if we have less than rlwidth characters
210 * in the buffer, call m_getfld again.
211 */
212
213 if ((i = strlen(tmpbuf)) < rlwidth) {
214 bufsz = rlwidth - i;
215 state = m_getfld (&gstate, name, tmpbuf + i, &bufsz, inb);
216 }
217
218 if (outnum == 0) {
219 state = FILEEOF; /* stop now if scan cmd */
220 if (bodycomp && startbody == NULL)
221 startbody = tmpbuf;
222 goto finished;
223 }
224 if (scnout) {
225 PUTC('\n');
226 FPUTS (tmpbuf);
227 }
228 /*
229 * The previous code here used to call m_getfld() using
230 * pointers to the underlying output stdio buffers to
231 * avoid the extra copy. Tests by Markus Schnalke show
232 * no noticeable performance loss on larger mailboxes
233 * if we incur an extra copy, and messing around with
234 * internal stdio buffers is becoming more and more
235 * unportable as times go on. So from now on just deal
236 * with the overhead of an extra copy.
237 *
238 * Subtle change - with the previous code tmpbuf wasn't
239 * used, so we could reuse it for the {body} component.
240 * Now since we're using tmpbuf as our read buffer we
241 * need to save the beginning of the body for later.
242 * See the above (and below) use of startbody.
243 */
244 body:;
245 if (bodycomp && startbody == NULL) {
246 startbody = tmpbuf;
247 tmpbuf = *nxtbuf++;
248 }
249
250 while (state == BODY) {
251 bufsz = rlwidth;
252 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
253 if (scnout)
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 (scnout) {
269 FPUTS ("\n\nBAD MSG:\n");
270 FPUTS (name);
271 PUTC('\n');
272 state = BODY;
273 goto body;
274 }
275 goto finished;
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 (scnout)
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 (scnout && (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(void) {
352 m_getfld_state_destroy (&gstate);
353 }
354
355 void
356 scan_detect_mbox_style (FILE *f) {
357 m_unknown (&gstate, f);
358 }