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