]>
diplodocus.org Git - nmh/blob - uip/show.c
8b749f4089287e8295868fe50e89ebd11aea23b7
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.
9 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
18 #include "sbr/m_maildir.h"
20 #define SHOW_SWITCHES \
21 X("checkmime", 0, CHECKMIMESW) \
22 X("nocheckmime", 0, NOCHECKMIMESW) \
23 X("header", 0, HEADSW) \
24 X("noheader", 0, NHEADSW) \
25 X("form formfile", 0, FORMSW) \
26 X("moreproc program", 0, PROGSW) \
27 X("nomoreproc", 0, NPROGSW) \
28 X("length lines", 0, LENSW) \
29 X("width columns", 0, WIDTHSW) \
30 X("showproc program", 0, SHOWSW) \
31 X("showmimeproc program", 0, SHOWMIMESW) \
32 X("noshowproc", 0, NSHOWSW) \
33 X("draft", 0, DRFTSW) \
34 X("file file", -4, FILESW) /* interface from showfile */ \
35 X("fmtproc program", 0, FMTPROCSW) \
36 X("nofmtproc", 0, NFMTPROCSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 * switches for mhlproc \
42 X("concat", 0, CONCATSW) \
43 X("noconcat", 0, NCONCATSW) \
45 * switches for mhshow \
47 X("part number", 0, PARTSW) \
48 X("type content", 0, TYPESW) \
49 X("prefer content", 0, PREFERSW) \
50 X("markform file", 0, MARKFORMSW) \
51 X("rcache policy", 0, RCACHESW) \
52 X("wcache policy", 0, WCACHESW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(SHOW
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
65 static int is_nontext(char *);
73 main (int argc
, char **argv
)
78 bool checkmime
= true;
80 int isdf
= 0, mode
= SHOW
, msgnum
;
81 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
82 char buf
[BUFSIZ
], **argp
, **arguments
;
83 struct msgs
*mp
= NULL
;
84 struct msgs_array msgs
= { 0, 0, NULL
};
85 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
87 if (nmh_init(argv
[0], true, true)) { return 1; }
89 if (!strcasecmp (invo_name
, "next")) {
91 } else if (!strcasecmp (invo_name
, "prev")) {
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
106 goto non_mhl_switches
;
113 /* mhl can't handle these, so keep them separate. */
114 app_msgarg(&non_mhl_vec
, --cp
);
120 app_msgarg(&vec
, --cp
);
124 snprintf (buf
, sizeof(buf
),
125 "%s [+folder] %s[switches] [switches for showproc]",
126 invo_name
, mode
== SHOW
? "[msgs] ": "");
127 print_help (buf
, switches
, 1);
130 print_version(invo_name
);
135 die("only one file at a time!");
140 die( "usage: %s [+folder] [switches] [switches for showproc]",
146 die("only one file at a time!");
147 if (!(cp
= *argp
++) || *cp
== '-')
148 die("missing argument to %s", argp
[-2]);
149 file
= path (cp
, TFILE
);
153 app_msgarg(&vec
, --cp
);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 die("missing argument to %s", argp
[-2]);
156 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
169 app_msgarg(&vec
, --cp
);
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
172 app_msgarg(&vec
, cp
);
176 if (!(showproc
= *argp
++) || *showproc
== '-')
177 die("missing argument to %s", argp
[-2]);
185 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
186 die("missing argument to %s", argp
[-2]);
197 if (*cp
== '+' || *cp
== '@') {
199 die("only one folder at a time!");
200 folder
= pluspath (cp
);
204 app_msgarg(&msgs
, cp
);
208 if (!context_find ("path"))
209 free (path ("./", TFOLDER
));
211 if (draftsw
|| file
) {
213 die("only one file at a time!");
215 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
217 app_msgarg(&vec
, file
);
225 app_msgarg(&msgs
, "next");
228 app_msgarg(&msgs
, "prev");
231 app_msgarg(&msgs
, "cur");
237 folder
= getfolder (1);
238 maildir
= m_maildir (folder
);
240 if (chdir (maildir
) == NOTOK
)
241 adios (maildir
, "unable to change directory to");
243 /* read folder and create message structure */
244 if (!(mp
= folder_read (folder
, 1)))
245 die("unable to read folder %s", folder
);
247 /* check for empty folder */
249 die("no messages in %s", folder
);
251 /* parse all the message ranges/sequences and set SELECTED */
252 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
253 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
257 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
258 * since we will use that as a tag to know which messages
259 * to remove from the "unseen" sequence.
261 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
262 if (is_selected(mp
, msgnum
))
263 set_unseen (mp
, msgnum
);
265 seq_setprev (mp
); /* set the Previous-Sequence */
266 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
268 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
269 if (is_selected(mp
, msgnum
))
270 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
272 seq_setcur (mp
, mp
->hghsel
); /* update current message */
273 seq_save (mp
); /* synchronize sequences */
274 context_replace (pfolder
, folder
); /* update current folder */
275 context_save (); /* save the context file */
280 * Decide which "proc" to use
285 /* check if any messages are non-text MIME messages */
286 if (! mime
&& checkmime
) {
287 if (!draftsw
&& !file
) {
288 /* loop through selected messages and check for MIME */
289 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
290 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
295 /* check the file or draft for MIME */
296 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
308 if (folder
&& !draftsw
&& !file
)
309 setenv("mhfolder", folder
, 1);
311 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
313 if (headersw
&& vec
.size
== 1)
314 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
316 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
318 if (headersw
&& vec
.size
== 1)
319 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
321 /* If "mhl", then run it internally */
322 argsplit_insert(&vec
, "mhl", &program
);
323 app_msgarg(&vec
, NULL
);
324 mhl (vec
.size
, vec
.msgs
);
331 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
332 if (draftsw
|| file
) {
333 /* Insert the switch before the filename. */
334 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
335 vec
.msgs
[vec
.size
- 2] = *mp
;
337 app_msgarg(&vec
, *mp
);
341 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
342 /* Add "-file" if showing file or draft, */
343 if (draftsw
|| file
) {
344 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
345 vec
.msgs
[vec
.size
- 2] = "-file";
347 /* and add -show for backward compatibility */
348 app_msgarg(&vec
, "-show");
349 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
350 /* If "mhshow", add "-file" if showing file or draft. */
351 if (draftsw
|| file
) {
352 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
353 vec
.msgs
[vec
.size
- 2] = "-file";
356 if (headersw
&& vec
.size
== 1)
357 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
363 argsplit_insert(&vec
, proc
, &program
);
364 app_msgarg(&vec
, NULL
);
365 execvp (program
, vec
.msgs
);
366 adios (proc
, "unable to exec");
367 return 0; /* dead code to satisfy the compiler */
372 * Check if a message or file contains any non-text parts
375 is_nontext (char *msgnam
)
379 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
381 m_getfld_state_t gstate
;
383 if ((fp
= fopen (msgnam
, "r")) == NULL
)
385 gstate
= m_getfld_state_init(fp
);
387 int bufsz
= sizeof buf
;
388 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
392 * Check Content-Type field
394 if (!strcasecmp (name
, TYPE_FIELD
)) {
398 cp
= mh_xstrdup(buf
);
399 while (state
== FLDPLUS
) {
401 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
408 for (; isspace ((unsigned char) *bp
); bp
++)
413 for (bp
++, i
= 0;;) {
443 for (dp
= bp
; istoken (*dp
); dp
++)
450 if ((result
= (strcasecmp (bp
, "plain") != 0)))
453 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
456 if ((result
= !uprf (dp
, "charset")))
458 dp
+= LEN("charset");
459 while (isspace ((unsigned char) *dp
))
463 while (isspace ((unsigned char) *dp
))
466 if ((bp
= strchr(++dp
, '"')))
469 for (bp
= dp
; *bp
; bp
++)
470 if (!istoken (*bp
)) {
476 /* Default character set */
479 /* Check the character set */
480 result
= !check_charset (dp
, strlen (dp
));
482 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
493 m_getfld_state_destroy (&gstate
);
500 * Check Content-Transfer-Encoding field
502 if (!strcasecmp (name
, ENCODING_FIELD
)) {
503 cp
= mh_xstrdup(buf
);
504 while (state
== FLDPLUS
) {
506 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
509 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
511 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
514 result
= (strcasecmp (bp
, "7bit")
515 && strcasecmp (bp
, "8bit")
516 && strcasecmp (bp
, "binary"));
521 m_getfld_state_destroy (&gstate
);
528 * Just skip the rest of this header
529 * field and go to next one.
531 while (state
== FLDPLUS
) {
533 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
538 * We've passed the message header,
539 * so message is just text.
543 m_getfld_state_destroy (&gstate
);