]>
diplodocus.org Git - nmh/blob - uip/show.c
c6429acc6b77dc2824213f681c74ab61186813d5
1 /* show.c -- show/list messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
13 #include "sbr/m_maildir.h"
15 #define SHOW_SWITCHES \
16 X("checkmime", 0, CHECKMIMESW) \
17 X("nocheckmime", 0, NOCHECKMIMESW) \
18 X("header", 0, HEADSW) \
19 X("noheader", 0, NHEADSW) \
20 X("form formfile", 0, FORMSW) \
21 X("moreproc program", 0, PROGSW) \
22 X("nomoreproc", 0, NPROGSW) \
23 X("length lines", 0, LENSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("showproc program", 0, SHOWSW) \
26 X("showmimeproc program", 0, SHOWMIMESW) \
27 X("noshowproc", 0, NSHOWSW) \
28 X("draft", 0, DRFTSW) \
29 X("file file", -4, FILESW) /* interface from showfile */ \
30 X("fmtproc program", 0, FMTPROCSW) \
31 X("nofmtproc", 0, NFMTPROCSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 * switches for mhlproc \
37 X("concat", 0, CONCATSW) \
38 X("noconcat", 0, NCONCATSW) \
40 * switches for mhshow \
42 X("part number", 0, PARTSW) \
43 X("type content", 0, TYPESW) \
44 X("prefer content", 0, PREFERSW) \
45 X("markform file", 0, MARKFORMSW) \
46 X("rcache policy", 0, RCACHESW) \
47 X("wcache policy", 0, WCACHESW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(SHOW
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
60 static int is_nontext(char *);
68 main (int argc
, char **argv
)
73 bool checkmime
= true;
75 int isdf
= 0, mode
= SHOW
, msgnum
;
76 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
77 char buf
[BUFSIZ
], **argp
, **arguments
;
78 struct msgs
*mp
= NULL
;
79 struct msgs_array msgs
= { 0, 0, NULL
};
80 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
82 if (nmh_init(argv
[0], true, true)) { return 1; }
84 if (!strcasecmp (invo_name
, "next")) {
86 } else if (!strcasecmp (invo_name
, "prev")) {
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
92 while ((cp
= *argp
++)) {
94 switch (smatch (++cp
, switches
)) {
96 ambigsw (cp
, switches
);
101 goto non_mhl_switches
;
108 /* mhl can't handle these, so keep them separate. */
109 app_msgarg(&non_mhl_vec
, --cp
);
115 app_msgarg(&vec
, --cp
);
119 snprintf (buf
, sizeof(buf
),
120 "%s [+folder] %s[switches] [switches for showproc]",
121 invo_name
, mode
== SHOW
? "[msgs] ": "");
122 print_help (buf
, switches
, 1);
125 print_version(invo_name
);
130 die("only one file at a time!");
135 die( "usage: %s [+folder] [switches] [switches for showproc]",
141 die("only one file at a time!");
142 if (!(cp
= *argp
++) || *cp
== '-')
143 die("missing argument to %s", argp
[-2]);
144 file
= path (cp
, TFILE
);
148 app_msgarg(&vec
, --cp
);
149 if (!(cp
= *argp
++) || *cp
== '-')
150 die("missing argument to %s", argp
[-2]);
151 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
164 app_msgarg(&vec
, --cp
);
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
167 app_msgarg(&vec
, cp
);
171 if (!(showproc
= *argp
++) || *showproc
== '-')
172 die("missing argument to %s", argp
[-2]);
180 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
181 die("missing argument to %s", argp
[-2]);
192 if (*cp
== '+' || *cp
== '@') {
194 die("only one folder at a time!");
195 folder
= pluspath (cp
);
199 app_msgarg(&msgs
, cp
);
203 if (!context_find ("path"))
204 free (path ("./", TFOLDER
));
206 if (draftsw
|| file
) {
208 die("only one file at a time!");
210 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
212 app_msgarg(&vec
, file
);
220 app_msgarg(&msgs
, "next");
223 app_msgarg(&msgs
, "prev");
226 app_msgarg(&msgs
, "cur");
232 folder
= getfolder (1);
233 maildir
= m_maildir (folder
);
235 if (chdir (maildir
) == NOTOK
)
236 adios (maildir
, "unable to change directory to");
238 /* read folder and create message structure */
239 if (!(mp
= folder_read (folder
, 1)))
240 die("unable to read folder %s", folder
);
242 /* check for empty folder */
244 die("no messages in %s", folder
);
246 /* parse all the message ranges/sequences and set SELECTED */
247 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
248 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
252 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
253 * since we will use that as a tag to know which messages
254 * to remove from the "unseen" sequence.
256 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
257 if (is_selected(mp
, msgnum
))
258 set_unseen (mp
, msgnum
);
260 seq_setprev (mp
); /* set the Previous-Sequence */
261 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
263 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
264 if (is_selected(mp
, msgnum
))
265 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
267 seq_setcur (mp
, mp
->hghsel
); /* update current message */
268 seq_save (mp
); /* synchronize sequences */
269 context_replace (pfolder
, folder
); /* update current folder */
270 context_save (); /* save the context file */
275 * Decide which "proc" to use
280 /* check if any messages are non-text MIME messages */
281 if (! mime
&& checkmime
) {
282 if (!draftsw
&& !file
) {
283 /* loop through selected messages and check for MIME */
284 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
285 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
290 /* check the file or draft for MIME */
291 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
303 if (folder
&& !draftsw
&& !file
)
304 setenv("mhfolder", folder
, 1);
306 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
308 if (headersw
&& vec
.size
== 1)
309 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
311 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
313 if (headersw
&& vec
.size
== 1)
314 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
316 /* If "mhl", then run it internally */
317 argsplit_insert(&vec
, "mhl", &program
);
318 app_msgarg(&vec
, NULL
);
319 mhl (vec
.size
, vec
.msgs
);
326 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
327 if (draftsw
|| file
) {
328 /* Insert the switch before the filename. */
329 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
330 vec
.msgs
[vec
.size
- 2] = *mp
;
332 app_msgarg(&vec
, *mp
);
336 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
337 /* Add "-file" if showing file or draft, */
338 if (draftsw
|| file
) {
339 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
340 vec
.msgs
[vec
.size
- 2] = "-file";
342 /* and add -show for backward compatibility */
343 app_msgarg(&vec
, "-show");
344 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
345 /* If "mhshow", add "-file" if showing file or draft. */
346 if (draftsw
|| file
) {
347 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
348 vec
.msgs
[vec
.size
- 2] = "-file";
351 if (headersw
&& vec
.size
== 1)
352 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
358 argsplit_insert(&vec
, proc
, &program
);
359 app_msgarg(&vec
, NULL
);
360 execvp (program
, vec
.msgs
);
361 adios (proc
, "unable to exec");
362 return 0; /* dead code to satisfy the compiler */
367 * Check if a message or file contains any non-text parts
370 is_nontext (char *msgnam
)
374 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
376 m_getfld_state_t gstate
;
378 if ((fp
= fopen (msgnam
, "r")) == NULL
)
380 gstate
= m_getfld_state_init(fp
);
382 int bufsz
= sizeof buf
;
383 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
387 * Check Content-Type field
389 if (!strcasecmp (name
, TYPE_FIELD
)) {
393 cp
= mh_xstrdup(buf
);
394 while (state
== FLDPLUS
) {
396 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
403 for (; isspace ((unsigned char) *bp
); bp
++)
408 for (bp
++, i
= 0;;) {
438 for (dp
= bp
; istoken (*dp
); dp
++)
445 if ((result
= (strcasecmp (bp
, "plain") != 0)))
448 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
451 if ((result
= !uprf (dp
, "charset")))
453 dp
+= LEN("charset");
454 while (isspace ((unsigned char) *dp
))
458 while (isspace ((unsigned char) *dp
))
461 if ((bp
= strchr(++dp
, '"')))
464 for (bp
= dp
; *bp
; bp
++)
465 if (!istoken (*bp
)) {
471 /* Default character set */
474 /* Check the character set */
475 result
= !check_charset (dp
, strlen (dp
));
477 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
488 m_getfld_state_destroy (&gstate
);
495 * Check Content-Transfer-Encoding field
497 if (!strcasecmp (name
, ENCODING_FIELD
)) {
498 cp
= mh_xstrdup(buf
);
499 while (state
== FLDPLUS
) {
501 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
504 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
506 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
509 result
= (strcasecmp (bp
, "7bit")
510 && strcasecmp (bp
, "8bit")
511 && strcasecmp (bp
, "binary"));
516 m_getfld_state_destroy (&gstate
);
523 * Just skip the rest of this header
524 * field and go to next one.
526 while (state
== FLDPLUS
) {
528 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
533 * We've passed the message header,
534 * so message is just text.
538 m_getfld_state_destroy (&gstate
);