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