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>
19 # define _ptr _p /* Gag */
20 # define _cnt _w /* Wretch */
27 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
30 #define MAXSCANL 256 /* longest possible scan line */
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
41 static struct format *fmt;
43 static struct format *fmt_top;
46 static struct comp *datecomp; /* pntr to "date" comp */
47 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
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 */
53 static int dat[5]; /* aux. data for format routine */
55 char *scanl = 0; /* text of most recent scanline */
57 #define DIEWRERR() adios (scnmsg, "write error on")
60 if (mh_fputs(buf,scnout) == EOF)\
67 int sc_width (void); /* from termsbr.c */
68 static int mh_fputs(char *, FILE *);
72 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
73 int unseen, char *folder, long size, int noisy)
75 int i, compnum, encrypted, state;
76 char *cp, *tmpbuf, **nxtbuf;
79 struct comp **savecomp;
83 static int rlwidth, slwidth;
86 char returnpath[BUFSIZ];
87 char deliverydate[BUFSIZ];
90 /* first-time only initialization */
93 if ((width = sc_width ()) < WIDTH/2)
95 else if (width > MAXSCANL)
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);
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 if ((*nxtbuf++ = malloc(rlwidth)) == NULL)
137 adios (NULL, "unable to allocate component buffer");
141 * each-message initialization
143 nxtbuf = compbuffers;
146 dat[0] = innum ? innum : outnum;
151 * Get the first field. If the message is non-empty
152 * and we're doing an "inc", open the output file.
154 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
156 advise("read", "unable to"); /* "read error" */
165 scnmsg = m_name (outnum);
166 if (*scnmsg == '?') /* msg num out of range */
169 scnmsg = "/dev/null";
171 if ((scnout = fopen (scnmsg, "w")) == NULL)
172 adios (scnmsg, "unable to write");
175 * Add the Return-Path and Delivery-Date
176 * header fields to message.
178 if (get_returnpath (returnpath, sizeof(returnpath),
179 deliverydate, sizeof(deliverydate))) {
180 FPUTS ("Return-Path: ");
182 FPUTS ("Delivery-Date: ");
183 FPUTS (deliverydate);
188 /* scan - main loop */
189 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
196 if ( putc (':', scnout) == EOF) DIEWRERR();
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).
205 if ((cptr = wantcomp[CHASH(name)])) {
207 if (!strcasecmp(name, cptr->c_name)) {
208 if (! cptr->c_text) {
209 cptr->c_text = tmpbuf;
210 for (cp = tmpbuf + strlen (tmpbuf) - 1;
221 } while ((cptr = cptr->c_next));
224 while (state == FLDPLUS) {
225 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
234 state = FILEEOF; /* stop now if scan cmd */
237 if (putc ('\n', scnout) == EOF) DIEWRERR();
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.
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".
251 while (state == BODY) {
253 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
255 if (scnout->_cnt <= 0) {
257 if (fflush(scnout) == EOF)
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;
265 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
266 scnout->_cnt -= msg_count;
267 scnout->_ptr += msg_count;
275 innum ? "??Format error (message %d) in "
276 : "??Format error in ",
277 outnum ? outnum : innum);
278 fprintf (stderr, "component %d\n", compnum);
281 FPUTS ("\n\nBAD MSG:\n");
283 if (putc ('\n', scnout) == EOF) DIEWRERR();
293 adios (NULL, "getfld() returned %d", state);
298 * format and output the scan line.
302 advise("read", "unable to"); /* "read error" */
306 /* Save and restore buffer so we don't trash our dynamic pool! */
308 saved_c_text = bodycomp->c_text;
309 bodycomp->c_text = tmpbuf;
316 dat[2] = ftell(scnout);
317 if (dat[2] == EOF) DIEWRERR();
320 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
323 fstat (fileno(inb), &st);
324 if (!size && !outnum)
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 = -1;
336 datecomp->c_flags = 0;
341 fmt_scan (fmt, scanl, slwidth, dat);
344 fmt = fmt_scan (fmt, scanl, slwidth, dat);
346 fmt = fmt_top; /* reset for old format files */
350 bodycomp->c_text = saved_c_text;
353 fputs (scanl, stdout);
355 FINDCOMP (cptr, "encrypted");
356 encrypted = cptr && cptr->c_text;
358 /* return dynamically allocated buffers to pool */
359 while ((cptr = *savecomp++)) {
360 *--nxtbuf = cptr->c_text;
365 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
368 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
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.
379 OfficialName (char *name)
386 mh_fputs(char *s, FILE *stream)
391 if (putc (c,stream) == EOF )