]> diplodocus.org Git - nmh/blob - uip/scansbr.c
Cope with sasl_decode64() returning SASL_CONTINUE as well as SASL_OK.
[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
72 int
73 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
74 int unseen, char *folder, long size, int noisy)
75 {
76 int i, compnum, encrypted, state;
77 unsigned char *cp, *tmpbuf;
78 char **nxtbuf;
79 char *saved_c_text;
80 struct comp *cptr;
81 struct comp **savecomp;
82 char *scnmsg;
83 FILE *scnout;
84 char name[NAMESZ];
85 static int rlwidth, slwidth;
86
87 #ifdef RPATHS
88 char returnpath[BUFSIZ];
89 char deliverydate[BUFSIZ];
90 #endif
91
92 /* first-time only initialization */
93 if (!scanl) {
94 if (width == 0) {
95 if ((width = sc_width ()) < WIDTH/2)
96 width = WIDTH/2;
97 else if (width > MAXSCANL)
98 width = MAXSCANL;
99 }
100 dat[3] = slwidth = width;
101 scanl = (char *) mh_xmalloc((size_t) MB_CUR_MAX * (slwidth + 2) );
102 if (outnum)
103 umask(~m_gmprot());
104
105 /* Compile format string */
106 ncomps = fmt_compile (nfs, &fmt) + 1;
107
108 #ifdef JLR
109 fmt_top = fmt;
110 #endif /* JLR */
111 FINDCOMP(bodycomp, "body");
112 FINDCOMP(datecomp, "date");
113 FINDCOMP(cptr, "folder");
114 if (cptr && folder)
115 cptr->c_text = folder;
116 FINDCOMP(cptr, "encrypted");
117 if (!cptr)
118 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
119 cptr->c_name = "encrypted";
120 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
121 wantcomp[i] = cptr;
122 ncomps++;
123 }
124 FINDCOMP (cptr, "dtimenow");
125 if (cptr)
126 cptr->c_text = getcpy(dtimenow (0));
127 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
128 if (nxtbuf == NULL)
129 adios (NULL, "unable to allocate component buffers");
130 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
131 sizeof(struct comp *));
132 if (used_buf == NULL)
133 adios (NULL, "unable to allocate component buffer stack");
134 used_buf += ncomps+1; *--used_buf = 0;
135 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
136 for (i = ncomps; i--; )
137 *nxtbuf++ = mh_xmalloc(rlwidth);
138 }
139
140 /*
141 * each-message initialization
142 */
143 nxtbuf = compbuffers;
144 savecomp = used_buf;
145 tmpbuf = *nxtbuf++;
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 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
155 if (ferror(inb)) {
156 advise("read", "unable to"); /* "read error" */
157 return SCNFAT;
158 } else {
159 return SCNEOF;
160 }
161 }
162
163 if (outnum) {
164 if (outnum > 0) {
165 scnmsg = m_name (outnum);
166 if (*scnmsg == '?') /* msg num out of range */
167 return SCNNUM;
168 } else {
169 scnmsg = "/dev/null";
170 }
171 if ((scnout = fopen (scnmsg, "w")) == NULL)
172 adios (scnmsg, "unable to write");
173 #ifdef RPATHS
174 /*
175 * Add the Return-Path and Delivery-Date
176 * header fields to message.
177 */
178 if (get_returnpath (returnpath, sizeof(returnpath),
179 deliverydate, sizeof(deliverydate))) {
180 FPUTS ("Return-Path: ");
181 FPUTS (returnpath);
182 FPUTS ("Delivery-Date: ");
183 FPUTS (deliverydate);
184 }
185 #endif /* RPATHS */
186 }
187
188 /* scan - main loop */
189 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
190 switch (state) {
191 case FLD:
192 case FLDPLUS:
193 compnum++;
194 if (outnum) {
195 FPUTS (name);
196 if ( putc (':', scnout) == EOF) DIEWRERR();
197 FPUTS (tmpbuf);
198 }
199 /*
200 * if we're interested in this component, save a pointer
201 * to the component text, then start using our next free
202 * buffer as the component temp buffer (buffer switching
203 * saves an extra copy of the component text).
204 */
205 if ((cptr = wantcomp[CHASH(name)])) {
206 do {
207 if (!mh_strcasecmp(name, cptr->c_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 break;
220 }
221 } while ((cptr = cptr->c_next));
222 }
223
224 while (state == FLDPLUS) {
225 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
226 if (outnum)
227 FPUTS (tmpbuf);
228 }
229 break;
230
231 case BODY:
232 compnum = -1;
233 if (! outnum) {
234 state = FILEEOF; /* stop now if scan cmd */
235 goto finished;
236 }
237 if (putc ('\n', scnout) == EOF) DIEWRERR();
238 FPUTS (tmpbuf);
239 /*
240 * performance hack: some people like to run "inc" on
241 * things like net.sources or large digests. We do a
242 * copy directly into the output buffer rather than
243 * going through an intermediate buffer.
244 *
245 * We need the amount of data m_getfld found & don't
246 * want to do a strlen on the long buffer so there's
247 * a hack in m_getfld to save the amount of data it
248 * returned in the global "msg_count".
249 */
250 body:;
251 while (state == BODY) {
252 #ifdef LINUX_STDIO
253 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
254 #else
255 if (scnout->_cnt <= 0) {
256 #endif
257 if (fflush(scnout) == EOF)
258 DIEWRERR ();
259 }
260 #ifdef LINUX_STDIO
261 state = m_getfld(state, name, scnout->_IO_write_ptr,
262 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
263 scnout->_IO_write_ptr += msg_count;
264 #else
265 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
266 scnout->_cnt -= msg_count;
267 scnout->_ptr += msg_count;
268 #endif
269 }
270 goto finished;
271
272 case LENERR:
273 case FMTERR:
274 fprintf (stderr,
275 innum ? "??Format error (message %d) in "
276 : "??Format error in ",
277 outnum ? outnum : innum);
278 fprintf (stderr, "component %d\n", compnum);
279
280 if (outnum) {
281 FPUTS ("\n\nBAD MSG:\n");
282 FPUTS (name);
283 if (putc ('\n', scnout) == EOF) DIEWRERR();
284 state = BODY;
285 goto body;
286 }
287 /* fall through */
288
289 case FILEEOF:
290 goto finished;
291
292 default:
293 adios (NULL, "getfld() returned %d", state);
294 }
295 }
296
297 /*
298 * format and output the scan line.
299 */
300 finished:
301 if (ferror(inb)) {
302 advise("read", "unable to"); /* "read error" */
303 return SCNFAT;
304 }
305
306 /* Save and restore buffer so we don't trash our dynamic pool! */
307 if (bodycomp) {
308 saved_c_text = bodycomp->c_text;
309 bodycomp->c_text = tmpbuf;
310 }
311
312 if (size)
313 dat[2] = size;
314 else if (outnum > 0)
315 {
316 dat[2] = ftell(scnout);
317 if (dat[2] == EOF) DIEWRERR();
318 }
319
320 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
321 struct stat st;
322
323 fstat (fileno(inb), &st);
324 if (!size && !outnum)
325 dat[2] = st.st_size;
326 if (datecomp) {
327 if (! datecomp->c_text) {
328 if (datecomp->c_tws == NULL)
329 datecomp->c_tws = (struct tws *)
330 calloc((size_t) 1, sizeof(*datecomp->c_tws));
331 if (datecomp->c_tws == NULL)
332 adios (NULL, "unable to allocate tws buffer");
333 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
334 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
335 } else {
336 datecomp->c_flags &= ~CF_DATEFAB;
337 }
338 }
339 }
340
341 fmt_scan (fmt, scanl, slwidth, dat);
342
343 #if 0
344 fmt = fmt_scan (fmt, scanl, slwidth, dat);
345 if (!fmt)
346 fmt = fmt_top; /* reset for old format files */
347 #endif
348
349 if (bodycomp)
350 bodycomp->c_text = saved_c_text;
351
352 if (noisy)
353 fputs (scanl, stdout);
354
355 FINDCOMP (cptr, "encrypted");
356 encrypted = cptr && cptr->c_text;
357
358 /* return dynamically allocated buffers to pool */
359 while ((cptr = *savecomp++)) {
360 *--nxtbuf = cptr->c_text;
361 cptr->c_text = NULL;
362 }
363 *--nxtbuf = tmpbuf;
364
365 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
366 DIEWRERR();
367
368 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
369 }
370
371
372 /*
373 * Cheat: we are loaded with adrparse, which wants a routine called
374 * OfficialName(). We call adrparse:getm() with the correct arguments
375 * to prevent OfficialName() from being called. Hence, the following
376 * is to keep the loader happy.
377 */
378 char *
379 OfficialName (char *name)
380 {
381 return name;
382 }
383
384
385 static int
386 mh_fputs(char *s, FILE *stream)
387 {
388 char c;
389
390 while ((c = *s++))
391 if (putc (c,stream) == EOF )
392 return(EOF);
393 return (0);
394 }
395