]>
diplodocus.org Git - nmh/blob - uip/show.c
2 * show.c -- show/list messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 #define SHOW_SWITCHES \
14 X("checkmime", 0, CHECKMIMESW) \
15 X("nocheckmime", 0, NOCHECKMIMESW) \
16 X("header", 0, HEADSW) \
17 X("noheader", 0, NHEADSW) \
18 X("form formfile", 0, FORMSW) \
19 X("moreproc program", 0, PROGSW) \
20 X("nomoreproc", 0, NPROGSW) \
21 X("length lines", 0, LENSW) \
22 X("width columns", 0, WIDTHSW) \
23 X("showproc program", 0, SHOWSW) \
24 X("showmimeproc program", 0, SHOWMIMESW) \
25 X("noshowproc", 0, NSHOWSW) \
26 X("draft", 0, DRFTSW) \
27 X("file file", -4, FILESW) /* interface from showfile */ \
28 X("fmtproc program", 0, FMTPROCSW) \
29 X("nofmtproc", 0, NFMTPROCSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 * switches for mhlproc \
35 X("concat", 0, CONCATSW) \
36 X("noconcat", 0, NCONCATSW) \
38 * switches for mhshow \
40 X("part number", 0, PARTSW) \
41 X("type content", 0, TYPESW) \
42 X("prefer content", 0, PREFERSW) \
43 X("markform file", 0, MARKFORMSW) \
44 X("rcache policy", 0, RCACHESW) \
45 X("wcache policy", 0, WCACHESW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(SHOW
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
58 static int is_nontext(char *);
66 main (int argc
, char **argv
)
68 int draftsw
= 0, headersw
= 1;
69 int nshow
= 0, checkmime
= 1, mime
= 0;
70 int isdf
= 0, mode
= SHOW
, msgnum
;
71 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
72 char buf
[BUFSIZ
], **argp
, **arguments
;
73 struct msgs
*mp
= NULL
;
74 struct msgs_array msgs
= { 0, 0, NULL
};
75 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
77 if (nmh_init(argv
[0], 1)) { return 1; }
79 if (!strcasecmp (invo_name
, "next")) {
81 } else if (!strcasecmp (invo_name
, "prev")) {
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
96 goto non_mhl_switches
;
102 /* mhl can't handle these, so keep them separate. */
103 app_msgarg(&non_mhl_vec
, --cp
);
109 app_msgarg(&vec
, --cp
);
113 snprintf (buf
, sizeof(buf
),
114 "%s [+folder] %s[switches] [switches for showproc]",
115 invo_name
, mode
== SHOW
? "[msgs] ": "");
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
124 adios (NULL
, "only one file at a time!");
130 "usage: %s [+folder] [switches] [switches for showproc]",
136 adios (NULL
, "only one file at a time!");
137 if (!(cp
= *argp
++) || *cp
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
139 file
= path (cp
, TFILE
);
143 app_msgarg(&vec
, --cp
);
144 if (!(cp
= *argp
++) || *cp
== '-')
145 adios (NULL
, "missing argument to %s", argp
[-2]);
146 app_msgarg(&vec
, getcpy (etcpath(cp
)));
159 app_msgarg(&vec
, --cp
);
160 if (!(cp
= *argp
++) || *cp
== '-')
161 adios (NULL
, "missing argument to %s", argp
[-2]);
162 app_msgarg(&vec
, cp
);
166 if (!(showproc
= *argp
++) || *showproc
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
175 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
187 if (*cp
== '+' || *cp
== '@') {
189 adios (NULL
, "only one folder at a time!");
191 folder
= pluspath (cp
);
195 app_msgarg(&msgs
, cp
);
199 if (!context_find ("path"))
200 free (path ("./", TFOLDER
));
202 if (draftsw
|| file
) {
204 adios (NULL
, "only one file at a time!");
206 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
208 app_msgarg(&vec
, file
);
216 app_msgarg(&msgs
, "next");
219 app_msgarg(&msgs
, "prev");
222 app_msgarg(&msgs
, "cur");
228 folder
= getfolder (1);
229 maildir
= m_maildir (folder
);
231 if (chdir (maildir
) == NOTOK
)
232 adios (maildir
, "unable to change directory to");
234 /* read folder and create message structure */
235 if (!(mp
= folder_read (folder
, 1)))
236 adios (NULL
, "unable to read folder %s", folder
);
238 /* check for empty folder */
240 adios (NULL
, "no messages in %s", folder
);
242 /* parse all the message ranges/sequences and set SELECTED */
243 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
244 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
248 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
249 * since we will use that as a tag to know which messages
250 * to remove from the "unseen" sequence.
252 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
253 if (is_selected(mp
, msgnum
))
254 set_unseen (mp
, msgnum
);
256 seq_setprev (mp
); /* set the Previous-Sequence */
257 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
259 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
260 if (is_selected(mp
, msgnum
))
261 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
263 seq_setcur (mp
, mp
->hghsel
); /* update current message */
264 seq_save (mp
); /* synchronize sequences */
265 context_replace (pfolder
, folder
); /* update current folder */
266 context_save (); /* save the context file */
271 * Decide which "proc" to use
276 /* check if any messages are non-text MIME messages */
277 if (! mime
&& checkmime
) {
278 if (!draftsw
&& !file
) {
279 /* loop through selected messages and check for MIME */
280 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
281 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
286 /* check the file or draft for MIME */
287 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
299 if (folder
&& !draftsw
&& !file
)
300 m_putenv ("mhfolder", folder
);
302 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
304 if (headersw
&& vec
.size
== 1)
305 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
307 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
309 if (headersw
&& vec
.size
== 1)
310 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
312 /* If "mhl", then run it internally */
313 argsplit_insert(&vec
, "mhl", &program
);
314 app_msgarg(&vec
, NULL
);
315 mhl (vec
.size
, vec
.msgs
);
322 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
323 if (draftsw
|| file
) {
324 /* Insert the switch before the filename. */
325 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
326 vec
.msgs
[vec
.size
- 2] = *mp
;
328 app_msgarg(&vec
, *mp
);
332 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
333 /* Add "-file" if showing file or draft, */
334 if (draftsw
|| file
) {
335 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
336 vec
.msgs
[vec
.size
- 2] = "-file";
338 /* and add -show for backward compatibility */
339 app_msgarg(&vec
, "-show");
340 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
341 /* If "mhshow", add "-file" if showing file or draft. */
342 if (draftsw
|| file
) {
343 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
344 vec
.msgs
[vec
.size
- 2] = "-file";
347 if (headersw
&& vec
.size
== 1)
348 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
354 argsplit_insert(&vec
, proc
, &program
);
355 app_msgarg(&vec
, NULL
);
356 execvp (program
, vec
.msgs
);
357 adios (proc
, "unable to exec");
358 return 0; /* dead code to satisfy the compiler */
363 * Check if a message or file contains any non-text parts
366 is_nontext (char *msgnam
)
370 char buf
[BUFSIZ
], name
[NAMESZ
];
372 m_getfld_state_t gstate
= 0;
374 if ((fp
= fopen (msgnam
, "r")) == NULL
)
378 int bufsz
= sizeof buf
;
379 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
383 * Check Content-Type field
385 if (!strcasecmp (name
, TYPE_FIELD
)) {
389 cp
= mh_xstrdup(buf
);
390 while (state
== FLDPLUS
) {
392 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
399 for (; isspace ((unsigned char) *bp
); bp
++)
404 for (bp
++, i
= 0;;) {
434 for (dp
= bp
; istoken (*dp
); dp
++)
441 if ((result
= (strcasecmp (bp
, "plain") != 0)))
444 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
447 if ((result
= !uprf (dp
, "charset")))
449 dp
+= LEN("charset");
450 while (isspace ((unsigned char) *dp
))
454 while (isspace ((unsigned char) *dp
))
457 if ((bp
= strchr(++dp
, '"')))
460 for (bp
= dp
; *bp
; bp
++)
461 if (!istoken (*bp
)) {
467 /* Default character set */
470 /* Check the character set */
471 result
= !check_charset (dp
, strlen (dp
));
473 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
484 m_getfld_state_destroy (&gstate
);
491 * Check Content-Transfer-Encoding field
493 if (!strcasecmp (name
, ENCODING_FIELD
)) {
494 cp
= mh_xstrdup(buf
);
495 while (state
== FLDPLUS
) {
497 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
500 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
502 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
505 result
= (strcasecmp (bp
, "7bit")
506 && strcasecmp (bp
, "8bit")
507 && strcasecmp (bp
, "binary"));
512 m_getfld_state_destroy (&gstate
);
519 * Just skip the rest of this header
520 * field and go to next one.
522 while (state
== FLDPLUS
) {
524 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
529 * We've passed the message header,
530 * so message is just text.
534 m_getfld_state_destroy (&gstate
);