]> diplodocus.org Git - nmh/blob - uip/scansbr.c
Fixed typo in mh-profile.man.
[nmh] / uip / scansbr.c
1
2 /*
3 * scansbr.c -- routines to help scan along...
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.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
17 /*
18 * Buffer size for content part of header fields. We want this
19 * to be large enough so that we don't do a lot of extra FLDPLUS
20 * calls on m_getfld but small enough so that we don't snarf
21 * the entire message body when we're only going to display 30
22 * characters of it.
23 */
24 #define SBUFSIZ 512
25
26 static struct format *fmt;
27 static struct comp *datecomp; /* pntr to "date" comp */
28 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
29 * (if referenced) */
30 static int ncomps = 0; /* # of interesting components */
31 static char **compbuffers = 0; /* buffers for component text */
32 static struct comp **used_buf = 0; /* stack for comp that use buffers */
33
34 static int dat[5]; /* aux. data for format routine */
35
36 static m_getfld_state_t gstate; /* for accessor functions below */
37
38 #define DIEWRERR() adios (scnmsg, "write error on")
39
40 #define FPUTS(buf) {\
41 if (mh_fputs(buf,scnout) == EOF)\
42 DIEWRERR();\
43 }
44
45 /*
46 * prototypes
47 */
48 static int mh_fputs(char *, FILE *);
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 int i, compnum, encrypted, state;
55 char *cp, *tmpbuf, *startbody, **nxtbuf;
56 char *saved_c_text = NULL;
57 struct comp *cptr;
58 struct comp **savecomp;
59 char *scnmsg = NULL;
60 FILE *scnout = NULL;
61 char name[NAMESZ];
62 int bufsz;
63 static int rlwidth, slwidth;
64
65 /* first-time only initialization, which will always happen the
66 way the code is now, with callers initializing *scanl to NULL.
67 scanl used to be a global. */
68 if (! *scanl) {
69 if (width == -1) {
70 /* Default: width of the terminal, but at least WIDTH/2. */
71 if ((width = sc_width ()) < WIDTH/2)
72 width = WIDTH/2;
73 } else if (width == 0) {
74 /* Unlimited width. */
75 width = INT_MAX;
76 }
77 dat[3] = slwidth = width;
78 *scanl = charstring_create (width);
79 if (outnum)
80 umask(~m_gmprot());
81
82 /* Compile format string */
83 ncomps = fmt_compile (nfs, &fmt, 1) + 2;
84
85 bodycomp = fmt_findcomp("body");
86 datecomp = fmt_findcomp("date");
87 cptr = fmt_findcomp("folder");
88 if (cptr && folder)
89 cptr->c_text = getcpy(folder);
90 if (fmt_addcompentry("encrypted")) {
91 ncomps++;
92 }
93 cptr = fmt_findcomp("dtimenow");
94 if (cptr)
95 cptr->c_text = getcpy(dtimenow (0));
96
97 /*
98 * In other programs I got rid of this complicated buffer switching,
99 * but since scan reads lots of messages at once and this complicated
100 * memory management, I decided to keep it; otherwise there was
101 * the potential for a lot of malloc() and free()s, and I could
102 * see the malloc() pool really getting fragmented. Maybe it
103 * wouldn't be an issue in practice; perhaps this will get
104 * revisited someday.
105 *
106 * So, some notes for what's going on:
107 *
108 * nxtbuf is an array of pointers that contains malloc()'d buffers
109 * to hold our component text. used_buf is an array of struct comp
110 * pointers that holds pointers to component structures we found while
111 * processing a message.
112 *
113 * We read in the message with m_getfld(), using "tmpbuf" as our
114 * input buffer. tmpbuf is set at the start of message processing
115 * to the first buffer in our buffer pool (nxtbuf).
116 *
117 * Every time we find a component we care about, we set that component's
118 * text buffer to the current value of tmpbuf, and then switch tmpbuf
119 * to the next buffer in our pool. We also add that component to
120 * our used_buf pool.
121 *
122 * When we're done, we go back and zero out all of the component
123 * text buffer pointers that we saved in used_buf.
124 *
125 * Note that this means c_text memory is NOT owned by the fmt_module
126 * and it's our responsibility to free it.
127 */
128
129 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
130 if (nxtbuf == NULL)
131 adios (NULL, "unable to allocate component buffers");
132 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
133 sizeof(struct comp *));
134 if (used_buf == NULL)
135 adios (NULL, "unable to allocate component buffer stack");
136 used_buf += ncomps+1; *--used_buf = 0;
137 rlwidth = bodycomp && (width > SBUFSIZ) ? min (width, BUFSIZ) : SBUFSIZ;
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 } else {
164 return SCNEOF;
165 }
166 }
167
168 if (outnum) {
169 if (outnum > 0) {
170 scnmsg = m_name (outnum);
171 if (*scnmsg == '?') /* msg num out of range */
172 return SCNNUM;
173 } else {
174 scnmsg = "/dev/null";
175 }
176 if ((scnout = fopen (scnmsg, "w")) == NULL)
177 adios (scnmsg, "unable to write");
178 }
179
180 /* scan - main loop */
181 for (compnum = 1; ;
182 bufsz = rlwidth, state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) {
183 switch (state) {
184 case FLD:
185 case FLDPLUS:
186 compnum++;
187 if (outnum) {
188 FPUTS (name);
189 if ( putc (':', scnout) == EOF) DIEWRERR();
190 FPUTS (tmpbuf);
191 }
192 /*
193 * if we're interested in this component, save a pointer
194 * to the component text, then start using our next free
195 * buffer as the component temp buffer (buffer switching
196 * saves an extra copy of the component text).
197 */
198 if ((cptr = fmt_findcasecomp(name))) {
199 if (! cptr->c_text) {
200 cptr->c_text = tmpbuf;
201 for (cp = tmpbuf + strlen (tmpbuf) - 1;
202 cp >= tmpbuf; cp--)
203 if (isspace ((unsigned char) *cp))
204 *cp = 0;
205 else
206 break;
207 *--savecomp = cptr;
208 tmpbuf = *nxtbuf++;
209 }
210 }
211
212 while (state == FLDPLUS) {
213 bufsz = rlwidth;
214 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
215 if (outnum)
216 FPUTS (tmpbuf);
217 }
218 break;
219
220 case BODY:
221 compnum = -1;
222 /*
223 * A slight hack ... if we have less than rlwidth characters
224 * in the buffer, call m_getfld again.
225 */
226
227 if ((i = strlen(tmpbuf)) < rlwidth) {
228 bufsz = rlwidth - i;
229 state = m_getfld (&gstate, name, tmpbuf + i, &bufsz, inb);
230 }
231
232 if (! outnum) {
233 state = FILEEOF; /* stop now if scan cmd */
234 if (bodycomp && startbody == NULL)
235 startbody = tmpbuf;
236 goto finished;
237 }
238 if (putc ('\n', scnout) == EOF) DIEWRERR();
239 FPUTS (tmpbuf);
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 noticable 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 FPUTS(tmpbuf);
266 }
267 goto finished;
268
269 case LENERR:
270 case FMTERR:
271 if (innum)
272 fprintf (stderr, "??Format error (message %d) in ",
273 outnum ? outnum : innum);
274 else
275 fprintf (stderr, "??Format error in ");
276
277 fprintf (stderr, "component %d\n", compnum);
278
279 if (outnum) {
280 FPUTS ("\n\nBAD MSG:\n");
281 FPUTS (name);
282 if (putc ('\n', scnout) == EOF) DIEWRERR();
283 state = BODY;
284 goto body;
285 }
286 /* fall through */
287
288 case FILEEOF:
289 goto finished;
290
291 default:
292 adios (NULL, "getfld() returned %d", state);
293 }
294 }
295
296 /*
297 * format and output the scan line.
298 */
299 finished:
300 if (ferror(inb)) {
301 advise("read", "unable to"); /* "read error" */
302 return SCNFAT;
303 }
304
305 /* Save and restore buffer so we don't trash our dynamic pool! */
306 if (bodycomp) {
307 saved_c_text = bodycomp->c_text;
308 bodycomp->c_text = startbody;
309 }
310
311 if (size)
312 dat[2] = size;
313 else if (outnum > 0)
314 {
315 dat[2] = ftell(scnout);
316 if (dat[2] == EOF) DIEWRERR();
317 }
318
319 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
320 struct stat st;
321
322 fstat (fileno(inb), &st);
323 if (!size && !outnum)
324 dat[2] = st.st_size;
325 if (datecomp) {
326 if (! datecomp->c_text) {
327 if (datecomp->c_tws == NULL)
328 datecomp->c_tws = (struct tws *)
329 calloc((size_t) 1, sizeof(*datecomp->c_tws));
330 if (datecomp->c_tws == NULL)
331 adios (NULL, "unable to allocate tws buffer");
332 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
333 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
334 } else {
335 datecomp->c_flags &= ~CF_DATEFAB;
336 }
337 }
338 }
339
340 fmt_scan (fmt, *scanl, slwidth, dat, NULL);
341
342 if (bodycomp)
343 bodycomp->c_text = saved_c_text;
344
345 if (noisy)
346 fputs (charstring_buffer (*scanl), stdout);
347
348 cptr = fmt_findcomp ("encrypted");
349 encrypted = cptr && cptr->c_text;
350
351 /* return dynamically allocated buffers to pool */
352 while ((cptr = *savecomp++)) {
353 cptr->c_text = NULL;
354 }
355
356 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
357 DIEWRERR();
358
359 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
360 }
361
362
363 static int
364 mh_fputs(char *s, FILE *stream)
365 {
366 char c;
367
368 while ((c = *s++))
369 if (putc (c,stream) == EOF )
370 return(EOF);
371 return (0);
372 }
373
374 /* The following two functions allow access to the global gstate above. */
375 void
376 scan_finished () {
377 m_getfld_state_destroy (&gstate);
378 }
379
380 void
381 scan_detect_mbox_style (FILE *f) {
382 m_unknown (&gstate, f);
383 }