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