]>
diplodocus.org Git - nmh/blob - uip/show.c
9229f51e8d2bc0d41f9d414f09e07a854681b23c
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/print_help.h"
10 #include "sbr/arglist.h"
11 #include "sbr/error.h"
15 #include "sbr/m_maildir.h"
17 #define SHOW_SWITCHES \
18 X("checkmime", 0, CHECKMIMESW) \
19 X("nocheckmime", 0, NOCHECKMIMESW) \
20 X("header", 0, HEADSW) \
21 X("noheader", 0, NHEADSW) \
22 X("form formfile", 0, FORMSW) \
23 X("moreproc program", 0, PROGSW) \
24 X("nomoreproc", 0, NPROGSW) \
25 X("length lines", 0, LENSW) \
26 X("width columns", 0, WIDTHSW) \
27 X("showproc program", 0, SHOWSW) \
28 X("showmimeproc program", 0, SHOWMIMESW) \
29 X("noshowproc", 0, NSHOWSW) \
30 X("draft", 0, DRFTSW) \
31 X("file file", -4, FILESW) /* interface from showfile */ \
32 X("fmtproc program", 0, FMTPROCSW) \
33 X("nofmtproc", 0, NFMTPROCSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
37 * switches for mhlproc \
39 X("concat", 0, CONCATSW) \
40 X("noconcat", 0, NCONCATSW) \
42 * switches for mhshow \
44 X("part number", 0, PARTSW) \
45 X("type content", 0, TYPESW) \
46 X("prefer content", 0, PREFERSW) \
47 X("markform file", 0, MARKFORMSW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(SHOW
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
62 static int is_nontext(char *);
70 main (int argc
, char **argv
)
75 bool checkmime
= true;
77 int isdf
= 0, mode
= SHOW
, msgnum
;
78 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
79 char buf
[BUFSIZ
], **argp
, **arguments
;
80 struct msgs
*mp
= NULL
;
81 struct msgs_array msgs
= { 0, 0, NULL
};
82 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
84 if (nmh_init(argv
[0], true, true)) { return 1; }
86 if (!strcasecmp (invo_name
, "next")) {
88 } else if (!strcasecmp (invo_name
, "prev")) {
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
103 goto non_mhl_switches
;
110 /* mhl can't handle these, so keep them separate. */
111 app_msgarg(&non_mhl_vec
, --cp
);
117 app_msgarg(&vec
, --cp
);
121 snprintf (buf
, sizeof(buf
),
122 "%s [+folder] %s[switches] [switches for showproc]",
123 invo_name
, mode
== SHOW
? "[msgs] ": "");
124 print_help (buf
, switches
, 1);
127 print_version(invo_name
);
132 die("only one file at a time!");
137 die( "usage: %s [+folder] [switches] [switches for showproc]",
143 die("only one file at a time!");
144 if (!(cp
= *argp
++) || *cp
== '-')
145 die("missing argument to %s", argp
[-2]);
146 file
= path (cp
, TFILE
);
150 app_msgarg(&vec
, --cp
);
151 if (!(cp
= *argp
++) || *cp
== '-')
152 die("missing argument to %s", argp
[-2]);
153 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
166 app_msgarg(&vec
, --cp
);
167 if (!(cp
= *argp
++) || *cp
== '-')
168 die("missing argument to %s", argp
[-2]);
169 app_msgarg(&vec
, cp
);
173 if (!(showproc
= *argp
++) || *showproc
== '-')
174 die("missing argument to %s", argp
[-2]);
182 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
183 die("missing argument to %s", argp
[-2]);
194 if (*cp
== '+' || *cp
== '@') {
196 die("only one folder at a time!");
197 folder
= pluspath (cp
);
201 app_msgarg(&msgs
, cp
);
205 if (!context_find ("path"))
206 free (path ("./", TFOLDER
));
208 if (draftsw
|| file
) {
210 die("only one file at a time!");
212 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
214 app_msgarg(&vec
, file
);
222 app_msgarg(&msgs
, "next");
225 app_msgarg(&msgs
, "prev");
228 app_msgarg(&msgs
, "cur");
234 folder
= getfolder (1);
235 maildir
= m_maildir (folder
);
237 if (chdir (maildir
) == NOTOK
)
238 adios (maildir
, "unable to change directory to");
240 /* read folder and create message structure */
241 if (!(mp
= folder_read (folder
, 1)))
242 die("unable to read folder %s", folder
);
244 /* check for empty folder */
246 die("no messages in %s", folder
);
248 /* parse all the message ranges/sequences and set SELECTED */
249 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
250 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
254 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
255 * since we will use that as a tag to know which messages
256 * to remove from the "unseen" sequence.
258 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
259 if (is_selected(mp
, msgnum
))
260 set_unseen (mp
, msgnum
);
262 seq_setprev (mp
); /* set the Previous-Sequence */
263 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
265 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
266 if (is_selected(mp
, msgnum
))
267 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
269 seq_setcur (mp
, mp
->hghsel
); /* update current message */
270 seq_save (mp
); /* synchronize sequences */
271 context_replace (pfolder
, folder
); /* update current folder */
272 context_save (); /* save the context file */
277 * Decide which "proc" to use
282 /* check if any messages are non-text MIME messages */
283 if (! mime
&& checkmime
) {
284 if (!draftsw
&& !file
) {
285 /* loop through selected messages and check for MIME */
286 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
287 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
292 /* check the file or draft for MIME */
293 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
305 if (folder
&& !draftsw
&& !file
)
306 setenv("mhfolder", folder
, 1);
308 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
310 if (headersw
&& vec
.size
== 1)
311 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
313 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
315 if (headersw
&& vec
.size
== 1)
316 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
318 /* If "mhl", then run it internally */
319 argsplit_insert(&vec
, "mhl", &program
);
320 app_msgarg(&vec
, NULL
);
321 mhl (vec
.size
, vec
.msgs
);
328 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
329 if (draftsw
|| file
) {
330 /* Insert the switch before the filename. */
331 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
332 vec
.msgs
[vec
.size
- 2] = *mp
;
334 app_msgarg(&vec
, *mp
);
338 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
339 /* Add "-file" if showing file or draft, */
340 if (draftsw
|| file
) {
341 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
342 vec
.msgs
[vec
.size
- 2] = "-file";
344 /* and add -show for backward compatibility */
345 app_msgarg(&vec
, "-show");
346 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
347 /* If "mhshow", add "-file" if showing file or draft. */
348 if (draftsw
|| file
) {
349 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
350 vec
.msgs
[vec
.size
- 2] = "-file";
353 if (headersw
&& vec
.size
== 1)
354 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
360 argsplit_insert(&vec
, proc
, &program
);
361 app_msgarg(&vec
, NULL
);
362 execvp (program
, vec
.msgs
);
363 adios (proc
, "unable to exec");
364 return 0; /* dead code to satisfy the compiler */
369 * Check if a message or file contains any non-text parts
372 is_nontext (char *msgnam
)
376 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
378 m_getfld_state_t gstate
;
380 if ((fp
= fopen (msgnam
, "r")) == NULL
)
382 gstate
= m_getfld_state_init(fp
);
384 int bufsz
= sizeof buf
;
385 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
389 * Check Content-Type field
391 if (!strcasecmp (name
, TYPE_FIELD
)) {
395 cp
= mh_xstrdup(buf
);
396 while (state
== FLDPLUS
) {
398 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
405 for (; isspace ((unsigned char) *bp
); bp
++)
410 for (bp
++, i
= 0;;) {
440 for (dp
= bp
; istoken (*dp
); dp
++)
447 if ((result
= (strcasecmp (bp
, "plain") != 0)))
450 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
453 if ((result
= !uprf (dp
, "charset")))
455 dp
+= LEN("charset");
456 while (isspace ((unsigned char) *dp
))
460 while (isspace ((unsigned char) *dp
))
463 if ((bp
= strchr(++dp
, '"')))
466 for (bp
= dp
; *bp
; bp
++)
467 if (!istoken (*bp
)) {
473 /* Default character set */
476 /* Check the character set */
477 result
= !check_charset (dp
, strlen (dp
));
479 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
490 m_getfld_state_destroy (&gstate
);
497 * Check Content-Transfer-Encoding field
499 if (!strcasecmp (name
, ENCODING_FIELD
)) {
500 cp
= mh_xstrdup(buf
);
501 while (state
== FLDPLUS
) {
503 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
506 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
508 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
511 result
= (strcasecmp (bp
, "7bit")
512 && strcasecmp (bp
, "8bit")
513 && strcasecmp (bp
, "binary"));
518 m_getfld_state_destroy (&gstate
);
525 * Just skip the rest of this header
526 * field and go to next one.
528 while (state
== FLDPLUS
) {
530 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
535 * We've passed the message header,
536 * so message is just text.
540 m_getfld_state_destroy (&gstate
);