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