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