]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
3 * scansbr.c -- routines to help scan along...
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.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/scansbr.h>
18 # define _ptr _p /* Gag */
19 # define _cnt _w /* Wretch */
22 #define MAXSCANL 256 /* longest possible scan line */
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
33 static struct format
*fmt
;
35 static struct format
*fmt_top
;
38 static struct comp
*datecomp
; /* pntr to "date" comp */
39 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
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 */
45 static int dat
[5]; /* aux. data for format routine */
47 char *scanl
= 0; /* text of most recent scanline */
49 #define DIEWRERR() adios (scnmsg, "write error on")
52 if (mh_fputs(buf,scnout) == EOF)\
59 int sc_width (void); /* from termsbr.c */
60 static int mh_fputs(char *, FILE *);
62 #ifdef MULTIBYTE_SUPPORT
63 #define SCAN_CHARWIDTH MB_CUR_MAX
65 #define SCAN_CHARWIDTH 1
69 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
70 int unseen
, char *folder
, long size
, int noisy
)
72 int i
, compnum
, encrypted
, state
;
73 unsigned char *cp
, *tmpbuf
;
75 char *saved_c_text
= NULL
;
77 struct comp
**savecomp
;
81 static int rlwidth
, slwidth
;
83 /* first-time only initialization */
86 if ((width
= sc_width ()) < WIDTH
/2)
88 else if (width
> MAXSCANL
)
91 dat
[3] = slwidth
= width
;
92 scanl
= (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH
* (slwidth
+ 2) );
96 /* Compile format string */
97 ncomps
= fmt_compile (nfs
, &fmt
) + 1;
102 FINDCOMP(bodycomp
, "body");
103 FINDCOMP(datecomp
, "date");
104 FINDCOMP(cptr
, "folder");
106 cptr
->c_text
= folder
;
107 FINDCOMP(cptr
, "encrypted");
109 if ((cptr
= (struct comp
*) calloc (1, sizeof(*cptr
)))) {
110 cptr
->c_name
= "encrypted";
111 cptr
->c_next
= wantcomp
[i
= CHASH (cptr
->c_name
)];
115 FINDCOMP (cptr
, "dtimenow");
117 cptr
->c_text
= getcpy(dtimenow (0));
118 nxtbuf
= compbuffers
= (char **) calloc((size_t) ncomps
, sizeof(char *));
120 adios (NULL
, "unable to allocate component buffers");
121 used_buf
= (struct comp
**) calloc((size_t) (ncomps
+1),
122 sizeof(struct comp
*));
123 if (used_buf
== NULL
)
124 adios (NULL
, "unable to allocate component buffer stack");
125 used_buf
+= ncomps
+1; *--used_buf
= 0;
126 rlwidth
= bodycomp
&& (width
> SBUFSIZ
) ? width
: SBUFSIZ
;
127 for (i
= ncomps
; i
--; )
128 *nxtbuf
++ = mh_xmalloc(rlwidth
);
132 * each-message initialization
134 nxtbuf
= compbuffers
;
137 dat
[0] = innum
? innum
: outnum
;
142 * Get the first field. If the message is non-empty
143 * and we're doing an "inc", open the output file.
145 if ((state
= m_getfld (FLD
, name
, tmpbuf
, rlwidth
, inb
)) == FILEEOF
) {
147 advise("read", "unable to"); /* "read error" */
156 scnmsg
= m_name (outnum
);
157 if (*scnmsg
== '?') /* msg num out of range */
160 scnmsg
= "/dev/null";
162 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
163 adios (scnmsg
, "unable to write");
166 /* scan - main loop */
167 for (compnum
= 1; ; state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
)) {
174 if ( putc (':', scnout
) == EOF
) DIEWRERR();
178 * if we're interested in this component, save a pointer
179 * to the component text, then start using our next free
180 * buffer as the component temp buffer (buffer switching
181 * saves an extra copy of the component text).
183 if ((cptr
= wantcomp
[CHASH(name
)])) {
185 if (!mh_strcasecmp(name
, cptr
->c_name
)) {
186 if (! cptr
->c_text
) {
187 cptr
->c_text
= tmpbuf
;
188 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
199 } while ((cptr
= cptr
->c_next
));
202 while (state
== FLDPLUS
) {
203 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
);
212 state
= FILEEOF
; /* stop now if scan cmd */
215 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
218 * performance hack: some people like to run "inc" on
219 * things like net.sources or large digests. We do a
220 * copy directly into the output buffer rather than
221 * going through an intermediate buffer.
223 * We need the amount of data m_getfld found & don't
224 * want to do a strlen on the long buffer so there's
225 * a hack in m_getfld to save the amount of data it
226 * returned in the global "msg_count".
229 while (state
== BODY
) {
231 if (scnout
->_IO_write_ptr
== scnout
->_IO_write_end
) {
232 #elif defined(__DragonFly__)
233 if (((struct __FILE_public
*)scnout
)->_w
<= 0) {
235 if (scnout
->_cnt
<= 0) {
237 if (fflush(scnout
) == EOF
)
241 state
= m_getfld(state
, name
, scnout
->_IO_write_ptr
,
242 (long)scnout
->_IO_write_ptr
-(long)scnout
->_IO_write_end
, inb
);
243 scnout
->_IO_write_ptr
+= msg_count
;
244 #elif defined(__DragonFly__)
245 state
= m_getfld( state
, name
, ((struct __FILE_public
*)scnout
)->_p
, -(((struct __FILE_public
*)scnout
)->_w
), inb
);
246 ((struct __FILE_public
*)scnout
)->_w
-= msg_count
;
247 ((struct __FILE_public
*)scnout
)->_p
+= msg_count
;
249 state
= m_getfld( state
, name
, scnout
->_ptr
, -(scnout
->_cnt
), inb
);
250 scnout
->_cnt
-= msg_count
;
251 scnout
->_ptr
+= msg_count
;
259 innum
? "??Format error (message %d) in "
260 : "??Format error in ",
261 outnum
? outnum
: innum
);
262 fprintf (stderr
, "component %d\n", compnum
);
265 FPUTS ("\n\nBAD MSG:\n");
267 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
277 adios (NULL
, "getfld() returned %d", state
);
282 * format and output the scan line.
286 advise("read", "unable to"); /* "read error" */
290 /* Save and restore buffer so we don't trash our dynamic pool! */
292 saved_c_text
= bodycomp
->c_text
;
293 bodycomp
->c_text
= tmpbuf
;
300 dat
[2] = ftell(scnout
);
301 if (dat
[2] == EOF
) DIEWRERR();
304 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
307 fstat (fileno(inb
), &st
);
308 if (!size
&& !outnum
)
311 if (! datecomp
->c_text
) {
312 if (datecomp
->c_tws
== NULL
)
313 datecomp
->c_tws
= (struct tws
*)
314 calloc((size_t) 1, sizeof(*datecomp
->c_tws
));
315 if (datecomp
->c_tws
== NULL
)
316 adios (NULL
, "unable to allocate tws buffer");
317 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
318 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
320 datecomp
->c_flags
&= ~CF_DATEFAB
;
325 fmt_scan (fmt
, scanl
, slwidth
, dat
);
328 fmt
= fmt_scan (fmt
, scanl
, slwidth
, dat
);
330 fmt
= fmt_top
; /* reset for old format files */
334 bodycomp
->c_text
= saved_c_text
;
337 fputs (scanl
, stdout
);
339 FINDCOMP (cptr
, "encrypted");
340 encrypted
= cptr
&& cptr
->c_text
;
342 /* return dynamically allocated buffers to pool */
343 while ((cptr
= *savecomp
++)) {
344 *--nxtbuf
= cptr
->c_text
;
349 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
352 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
357 * Cheat: we are loaded with adrparse, which wants a routine called
358 * OfficialName(). We call adrparse:getm() with the correct arguments
359 * to prevent OfficialName() from being called. Hence, the following
360 * is to keep the loader happy.
363 OfficialName (char *name
)
370 mh_fputs(char *s
, FILE *stream
)
375 if (putc (c
,stream
) == EOF
)