]>
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 */
26 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
29 #define MAXSCANL 256 /* longest possible scan line */
32 * Buffer size for content part of header fields. We want this
33 * to be large enough so that we don't do a lot of extra FLDPLUS
34 * calls on m_getfld but small enough so that we don't snarf
35 * the entire message body when we're only going to display 30
40 static struct format
*fmt
;
42 static struct format
*fmt_top
;
45 static struct comp
*datecomp
; /* pntr to "date" comp */
46 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
48 static int ncomps
= 0; /* # of interesting components */
49 static char **compbuffers
= 0; /* buffers for component text */
50 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
52 static int dat
[5]; /* aux. data for format routine */
54 char *scanl
= 0; /* text of most recent scanline */
56 #define DIEWRERR() adios (scnmsg, "write error on")
59 if (mh_fputs(buf,scnout) == EOF)\
66 int sc_width (void); /* from termsbr.c */
67 static int mh_fputs(char *, FILE *);
69 #ifdef MULTIBYTE_SUPPORT
70 #define SCAN_CHARWIDTH MB_CUR_MAX
72 #define SCAN_CHARWIDTH 1
76 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
77 int unseen
, char *folder
, long size
, int noisy
)
79 int i
, compnum
, encrypted
, state
;
80 unsigned char *cp
, *tmpbuf
;
82 char *saved_c_text
= NULL
;
84 struct comp
**savecomp
;
88 static int rlwidth
, slwidth
;
90 /* first-time only initialization */
93 if ((width
= sc_width ()) < WIDTH
/2)
95 else if (width
> MAXSCANL
)
98 dat
[3] = slwidth
= width
;
99 scanl
= (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH
* (slwidth
+ 2) );
103 /* Compile format string */
104 ncomps
= fmt_compile (nfs
, &fmt
) + 1;
109 FINDCOMP(bodycomp
, "body");
110 FINDCOMP(datecomp
, "date");
111 FINDCOMP(cptr
, "folder");
113 cptr
->c_text
= folder
;
114 FINDCOMP(cptr
, "encrypted");
116 if ((cptr
= (struct comp
*) calloc (1, sizeof(*cptr
)))) {
117 cptr
->c_name
= "encrypted";
118 cptr
->c_next
= wantcomp
[i
= CHASH (cptr
->c_name
)];
122 FINDCOMP (cptr
, "dtimenow");
124 cptr
->c_text
= getcpy(dtimenow (0));
125 nxtbuf
= compbuffers
= (char **) calloc((size_t) ncomps
, sizeof(char *));
127 adios (NULL
, "unable to allocate component buffers");
128 used_buf
= (struct comp
**) calloc((size_t) (ncomps
+1),
129 sizeof(struct comp
*));
130 if (used_buf
== NULL
)
131 adios (NULL
, "unable to allocate component buffer stack");
132 used_buf
+= ncomps
+1; *--used_buf
= 0;
133 rlwidth
= bodycomp
&& (width
> SBUFSIZ
) ? width
: SBUFSIZ
;
134 for (i
= ncomps
; i
--; )
135 *nxtbuf
++ = mh_xmalloc(rlwidth
);
139 * each-message initialization
141 nxtbuf
= compbuffers
;
144 dat
[0] = innum
? innum
: outnum
;
149 * Get the first field. If the message is non-empty
150 * and we're doing an "inc", open the output file.
152 if ((state
= m_getfld (FLD
, name
, tmpbuf
, rlwidth
, inb
)) == FILEEOF
) {
154 advise("read", "unable to"); /* "read error" */
163 scnmsg
= m_name (outnum
);
164 if (*scnmsg
== '?') /* msg num out of range */
167 scnmsg
= "/dev/null";
169 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
170 adios (scnmsg
, "unable to write");
173 /* scan - main loop */
174 for (compnum
= 1; ; state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
)) {
181 if ( putc (':', scnout
) == EOF
) DIEWRERR();
185 * if we're interested in this component, save a pointer
186 * to the component text, then start using our next free
187 * buffer as the component temp buffer (buffer switching
188 * saves an extra copy of the component text).
190 if ((cptr
= wantcomp
[CHASH(name
)])) {
192 if (!mh_strcasecmp(name
, cptr
->c_name
)) {
193 if (! cptr
->c_text
) {
194 cptr
->c_text
= tmpbuf
;
195 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
206 } while ((cptr
= cptr
->c_next
));
209 while (state
== FLDPLUS
) {
210 state
= m_getfld (state
, name
, tmpbuf
, rlwidth
, inb
);
219 state
= FILEEOF
; /* stop now if scan cmd */
222 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
225 * performance hack: some people like to run "inc" on
226 * things like net.sources or large digests. We do a
227 * copy directly into the output buffer rather than
228 * going through an intermediate buffer.
230 * We need the amount of data m_getfld found & don't
231 * want to do a strlen on the long buffer so there's
232 * a hack in m_getfld to save the amount of data it
233 * returned in the global "msg_count".
236 while (state
== BODY
) {
238 if (scnout
->_IO_write_ptr
== scnout
->_IO_write_end
) {
239 #elif defined(__DragonFly__)
240 if (((struct __FILE_public
*)scnout
)->_w
<= 0) {
242 if (scnout
->_cnt
<= 0) {
244 if (fflush(scnout
) == EOF
)
248 state
= m_getfld(state
, name
, scnout
->_IO_write_ptr
,
249 (long)scnout
->_IO_write_ptr
-(long)scnout
->_IO_write_end
, inb
);
250 scnout
->_IO_write_ptr
+= msg_count
;
251 #elif defined(__DragonFly__)
252 state
= m_getfld( state
, name
, ((struct __FILE_public
*)scnout
)->_p
, -(((struct __FILE_public
*)scnout
)->_w
), inb
);
253 ((struct __FILE_public
*)scnout
)->_w
-= msg_count
;
254 ((struct __FILE_public
*)scnout
)->_p
+= msg_count
;
256 state
= m_getfld( state
, name
, scnout
->_ptr
, -(scnout
->_cnt
), inb
);
257 scnout
->_cnt
-= msg_count
;
258 scnout
->_ptr
+= msg_count
;
266 innum
? "??Format error (message %d) in "
267 : "??Format error in ",
268 outnum
? outnum
: innum
);
269 fprintf (stderr
, "component %d\n", compnum
);
272 FPUTS ("\n\nBAD MSG:\n");
274 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
284 adios (NULL
, "getfld() returned %d", state
);
289 * format and output the scan line.
293 advise("read", "unable to"); /* "read error" */
297 /* Save and restore buffer so we don't trash our dynamic pool! */
299 saved_c_text
= bodycomp
->c_text
;
300 bodycomp
->c_text
= tmpbuf
;
307 dat
[2] = ftell(scnout
);
308 if (dat
[2] == EOF
) DIEWRERR();
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
|= CF_DATEFAB
|CF_TRUE
;
327 datecomp
->c_flags
&= ~CF_DATEFAB
;
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
))
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
)