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 *);
71 #ifdef MULTIBYTE_SUPPORT
72 #define SCAN_CHARWIDTH MB_CUR_MAX
74 #define SCAN_CHARWIDTH 1
78 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
79 int unseen, char *folder, long size, int noisy)
81 int i, compnum, encrypted, state;
82 unsigned char *cp, *tmpbuf;
86 struct comp **savecomp;
90 static int rlwidth, slwidth;
93 char returnpath[BUFSIZ];
94 char deliverydate[BUFSIZ];
97 /* first-time only initialization */
100 if ((width = sc_width ()) < WIDTH/2)
102 else if (width > MAXSCANL)
105 dat[3] = slwidth = width;
106 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
110 /* Compile format string */
111 ncomps = fmt_compile (nfs, &fmt) + 1;
116 FINDCOMP(bodycomp, "body");
117 FINDCOMP(datecomp, "date");
118 FINDCOMP(cptr, "folder");
120 cptr->c_text = folder;
121 FINDCOMP(cptr, "encrypted");
123 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
124 cptr->c_name = "encrypted";
125 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
129 FINDCOMP (cptr, "dtimenow");
131 cptr->c_text = getcpy(dtimenow (0));
132 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
134 adios (NULL, "unable to allocate component buffers");
135 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
136 sizeof(struct comp *));
137 if (used_buf == NULL)
138 adios (NULL, "unable to allocate component buffer stack");
139 used_buf += ncomps+1; *--used_buf = 0;
140 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
141 for (i = ncomps; i--; )
142 *nxtbuf++ = mh_xmalloc(rlwidth);
146 * each-message initialization
148 nxtbuf = compbuffers;
151 dat[0] = innum ? innum : outnum;
156 * Get the first field. If the message is non-empty
157 * and we're doing an "inc", open the output file.
159 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
161 advise("read", "unable to"); /* "read error" */
170 scnmsg = m_name (outnum);
171 if (*scnmsg == '?') /* msg num out of range */
174 scnmsg = "/dev/null";
176 if ((scnout = fopen (scnmsg, "w")) == NULL)
177 adios (scnmsg, "unable to write");
180 * Add the Return-Path and Delivery-Date
181 * header fields to message.
183 if (get_returnpath (returnpath, sizeof(returnpath),
184 deliverydate, sizeof(deliverydate))) {
185 FPUTS ("Return-Path: ");
187 FPUTS ("Delivery-Date: ");
188 FPUTS (deliverydate);
193 /* scan - main loop */
194 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
201 if ( putc (':', scnout) == EOF) DIEWRERR();
205 * if we're interested in this component, save a pointer
206 * to the component text, then start using our next free
207 * buffer as the component temp buffer (buffer switching
208 * saves an extra copy of the component text).
210 if ((cptr = wantcomp[CHASH(name)])) {
212 if (!mh_strcasecmp(name, cptr->c_name)) {
213 if (! cptr->c_text) {
214 cptr->c_text = tmpbuf;
215 for (cp = tmpbuf + strlen (tmpbuf) - 1;
226 } while ((cptr = cptr->c_next));
229 while (state == FLDPLUS) {
230 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
239 state = FILEEOF; /* stop now if scan cmd */
242 if (putc ('\n', scnout) == EOF) DIEWRERR();
245 * performance hack: some people like to run "inc" on
246 * things like net.sources or large digests. We do a
247 * copy directly into the output buffer rather than
248 * going through an intermediate buffer.
250 * We need the amount of data m_getfld found & don't
251 * want to do a strlen on the long buffer so there's
252 * a hack in m_getfld to save the amount of data it
253 * returned in the global "msg_count".
256 while (state == BODY) {
258 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
260 if (scnout->_cnt <= 0) {
262 if (fflush(scnout) == EOF)
266 state = m_getfld(state, name, scnout->_IO_write_ptr,
267 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
268 scnout->_IO_write_ptr += msg_count;
270 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
271 scnout->_cnt -= msg_count;
272 scnout->_ptr += msg_count;
280 innum ? "??Format error (message %d) in "
281 : "??Format error in ",
282 outnum ? outnum : innum);
283 fprintf (stderr, "component %d\n", compnum);
286 FPUTS ("\n\nBAD MSG:\n");
288 if (putc ('\n', scnout) == EOF) DIEWRERR();
298 adios (NULL, "getfld() returned %d", state);
303 * format and output the scan line.
307 advise("read", "unable to"); /* "read error" */
311 /* Save and restore buffer so we don't trash our dynamic pool! */
313 saved_c_text = bodycomp->c_text;
314 bodycomp->c_text = tmpbuf;
321 dat[2] = ftell(scnout);
322 if (dat[2] == EOF) DIEWRERR();
325 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
328 fstat (fileno(inb), &st);
329 if (!size && !outnum)
332 if (! datecomp->c_text) {
333 if (datecomp->c_tws == NULL)
334 datecomp->c_tws = (struct tws *)
335 calloc((size_t) 1, sizeof(*datecomp->c_tws));
336 if (datecomp->c_tws == NULL)
337 adios (NULL, "unable to allocate tws buffer");
338 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
339 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
341 datecomp->c_flags &= ~CF_DATEFAB;
346 fmt_scan (fmt, scanl, slwidth, dat);
349 fmt = fmt_scan (fmt, scanl, slwidth, dat);
351 fmt = fmt_top; /* reset for old format files */
355 bodycomp->c_text = saved_c_text;
358 fputs (scanl, stdout);
360 FINDCOMP (cptr, "encrypted");
361 encrypted = cptr && cptr->c_text;
363 /* return dynamically allocated buffers to pool */
364 while ((cptr = *savecomp++)) {
365 *--nxtbuf = cptr->c_text;
370 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
373 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
378 * Cheat: we are loaded with adrparse, which wants a routine called
379 * OfficialName(). We call adrparse:getm() with the correct arguments
380 * to prevent OfficialName() from being called. Hence, the following
381 * is to keep the loader happy.
384 OfficialName (char *name)
391 mh_fputs(char *s, FILE *stream)
396 if (putc (c,stream) == EOF )