Remove OS-specific defines. Remove GECOS ampersand-magic, along with it.
[mmh] / uip / scansbr.c
1 /*
2 ** scansbr.c -- routines to help scan along...
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
13 #include <h/tws.h>
14 #include <h/utils.h>
15
16 #ifdef _FSTDIO
17 # define _ptr _p  /* Gag */
18 # define _cnt _w  /* Wretch */
19 #endif
20
21 #define MAXSCANL 256  /* longest possible scan line */
22
23 /*
24 ** Buffer size for content part of header fields.  We want this
25 ** to be large enough so that we don't do a lot of extra FLDPLUS
26 ** calls on m_getfld.
27 */
28 #define SBUFSIZ 512
29
30 static struct format *fmt;
31
32 static struct comp *datecomp;  /* pntr to "date" comp */
33 static int ncomps = 0;  /* # of interesting components */
34 static char **compbuffers = NULL;  /* buffers for component text */
35 static struct comp **used_buf = NULL;  /* stack for comp that use buffers */
36
37 static int dat[5];  /* aux. data for format routine */
38
39 char *scanl = NULL;  /* text of most recent scanline */
40
41 #define FPUTS(buf) {\
42                 if (fputs(buf, scnout) == EOF)\
43                         adios(scnmsg, "write error on");\
44         }
45
46 /*
47 ** prototypes
48 */
49 int sc_width(void);  /* from termsbr.c */
50
51 #ifdef MULTIBYTE_SUPPORT
52 #define SCAN_CHARWIDTH MB_CUR_MAX
53 #else
54 #define SCAN_CHARWIDTH 1
55 #endif
56
57 int
58 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
59         int unseen)
60 {
61         static int slwidth;
62         int i, compnum, state;
63         unsigned char *cp, *tmpbuf;
64         char **nxtbuf;
65         struct comp *cptr;
66         struct comp **savecomp;
67         char *scnmsg = NULL;
68         FILE *scnout = NULL;
69         char name[NAMESZ];
70         int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
71         int scanfolder = (outnum == SCN_FOLD);
72         long fpos;
73         struct stat st;
74
75         /* first-time only initialization */
76         if (!scanl) {
77                 if (incing)
78                         umask(~m_gmprot());
79
80                 if (fmtstr) {
81                         if (width == 0) {
82                                 if ((width = sc_width()) < WIDTH/2)
83                                         width = WIDTH/2;
84                                 else if (width > MAXSCANL)
85                                         width = MAXSCANL;
86                         }
87                         dat[3] = slwidth = width;
88                         scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
89                                         (slwidth + 2));  /* probably for \n and \0 */
90                         /* Compile format string */
91                         ncomps = fmt_compile(fmtstr, &fmt) + 1;
92                         FINDCOMP(datecomp, "date");
93                 } else {
94                         ncomps = 1;
95                         datecomp = NULL;
96                 }
97
98                 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
99                                 sizeof(char *));
100                 if (!nxtbuf)
101                         adios(NULL, "unable to allocate component buffers");
102                 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
103                                 sizeof(struct comp *));
104                 if (!used_buf)
105                         adios(NULL, "unable to allocate component buffer stack");
106                 /* NULL-terminate array */
107                 used_buf += ncomps;
108                 *used_buf = NULL;
109                 /* allocate space for the items */
110                 for (i = ncomps; i--; )
111                         *nxtbuf++ = mh_xmalloc(SBUFSIZ);
112         }
113
114         /*
115         ** each-message initialization
116         */
117         nxtbuf = compbuffers;
118         savecomp = used_buf;
119         tmpbuf = *nxtbuf++;
120         dat[0] = innum ? innum : outnum;
121         dat[1] = curflg;
122         dat[4] = unseen;
123         fpos = ftell(inb);
124
125         /*
126         ** Get the first field.  If the message is non-empty
127         ** and we're doing an "inc", open the output file.
128         */
129         if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
130                 if (ferror(inb)) {
131                         advise("read", "unable to"); /* "read error" */
132                         return SCNFAT;
133                 } else {
134                         return SCNEOF;
135                 }
136         }
137
138         if (incing) {
139                 scnmsg = m_name(outnum);
140                 if (*scnmsg == '?')  /* msg num out of range */
141                         return SCNNUM;
142                 if (!(scnout = fopen(scnmsg, "w")))
143                         adios(scnmsg, "unable to write");
144         }
145
146         /* scan - main loop */
147         for (compnum = 1; ;
148                         state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
149                 switch (state) {
150                 case FLD:
151                 case FLDPLUS:
152                         compnum++;
153                         if (incing) {
154                                 FPUTS(name);
155                                 FPUTS(":");
156                                 FPUTS(tmpbuf);
157                         }
158                         /*
159                         ** if we're interested in this component, save
160                         ** a pointer to the component text, then start
161                         ** using our next free buffer as the component
162                         ** temp buffer (buffer switching saves an extra
163                         ** copy of the component text).
164                         */
165                         if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
166                                 do {
167                                         if (mh_strcasecmp(name, cptr->c_name)!=0) {
168                                                 continue;
169                                         }
170                                         if (!cptr->c_text) {
171                                                 cptr->c_text = tmpbuf;
172                                                 cp = tmpbuf+strlen(tmpbuf)-1;
173                                                 for (; cp >= tmpbuf; cp--) {
174                                                         if (isspace(*cp))
175                                                                 *cp = '\0';
176                                                         else
177                                                                 break;
178                                                 }
179                                                 *--savecomp = cptr;
180                                                 tmpbuf = *nxtbuf++;
181                                         }
182                                         break;
183                                 } while ((cptr = cptr->c_next));
184                         }
185
186                         while (state == FLDPLUS) {
187                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
188                                                 inb);
189                                 if (incing)
190                                         FPUTS(tmpbuf);
191                         }
192                         break;
193
194                 case BODY:
195                         compnum = -1;
196                         if (scanfolder) {
197                                 /* stop here if we scan a msg in a folder */
198                                 state = FILEEOF;
199                                 goto finished;
200                         }
201                         /* otherwise (mbox): snarf the body */
202                         if (incing) {
203                                 FPUTS("\n");
204                                 FPUTS(tmpbuf);
205                         }
206 body:;
207                         while (state == BODY) {
208                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
209                                                 inb);
210                                 if (incing) {
211                                         FPUTS(tmpbuf);
212                                 }
213                         }
214                         goto finished;
215
216                 case LENERR:
217                 case FMTERR:
218                         fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
219                         fprintf(stderr, "component %d\n", compnum);
220
221                         if (incing) {
222                                 FPUTS("\n\nBAD MSG:\n");
223                                 FPUTS(name);
224                                 FPUTS("\n");
225                                 state = BODY;
226                                 goto body;
227                         }
228                         /* fall through if we scan only */
229
230                 case FILEEOF:
231                         goto finished;
232
233                 default:
234                         adios(NULL, "getfld() returned %d", state);
235                 }
236         }
237
238 finished:
239
240         /* Format and output the scan line. */
241         if (ferror(inb)) {
242                 advise("read", "unable to");
243                 return SCNFAT;
244         }
245
246         if (incing) {
247                 if ((dat[2] = ftell(scnout)) == EOF)
248                         adios(scnmsg, "write error on");
249         } else if (!scanfolder) {
250                 if ((dat[2] = ftell(inb)) == EOF)
251                         adios(scnmsg, "write error on");
252                 dat[2] -= fpos;
253         }
254
255         if (fmtstr) {
256                 fstat(fileno(inb), &st);
257                 if (scanfolder) {
258                         dat[2] = st.st_size;
259                 }
260                 if (datecomp && !datecomp->c_text) {
261                         if (!datecomp->c_text) {
262                                 if (!datecomp->c_tws)
263                                         datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
264                                 if (!datecomp->c_tws)
265                                         adios(NULL, "unable to allocate tws buffer");
266                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
267                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
268                         } else {
269                                 datecomp->c_flags &= ~CF_DATEFAB;
270                         }
271                 }
272
273                 /* print the scan line */
274                 fmt_scan(fmt, scanl, slwidth, dat);
275                 fputs(scanl, stdout);
276         }
277
278         /* return dynamically allocated buffers to pool */
279         while ((cptr = *savecomp++)) {
280                 *--nxtbuf = cptr->c_text;
281                 cptr->c_text = NULL;
282         }
283         *--nxtbuf = tmpbuf;
284
285         if (incing && (ferror(scnout) || fclose(scnout) == EOF))
286                 adios(scnmsg, "write error on");
287
288         return (state != FILEEOF ? SCNERR : SCNMSG);
289 }