2 ** scansbr.c -- routines to help scan along...
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
17 # define _ptr _p /* Gag */
18 # define _cnt _w /* Wretch */
25 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
28 #define MAXSCANL 256 /* longest possible scan line */
31 ** Buffer size for content part of header fields. We want this
32 ** to be large enough so that we don't do a lot of extra FLDPLUS
33 ** calls on m_getfld but small enough so that we don't snarf
34 ** the entire message body when we're only going to display 30
39 static struct format *fmt;
41 static struct comp *datecomp; /* pntr to "date" comp */
42 static struct comp *bodycomp; /* pntr to "body" pseudo-comp (if referenced) */
43 static int ncomps = 0; /* # of interesting components */
44 static char **compbuffers = 0; /* buffers for component text */
45 static struct comp **used_buf = 0; /* stack for comp that use buffers */
47 static int dat[5]; /* aux. data for format routine */
49 char *scanl = 0; /* text of most recent scanline */
51 #define DIEWRERR() adios(scnmsg, "write error on")
54 if (mh_fputs(buf,scnout) == EOF)\
61 int sc_width(void); /* from termsbr.c */
62 static int mh_fputs(char *, FILE *);
64 #ifdef MULTIBYTE_SUPPORT
65 #define SCAN_CHARWIDTH MB_CUR_MAX
67 #define SCAN_CHARWIDTH 1
71 scan(FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
72 int unseen, char *folder, long size, int noisy)
74 int i, compnum, state;
75 unsigned char *cp, *tmpbuf;
77 char *saved_c_text = NULL;
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 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
104 /* Compile format string */
105 ncomps = fmt_compile(nfs, &fmt) + 1;
107 FINDCOMP(bodycomp, "body");
108 FINDCOMP(datecomp, "date");
109 FINDCOMP(cptr, "folder");
111 cptr->c_text = folder;
112 FINDCOMP(cptr, "dtimenow");
114 cptr->c_text = getcpy(dtimenow(0));
115 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
118 adios(NULL, "unable to allocate component buffers");
119 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
120 sizeof(struct comp *));
121 if (used_buf == NULL)
122 adios(NULL, "unable to allocate component buffer stack");
123 used_buf += ncomps+1; *--used_buf = 0;
124 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
125 for (i = ncomps; i--; )
126 *nxtbuf++ = mh_xmalloc(rlwidth);
130 ** each-message initialization
132 nxtbuf = compbuffers;
135 dat[0] = innum ? innum : outnum;
140 ** Get the first field. If the message is non-empty
141 ** and we're doing an "inc", open the output file.
143 if ((state = m_getfld(FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
145 advise("read", "unable to"); /* "read error" */
154 scnmsg = m_name(outnum);
155 if (*scnmsg == '?') /* msg num out of range */
158 scnmsg = "/dev/null";
160 if ((scnout = fopen(scnmsg, "w")) == NULL)
161 adios(scnmsg, "unable to write");
164 ** Add the Return-Path and Delivery-Date
165 ** header fields to message.
167 if (get_returnpath(returnpath, sizeof(returnpath),
168 deliverydate, sizeof(deliverydate))) {
169 FPUTS("Return-Path: ");
171 FPUTS("Delivery-Date: ");
177 /* scan - main loop */
179 state = m_getfld(state, name, tmpbuf, rlwidth, inb)) {
186 if (putc(':', scnout) == EOF)
191 ** if we're interested in this component, save
192 ** a pointer to the component text, then start
193 ** using our next free buffer as the component
194 ** temp buffer (buffer switching saves an extra
195 ** copy of the component text).
197 if ((cptr = wantcomp[CHASH(name)])) {
199 if (!mh_strcasecmp(name, cptr->c_name)) {
200 if (! cptr->c_text) {
201 cptr->c_text = tmpbuf;
202 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
212 } while ((cptr = cptr->c_next));
215 while (state == FLDPLUS) {
216 state = m_getfld(state, name, tmpbuf, rlwidth,
226 state = FILEEOF; /* stop now if scan cmd */
229 if (putc('\n', scnout) == EOF) DIEWRERR();
232 ** performance hack: some people like to run "inc"
233 ** on things like net.sources or large digests.
234 ** We do a copy directly into the output buffer
235 ** rather than going through an intermediate buffer.
237 ** We need the amount of data m_getfld found &
238 ** don't want to do a strlen on the long buffer so
239 ** there's a hack in m_getfld to save the amount
240 ** of data it returned in the global "msg_count".
243 while (state == BODY) {
245 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
246 #elif defined(__DragonFly__)
247 if (((struct __FILE_public *)scnout)->_w <= 0) {
249 if (scnout->_cnt <= 0) {
251 if (fflush(scnout) == EOF)
255 state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
256 scnout->_IO_write_ptr += msg_count;
257 #elif defined(__DragonFly__)
258 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
259 ((struct __FILE_public *)scnout)->_w -= msg_count;
260 ((struct __FILE_public *)scnout)->_p += msg_count;
262 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb);
263 scnout->_cnt -= msg_count;
264 scnout->_ptr += msg_count;
271 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
272 fprintf(stderr, "component %d\n", compnum);
275 FPUTS("\n\nBAD MSG:\n");
277 if (putc('\n', scnout) == EOF)
288 adios(NULL, "getfld() returned %d", state);
293 ** format and output the scan line.
297 advise("read", "unable to"); /* "read error" */
301 /* Save and restore buffer so we don't trash our dynamic pool! */
303 saved_c_text = bodycomp->c_text;
304 bodycomp->c_text = tmpbuf;
309 else if (outnum > 0) {
310 dat[2] = ftell(scnout);
315 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
318 fstat(fileno(inb), &st);
319 if (!size && !outnum)
322 if (! datecomp->c_text) {
323 if (datecomp->c_tws == NULL)
324 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
325 if (datecomp->c_tws == NULL)
326 adios(NULL, "unable to allocate tws buffer");
327 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
328 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
330 datecomp->c_flags &= ~CF_DATEFAB;
335 fmt_scan(fmt, scanl, slwidth, dat);
338 bodycomp->c_text = saved_c_text;
341 fputs(scanl, stdout);
343 /* return dynamically allocated buffers to pool */
344 while ((cptr = *savecomp++)) {
345 *--nxtbuf = cptr->c_text;
350 if (outnum && (ferror(scnout) || fclose(scnout) == EOF))
353 return (state != FILEEOF ? SCNERR : SCNMSG);
357 mh_fputs(char *s, FILE *stream)
362 if (putc(c,stream) == EOF )