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