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 format *fmt_top;
44 static struct comp *datecomp; /* pntr to "date" comp */
45 static struct comp *bodycomp; /* pntr to "body" pseudo-comp (if referenced) */
46 static int ncomps = 0; /* # of interesting components */
47 static char **compbuffers = 0; /* buffers for component text */
48 static struct comp **used_buf = 0; /* stack for comp that use buffers */
50 static int dat[5]; /* aux. data for format routine */
52 char *scanl = 0; /* text of most recent scanline */
54 #define DIEWRERR() adios(scnmsg, "write error on")
57 if (mh_fputs(buf,scnout) == EOF)\
64 int sc_width(void); /* from termsbr.c */
65 static int mh_fputs(char *, FILE *);
67 #ifdef MULTIBYTE_SUPPORT
68 #define SCAN_CHARWIDTH MB_CUR_MAX
70 #define SCAN_CHARWIDTH 1
74 scan(FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
75 int unseen, char *folder, long size, int noisy)
77 int i, compnum, encrypted, state;
78 unsigned char *cp, *tmpbuf;
80 char *saved_c_text = NULL;
82 struct comp **savecomp;
86 static int rlwidth, slwidth;
89 char returnpath[BUFSIZ];
90 char deliverydate[BUFSIZ];
93 /* first-time only initialization */
96 if ((width = sc_width()) < WIDTH/2)
98 else if (width > MAXSCANL)
101 dat[3] = slwidth = width;
102 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
107 /* Compile format string */
108 ncomps = fmt_compile(nfs, &fmt) + 1;
113 FINDCOMP(bodycomp, "body");
114 FINDCOMP(datecomp, "date");
115 FINDCOMP(cptr, "folder");
117 cptr->c_text = folder;
118 FINDCOMP(cptr, "encrypted");
120 if ((cptr = (struct comp *)calloc(1, sizeof(*cptr)))) {
121 cptr->c_name = "encrypted";
122 cptr->c_next = wantcomp[i = CHASH(cptr->c_name)];
126 FINDCOMP(cptr, "dtimenow");
128 cptr->c_text = getcpy(dtimenow(0));
129 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
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: ");
191 /* scan - main loop */
193 state = m_getfld(state, name, tmpbuf, rlwidth, inb)) {
200 if (putc(':', scnout) == EOF)
205 ** if we're interested in this component, save
206 ** a pointer to the component text, then start
207 ** using our next free buffer as the component
208 ** temp buffer (buffer switching saves an extra
209 ** copy of the component text).
211 if ((cptr = wantcomp[CHASH(name)])) {
213 if (!mh_strcasecmp(name, cptr->c_name)) {
214 if (! cptr->c_text) {
215 cptr->c_text = tmpbuf;
216 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
226 } while ((cptr = cptr->c_next));
229 while (state == FLDPLUS) {
230 state = m_getfld(state, name, tmpbuf, rlwidth,
240 state = FILEEOF; /* stop now if scan cmd */
243 if (putc('\n', scnout) == EOF) DIEWRERR();
246 ** performance hack: some people like to run "inc"
247 ** on things like net.sources or large digests.
248 ** We do a copy directly into the output buffer
249 ** rather than going through an intermediate buffer.
251 ** We need the amount of data m_getfld found &
252 ** don't want to do a strlen on the long buffer so
253 ** there's a hack in m_getfld to save the amount
254 ** of data it returned in the global "msg_count".
257 while (state == BODY) {
259 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
260 #elif defined(__DragonFly__)
261 if (((struct __FILE_public *)scnout)->_w <= 0) {
263 if (scnout->_cnt <= 0) {
265 if (fflush(scnout) == EOF)
269 state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
270 scnout->_IO_write_ptr += msg_count;
271 #elif defined(__DragonFly__)
272 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
273 ((struct __FILE_public *)scnout)->_w -= msg_count;
274 ((struct __FILE_public *)scnout)->_p += msg_count;
276 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb);
277 scnout->_cnt -= msg_count;
278 scnout->_ptr += msg_count;
285 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
286 fprintf(stderr, "component %d\n", compnum);
289 FPUTS("\n\nBAD MSG:\n");
291 if (putc('\n', scnout) == EOF)
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;
323 else if (outnum > 0) {
324 dat[2] = ftell(scnout);
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 *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
339 if (datecomp->c_tws == NULL)
340 adios(NULL, "unable to allocate tws buffer");
341 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
342 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
344 datecomp->c_flags &= ~CF_DATEFAB;
349 fmt_scan(fmt, scanl, slwidth, dat);
352 fmt = fmt_scan(fmt, scanl, slwidth, dat);
354 fmt = fmt_top; /* reset for old format files */
358 bodycomp->c_text = saved_c_text;
361 fputs(scanl, stdout);
363 FINDCOMP(cptr, "encrypted");
364 encrypted = cptr && cptr->c_text;
366 /* return dynamically allocated buffers to pool */
367 while ((cptr = *savecomp++)) {
368 *--nxtbuf = cptr->c_text;
373 if (outnum && (ferror(scnout) || fclose(scnout) == EOF))
376 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
380 mh_fputs(char *s, FILE *stream)
385 if (putc(c,stream) == EOF )