]> diplodocus.org Git - nmh/blob - uip/scansbr.c
decode_rfc2047(): Stop unused-variable warning if HAVE_ICONV false.
[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, encrypted, 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 if (fmt_addcompentry("encrypted")) {
100 ncomps++;
101 }
102 cptr = fmt_findcomp("dtimenow");
103 if (cptr)
104 cptr->c_text = getcpy(dtimenow (0));
105
106 /*
107 * In other programs I got rid of this complicated buffer switching,
108 * but since scan reads lots of messages at once and this complicated
109 * memory management, I decided to keep it; otherwise there was
110 * the potential for a lot of malloc() and free()s, and I could
111 * see the malloc() pool really getting fragmented. Maybe it
112 * wouldn't be an issue in practice; perhaps this will get
113 * revisited someday.
114 *
115 * So, some notes for what's going on:
116 *
117 * nxtbuf is an array of pointers that contains malloc()'d buffers
118 * to hold our component text. used_buf is an array of struct comp
119 * pointers that holds pointers to component structures we found while
120 * processing a message.
121 *
122 * We read in the message with m_getfld(), using "tmpbuf" as our
123 * input buffer. tmpbuf is set at the start of message processing
124 * to the first buffer in our buffer pool (nxtbuf).
125 *
126 * Every time we find a component we care about, we set that component's
127 * text buffer to the current value of tmpbuf, and then switch tmpbuf
128 * to the next buffer in our pool. We also add that component to
129 * our used_buf pool.
130 *
131 * When we're done, we go back and zero out all of the component
132 * text buffer pointers that we saved in used_buf.
133 *
134 * Note that this means c_text memory is NOT owned by the fmt_module
135 * and it's our responsibility to free it.
136 */
137
138 nxtbuf = compbuffers = mh_xcalloc(ncomps, sizeof *nxtbuf);
139 used_buf = mh_xcalloc(ncomps + 1, sizeof *used_buf);
140 used_buf += ncomps+1; *--used_buf = 0;
141 rlwidth = NMH_BUFSIZ;
142 for (i = ncomps; i--; )
143 *nxtbuf++ = mh_xmalloc(rlwidth);
144 }
145
146 /*
147 * each-message initialization
148 */
149 nxtbuf = compbuffers;
150 savecomp = used_buf;
151 tmpbuf = *nxtbuf++;
152 startbody = NULL;
153 dat[0] = innum ? innum : outnum;
154 dat[1] = curflg;
155 dat[4] = unseen;
156
157 /*
158 * Get the first field. If the message is non-empty
159 * and we're doing an "inc", open the output file.
160 */
161 bufsz = rlwidth;
162 m_getfld_state_reset (&gstate);
163 if ((state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) == FILEEOF) {
164 if (ferror(inb)) {
165 advise("read", "unable to"); /* "read error" */
166 return SCNFAT;
167 }
168 return SCNEOF;
169 }
170
171 if (outnum > 0) {
172 scnmsg = m_name (outnum);
173 if (*scnmsg == '?') /* msg num out of range */
174 return SCNNUM;
175 if ((scnout = fopen (scnmsg, "w")) == NULL)
176 adios (scnmsg, "unable to write");
177 }
178
179 /* scan - main loop */
180 for (compnum = 1; ;
181 bufsz = rlwidth, state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) {
182 switch (state) {
183 case FLD:
184 case FLDPLUS:
185 compnum++;
186 if (scnout) {
187 FPUTS (name);
188 PUTC(':');
189 FPUTS (tmpbuf);
190 }
191 /*
192 * if we're interested in this component, save a pointer
193 * to the component text, then start using our next free
194 * buffer as the component temp buffer (buffer switching
195 * saves an extra copy of the component text).
196 */
197 if ((cptr = fmt_findcasecomp(name))) {
198 if (! cptr->c_text) {
199 cptr->c_text = tmpbuf;
200 for (cp = tmpbuf + strlen (tmpbuf) - 1;
201 cp >= tmpbuf; cp--)
202 if (isspace ((unsigned char) *cp))
203 *cp = 0;
204 else
205 break;
206 *--savecomp = cptr;
207 tmpbuf = *nxtbuf++;
208 }
209 }
210
211 while (state == FLDPLUS) {
212 bufsz = rlwidth;
213 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
214 if (scnout)
215 FPUTS (tmpbuf);
216 }
217 break;
218
219 case BODY:
220 /*
221 * A slight hack ... if we have less than rlwidth characters
222 * in the buffer, call m_getfld again.
223 */
224
225 if ((i = strlen(tmpbuf)) < rlwidth) {
226 bufsz = rlwidth - i;
227 state = m_getfld (&gstate, name, tmpbuf + i, &bufsz, inb);
228 }
229
230 if (outnum == 0) {
231 state = FILEEOF; /* stop now if scan cmd */
232 if (bodycomp && startbody == NULL)
233 startbody = tmpbuf;
234 goto finished;
235 }
236 if (scnout) {
237 PUTC('\n');
238 FPUTS (tmpbuf);
239 }
240 /*
241 * The previous code here used to call m_getfld() using
242 * pointers to the underlying output stdio buffers to
243 * avoid the extra copy. Tests by Markus Schnalke show
244 * no noticeable performance loss on larger mailboxes
245 * if we incur an extra copy, and messing around with
246 * internal stdio buffers is becoming more and more
247 * unportable as times go on. So from now on just deal
248 * with the overhead of an extra copy.
249 *
250 * Subtle change - with the previous code tmpbuf wasn't
251 * used, so we could reuse it for the {body} component.
252 * Now since we're using tmpbuf as our read buffer we
253 * need to save the beginning of the body for later.
254 * See the above (and below) use of startbody.
255 */
256 body:;
257 if (bodycomp && startbody == NULL) {
258 startbody = tmpbuf;
259 tmpbuf = *nxtbuf++;
260 }
261
262 while (state == BODY) {
263 bufsz = rlwidth;
264 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
265 if (scnout)
266 FPUTS(tmpbuf);
267 }
268 goto finished;
269
270 case LENERR:
271 case FMTERR:
272 if (innum)
273 fprintf (stderr, "??Format error (message %d) in ",
274 outnum ? outnum : innum);
275 else
276 fprintf (stderr, "??Format error in ");
277
278 fprintf (stderr, "component %d\n", compnum);
279
280 if (scnout) {
281 FPUTS ("\n\nBAD MSG:\n");
282 FPUTS (name);
283 PUTC('\n');
284 state = BODY;
285 goto body;
286 }
287 goto finished;
288
289 case FILEEOF:
290 goto finished;
291
292 default:
293 die("getfld() returned %d", state);
294 }
295 }
296
297 /*
298 * format and output the scan line.
299 */
300 finished:
301 if (ferror(inb)) {
302 advise("read", "unable to"); /* "read error" */
303 return SCNFAT;
304 }
305
306 /* Save and restore buffer so we don't trash our dynamic pool! */
307 if (bodycomp) {
308 saved_c_text = bodycomp->c_text;
309 bodycomp->c_text = startbody;
310 }
311
312 if (size)
313 dat[2] = size;
314 else if (scnout)
315 {
316 dat[2] = ftell(scnout);
317 if (dat[2] == EOF) DIEWRERR();
318 }
319
320 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
321 struct stat st;
322
323 fstat (fileno(inb), &st);
324 if (!size && !outnum)
325 dat[2] = st.st_size;
326 if (datecomp) {
327 if (! datecomp->c_text) {
328 if (datecomp->c_tws == NULL)
329 NEW0(datecomp->c_tws);
330 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
331 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
332 } else {
333 datecomp->c_flags &= ~CF_DATEFAB;
334 }
335 }
336 }
337
338 fmt_scan (fmt, *scanl, slwidth, dat, NULL);
339
340 if (bodycomp)
341 bodycomp->c_text = saved_c_text;
342
343 if (noisy)
344 fputs (charstring_buffer (*scanl), stdout);
345
346 cptr = fmt_findcomp ("encrypted");
347 encrypted = cptr && cptr->c_text;
348
349 /* return dynamically allocated buffers to pool */
350 while ((cptr = *savecomp++)) {
351 cptr->c_text = NULL;
352 }
353
354 if (scnout && (ferror(scnout) || fclose (scnout) == EOF))
355 DIEWRERR();
356
357 return state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG;
358 }
359
360
361 /* The following two functions allow access to the global gstate above. */
362 void
363 scan_finished(void)
364 {
365 m_getfld_state_destroy (&gstate);
366 }
367
368 void
369 scan_detect_mbox_style (FILE *f)
370 {
371 m_unknown (&gstate, f);
372 }