]> diplodocus.org Git - nmh/blob - uip/scansbr.c
Fix stupid accidental dependence on a bash quirk in previous
[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
18 #ifdef _FSTDIO
19 # define _ptr _p /* Gag */
20 # define _cnt _w /* Wretch */
21 #endif
22
23 #ifdef SCO_5_STDIO
24 # define _ptr __ptr
25 # define _cnt __cnt
26 # define _base __base
27 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
28 #endif
29
30 #define MAXSCANL 256 /* longest possible scan line */
31
32 /*
33 * Buffer size for content part of header fields. We want this
34 * to be large enough so that we don't do a lot of extra FLDPLUS
35 * calls on m_getfld but small enough so that we don't snarf
36 * the entire message body when we're only going to display 30
37 * characters of it.
38 */
39 #define SBUFSIZ 512
40
41 static struct format *fmt;
42 #ifdef JLR
43 static struct format *fmt_top;
44 #endif /* JLR */
45
46 static struct comp *datecomp; /* pntr to "date" comp */
47 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
48 * (if referenced) */
49 static int ncomps = 0; /* # of interesting components */
50 static char **compbuffers = 0; /* buffers for component text */
51 static struct comp **used_buf = 0; /* stack for comp that use buffers */
52
53 static int dat[5]; /* aux. data for format routine */
54
55 char *scanl = 0; /* text of most recent scanline */
56
57 #define DIEWRERR() adios (scnmsg, "write error on")
58
59 #define FPUTS(buf) {\
60 if (mh_fputs(buf,scnout) == EOF)\
61 DIEWRERR();\
62 }
63
64 /*
65 * prototypes
66 */
67 int sc_width (void); /* from termsbr.c */
68 static int mh_fputs(char *, FILE *);
69
70
71 int
72 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
73 int unseen, char *folder, long size, int noisy)
74 {
75 int i, compnum, encrypted, state;
76 char *cp, *tmpbuf, **nxtbuf;
77 char *saved_c_text;
78 struct comp *cptr;
79 struct comp **savecomp;
80 char *scnmsg;
81 FILE *scnout;
82 char name[NAMESZ];
83 static int rlwidth, slwidth;
84
85 #ifdef RPATHS
86 char returnpath[BUFSIZ];
87 char deliverydate[BUFSIZ];
88 #endif
89
90 /* first-time only initialization */
91 if (!scanl) {
92 if (width == 0) {
93 if ((width = sc_width ()) < WIDTH/2)
94 width = WIDTH/2;
95 else if (width > MAXSCANL)
96 width = MAXSCANL;
97 }
98 dat[3] = slwidth = width;
99 if ((scanl = (char *) malloc((size_t) (slwidth + 2) )) == NULL)
100 adios (NULL, "unable to malloc scan line (%d bytes)", slwidth+2);
101 if (outnum)
102 umask(~m_gmprot());
103
104 /* Compile format string */
105 ncomps = fmt_compile (nfs, &fmt) + 1;
106
107 #ifdef JLR
108 fmt_top = fmt;
109 #endif /* JLR */
110 FINDCOMP(bodycomp, "body");
111 FINDCOMP(datecomp, "date");
112 FINDCOMP(cptr, "folder");
113 if (cptr && folder)
114 cptr->c_text = folder;
115 FINDCOMP(cptr, "encrypted");
116 if (!cptr)
117 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
118 cptr->c_name = "encrypted";
119 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
120 wantcomp[i] = cptr;
121 ncomps++;
122 }
123 FINDCOMP (cptr, "dtimenow");
124 if (cptr)
125 cptr->c_text = getcpy(dtimenow (0));
126 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
127 if (nxtbuf == NULL)
128 adios (NULL, "unable to allocate component buffers");
129 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
130 sizeof(struct comp *));
131 if (used_buf == NULL)
132 adios (NULL, "unable to allocate component buffer stack");
133 used_buf += ncomps+1; *--used_buf = 0;
134 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
135 for (i = ncomps; i--; )
136 if ((*nxtbuf++ = malloc(rlwidth)) == NULL)
137 adios (NULL, "unable to allocate component buffer");
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 (!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