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