3 * scansbr.c -- routines to help scan along...
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.
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
15 #include <h/scansbr.h>
20 # define _ptr _p /* Gag */
21 # define _cnt _w /* Wretch */
28 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
31 #define MAXSCANL 256 /* longest possible scan line */
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
42 static struct format *fmt;
44 static struct format *fmt_top;
47 static struct comp *datecomp; /* pntr to "date" comp */
48 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
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 */
54 static int dat[5]; /* aux. data for format routine */
56 char *scanl = 0; /* text of most recent scanline */
58 #define DIEWRERR() adios (scnmsg, "write error on")
61 if (mh_fputs(buf,scnout) == EOF)\
68 int sc_width (void); /* from termsbr.c */
69 static int mh_fputs(char *, FILE *);
73 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
74 int unseen, char *folder, long size, int noisy)
76 int i, compnum, encrypted, state;
77 char *cp, *tmpbuf, **nxtbuf;
80 struct comp **savecomp;
84 static int rlwidth, slwidth;
87 char returnpath[BUFSIZ];
88 char deliverydate[BUFSIZ];
91 /* first-time only initialization */
94 if ((width = sc_width ()) < WIDTH/2)
96 else if (width > MAXSCANL)
99 dat[3] = slwidth = width;
100 scanl = (char *) mh_xmalloc((size_t) (slwidth + 2) );
104 /* Compile format string */
105 ncomps = fmt_compile (nfs, &fmt) + 1;
110 FINDCOMP(bodycomp, "body");
111 FINDCOMP(datecomp, "date");
112 FINDCOMP(cptr, "folder");
114 cptr->c_text = folder;
115 FINDCOMP(cptr, "encrypted");
117 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
118 cptr->c_name = "encrypted";
119 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
123 FINDCOMP (cptr, "dtimenow");
125 cptr->c_text = getcpy(dtimenow (0));
126 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
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);
140 * each-message initialization
142 nxtbuf = compbuffers;
145 dat[0] = innum ? innum : outnum;
150 * Get the first field. If the message is non-empty
151 * and we're doing an "inc", open the output file.
153 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
155 advise("read", "unable to"); /* "read error" */
164 scnmsg = m_name (outnum);
165 if (*scnmsg == '?') /* msg num out of range */
168 scnmsg = "/dev/null";
170 if ((scnout = fopen (scnmsg, "w")) == NULL)
171 adios (scnmsg, "unable to write");
174 * Add the Return-Path and Delivery-Date
175 * header fields to message.
177 if (get_returnpath (returnpath, sizeof(returnpath),
178 deliverydate, sizeof(deliverydate))) {
179 FPUTS ("Return-Path: ");
181 FPUTS ("Delivery-Date: ");
182 FPUTS (deliverydate);
187 /* scan - main loop */
188 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
195 if ( putc (':', scnout) == EOF) DIEWRERR();
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).
204 if ((cptr = wantcomp[CHASH(name)])) {
206 if (!strcasecmp(name, cptr->c_name)) {
207 if (! cptr->c_text) {
208 cptr->c_text = tmpbuf;
209 for (cp = tmpbuf + strlen (tmpbuf) - 1;
220 } while ((cptr = cptr->c_next));
223 while (state == FLDPLUS) {
224 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
233 state = FILEEOF; /* stop now if scan cmd */
236 if (putc ('\n', scnout) == EOF) DIEWRERR();
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.
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".
250 while (state == BODY) {
252 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
254 if (scnout->_cnt <= 0) {
256 if (fflush(scnout) == EOF)
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;
264 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
265 scnout->_cnt -= msg_count;
266 scnout->_ptr += msg_count;
274 innum ? "??Format error (message %d) in "
275 : "??Format error in ",
276 outnum ? outnum : innum);
277 fprintf (stderr, "component %d\n", compnum);
280 FPUTS ("\n\nBAD MSG:\n");
282 if (putc ('\n', scnout) == EOF) DIEWRERR();
292 adios (NULL, "getfld() returned %d", state);
297 * format and output the scan line.
301 advise("read", "unable to"); /* "read error" */
305 /* Save and restore buffer so we don't trash our dynamic pool! */
307 saved_c_text = bodycomp->c_text;
308 bodycomp->c_text = tmpbuf;
315 dat[2] = ftell(scnout);
316 if (dat[2] == EOF) DIEWRERR();
319 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
322 fstat (fileno(inb), &st);
323 if (!size && !outnum)
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;
335 datecomp->c_flags &= ~CF_DATEFAB;
340 fmt_scan (fmt, scanl, slwidth, dat);
343 fmt = fmt_scan (fmt, scanl, slwidth, dat);
345 fmt = fmt_top; /* reset for old format files */
349 bodycomp->c_text = saved_c_text;
352 fputs (scanl, stdout);
354 FINDCOMP (cptr, "encrypted");
355 encrypted = cptr && cptr->c_text;
357 /* return dynamically allocated buffers to pool */
358 while ((cptr = *savecomp++)) {
359 *--nxtbuf = cptr->c_text;
364 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
367 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
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.
378 OfficialName (char *name)
385 mh_fputs(char *s, FILE *stream)
390 if (putc (c,stream) == EOF )