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>
19 #define MAXSCANL 256 /* longest possible scan line */
21 static struct format *fmt;
23 static struct comp *datecomp; /* pntr to "date" comp */
24 static int ncomps = 0; /* # of interesting components */
26 static int dat[5]; /* aux. data for format routine */
28 char *scanl = NULL; /* text of most recent scanline */
31 if (fputs(buf, scnout) == EOF)\
32 adios(EX_IOERR, scnmsg, "write error on");\
38 int sc_width(void); /* from termsbr.c */
40 #ifdef MULTIBYTE_SUPPORT
41 #define SCAN_CHARWIDTH MB_CUR_MAX
43 #define SCAN_CHARWIDTH 1
47 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
53 struct field f = {{0}};
58 int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
59 int scanfolder = (outnum == SCN_FOLD);
64 /* first-time only initialization */
71 if ((width = sc_width()) < WIDTH/2)
73 else if (width > MAXSCANL)
76 dat[3] = slwidth = width;
77 scanl = mh_xcalloc(slwidth + 2, SCAN_CHARWIDTH); /* probably for \n and \0 */
78 /* Compile format string */
79 ncomps = fmt_compile(fmtstr, &fmt) + 1;
80 FINDCOMP(datecomp, "date");
92 ** each-message initialization
94 dat[0] = innum ? innum : outnum;
100 scnmsg = m_name(outnum);
101 if (*scnmsg == '?') /* msg num out of range */
103 if (!(scnout = fopen(scnmsg, "w")))
104 adios(EX_IOERR, scnmsg, "unable to write");
106 /* scan - main loop */
107 for (compnum = 1, state = FLD2; ; ) {
108 state = m_getfld2(state, &f, inb);
117 if (fmtstr && (cptr = wantcomp[CHASH(f.name)])) {
119 ** we're interested in this component,
120 ** but find the right one in the hash
121 ** collision chain ...
124 if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
131 cptr->c_text = mh_xstrdup(f.value);
132 cp = cptr->c_text + strlen(cptr->c_text) - 1;
133 for (; cp >= cptr->c_text; cp--) {
141 } while ((cptr = cptr->c_next));
148 /* stop here if we scan a msg in a folder */
152 /* otherwise (mbox): snarf the body */
153 if (strncmp("From ", f.value, 5)==0) {
163 while ((state = m_getfld2(state, &f, inb)) == BODY2) {
165 ** recognize From lines without blank lines
166 ** before them as well.
168 if (strncmp("From ", f.value, 5)==0) {
173 ** delay the printing of blank lines
174 ** because if it's the end of the message,
175 ** then we must omit the blank line,
176 ** as it is not part of the message but
177 ** part of the mbox format
180 /* print the delayed blank line */
184 if (strcmp(f.value, "\n")==0) {
195 advise(NULL, "line \"%s\" too long", trim(f.value));
199 if (strncmp("From ", f.value, 5)==0) {
207 fprintf(stderr, innum ?
208 "??Format error (message %d) in " :
209 "??Format error in ",
210 outnum ? outnum : innum);
211 fprintf(stderr, "component %d\n", compnum);
214 FPUTS("\n\nBAD MSG:\n");
215 FPUTS(f.name); /* XXX use f.field? */
220 /* fall through if we scan only */
226 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
231 /* Format and output the scan line. */
233 advise("read", "unable to");
238 if ((dat[2] = ftell(scnout)) == EOF)
239 adios(EX_IOERR, scnmsg, "write error on");
240 } else if (!scanfolder) {
241 if ((dat[2] = ftell(inb)) == EOF)
242 adios(EX_IOERR, scnmsg, "write error on");
247 fstat(fileno(inb), &st);
251 if (datecomp && !datecomp->c_text) {
252 if (!datecomp->c_text) {
253 if (!datecomp->c_tws)
254 datecomp->c_tws = mh_xcalloc(1, sizeof(*datecomp->c_tws));
255 if (!datecomp->c_tws)
256 adios(EX_OSERR, NULL, "unable to allocate tws buffer");
257 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
258 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
260 datecomp->c_flags &= ~CF_DATEFAB;
264 /* print the scan line */
265 fmt_scan(fmt, scanl, slwidth, dat);
266 fputs(scanl, stdout);
269 /* clean up old values */
270 for (i=0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
271 for (cptr=wantcomp[i]; cptr; cptr=cptr->c_next) {
279 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
280 adios(EX_IOERR, scnmsg, "write error on");
282 return (state == FILEEOF2 ? SCNMSG : SCNERR);