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