]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
3 * scansbr.c -- routines to help scan along...
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.
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
15 #include <h/scansbr.h>
20 # define _ptr _p /* Gag */
21 # define _cnt _w /* Wretch */
28 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
31 #define MAXSCANL 256 /* longest possible scan line */
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
42 static struct format
*fmt
;
44 static struct format
*fmt_top
;
47 static struct comp
*datecomp
; /* pntr to "date" comp */
48 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
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 */
54 static int dat
[5]; /* aux. data for format routine */
56 char *scanl
= 0; /* text of most recent scanline */
58 #define DIEWRERR() adios (scnmsg, "write error on")
61 if (mh_fputs(buf,scnout) == EOF)\
68 int sc_width (void); /* from termsbr.c */
69 static int mh_fputs(char *, FILE *);
71 #ifdef MULTIBYTE_SUPPORT
72 #define SCAN_CHARWIDTH MB_CUR_MAX
74 #define SCAN_CHARWIDTH 1
78 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
79 int unseen
, char *folder
, long size
, int noisy
)
81 int i
, compnum
, encrypted
, state
;
82 unsigned char *cp
, *tmpbuf
;
86 struct comp
**savecomp
;
90 static int rlwidth
, slwidth
;
93 char returnpath
[BUFSIZ
];
94 char deliverydate
[BUFSIZ
];
97 /* first-time only initialization */
100 if ((width
= sc_width ()) < WIDTH
/2)
102 else if (width
> MAXSCANL
)
105 dat
[3] = slwidth
= width
;
106 scanl
= (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH
* (slwidth
+ 2) );
110 /* Compile format string */
111 ncomps
= fmt_compile (nfs
, &fmt
) + 1;
116 FINDCOMP(bodycomp
, "body");
117 FINDCOMP(datecomp
, "date");
118 FINDCOMP(cptr
, "folder");
120 cptr
->c_text
= folder
;
121 FINDCOMP(cptr
, "encrypted");
123 if ((cptr
= (struct comp
*) calloc (1, sizeof(*cptr
)))) {
124 cptr
->c_name
= "encrypted";
125 cptr
->c_next
= wantcomp
[i
= CHASH (cptr
->c_name
)];
129 FINDCOMP (cptr
, "dtimenow");
131 cptr
->c_text
= getcpy(dtimenow (0));
132 nxtbuf
= compbuffers
= (char **) calloc((size_t) ncomps
, sizeof(char *));
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
);
146 * each-message initialization
148 nxtbuf
= compbuffers
;
151 dat
[0] = innum
? innum
: outnum
;
156 * Get the first field. If the message is non-empty
157 * and we're doing an "inc", open the output file.
159 if ((state
= m_getfld (FLD
, name
, tmpbuf
, rlwidth
, inb
)) == FILEEOF
) {
161 advise("read", "unable to"); /* "read error" */
170 scnmsg
= m_name (outnum
);
171 if (*scnmsg
== '?') /* msg num out of range */
174 scnmsg
= "/dev/null";
176 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
177 adios (scnmsg
, "unable to write");
180 * Add the Return-Path and Delivery-Date
181 * header fields to message.
183 if (get_returnpath (returnpath
, sizeof(returnpath
),
184 deliverydate
, sizeof(deliverydate
))) {
185 FPUTS ("Return-Path: ");
187 FPUTS ("Delivery-Date: ");
188 FPUTS (deliverydate
);
193 /* scan - main loop */
194 for (compnum
= 1; ; state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
)) {
201 if ( putc (':', scnout
) == EOF
) DIEWRERR();
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).
210 if ((cptr
= wantcomp
[CHASH(name
)])) {
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;
226 } while ((cptr
= cptr
->c_next
));
229 while (state
== FLDPLUS
) {
230 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
);
239 state
= FILEEOF
; /* stop now if scan cmd */
242 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
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.
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".
256 while (state
== BODY
) {
258 if (scnout
->_IO_write_ptr
== scnout
->_IO_write_end
) {
259 #elif defined(__DragonFly__)
260 if (((struct __FILE_public
*)scnout
)->_w
<= 0) {
262 if (scnout
->_cnt
<= 0) {
264 if (fflush(scnout
) == EOF
)
268 state
= m_getfld(state
, name
, scnout
->_IO_write_ptr
,
269 (long)scnout
->_IO_write_ptr
-(long)scnout
->_IO_write_end
, inb
);
270 scnout
->_IO_write_ptr
+= msg_count
;
271 #elif defined(__DragonFly__)
272 state
= m_getfld( state
, name
, ((struct __FILE_public
*)scnout
)->_p
, -(((struct __FILE_public
*)scnout
)->_w
), inb
);
273 ((struct __FILE_public
*)scnout
)->_w
-= msg_count
;
274 ((struct __FILE_public
*)scnout
)->_p
+= msg_count
;
276 state
= m_getfld( state
, name
, scnout
->_ptr
, -(scnout
->_cnt
), inb
);
277 scnout
->_cnt
-= msg_count
;
278 scnout
->_ptr
+= msg_count
;
286 innum
? "??Format error (message %d) in "
287 : "??Format error in ",
288 outnum
? outnum
: innum
);
289 fprintf (stderr
, "component %d\n", compnum
);
292 FPUTS ("\n\nBAD MSG:\n");
294 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
304 adios (NULL
, "getfld() returned %d", state
);
309 * format and output the scan line.
313 advise("read", "unable to"); /* "read error" */
317 /* Save and restore buffer so we don't trash our dynamic pool! */
319 saved_c_text
= bodycomp
->c_text
;
320 bodycomp
->c_text
= tmpbuf
;
327 dat
[2] = ftell(scnout
);
328 if (dat
[2] == EOF
) DIEWRERR();
331 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
334 fstat (fileno(inb
), &st
);
335 if (!size
&& !outnum
)
338 if (! datecomp
->c_text
) {
339 if (datecomp
->c_tws
== NULL
)
340 datecomp
->c_tws
= (struct tws
*)
341 calloc((size_t) 1, sizeof(*datecomp
->c_tws
));
342 if (datecomp
->c_tws
== NULL
)
343 adios (NULL
, "unable to allocate tws buffer");
344 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
345 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
347 datecomp
->c_flags
&= ~CF_DATEFAB
;
352 fmt_scan (fmt
, scanl
, slwidth
, dat
);
355 fmt
= fmt_scan (fmt
, scanl
, slwidth
, dat
);
357 fmt
= fmt_top
; /* reset for old format files */
361 bodycomp
->c_text
= saved_c_text
;
364 fputs (scanl
, stdout
);
366 FINDCOMP (cptr
, "encrypted");
367 encrypted
= cptr
&& cptr
->c_text
;
369 /* return dynamically allocated buffers to pool */
370 while ((cptr
= *savecomp
++)) {
371 *--nxtbuf
= cptr
->c_text
;
376 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
379 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
384 * Cheat: we are loaded with adrparse, which wants a routine called
385 * OfficialName(). We call adrparse:getm() with the correct arguments
386 * to prevent OfficialName() from being called. Hence, the following
387 * is to keep the loader happy.
390 OfficialName (char *name
)
397 mh_fputs(char *s
, FILE *stream
)
402 if (putc (c
,stream
) == EOF
)