]> diplodocus.org Git - nmh/blob - uip/scansbr.c
* test/tests/bad-input/test-header: Add test for it.
[nmh] / uip / scansbr.c
1
2 /*
3 * scansbr.c -- routines to help scan along...
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
15 #include <h/scansbr.h>
16 #include <h/tws.h>
17 #include <h/utils.h>
18
19 #ifdef _FSTDIO
20 # define _ptr _p /* Gag */
21 # define _cnt _w /* Wretch */
22 #endif
23
24 #ifdef SCO_5_STDIO
25 # define _ptr __ptr
26 # define _cnt __cnt
27 # define _base __base
28 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
29 #endif
30
31 #define MAXSCANL 256 /* longest possible scan line */
32
33 /*
34 * Buffer size for content part of header fields. We want this
35 * to be large enough so that we don't do a lot of extra FLDPLUS
36 * calls on m_getfld but small enough so that we don't snarf
37 * the entire message body when we're only going to display 30
38 * characters of it.
39 */
40 #define SBUFSIZ 512
41
42 static struct format *fmt;
43 #ifdef JLR
44 static struct format *fmt_top;
45 #endif /* JLR */
46
47 static struct comp *datecomp; /* pntr to "date" comp */
48 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
49 * (if referenced) */
50 static int ncomps = 0; /* # of interesting components */
51 static char **compbuffers = 0; /* buffers for component text */
52 static struct comp **used_buf = 0; /* stack for comp that use buffers */
53
54 static int dat[5]; /* aux. data for format routine */
55
56 char *scanl = 0; /* text of most recent scanline */
57
58 #define DIEWRERR() adios (scnmsg, "write error on")
59
60 #define FPUTS(buf) {\
61 if (mh_fputs(buf,scnout) == EOF)\
62 DIEWRERR();\
63 }
64
65 /*
66 * prototypes
67 */
68 int sc_width (void); /* from termsbr.c */
69 static int mh_fputs(char *, FILE *);
70
71 #ifdef MULTIBYTE_SUPPORT
72 #define SCAN_CHARWIDTH MB_CUR_MAX
73 #else
74 #define SCAN_CHARWIDTH 1
75 #endif
76
77 int
78 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
79 int unseen, char *folder, long size, int noisy)
80 {
81 int i, compnum, encrypted, state;
82 unsigned char *cp, *tmpbuf;
83 char **nxtbuf;
84 char *saved_c_text;
85 struct comp *cptr;
86 struct comp **savecomp;
87 char *scnmsg;
88 FILE *scnout;
89 char name[NAMESZ];
90 static int rlwidth, slwidth;
91
92 #ifdef RPATHS
93 char returnpath[BUFSIZ];
94 char deliverydate[BUFSIZ];
95 #endif
96
97 /* first-time only initialization */
98 if (!scanl) {
99 if (width == 0) {
100 if ((width = sc_width ()) < WIDTH/2)
101 width = WIDTH/2;
102 else if (width > MAXSCANL)
103 width = MAXSCANL;
104 }
105 dat[3] = slwidth = width;
106 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
107 if (outnum)
108 umask(~m_gmprot());
109
110 /* Compile format string */
111 ncomps = fmt_compile (nfs, &fmt) + 1;
112
113 #ifdef JLR
114 fmt_top = fmt;
115 #endif /* JLR */
116 FINDCOMP(bodycomp, "body");
117 FINDCOMP(datecomp, "date");
118 FINDCOMP(cptr, "folder");
119 if (cptr && folder)
120 cptr->c_text = folder;
121 FINDCOMP(cptr, "encrypted");
122 if (!cptr)
123 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
124 cptr->c_name = "encrypted";
125 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
126 wantcomp[i] = cptr;
127 ncomps++;
128 }
129 FINDCOMP (cptr, "dtimenow");
130 if (cptr)
131 cptr->c_text = getcpy(dtimenow (0));
132 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
133 if (nxtbuf == NULL)
134 adios (NULL, "unable to allocate component buffers");
135 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
136 sizeof(struct comp *));
137 if (used_buf == NULL)
138 adios (NULL, "unable to allocate component buffer stack");
139 used_buf += ncomps+1; *--used_buf = 0;
140 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
141 for (i = ncomps; i--; )
142 *nxtbuf++ = mh_xmalloc(rlwidth);
143 }
144
145 /*
146 * each-message initialization
147 */
148 nxtbuf = compbuffers;
149 savecomp = used_buf;
150 tmpbuf = *nxtbuf++;
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 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
160 if (ferror(inb)) {
161 advise("read", "unable to"); /* "read error" */
162 return SCNFAT;
163 } else {
164 return SCNEOF;
165 }
166 }
167
168 if (outnum) {
169 if (outnum > 0) {
170 scnmsg = m_name (outnum);
171 if (*scnmsg == '?') /* msg num out of range */
172 return SCNNUM;
173 } else {
174 scnmsg = "/dev/null";
175 }
176 if ((scnout = fopen (scnmsg, "w")) == NULL)
177 adios (scnmsg, "unable to write");
178 #ifdef RPATHS
179 /*
180 * Add the Return-Path and Delivery-Date
181 * header fields to message.
182 */
183 if (get_returnpath (returnpath, sizeof(returnpath),
184 deliverydate, sizeof(deliverydate))) {
185 FPUTS ("Return-Path: ");
186 FPUTS (returnpath);
187 FPUTS ("Delivery-Date: ");
188 FPUTS (deliverydate);
189 }
190 #endif /* RPATHS */
191 }
192
193 /* scan - main loop */
194 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
195 switch (state) {
196 case FLD:
197 case FLDPLUS:
198 compnum++;
199 if (outnum) {
200 FPUTS (name);
201 if ( putc (':', scnout) == EOF) DIEWRERR();
202 FPUTS (tmpbuf);
203 }
204 /*
205 * if we're interested in this component, save a pointer
206 * to the component text, then start using our next free
207 * buffer as the component temp buffer (buffer switching
208 * saves an extra copy of the component text).
209 */
210 if ((cptr = wantcomp[CHASH(name)])) {
211 do {
212 if (!mh_strcasecmp(name, cptr->c_name)) {
213 if (! cptr->c_text) {
214 cptr->c_text = tmpbuf;
215 for (cp = tmpbuf + strlen (tmpbuf) - 1;
216 cp >= tmpbuf; cp--)
217 if (isspace (*cp))
218 *cp = 0;
219 else
220 break;
221 *--savecomp = cptr;
222 tmpbuf = *nxtbuf++;
223 }
224 break;
225 }
226 } while ((cptr = cptr->c_next));
227 }
228
229 while (state == FLDPLUS) {
230 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
231 if (outnum)
232 FPUTS (tmpbuf);
233 }
234 break;
235
236 case BODY:
237 compnum = -1;
238 if (! outnum) {
239 state = FILEEOF; /* stop now if scan cmd */
240 goto finished;
241 }
242 if (putc ('\n', scnout) == EOF) DIEWRERR();
243 FPUTS (tmpbuf);
244 /*
245 * performance hack: some people like to run "inc" on
246 * things like net.sources or large digests. We do a
247 * copy directly into the output buffer rather than
248 * going through an intermediate buffer.
249 *
250 * We need the amount of data m_getfld found & don't
251 * want to do a strlen on the long buffer so there's
252 * a hack in m_getfld to save the amount of data it
253 * returned in the global "msg_count".
254 */
255 body:;
256 while (state == BODY) {
257 #ifdef LINUX_STDIO
258 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
259 #elif defined(__DragonFly__)
260 if (((struct __FILE_public *)scnout)->_w <= 0) {
261 #else
262 if (scnout->_cnt <= 0) {
263 #endif
264 if (fflush(scnout) == EOF)
265 DIEWRERR ();
266 }
267 #ifdef LINUX_STDIO
268 state = m_getfld(state, name, scnout->_IO_write_ptr,
269 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
270 scnout->_IO_write_ptr += msg_count;
271 #elif defined(__DragonFly__)
272 state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
273 ((struct __FILE_public *)scnout)->_w -= msg_count;
274 ((struct __FILE_public *)scnout)->_p += msg_count;
275 #else
276 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
277 scnout->_cnt -= msg_count;
278 scnout->_ptr += msg_count;
279 #endif
280 }
281 goto finished;
282
283 case LENERR:
284 case FMTERR:
285 fprintf (stderr,
286 innum ? "??Format error (message %d) in "
287 : "??Format error in ",
288 outnum ? outnum : innum);
289 fprintf (stderr, "component %d\n", compnum);
290
291 if (outnum) {
292 FPUTS ("\n\nBAD MSG:\n");
293 FPUTS (name);
294 if (putc ('\n', scnout) == EOF) DIEWRERR();
295 state = BODY;
296 goto body;
297 }
298 /* fall through */
299
300 case FILEEOF:
301 goto finished;
302
303 default:
304 adios (NULL, "getfld() returned %d", state);
305 }
306 }
307
308 /*
309 * format and output the scan line.
310 */
311 finished:
312 if (ferror(inb)) {
313 advise("read", "unable to"); /* "read error" */
314 return SCNFAT;
315 }
316
317 /* Save and restore buffer so we don't trash our dynamic pool! */
318 if (bodycomp) {
319 saved_c_text = bodycomp->c_text;
320 bodycomp->c_text = tmpbuf;
321 }
322
323 if (size)
324 dat[2] = size;
325 else if (outnum > 0)
326 {
327 dat[2] = ftell(scnout);
328 if (dat[2] == EOF) DIEWRERR();
329 }
330
331 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
332 struct stat st;
333
334 fstat (fileno(inb), &st);
335 if (!size && !outnum)
336 dat[2] = st.st_size;
337 if (datecomp) {
338 if (! datecomp->c_text) {
339 if (datecomp->c_tws == NULL)
340 datecomp->c_tws = (struct tws *)
341 calloc((size_t) 1, sizeof(*datecomp->c_tws));
342 if (datecomp->c_tws == NULL)
343 adios (NULL, "unable to allocate tws buffer");
344 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
345 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
346 } else {
347 datecomp->c_flags &= ~CF_DATEFAB;
348 }
349 }
350 }
351
352 fmt_scan (fmt, scanl, slwidth, dat);
353
354 #if 0
355 fmt = fmt_scan (fmt, scanl, slwidth, dat);
356 if (!fmt)
357 fmt = fmt_top; /* reset for old format files */
358 #endif
359
360 if (bodycomp)
361 bodycomp->c_text = saved_c_text;
362
363 if (noisy)
364 fputs (scanl, stdout);
365
366 FINDCOMP (cptr, "encrypted");
367 encrypted = cptr && cptr->c_text;
368
369 /* return dynamically allocated buffers to pool */
370 while ((cptr = *savecomp++)) {
371 *--nxtbuf = cptr->c_text;
372 cptr->c_text = NULL;
373 }
374 *--nxtbuf = tmpbuf;
375
376 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
377 DIEWRERR();
378
379 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
380 }
381
382
383 /*
384 * Cheat: we are loaded with adrparse, which wants a routine called
385 * OfficialName(). We call adrparse:getm() with the correct arguments
386 * to prevent OfficialName() from being called. Hence, the following
387 * is to keep the loader happy.
388 */
389 char *
390 OfficialName (char *name)
391 {
392 return name;
393 }
394
395
396 static int
397 mh_fputs(char *s, FILE *stream)
398 {
399 char c;
400
401 while ((c = *s++))
402 if (putc (c,stream) == EOF )
403 return(EOF);
404 return (0);
405 }
406