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 = free_field;
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 = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
78 (slwidth + 2)); /* probably for \n and \0 */
79 /* Compile format string */
80 ncomps = fmt_compile(fmtstr, &fmt) + 1;
81 FINDCOMP(datecomp, "date");
93 ** each-message initialization
95 dat[0] = innum ? innum : outnum;
101 scnmsg = m_name(outnum);
102 if (*scnmsg == '?') /* msg num out of range */
104 if (!(scnout = fopen(scnmsg, "w")))
105 adios(EX_IOERR, scnmsg, "unable to write");
107 /* scan - main loop */
108 for (compnum = 1, state = FLD2; ; ) {
109 state = m_getfld2(state, &f, inb);
118 if (fmtstr && (cptr = wantcomp[CHASH(f.name)])) {
120 ** we're interested in this component,
121 ** but find the right one in the hash
122 ** collision chain ...
125 if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
132 cptr->c_text = getcpy(f.value);
133 cp = cptr->c_text + strlen(cptr->c_text) - 1;
134 for (; cp >= cptr->c_text; cp--) {
142 } while ((cptr = cptr->c_next));
149 /* stop here if we scan a msg in a folder */
153 /* otherwise (mbox): snarf the body */
154 if (strncmp("From ", f.value, 5)==0) {
164 while ((state = m_getfld2(state, &f, inb)) == BODY2) {
166 ** recognize From lines without blank lines
167 ** before them as well.
169 if (strncmp("From ", f.value, 5)==0) {
174 ** delay the printing of blank lines
175 ** because if it's the end of the message,
176 ** then we must omit the blank line,
177 ** as it is not part of the message but
178 ** part of the mbox format
181 /* print the delayed blank line */
185 if (strcmp(f.value, "\n")==0) {
196 advise(NULL, "line \"%s\" too long", trim(f.value));
200 if (strncmp("From ", f.value, 5)==0) {
208 fprintf(stderr, innum ?
209 "??Format error (message %d) in " :
210 "??Format error in ",
211 outnum ? outnum : innum);
212 fprintf(stderr, "component %d\n", compnum);
215 FPUTS("\n\nBAD MSG:\n");
216 FPUTS(f.name); /* XXX use f.field? */
221 /* fall through if we scan only */
227 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
232 /* Format and output the scan line. */
234 advise("read", "unable to");
239 if ((dat[2] = ftell(scnout)) == EOF)
240 adios(EX_IOERR, scnmsg, "write error on");
241 } else if (!scanfolder) {
242 if ((dat[2] = ftell(inb)) == EOF)
243 adios(EX_IOERR, scnmsg, "write error on");
248 fstat(fileno(inb), &st);
252 if (datecomp && !datecomp->c_text) {
253 if (!datecomp->c_text) {
254 if (!datecomp->c_tws)
255 datecomp->c_tws = (struct tws *) mh_xcalloc((size_t) 1, sizeof(*datecomp->c_tws));
256 if (!datecomp->c_tws)
257 adios(EX_OSERR, NULL, "unable to allocate tws buffer");
258 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
259 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
261 datecomp->c_flags &= ~CF_DATEFAB;
265 /* print the scan line */
266 fmt_scan(fmt, scanl, slwidth, dat);
267 fputs(scanl, stdout);
270 /* clean up old values */
271 for (i=0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
272 for (cptr=wantcomp[i]; cptr; cptr=cptr->c_next) {
280 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
281 adios(EX_IOERR, scnmsg, "write error on");
283 return (state == FILEEOF2 ? SCNMSG : SCNERR);