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