]> diplodocus.org Git - nmh/blob - uip/scansbr.c
Fix typo in man page
[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)
138 ? min (width, NMH_BUFSIZ)
139 : SBUFSIZ;
140 for (i = ncomps; i--; )
141 *nxtbuf++ = mh_xmalloc(rlwidth);
142 }
143
144 /*
145 * each-message initialization
146 */
147 nxtbuf = compbuffers;
148 savecomp = used_buf;
149 tmpbuf = *nxtbuf++;
150 startbody = NULL;
151 dat[0] = innum ? innum : outnum;
152 dat[1] = curflg;
153 dat[4] = unseen;
154
155 /*
156 * Get the first field. If the message is non-empty
157 * and we're doing an "inc", open the output file.
158 */
159 bufsz = rlwidth;
160 m_getfld_state_reset (&gstate);
161 if ((state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) == FILEEOF) {
162 if (ferror(inb)) {
163 advise("read", "unable to"); /* "read error" */
164 return SCNFAT;
165 } else {
166 return SCNEOF;
167 }
168 }
169
170 if (outnum) {
171 if (outnum > 0) {
172 scnmsg = m_name (outnum);
173 if (*scnmsg == '?') /* msg num out of range */
174 return SCNNUM;
175 } else {
176 scnmsg = "/dev/null";
177 }
178 if ((scnout = fopen (scnmsg, "w")) == NULL)
179 adios (scnmsg, "unable to write");
180 }
181
182 /* scan - main loop */
183 for (compnum = 1; ;
184 bufsz = rlwidth, state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb)) {
185 switch (state) {
186 case FLD:
187 case FLDPLUS:
188 compnum++;
189 if (outnum) {
190 FPUTS (name);
191 if ( putc (':', scnout) == EOF) DIEWRERR();
192 FPUTS (tmpbuf);
193 }
194 /*
195 * if we're interested in this component, save a pointer
196 * to the component text, then start using our next free
197 * buffer as the component temp buffer (buffer switching
198 * saves an extra copy of the component text).
199 */
200 if ((cptr = fmt_findcasecomp(name))) {
201 if (! cptr->c_text) {
202 cptr->c_text = tmpbuf;
203 for (cp = tmpbuf + strlen (tmpbuf) - 1;
204 cp >= tmpbuf; cp--)
205 if (isspace ((unsigned char) *cp))
206 *cp = 0;
207 else
208 break;
209 *--savecomp = cptr;
210 tmpbuf = *nxtbuf++;
211 }
212 }
213
214 while (state == FLDPLUS) {
215 bufsz = rlwidth;
216 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
217 if (outnum)
218 FPUTS (tmpbuf);
219 }
220 break;
221
222 case BODY:
223 compnum = -1;
224 /*
225 * A slight hack ... if we have less than rlwidth characters
226 * in the buffer, call m_getfld again.
227 */
228
229 if ((i = strlen(tmpbuf)) < rlwidth) {
230 bufsz = rlwidth - i;
231 state = m_getfld (&gstate, name, tmpbuf + i, &bufsz, inb);
232 }
233
234 if (! outnum) {
235 state = FILEEOF; /* stop now if scan cmd */
236 if (bodycomp && startbody == NULL)
237 startbody = tmpbuf;
238 goto finished;
239 }
240 if (putc ('\n', scnout) == EOF) DIEWRERR();
241 FPUTS (tmpbuf);
242 /*
243 * The previous code here used to call m_getfld() using
244 * pointers to the underlying output stdio buffers to
245 * avoid the extra copy. Tests by Markus Schnalke show
246 * no noticable performance loss on larger mailboxes
247 * if we incur an extra copy, and messing around with
248 * internal stdio buffers is becoming more and more
249 * unportable as times go on. So from now on just deal
250 * with the overhead of an extra copy.
251 *
252 * Subtle change - with the previous code tmpbuf wasn't
253 * used, so we could reuse it for the {body} component.
254 * Now since we're using tmpbuf as our read buffer we
255 * need to save the beginning of the body for later.
256 * See the above (and below) use of startbody.
257 */
258 body:;
259 if (bodycomp && startbody == NULL) {
260 startbody = tmpbuf;
261 tmpbuf = *nxtbuf++;
262 }
263
264 while (state == BODY) {
265 bufsz = rlwidth;
266 state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb);
267 FPUTS(tmpbuf);
268 }
269 goto finished;
270
271 case LENERR:
272 case FMTERR:
273 if (innum)
274 fprintf (stderr, "??Format error (message %d) in ",
275 outnum ? outnum : innum);
276 else
277 fprintf (stderr, "??Format error in ");
278
279 fprintf (stderr, "component %d\n", compnum);
280
281 if (outnum) {
282 FPUTS ("\n\nBAD MSG:\n");
283 FPUTS (name);
284 if (putc ('\n', scnout) == EOF) DIEWRERR();
285 state = BODY;
286 goto body;
287 }
288 /* fall through */
289
290 case FILEEOF:
291 goto finished;
292
293 default:
294 adios (NULL, "getfld() returned %d", state);
295 }
296 }
297
298 /*
299 * format and output the scan line.
300 */
301 finished:
302 if (ferror(inb)) {
303 advise("read", "unable to"); /* "read error" */
304 return SCNFAT;
305 }
306
307 /* Save and restore buffer so we don't trash our dynamic pool! */
308 if (bodycomp) {
309 saved_c_text = bodycomp->c_text;
310 bodycomp->c_text = startbody;
311 }
312
313 if (size)
314 dat[2] = size;
315 else if (outnum > 0)
316 {
317 dat[2] = ftell(scnout);
318 if (dat[2] == EOF) DIEWRERR();
319 }
320
321 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
322 struct stat st;
323
324 fstat (fileno(inb), &st);
325 if (!size && !outnum)
326 dat[2] = st.st_size;
327 if (datecomp) {
328 if (! datecomp->c_text) {
329 if (datecomp->c_tws == NULL)
330 datecomp->c_tws = (struct tws *)
331 calloc((size_t) 1, sizeof(*datecomp->c_tws));
332 if (datecomp->c_tws == NULL)
333 adios (NULL, "unable to allocate tws buffer");
334 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
335 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
336 } else {
337 datecomp->c_flags &= ~CF_DATEFAB;
338 }
339 }
340 }
341
342 fmt_scan (fmt, *scanl, slwidth, dat, NULL);
343
344 if (bodycomp)
345 bodycomp->c_text = saved_c_text;
346
347 if (noisy)
348 fputs (charstring_buffer (*scanl), stdout);
349
350 cptr = fmt_findcomp ("encrypted");
351 encrypted = cptr && cptr->c_text;
352
353 /* return dynamically allocated buffers to pool */
354 while ((cptr = *savecomp++)) {
355 cptr->c_text = NULL;
356 }
357
358 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
359 DIEWRERR();
360
361 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
362 }
363
364
365 static int
366 mh_fputs(char *s, FILE *stream)
367 {
368 char c;
369
370 while ((c = *s++))
371 if (putc (c,stream) == EOF )
372 return(EOF);
373 return (0);
374 }
375
376 /* The following two functions allow access to the global gstate above. */
377 void
378 scan_finished () {
379 m_getfld_state_destroy (&gstate);
380 }
381
382 void
383 scan_detect_mbox_style (FILE *f) {
384 m_unknown (&gstate, f);
385 }