]>
diplodocus.org Git - nmh/blob - uip/show.c
99a660813d79d1f490df25448b974acf4e37281d
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/getarguments.h"
10 #include "sbr/seq_setunseen.h"
11 #include "sbr/seq_setprev.h"
12 #include "sbr/seq_setcur.h"
13 #include "sbr/seq_save.h"
14 #include "sbr/smatch.h"
15 #include "sbr/r1bindex.h"
17 #include "sbr/check_charset.h"
18 #include "sbr/m_draft.h"
19 #include "sbr/m_convert.h"
20 #include "sbr/getfolder.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_replace.h"
24 #include "sbr/context_find.h"
25 #include "sbr/ambigsw.h"
27 #include "sbr/print_version.h"
28 #include "sbr/print_help.h"
29 #include "sbr/arglist.h"
30 #include "sbr/error.h"
34 #include "sbr/m_maildir.h"
36 #define SHOW_SWITCHES \
37 X("checkmime", 0, CHECKMIMESW) \
38 X("nocheckmime", 0, NOCHECKMIMESW) \
39 X("header", 0, HEADSW) \
40 X("noheader", 0, NHEADSW) \
41 X("form formfile", 0, FORMSW) \
42 X("moreproc program", 0, PROGSW) \
43 X("nomoreproc", 0, NPROGSW) \
44 X("length lines", 0, LENSW) \
45 X("width columns", 0, WIDTHSW) \
46 X("showproc program", 0, SHOWSW) \
47 X("showmimeproc program", 0, SHOWMIMESW) \
48 X("noshowproc", 0, NSHOWSW) \
49 X("draft", 0, DRFTSW) \
50 X("file file", -4, FILESW) /* interface from showfile */ \
51 X("fmtproc program", 0, FMTPROCSW) \
52 X("nofmtproc", 0, NFMTPROCSW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
56 * switches for mhlproc \
58 X("concat", 0, CONCATSW) \
59 X("noconcat", 0, NCONCATSW) \
61 * switches for mhshow \
63 X("part number", 0, PARTSW) \
64 X("type content", 0, TYPESW) \
65 X("prefer content", 0, PREFERSW) \
66 X("markform file", 0, MARKFORMSW) \
67 X("rcache policy", 0, RCACHESW) \
68 X("wcache policy", 0, WCACHESW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(SHOW
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
81 static int is_nontext(char *);
89 main (int argc
, char **argv
)
94 bool checkmime
= true;
96 int isdf
= 0, mode
= SHOW
, msgnum
;
97 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
98 char buf
[BUFSIZ
], **argp
, **arguments
;
99 struct msgs
*mp
= NULL
;
100 struct msgs_array msgs
= { 0, 0, NULL
};
101 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
103 if (nmh_init(argv
[0], true, true)) { return 1; }
105 if (!strcasecmp (invo_name
, "next")) {
107 } else if (!strcasecmp (invo_name
, "prev")) {
110 arguments
= getarguments (invo_name
, argc
, argv
, 1);
113 while ((cp
= *argp
++)) {
115 switch (smatch (++cp
, switches
)) {
117 ambigsw (cp
, switches
);
122 goto non_mhl_switches
;
129 /* mhl can't handle these, so keep them separate. */
130 app_msgarg(&non_mhl_vec
, --cp
);
136 app_msgarg(&vec
, --cp
);
140 snprintf (buf
, sizeof(buf
),
141 "%s [+folder] %s[switches] [switches for showproc]",
142 invo_name
, mode
== SHOW
? "[msgs] ": "");
143 print_help (buf
, switches
, 1);
146 print_version(invo_name
);
151 die("only one file at a time!");
156 die( "usage: %s [+folder] [switches] [switches for showproc]",
162 die("only one file at a time!");
163 if (!(cp
= *argp
++) || *cp
== '-')
164 die("missing argument to %s", argp
[-2]);
165 file
= path (cp
, TFILE
);
169 app_msgarg(&vec
, --cp
);
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
172 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
185 app_msgarg(&vec
, --cp
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 app_msgarg(&vec
, cp
);
192 if (!(showproc
= *argp
++) || *showproc
== '-')
193 die("missing argument to %s", argp
[-2]);
201 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
202 die("missing argument to %s", argp
[-2]);
213 if (*cp
== '+' || *cp
== '@') {
215 die("only one folder at a time!");
216 folder
= pluspath (cp
);
220 app_msgarg(&msgs
, cp
);
224 if (!context_find ("path"))
225 free (path ("./", TFOLDER
));
227 if (draftsw
|| file
) {
229 die("only one file at a time!");
231 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
233 app_msgarg(&vec
, file
);
241 app_msgarg(&msgs
, "next");
244 app_msgarg(&msgs
, "prev");
247 app_msgarg(&msgs
, "cur");
253 folder
= getfolder (1);
254 maildir
= m_maildir (folder
);
256 if (chdir (maildir
) == NOTOK
)
257 adios (maildir
, "unable to change directory to");
259 /* read folder and create message structure */
260 if (!(mp
= folder_read (folder
, 1)))
261 die("unable to read folder %s", folder
);
263 /* check for empty folder */
265 die("no messages in %s", folder
);
267 /* parse all the message ranges/sequences and set SELECTED */
268 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
269 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
273 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
274 * since we will use that as a tag to know which messages
275 * to remove from the "unseen" sequence.
277 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
278 if (is_selected(mp
, msgnum
))
279 set_unseen (mp
, msgnum
);
281 seq_setprev (mp
); /* set the Previous-Sequence */
282 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
284 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
285 if (is_selected(mp
, msgnum
))
286 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
288 seq_setcur (mp
, mp
->hghsel
); /* update current message */
289 seq_save (mp
); /* synchronize sequences */
290 context_replace (pfolder
, folder
); /* update current folder */
291 context_save (); /* save the context file */
296 * Decide which "proc" to use
301 /* check if any messages are non-text MIME messages */
302 if (! mime
&& checkmime
) {
303 if (!draftsw
&& !file
) {
304 /* loop through selected messages and check for MIME */
305 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
306 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
311 /* check the file or draft for MIME */
312 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
324 if (folder
&& !draftsw
&& !file
)
325 setenv("mhfolder", folder
, 1);
327 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
329 if (headersw
&& vec
.size
== 1)
330 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
332 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
334 if (headersw
&& vec
.size
== 1)
335 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
337 /* If "mhl", then run it internally */
338 argsplit_insert(&vec
, "mhl", &program
);
339 app_msgarg(&vec
, NULL
);
340 mhl (vec
.size
, vec
.msgs
);
347 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
348 if (draftsw
|| file
) {
349 /* Insert the switch before the filename. */
350 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
351 vec
.msgs
[vec
.size
- 2] = *mp
;
353 app_msgarg(&vec
, *mp
);
357 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
358 /* Add "-file" if showing file or draft, */
359 if (draftsw
|| file
) {
360 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
361 vec
.msgs
[vec
.size
- 2] = "-file";
363 /* and add -show for backward compatibility */
364 app_msgarg(&vec
, "-show");
365 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
366 /* If "mhshow", add "-file" if showing file or draft. */
367 if (draftsw
|| file
) {
368 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
369 vec
.msgs
[vec
.size
- 2] = "-file";
372 if (headersw
&& vec
.size
== 1)
373 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
379 argsplit_insert(&vec
, proc
, &program
);
380 app_msgarg(&vec
, NULL
);
381 execvp (program
, vec
.msgs
);
382 adios (proc
, "unable to exec");
383 return 0; /* dead code to satisfy the compiler */
388 * Check if a message or file contains any non-text parts
391 is_nontext (char *msgnam
)
395 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
397 m_getfld_state_t gstate
;
399 if ((fp
= fopen (msgnam
, "r")) == NULL
)
401 gstate
= m_getfld_state_init(fp
);
403 int bufsz
= sizeof buf
;
404 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
408 * Check Content-Type field
410 if (!strcasecmp (name
, TYPE_FIELD
)) {
414 cp
= mh_xstrdup(buf
);
415 while (state
== FLDPLUS
) {
417 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
424 for (; isspace ((unsigned char) *bp
); bp
++)
429 for (bp
++, i
= 0;;) {
459 for (dp
= bp
; istoken (*dp
); dp
++)
466 if ((result
= (strcasecmp (bp
, "plain") != 0)))
469 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
472 if ((result
= !uprf (dp
, "charset")))
474 dp
+= LEN("charset");
475 while (isspace ((unsigned char) *dp
))
479 while (isspace ((unsigned char) *dp
))
482 if ((bp
= strchr(++dp
, '"')))
485 for (bp
= dp
; *bp
; bp
++)
486 if (!istoken (*bp
)) {
492 /* Default character set */
495 /* Check the character set */
496 result
= !check_charset (dp
, strlen (dp
));
498 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
509 m_getfld_state_destroy (&gstate
);
516 * Check Content-Transfer-Encoding field
518 if (!strcasecmp (name
, ENCODING_FIELD
)) {
519 cp
= mh_xstrdup(buf
);
520 while (state
== FLDPLUS
) {
522 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
525 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
527 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
530 result
= (strcasecmp (bp
, "7bit")
531 && strcasecmp (bp
, "8bit")
532 && strcasecmp (bp
, "binary"));
537 m_getfld_state_destroy (&gstate
);
544 * Just skip the rest of this header
545 * field and go to next one.
547 while (state
== FLDPLUS
) {
549 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
554 * We've passed the message header,
555 * so message is just text.
559 m_getfld_state_destroy (&gstate
);