3 * scansbr.c -- routines to help scan along...
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/scansbr.h>
18 # define _ptr _p /* Gag */
19 # define _cnt _w /* Wretch */
26 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
29 #define MAXSCANL 256 /* longest possible scan line */
32 * Buffer size for content part of header fields. We want this
33 * to be large enough so that we don't do a lot of extra FLDPLUS
34 * calls on m_getfld but small enough so that we don't snarf
35 * the entire message body when we're only going to display 30
40 static struct format *fmt;
42 static struct format *fmt_top;
45 static struct comp *datecomp; /* pntr to "date" comp */
46 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
48 static int ncomps = 0; /* # of interesting components */
49 static char **compbuffers = 0; /* buffers for component text */
50 static struct comp **used_buf = 0; /* stack for comp that use buffers */
52 static int dat[5]; /* aux. data for format routine */
54 char *scanl = 0; /* text of most recent scanline */
56 #define DIEWRERR() adios (scnmsg, "write error on")
59 if (mh_fputs(buf,scnout) == EOF)\
66 int sc_width (void); /* from termsbr.c */
67 static int mh_fputs(char *, FILE *);
69 #ifdef MULTIBYTE_SUPPORT
70 #define SCAN_CHARWIDTH MB_CUR_MAX
72 #define SCAN_CHARWIDTH 1
76 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
77 int unseen, char *folder, long size, int noisy)
79 int i, compnum, encrypted, state;
80 unsigned char *cp, *tmpbuf;
82 char *saved_c_text = NULL;
84 struct comp **savecomp;
88 static int rlwidth, slwidth;
91 char returnpath[BUFSIZ];
92 char deliverydate[BUFSIZ];
95 /* first-time only initialization */
98 if ((width = sc_width ()) < WIDTH/2)
100 else if (width > MAXSCANL)
103 dat[3] = slwidth = width;
104 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
108 /* Compile format string */
109 ncomps = fmt_compile (nfs, &fmt) + 1;
114 FINDCOMP(bodycomp, "body");
115 FINDCOMP(datecomp, "date");
116 FINDCOMP(cptr, "folder");
118 cptr->c_text = folder;
119 FINDCOMP(cptr, "encrypted");
121 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
122 cptr->c_name = "encrypted";
123 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
127 FINDCOMP (cptr, "dtimenow");
129 cptr->c_text = getcpy(dtimenow (0));
130 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
132 adios (NULL, "unable to allocate component buffers");
133 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
134 sizeof(struct comp *));
135 if (used_buf == NULL)
136 adios (NULL, "unable to allocate component buffer stack");
137 used_buf += ncomps+1; *--used_buf = 0;
138 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
139 for (i = ncomps; i--; )
140 *nxtbuf++ = mh_xmalloc(rlwidth);
144 * each-message initialization
146 nxtbuf = compbuffers;
149 dat[0] = innum ? innum : outnum;
154 * Get the first field. If the message is non-empty
155 * and we're doing an "inc", open the output file.
157 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
159 advise("read", "unable to"); /* "read error" */
168 scnmsg = m_name (outnum);
169 if (*scnmsg == '?') /* msg num out of range */
172 scnmsg = "/dev/null";
174 if ((scnout = fopen (scnmsg, "w")) == NULL)
175 adios (scnmsg, "unable to write");
178 * Add the Return-Path and Delivery-Date
179 * header fields to message.
181 if (get_returnpath (returnpath, sizeof(returnpath),
182 deliverydate, sizeof(deliverydate))) {
183 FPUTS ("Return-Path: ");
185 FPUTS ("Delivery-Date: ");
186 FPUTS (deliverydate);
191 /* scan - main loop */
192 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
199 if ( putc (':', scnout) == EOF) DIEWRERR();
203 * if we're interested in this component, save a pointer
204 * to the component text, then start using our next free
205 * buffer as the component temp buffer (buffer switching
206 * saves an extra copy of the component text).
208 if ((cptr = wantcomp[CHASH(name)])) {
210 if (!mh_strcasecmp(name, cptr->c_name)) {
211 if (! cptr->c_text) {
212 cptr->c_text = tmpbuf;
213 for (cp = tmpbuf + strlen (tmpbuf) - 1;
224 } while ((cptr = cptr->c_next));
227 while (state == FLDPLUS) {
228 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
237 state = FILEEOF; /* stop now if scan cmd */
240 if (putc ('\n', scnout) == EOF) DIEWRERR();
243 * performance hack: some people like to run "inc" on
244 * things like net.sources or large digests. We do a
245 * copy directly into the output buffer rather than
246 * going through an intermediate buffer.
248 * We need the amount of data m_getfld found & don't
249 * want to do a strlen on the long buffer so there's
250 * a hack in m_getfld to save the amount of data it
251 * returned in the global "msg_count".
254 while (state == BODY) {
256 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
257 #elif defined(__DragonFly__)
258 if (((struct __FILE_public *)scnout)->_w <= 0) {
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;
269 #elif defined(__DragonFly__)
270 state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
271 ((struct __FILE_public *)scnout)->_w -= msg_count;
272 ((struct __FILE_public *)scnout)->_p += msg_count;
274 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
275 scnout->_cnt -= msg_count;
276 scnout->_ptr += msg_count;
284 innum ? "??Format error (message %d) in "
285 : "??Format error in ",
286 outnum ? outnum : innum);
287 fprintf (stderr, "component %d\n", compnum);
290 FPUTS ("\n\nBAD MSG:\n");
292 if (putc ('\n', scnout) == EOF) DIEWRERR();
302 adios (NULL, "getfld() returned %d", state);
307 * format and output the scan line.
311 advise("read", "unable to"); /* "read error" */
315 /* Save and restore buffer so we don't trash our dynamic pool! */
317 saved_c_text = bodycomp->c_text;
318 bodycomp->c_text = tmpbuf;
325 dat[2] = ftell(scnout);
326 if (dat[2] == EOF) DIEWRERR();
329 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
332 fstat (fileno(inb), &st);
333 if (!size && !outnum)
336 if (! datecomp->c_text) {
337 if (datecomp->c_tws == NULL)
338 datecomp->c_tws = (struct tws *)
339 calloc((size_t) 1, sizeof(*datecomp->c_tws));
340 if (datecomp->c_tws == NULL)
341 adios (NULL, "unable to allocate tws buffer");
342 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
343 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
345 datecomp->c_flags &= ~CF_DATEFAB;
350 fmt_scan (fmt, scanl, slwidth, dat);
353 fmt = fmt_scan (fmt, scanl, slwidth, dat);
355 fmt = fmt_top; /* reset for old format files */
359 bodycomp->c_text = saved_c_text;
362 fputs (scanl, stdout);
364 FINDCOMP (cptr, "encrypted");
365 encrypted = cptr && cptr->c_text;
367 /* return dynamically allocated buffers to pool */
368 while ((cptr = *savecomp++)) {
369 *--nxtbuf = cptr->c_text;
374 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
377 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
382 * Cheat: we are loaded with adrparse, which wants a routine called
383 * OfficialName(). We call adrparse:getm() with the correct arguments
384 * to prevent OfficialName() from being called. Hence, the following
385 * is to keep the loader happy.
388 OfficialName (char *name)
395 mh_fputs(char *s, FILE *stream)
400 if (putc (c,stream) == EOF )