]>
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>
12 #include <zotnet/tws/tws.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 */
54 if (mh_fputs(buf,scnout) == EOF)\
55 adios (scnmsg, "write error on");\
61 int sc_width (void); /* from termsbr.c */
62 static int mh_fputs(char *, FILE *);
66 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
67 int unseen
, char *folder
, long size
, int noisy
)
69 int i
, compnum
, encrypted
, state
;
70 char *cp
, *tmpbuf
, **nxtbuf
;
73 struct comp
**savecomp
;
77 static int rlwidth
, slwidth
;
80 char returnpath
[BUFSIZ
];
81 char deliverydate
[BUFSIZ
];
84 /* first-time only initialization */
87 if ((width
= sc_width ()) < WIDTH
/2)
89 else if (width
> MAXSCANL
)
92 dat
[3] = slwidth
= width
;
93 if ((scanl
= (char *) malloc((size_t) (slwidth
+ 2) )) == NULL
)
94 adios (NULL
, "unable to malloc scan line (%d bytes)", slwidth
+2);
98 /* Compile format string */
99 ncomps
= fmt_compile (nfs
, &fmt
) + 1;
104 FINDCOMP(bodycomp
, "body");
105 FINDCOMP(datecomp
, "date");
106 FINDCOMP(cptr
, "folder");
108 cptr
->c_text
= folder
;
109 FINDCOMP(cptr
, "encrypted");
111 if ((cptr
= (struct comp
*) calloc (1, sizeof(*cptr
)))) {
112 cptr
->c_name
= "encrypted";
113 cptr
->c_next
= wantcomp
[i
= CHASH (cptr
->c_name
)];
117 FINDCOMP (cptr
, "dtimenow");
119 cptr
->c_text
= getcpy(dtimenow (0));
120 nxtbuf
= compbuffers
= (char **) calloc((size_t) ncomps
, sizeof(char *));
122 adios (NULL
, "unable to allocate component buffers");
123 used_buf
= (struct comp
**) calloc((size_t) (ncomps
+1),
124 sizeof(struct comp
*));
125 if (used_buf
== NULL
)
126 adios (NULL
, "unable to allocate component buffer stack");
127 used_buf
+= ncomps
+1; *--used_buf
= 0;
128 rlwidth
= bodycomp
&& (width
> SBUFSIZ
) ? width
: SBUFSIZ
;
129 for (i
= ncomps
; i
--; )
130 if ((*nxtbuf
++ = malloc(rlwidth
)) == NULL
)
131 adios (NULL
, "unable to allocate component buffer");
135 * each-message initialization
137 nxtbuf
= compbuffers
;
140 dat
[0] = innum
? innum
: outnum
;
145 * Get the first field. If the message is non-empty
146 * and we're doing an "inc", open the output file.
148 if ((state
= m_getfld (FLD
, name
, tmpbuf
, rlwidth
, inb
)) == FILEEOF
) {
150 advise("read", "unable to"); /* "read error" */
159 scnmsg
= m_name (outnum
);
160 if (*scnmsg
== '?') /* msg num out of range */
163 scnmsg
= "/dev/null";
165 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
166 adios (scnmsg
, "unable to write");
169 * Add the Return-Path and Delivery-Date
170 * header fields to message.
172 if (get_returnpath (returnpath
, sizeof(returnpath
),
173 deliverydate
, sizeof(deliverydate
))) {
174 FPUTS ("Return-Path: ");
176 FPUTS ("Delivery-Date: ");
177 FPUTS (deliverydate
);
182 /* scan - main loop */
183 for (compnum
= 1; ; state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
)) {
194 * if we're interested in this component, save a pointer
195 * to the component text, then start using our next free
196 * buffer as the component temp buffer (buffer switching
197 * saves an extra copy of the component text).
199 if ((cptr
= wantcomp
[CHASH(name
)])) {
201 if (!strcasecmp(name
, cptr
->c_name
)) {
202 if (! cptr
->c_text
) {
203 cptr
->c_text
= tmpbuf
;
204 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
215 } while ((cptr
= cptr
->c_next
));
218 while (state
== FLDPLUS
) {
219 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
);
228 state
= FILEEOF
; /* stop now if scan cmd */
234 * performance hack: some people like to run "inc" on
235 * things like net.sources or large digests. We do a
236 * copy directly into the output buffer rather than
237 * going through an intermediate buffer.
239 * We need the amount of data m_getfld found & don't
240 * want to do a strlen on the long buffer so there's
241 * a hack in m_getfld to save the amount of data it
242 * returned in the global "msg_count".
245 while (state
== BODY
) {
247 if (scnout
->_IO_write_ptr
== scnout
->_IO_write_end
) {
249 if (scnout
->_cnt
<= 0) {
251 if (fflush(scnout
) == EOF
)
252 adios (scnmsg
, "write error on");
255 state
= m_getfld(state
, name
, scnout
->_IO_write_ptr
,
256 (long)scnout
->_IO_write_ptr
-(long)scnout
->_IO_write_end
, inb
);
257 scnout
->_IO_write_ptr
+= msg_count
;
259 state
= m_getfld( state
, name
, scnout
->_ptr
, -(scnout
->_cnt
), inb
);
260 scnout
->_cnt
-= msg_count
;
261 scnout
->_ptr
+= msg_count
;
269 innum
? "??Format error (message %d) in "
270 : "??Format error in ",
271 outnum
? outnum
: innum
);
272 fprintf (stderr
, "component %d\n", compnum
);
275 FPUTS ("\n\nBAD MSG:\n");
287 adios (NULL
, "getfld() returned %d", state
);
292 * format and output the scan line.
296 advise("read", "unable to"); /* "read error" */
300 /* Save and restore buffer so we don't trash our dynamic pool! */
302 saved_c_text
= bodycomp
->c_text
;
303 bodycomp
->c_text
= tmpbuf
;
309 dat
[2] = ftell(scnout
);
311 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
314 fstat (fileno(inb
), &st
);
315 if (!size
&& !outnum
)
318 if (! datecomp
->c_text
) {
319 if (datecomp
->c_tws
== NULL
)
320 datecomp
->c_tws
= (struct tws
*)
321 calloc((size_t) 1, sizeof(*datecomp
->c_tws
));
322 if (datecomp
->c_tws
== NULL
)
323 adios (NULL
, "unable to allocate tws buffer");
324 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
325 datecomp
->c_flags
= -1;
327 datecomp
->c_flags
= 0;
332 fmt_scan (fmt
, scanl
, slwidth
, dat
);
335 fmt
= fmt_scan (fmt
, scanl
, slwidth
, dat
);
337 fmt
= fmt_top
; /* reset for old format files */
341 bodycomp
->c_text
= saved_c_text
;
344 fputs (scanl
, stdout
);
346 FINDCOMP (cptr
, "encrypted");
347 encrypted
= cptr
&& cptr
->c_text
;
349 /* return dynamically allocated buffers to pool */
350 while ((cptr
= *savecomp
++)) {
351 *--nxtbuf
= cptr
->c_text
;
356 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
357 adios (scnmsg
, "write error on");
359 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
364 * Cheat: we are loaded with adrparse, which wants a routine called
365 * OfficialName(). We call adrparse:getm() with the correct arguments
366 * to prevent OfficialName() from being called. Hence, the following
367 * is to keep the loader happy.
370 OfficialName (char *name
)
377 mh_fputs(char *s
, FILE *stream
)
382 if (putc (c
,stream
) == EOF
)