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