]> diplodocus.org Git - nmh/blob - uip/scansbr.c
7eef85f97ecfa378b116cd27df52834cd2ca905c
[nmh] / uip / scansbr.c
1
2 /*
3 * scansbr.c -- routines to help scan along...
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/scansbr.h>
14 #include <h/tws.h>
15 #include <h/utils.h>
16
17 #ifdef _FSTDIO
18 # define _ptr _p /* Gag */
19 # define _cnt _w /* Wretch */
20 #endif
21
22 #define MAXSCANL 256 /* longest possible scan line */
23
24 /*
25 * Buffer size for content part of header fields. We want this
26 * to be large enough so that we don't do a lot of extra FLDPLUS
27 * calls on m_getfld but small enough so that we don't snarf
28 * the entire message body when we're only going to display 30
29 * characters of it.
30 */
31 #define SBUFSIZ 512
32
33 static struct format *fmt;
34 #ifdef JLR
35 static struct format *fmt_top;
36 #endif /* JLR */
37
38 static struct comp *datecomp; /* pntr to "date" comp */
39 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
40 * (if referenced) */
41 static int ncomps = 0; /* # of interesting components */
42 static char **compbuffers = 0; /* buffers for component text */
43 static struct comp **used_buf = 0; /* stack for comp that use buffers */
44
45 static int dat[5]; /* aux. data for format routine */
46
47 char *scanl = 0; /* text of most recent scanline */
48
49 #define DIEWRERR() adios (scnmsg, "write error on")
50
51 #define FPUTS(buf) {\
52 if (mh_fputs(buf,scnout) == EOF)\
53 DIEWRERR();\
54 }
55
56 /*
57 * prototypes
58 */
59 static int mh_fputs(char *, FILE *);
60
61 #define SCAN_CHARWIDTH MB_CUR_MAX
62
63 int
64 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
65 int unseen, char *folder, long size, int noisy)
66 {
67 int i, compnum, encrypted, state;
68 unsigned char *cp, *tmpbuf;
69 char **nxtbuf;
70 char *saved_c_text = NULL;
71 struct comp *cptr;
72 struct comp **savecomp;
73 char *scnmsg = NULL;
74 FILE *scnout = NULL;
75 char name[NAMESZ];
76 static int rlwidth, slwidth;
77
78 /* first-time only initialization */
79 if (!scanl) {
80 if (width == 0) {
81 if ((width = sc_width ()) < WIDTH/2)
82 width = WIDTH/2;
83 else if (width > MAXSCANL)
84 width = MAXSCANL;
85 }
86 dat[3] = slwidth = width;
87 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
88 if (outnum)
89 umask(~m_gmprot());
90
91 /* Compile format string */
92 ncomps = fmt_compile (nfs, &fmt) + 1;
93
94 #ifdef JLR
95 fmt_top = fmt;
96 #endif /* JLR */
97 FINDCOMP(bodycomp, "body");
98 FINDCOMP(datecomp, "date");
99 FINDCOMP(cptr, "folder");
100 if (cptr && folder)
101 cptr->c_text = folder;
102 FINDCOMP(cptr, "encrypted");
103 if (!cptr)
104 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
105 cptr->c_name = "encrypted";
106 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
107 wantcomp[i] = cptr;
108 ncomps++;
109 }
110 FINDCOMP (cptr, "dtimenow");
111 if (cptr)
112 cptr->c_text = getcpy(dtimenow (0));
113 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
114 if (nxtbuf == NULL)
115 adios (NULL, "unable to allocate component buffers");
116 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
117 sizeof(struct comp *));
118 if (used_buf == NULL)
119 adios (NULL, "unable to allocate component buffer stack");
120 used_buf += ncomps+1; *--used_buf = 0;
121 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
122 for (i = ncomps; i--; )
123 *nxtbuf++ = mh_xmalloc(rlwidth);
124 }
125
126 /*
127 * each-message initialization
128 */
129 nxtbuf = compbuffers;
130 savecomp = used_buf;
131 tmpbuf = *nxtbuf++;
132 dat[0] = innum ? innum : outnum;
133 dat[1] = curflg;
134 dat[4] = unseen;
135
136 /*
137 * Get the first field. If the message is non-empty
138 * and we're doing an "inc", open the output file.
139 */
140 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
141 if (ferror(inb)) {
142 advise("read", "unable to"); /* "read error" */
143 return SCNFAT;
144 } else {
145 return SCNEOF;
146 }
147 }
148
149 if (outnum) {
150 if (outnum > 0) {
151 scnmsg = m_name (outnum);
152 if (*scnmsg == '?') /* msg num out of range */
153 return SCNNUM;
154 } else {
155 scnmsg = "/dev/null";
156 }
157 if ((scnout = fopen (scnmsg, "w")) == NULL)
158 adios (scnmsg, "unable to write");
159 }
160
161 /* scan - main loop */
162 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
163 switch (state) {
164 case FLD:
165 case FLDPLUS:
166 compnum++;
167 if (outnum) {
168 FPUTS (name);
169 if ( putc (':', scnout) == EOF) DIEWRERR();
170 FPUTS (tmpbuf);
171 }
172 /*
173 * if we're interested in this component, save a pointer
174 * to the component text, then start using our next free
175 * buffer as the component temp buffer (buffer switching
176 * saves an extra copy of the component text).
177 */
178 if ((cptr = wantcomp[CHASH(name)])) {
179 do {
180 if (!mh_strcasecmp(name, cptr->c_name)) {
181 if (! cptr->c_text) {
182 cptr->c_text = tmpbuf;
183 for (cp = tmpbuf + strlen (tmpbuf) - 1;
184 cp >= tmpbuf; cp--)
185 if (isspace (*cp))
186 *cp = 0;
187 else
188 break;
189 *--savecomp = cptr;
190 tmpbuf = *nxtbuf++;
191 }
192 break;
193 }
194 } while ((cptr = cptr->c_next));
195 }
196
197 while (state == FLDPLUS) {
198 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
199 if (outnum)
200 FPUTS (tmpbuf);
201 }
202 break;
203
204 case BODY:
205 compnum = -1;
206 /*
207 * A slight hack ... if we have less than rlwidth characters
208 * in the buffer, call m_getfld again.
209 */
210
211 if ((i = strlen(tmpbuf)) < rlwidth) {
212 state = m_getfld (state, name, tmpbuf + i,
213 rlwidth - i, inb);
214 }
215 if (! outnum) {
216 state = FILEEOF; /* stop now if scan cmd */
217 goto finished;
218 }
219 if (putc ('\n', scnout) == EOF) DIEWRERR();
220 FPUTS (tmpbuf);
221 /*
222 * performance hack: some people like to run "inc" on
223 * things like net.sources or large digests. We do a
224 * copy directly into the output buffer rather than
225 * going through an intermediate buffer.
226 *
227 * We need the amount of data m_getfld found & don't
228 * want to do a strlen on the long buffer so there's
229 * a hack in m_getfld to save the amount of data it
230 * returned in the global "msg_count".
231 */
232 body:;
233 while (state == BODY) {
234 #ifdef LINUX_STDIO
235 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
236 #elif defined(__DragonFly__)
237 if (((struct __FILE_public *)scnout)->_w <= 0) {
238 #else
239 if (scnout->_cnt <= 0) {
240 #endif
241 if (fflush(scnout) == EOF)
242 DIEWRERR ();
243 }
244 #ifdef LINUX_STDIO
245 state = m_getfld(state, name, scnout->_IO_write_ptr,
246 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
247 scnout->_IO_write_ptr += msg_count;
248 #elif defined(__DragonFly__)
249 state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
250 ((struct __FILE_public *)scnout)->_w -= msg_count;
251 ((struct __FILE_public *)scnout)->_p += msg_count;
252 #else
253 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
254 scnout->_cnt -= msg_count;
255 scnout->_ptr += msg_count;
256 #endif
257 }
258 goto finished;
259
260 case LENERR:
261 case FMTERR:
262 fprintf (stderr,
263 innum ? "??Format error (message %d) in "
264 : "??Format error in ",
265 outnum ? outnum : innum);
266 fprintf (stderr, "component %d\n", compnum);
267
268 if (outnum) {
269 FPUTS ("\n\nBAD MSG:\n");
270 FPUTS (name);
271 if (putc ('\n', scnout) == EOF) DIEWRERR();
272 state = BODY;
273 goto body;
274 }
275 /* fall through */
276
277 case FILEEOF:
278 goto finished;
279
280 default:
281 adios (NULL, "getfld() returned %d", state);
282 }
283 }
284
285 /*
286 * format and output the scan line.
287 */
288 finished:
289 if (ferror(inb)) {
290 advise("read", "unable to"); /* "read error" */
291 return SCNFAT;
292 }
293
294 /* Save and restore buffer so we don't trash our dynamic pool! */
295 if (bodycomp) {
296 saved_c_text = bodycomp->c_text;
297 bodycomp->c_text = tmpbuf;
298 }
299
300 if (size)
301 dat[2] = size;
302 else if (outnum > 0)
303 {
304 dat[2] = ftell(scnout);
305 if (dat[2] == EOF) DIEWRERR();
306 }
307
308 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
309 struct stat st;
310
311 fstat (fileno(inb), &st);
312 if (!size && !outnum)
313 dat[2] = st.st_size;
314 if (datecomp) {
315 if (! datecomp->c_text) {
316 if (datecomp->c_tws == NULL)
317 datecomp->c_tws = (struct tws *)
318 calloc((size_t) 1, sizeof(*datecomp->c_tws));
319 if (datecomp->c_tws == NULL)
320 adios (NULL, "unable to allocate tws buffer");
321 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
322 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
323 } else {
324 datecomp->c_flags &= ~CF_DATEFAB;
325 }
326 }
327 }
328
329 fmt_scan (fmt, scanl, slwidth, dat);
330
331 #if 0
332 fmt = fmt_scan (fmt, scanl, slwidth, dat);
333 if (!fmt)
334 fmt = fmt_top; /* reset for old format files */
335 #endif
336
337 if (bodycomp)
338 bodycomp->c_text = saved_c_text;
339
340 if (noisy)
341 fputs (scanl, stdout);
342
343 FINDCOMP (cptr, "encrypted");
344 encrypted = cptr && cptr->c_text;
345
346 /* return dynamically allocated buffers to pool */
347 while ((cptr = *savecomp++)) {
348 *--nxtbuf = cptr->c_text;
349 cptr->c_text = NULL;
350 }
351 *--nxtbuf = tmpbuf;
352
353 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
354 DIEWRERR();
355
356 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
357 }
358
359
360 /*
361 * Cheat: we are loaded with adrparse, which wants a routine called
362 * OfficialName(). We call adrparse:getm() with the correct arguments
363 * to prevent OfficialName() from being called. Hence, the following
364 * is to keep the loader happy.
365 */
366 char *
367 OfficialName (char *name)
368 {
369 return name;
370 }
371
372
373 static int
374 mh_fputs(char *s, FILE *stream)
375 {
376 char c;
377
378 while ((c = *s++))
379 if (putc (c,stream) == EOF )
380 return(EOF);
381 return (0);
382 }
383