]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
3 * scansbr.c -- routines to help scan along...
10 #include <h/fmt_scan.h>
11 #include <h/scansbr.h>
15 # define _ptr _p /* Gag */
16 # define _cnt _w /* Wretch */
23 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
26 #define MAXSCANL 256 /* longest possible scan line */
29 * Buffer size for content part of header fields. We want this
30 * to be large enough so that we don't do a lot of extra FLDPLUS
31 * calls on m_getfld but small enough so that we don't snarf
32 * the entire message body when we're only going to display 30
37 static struct format
*fmt
;
39 static struct format
*fmt_top
;
42 static struct comp
*datecomp
; /* pntr to "date" comp */
43 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
45 static int ncomps
= 0; /* # of interesting components */
46 static char **compbuffers
= 0; /* buffers for component text */
47 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
49 static int dat
[5]; /* aux. data for format routine */
51 char *scanl
= 0; /* text of most recent scanline */
53 #define DIEWRERR() adios (scnmsg, "write error on")
56 if (mh_fputs(buf,scnout) == EOF)\
63 int sc_width (void); /* from termsbr.c */
64 static int mh_fputs(char *, FILE *);
68 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
69 int unseen
, char *folder
, long size
, int noisy
)
71 int i
, compnum
, encrypted
, state
;
72 char *cp
, *tmpbuf
, **nxtbuf
;
75 struct comp
**savecomp
;
79 static int rlwidth
, slwidth
;
82 char returnpath
[BUFSIZ
];
83 char deliverydate
[BUFSIZ
];
86 /* first-time only initialization */
89 if ((width
= sc_width ()) < WIDTH
/2)
91 else if (width
> MAXSCANL
)
94 dat
[3] = slwidth
= width
;
95 if ((scanl
= (char *) malloc((size_t) (slwidth
+ 2) )) == NULL
)
96 adios (NULL
, "unable to malloc scan line (%d bytes)", slwidth
+2);
100 /* Compile format string */
101 ncomps
= fmt_compile (nfs
, &fmt
) + 1;
106 FINDCOMP(bodycomp
, "body");
107 FINDCOMP(datecomp
, "date");
108 FINDCOMP(cptr
, "folder");
110 cptr
->c_text
= folder
;
111 FINDCOMP(cptr
, "encrypted");
113 if ((cptr
= (struct comp
*) calloc (1, sizeof(*cptr
)))) {
114 cptr
->c_name
= "encrypted";
115 cptr
->c_next
= wantcomp
[i
= CHASH (cptr
->c_name
)];
119 FINDCOMP (cptr
, "dtimenow");
121 cptr
->c_text
= getcpy(dtimenow (0));
122 nxtbuf
= compbuffers
= (char **) calloc((size_t) ncomps
, sizeof(char *));
124 adios (NULL
, "unable to allocate component buffers");
125 used_buf
= (struct comp
**) calloc((size_t) (ncomps
+1),
126 sizeof(struct comp
*));
127 if (used_buf
== NULL
)
128 adios (NULL
, "unable to allocate component buffer stack");
129 used_buf
+= ncomps
+1; *--used_buf
= 0;
130 rlwidth
= bodycomp
&& (width
> SBUFSIZ
) ? width
: SBUFSIZ
;
131 for (i
= ncomps
; i
--; )
132 if ((*nxtbuf
++ = malloc(rlwidth
)) == NULL
)
133 adios (NULL
, "unable to allocate component buffer");
137 * each-message initialization
139 nxtbuf
= compbuffers
;
142 dat
[0] = innum
? innum
: outnum
;
147 * Get the first field. If the message is non-empty
148 * and we're doing an "inc", open the output file.
150 if ((state
= m_getfld (FLD
, name
, tmpbuf
, rlwidth
, inb
)) == FILEEOF
) {
152 advise("read", "unable to"); /* "read error" */
161 scnmsg
= m_name (outnum
);
162 if (*scnmsg
== '?') /* msg num out of range */
165 scnmsg
= "/dev/null";
167 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
168 adios (scnmsg
, "unable to write");
171 * Add the Return-Path and Delivery-Date
172 * header fields to message.
174 if (get_returnpath (returnpath
, sizeof(returnpath
),
175 deliverydate
, sizeof(deliverydate
))) {
176 FPUTS ("Return-Path: ");
178 FPUTS ("Delivery-Date: ");
179 FPUTS (deliverydate
);
184 /* scan - main loop */
185 for (compnum
= 1; ; state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
)) {
192 if ( putc (':', scnout
) == EOF
) DIEWRERR();
196 * if we're interested in this component, save a pointer
197 * to the component text, then start using our next free
198 * buffer as the component temp buffer (buffer switching
199 * saves an extra copy of the component text).
201 if ((cptr
= wantcomp
[CHASH(name
)])) {
203 if (!strcasecmp(name
, cptr
->c_name
)) {
204 if (! cptr
->c_text
) {
205 cptr
->c_text
= tmpbuf
;
206 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
217 } while ((cptr
= cptr
->c_next
));
220 while (state
== FLDPLUS
) {
221 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
);
230 state
= FILEEOF
; /* stop now if scan cmd */
233 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
236 * performance hack: some people like to run "inc" on
237 * things like net.sources or large digests. We do a
238 * copy directly into the output buffer rather than
239 * going through an intermediate buffer.
241 * We need the amount of data m_getfld found & don't
242 * want to do a strlen on the long buffer so there's
243 * a hack in m_getfld to save the amount of data it
244 * returned in the global "msg_count".
247 while (state
== BODY
) {
249 if (scnout
->_IO_write_ptr
== scnout
->_IO_write_end
) {
251 if (scnout
->_cnt
<= 0) {
253 if (fflush(scnout
) == EOF
)
257 state
= m_getfld(state
, name
, scnout
->_IO_write_ptr
,
258 (long)scnout
->_IO_write_ptr
-(long)scnout
->_IO_write_end
, inb
);
259 scnout
->_IO_write_ptr
+= msg_count
;
261 state
= m_getfld( state
, name
, scnout
->_ptr
, -(scnout
->_cnt
), inb
);
262 scnout
->_cnt
-= msg_count
;
263 scnout
->_ptr
+= msg_count
;
271 innum
? "??Format error (message %d) in "
272 : "??Format error in ",
273 outnum
? outnum
: innum
);
274 fprintf (stderr
, "component %d\n", compnum
);
277 FPUTS ("\n\nBAD MSG:\n");
279 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
289 adios (NULL
, "getfld() returned %d", state
);
294 * format and output the scan line.
298 advise("read", "unable to"); /* "read error" */
302 /* Save and restore buffer so we don't trash our dynamic pool! */
304 saved_c_text
= bodycomp
->c_text
;
305 bodycomp
->c_text
= tmpbuf
;
312 dat
[2] = ftell(scnout
);
313 if (dat
[2] == EOF
) DIEWRERR();
316 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
319 fstat (fileno(inb
), &st
);
320 if (!size
&& !outnum
)
323 if (! datecomp
->c_text
) {
324 if (datecomp
->c_tws
== NULL
)
325 datecomp
->c_tws
= (struct tws
*)
326 calloc((size_t) 1, sizeof(*datecomp
->c_tws
));
327 if (datecomp
->c_tws
== NULL
)
328 adios (NULL
, "unable to allocate tws buffer");
329 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
330 datecomp
->c_flags
= -1;
332 datecomp
->c_flags
= 0;
337 fmt_scan (fmt
, scanl
, slwidth
, dat
);
340 fmt
= fmt_scan (fmt
, scanl
, slwidth
, dat
);
342 fmt
= fmt_top
; /* reset for old format files */
346 bodycomp
->c_text
= saved_c_text
;
349 fputs (scanl
, stdout
);
351 FINDCOMP (cptr
, "encrypted");
352 encrypted
= cptr
&& cptr
->c_text
;
354 /* return dynamically allocated buffers to pool */
355 while ((cptr
= *savecomp
++)) {
356 *--nxtbuf
= cptr
->c_text
;
361 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
364 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
369 * Cheat: we are loaded with adrparse, which wants a routine called
370 * OfficialName(). We call adrparse:getm() with the correct arguments
371 * to prevent OfficialName() from being called. Hence, the following
372 * is to keep the loader happy.
375 OfficialName (char *name
)
382 mh_fputs(char *s
, FILE *stream
)
387 if (putc (c
,stream
) == EOF
)