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