Made the RPATHS config option the default and removed the #ifdefs.
[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 but small enough so that we don't snarf
34 ** the entire message body when we're only going to display 30
35 ** characters of it.
36 */
37 #define SBUFSIZ 512
38
39 static struct format *fmt;
40
41 static struct comp *datecomp;  /* pntr to "date" comp */
42 static struct comp *bodycomp;  /* pntr to "body" pseudo-comp (if referenced) */
43 static int ncomps = 0;  /* # of interesting components */
44 static char **compbuffers = NULL;  /* buffers for component text */
45 static struct comp **used_buf = NULL;  /* stack for comp that use buffers */
46
47 static int dat[5];  /* aux. data for format routine */
48
49 char *scanl = NULL;  /* text of most recent scanline */
50
51 #define DIEWRERR() adios(scnmsg, "write error on")
52
53 #define FPUTS(buf) {\
54                 if (fputs(buf,scnout) == EOF)\
55                         DIEWRERR();\
56         }
57
58 /*
59 ** prototypes
60 */
61 int sc_width(void);  /* from termsbr.c */
62
63 #ifdef MULTIBYTE_SUPPORT
64 #define SCAN_CHARWIDTH MB_CUR_MAX
65 #else
66 #define SCAN_CHARWIDTH 1
67 #endif
68
69 int
70 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
71         int unseen)
72 {
73         int i, compnum, state;
74         unsigned char *cp, *tmpbuf;
75         char **nxtbuf;
76         char *saved_c_text = NULL;
77         struct comp *cptr;
78         struct comp **savecomp;
79         char *scnmsg = NULL;
80         FILE *scnout = NULL;
81         char name[NAMESZ];
82         static int rlwidth, slwidth;
83         char returnpath[BUFSIZ];
84         char deliverydate[BUFSIZ];
85
86         /* first-time only initialization */
87         if (!scanl) {
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) );
97                 if (outnum)
98                         umask(~m_gmprot());
99
100                 /* Compile format string */
101                 ncomps = fmt_compile(fmtstr, &fmt) + 1;
102
103                 FINDCOMP(bodycomp, "body");
104                 FINDCOMP(datecomp, "date");
105                 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
106                                 sizeof(char *));
107                 if (nxtbuf == NULL)
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 == NULL)
112                         adios(NULL, "unable to allocate component buffer stack");
113                 used_buf += ncomps+1; *--used_buf = 0;
114                 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
115                 for (i = ncomps; i--; )
116                         *nxtbuf++ = mh_xmalloc(rlwidth);
117         }
118
119         /*
120         ** each-message initialization
121         */
122         nxtbuf = compbuffers;
123         savecomp = used_buf;
124         tmpbuf = *nxtbuf++;
125         dat[0] = innum ? innum : outnum;
126         dat[1] = curflg;
127         dat[4] = unseen;
128
129         /*
130         ** Get the first field.  If the message is non-empty
131         ** and we're doing an "inc", open the output file.
132         */
133         if ((state = m_getfld(FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
134                 if (ferror(inb)) {
135                         advise("read", "unable to"); /* "read error" */
136                         return SCNFAT;
137                 } else {
138                         return SCNEOF;
139                 }
140         }
141
142         if (outnum) {
143                 if (outnum > 0) {
144                         scnmsg = m_name(outnum);
145                         if (*scnmsg == '?')  /* msg num out of range */
146                                 return SCNNUM;
147                 } else {
148                         scnmsg = "/dev/null";
149                 }
150                 if ((scnout = fopen(scnmsg, "w")) == NULL)
151                         adios(scnmsg, "unable to write");
152                 /*
153                 ** Add the Return-Path and Delivery-Date
154                 ** header fields to message.
155                 */
156                 if (get_returnpath(returnpath, sizeof(returnpath),
157                                 deliverydate, sizeof(deliverydate))) {
158                         FPUTS("Return-Path: ");
159                         FPUTS(returnpath);
160                         FPUTS("Delivery-Date: ");
161                         FPUTS(deliverydate);
162                 }
163         }
164
165         /* scan - main loop */
166         for (compnum = 1; ;
167                         state = m_getfld(state, name, tmpbuf, rlwidth, inb)) {
168                 switch (state) {
169                 case FLD:
170                 case FLDPLUS:
171                         compnum++;
172                         if (outnum) {
173                                 FPUTS(name);
174                                 if (putc(':', scnout) == EOF)
175                                         DIEWRERR();
176                                 FPUTS(tmpbuf);
177                         }
178                         /*
179                         ** if we're interested in this component, save
180                         ** a pointer to the component text, then start
181                         ** using our next free buffer as the component
182                         ** temp buffer (buffer switching saves an extra
183                         ** copy of the component text).
184                         */
185                         if ((cptr = wantcomp[CHASH(name)])) {
186                                 do {
187                                         if (!mh_strcasecmp(name, cptr->c_name)) {
188                                                 if (! cptr->c_text) {
189                                                         cptr->c_text = tmpbuf;
190                                                         for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
191                                                                 if (isspace(*cp))
192                                                                         *cp = 0;
193                                                                 else
194                                                                         break;
195                                                         *--savecomp = cptr;
196                                                         tmpbuf = *nxtbuf++;
197                                                 }
198                                                 break;
199                                         }
200                                 } while ((cptr = cptr->c_next));
201                         }
202
203                         while (state == FLDPLUS) {
204                                 state = m_getfld(state, name, tmpbuf, rlwidth,
205                                                 inb);
206                                 if (outnum)
207                                         FPUTS(tmpbuf);
208                         }
209                         break;
210
211                 case BODY:
212                         compnum = -1;
213                         if (! outnum) {
214                                 state = FILEEOF; /* stop now if scan cmd */
215                                 goto finished;
216                         }
217                         if (putc('\n', scnout) == EOF) DIEWRERR();
218                         FPUTS(tmpbuf);
219                         /*
220                         ** performance hack: some people like to run "inc"
221                         ** on things like net.sources or large digests.
222                         ** We do a copy directly into the output buffer
223                         ** rather than going through an intermediate buffer.
224                         **
225                         ** We need the amount of data m_getfld found &
226                         ** don't want to do a strlen on the long buffer so
227                         ** there's a hack in m_getfld to save the amount
228                         ** of data it returned in the global "msg_count".
229                         */
230 body:;
231                         while (state == BODY) {
232 #ifdef LINUX_STDIO
233                                 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
234 #elif defined(__DragonFly__)
235                                 if (((struct __FILE_public *)scnout)->_w <= 0) {
236 #else
237                                 if (scnout->_cnt <= 0) {
238 #endif
239                                         if (fflush(scnout) == EOF)
240                                                 DIEWRERR();
241                                 }
242 #ifdef LINUX_STDIO
243                                 state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
244                                 scnout->_IO_write_ptr += msg_count;
245 #elif defined(__DragonFly__)
246                                 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
247                                 ((struct __FILE_public *)scnout)->_w -= msg_count;
248                                 ((struct __FILE_public *)scnout)->_p += msg_count;
249 #else
250                                 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb);
251                                 scnout->_cnt -= msg_count;
252                                 scnout->_ptr += msg_count;
253 #endif
254                         }
255                         goto finished;
256
257                 case LENERR:
258                 case FMTERR:
259                         fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
260                         fprintf(stderr, "component %d\n", compnum);
261
262                         if (outnum) {
263                                 FPUTS("\n\nBAD MSG:\n");
264                                 FPUTS(name);
265                                 if (putc('\n', scnout) == EOF)
266                                         DIEWRERR();
267                                 state = BODY;
268                                 goto body;
269                         }
270                         /* fall through */
271
272                 case FILEEOF:
273                         goto finished;
274
275                 default:
276                         adios(NULL, "getfld() returned %d", state);
277                 }
278         }
279
280         /*
281         ** format and output the scan line.
282         */
283 finished:
284         if (ferror(inb)) {
285                 advise("read", "unable to"); /* "read error" */
286                 return SCNFAT;
287         }
288
289         /* Save and restore buffer so we don't trash our dynamic pool! */
290         if (bodycomp) {
291                 saved_c_text = bodycomp->c_text;
292                 bodycomp->c_text = tmpbuf;
293         }
294
295         if (outnum > 0) {
296                 dat[2] = ftell(scnout);
297                 if (dat[2] == EOF)
298                         DIEWRERR();
299         }
300
301         if ((datecomp && !datecomp->c_text) || !outnum) {
302                 struct stat st;
303
304                 fstat(fileno(inb), &st);
305                 if (!outnum)
306                         dat[2] = st.st_size;
307                 if (datecomp) {
308                         if (! datecomp->c_text) {
309                                 if (datecomp->c_tws == NULL)
310                                         datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
311                                 if (datecomp->c_tws == NULL)
312                                         adios(NULL, "unable to allocate tws buffer");
313                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
314                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
315                         } else {
316                                 datecomp->c_flags &= ~CF_DATEFAB;
317                         }
318                 }
319         }
320
321         fmt_scan(fmt, scanl, slwidth, dat);
322
323         if (bodycomp)
324                 bodycomp->c_text = saved_c_text;
325
326         if (fmtstr)
327                 fputs(scanl, stdout);
328
329         /* return dynamically allocated buffers to pool */
330         while ((cptr = *savecomp++)) {
331                 *--nxtbuf = cptr->c_text;
332                 cptr->c_text = NULL;
333         }
334         *--nxtbuf = tmpbuf;
335
336         if (outnum && (ferror(scnout) || fclose(scnout) == EOF))
337                 DIEWRERR();
338
339         return (state != FILEEOF ? SCNERR : SCNMSG);
340 }