]>
diplodocus.org Git - nmh/blob - uip/show.c
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/arglist.h"
13 #include "sbr/error.h"
17 #include "sbr/m_maildir.h"
19 #define SHOW_SWITCHES \
20 X("checkmime", 0, CHECKMIMESW) \
21 X("nocheckmime", 0, NOCHECKMIMESW) \
22 X("header", 0, HEADSW) \
23 X("noheader", 0, NHEADSW) \
24 X("form formfile", 0, FORMSW) \
25 X("moreproc program", 0, PROGSW) \
26 X("nomoreproc", 0, NPROGSW) \
27 X("length lines", 0, LENSW) \
28 X("width columns", 0, WIDTHSW) \
29 X("showproc program", 0, SHOWSW) \
30 X("showmimeproc program", 0, SHOWMIMESW) \
31 X("noshowproc", 0, NSHOWSW) \
32 X("draft", 0, DRFTSW) \
33 X("file file", -4, FILESW) /* interface from showfile */ \
34 X("fmtproc program", 0, FMTPROCSW) \
35 X("nofmtproc", 0, NFMTPROCSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 * switches for mhlproc \
41 X("concat", 0, CONCATSW) \
42 X("noconcat", 0, NCONCATSW) \
44 * switches for mhshow \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("prefer content", 0, PREFERSW) \
49 X("markform file", 0, MARKFORMSW) \
50 X("rcache policy", 0, RCACHESW) \
51 X("wcache policy", 0, WCACHESW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(SHOW
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
64 static int is_nontext(char *);
72 main (int argc
, char **argv
)
77 bool checkmime
= true;
79 int isdf
= 0, mode
= SHOW
, msgnum
;
80 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
81 char buf
[BUFSIZ
], **argp
, **arguments
;
82 struct msgs
*mp
= NULL
;
83 struct msgs_array msgs
= { 0, 0, NULL
};
84 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
86 if (nmh_init(argv
[0], true, true)) { return 1; }
88 if (!strcasecmp (invo_name
, "next")) {
90 } else if (!strcasecmp (invo_name
, "prev")) {
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
105 goto non_mhl_switches
;
112 /* mhl can't handle these, so keep them separate. */
113 app_msgarg(&non_mhl_vec
, --cp
);
119 app_msgarg(&vec
, --cp
);
123 snprintf (buf
, sizeof(buf
),
124 "%s [+folder] %s[switches] [switches for showproc]",
125 invo_name
, mode
== SHOW
? "[msgs] ": "");
126 print_help (buf
, switches
, 1);
129 print_version(invo_name
);
134 die("only one file at a time!");
139 die( "usage: %s [+folder] [switches] [switches for showproc]",
145 die("only one file at a time!");
146 if (!(cp
= *argp
++) || *cp
== '-')
147 die("missing argument to %s", argp
[-2]);
148 file
= path (cp
, TFILE
);
152 app_msgarg(&vec
, --cp
);
153 if (!(cp
= *argp
++) || *cp
== '-')
154 die("missing argument to %s", argp
[-2]);
155 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
168 app_msgarg(&vec
, --cp
);
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
171 app_msgarg(&vec
, cp
);
175 if (!(showproc
= *argp
++) || *showproc
== '-')
176 die("missing argument to %s", argp
[-2]);
184 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
185 die("missing argument to %s", argp
[-2]);
196 if (*cp
== '+' || *cp
== '@') {
198 die("only one folder at a time!");
199 folder
= pluspath (cp
);
203 app_msgarg(&msgs
, cp
);
207 if (!context_find ("path"))
208 free (path ("./", TFOLDER
));
210 if (draftsw
|| file
) {
212 die("only one file at a time!");
214 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
216 app_msgarg(&vec
, file
);
224 app_msgarg(&msgs
, "next");
227 app_msgarg(&msgs
, "prev");
230 app_msgarg(&msgs
, "cur");
236 folder
= getfolder (1);
237 maildir
= m_maildir (folder
);
239 if (chdir (maildir
) == NOTOK
)
240 adios (maildir
, "unable to change directory to");
242 /* read folder and create message structure */
243 if (!(mp
= folder_read (folder
, 1)))
244 die("unable to read folder %s", folder
);
246 /* check for empty folder */
248 die("no messages in %s", folder
);
250 /* parse all the message ranges/sequences and set SELECTED */
251 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
252 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
256 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
257 * since we will use that as a tag to know which messages
258 * to remove from the "unseen" sequence.
260 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
261 if (is_selected(mp
, msgnum
))
262 set_unseen (mp
, msgnum
);
264 seq_setprev (mp
); /* set the Previous-Sequence */
265 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
267 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
268 if (is_selected(mp
, msgnum
))
269 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
271 seq_setcur (mp
, mp
->hghsel
); /* update current message */
272 seq_save (mp
); /* synchronize sequences */
273 context_replace (pfolder
, folder
); /* update current folder */
274 context_save (); /* save the context file */
279 * Decide which "proc" to use
284 /* check if any messages are non-text MIME messages */
285 if (! mime
&& checkmime
) {
286 if (!draftsw
&& !file
) {
287 /* loop through selected messages and check for MIME */
288 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
289 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
294 /* check the file or draft for MIME */
295 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
307 if (folder
&& !draftsw
&& !file
)
308 setenv("mhfolder", folder
, 1);
310 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
312 if (headersw
&& vec
.size
== 1)
313 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
315 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
317 if (headersw
&& vec
.size
== 1)
318 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
320 /* If "mhl", then run it internally */
321 argsplit_insert(&vec
, "mhl", &program
);
322 app_msgarg(&vec
, NULL
);
323 mhl (vec
.size
, vec
.msgs
);
330 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
331 if (draftsw
|| file
) {
332 /* Insert the switch before the filename. */
333 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
334 vec
.msgs
[vec
.size
- 2] = *mp
;
336 app_msgarg(&vec
, *mp
);
340 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
341 /* 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";
346 /* and add -show for backward compatibility */
347 app_msgarg(&vec
, "-show");
348 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
349 /* If "mhshow", add "-file" if showing file or draft. */
350 if (draftsw
|| file
) {
351 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
352 vec
.msgs
[vec
.size
- 2] = "-file";
355 if (headersw
&& vec
.size
== 1)
356 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
362 argsplit_insert(&vec
, proc
, &program
);
363 app_msgarg(&vec
, NULL
);
364 execvp (program
, vec
.msgs
);
365 adios (proc
, "unable to exec");
366 return 0; /* dead code to satisfy the compiler */
371 * Check if a message or file contains any non-text parts
374 is_nontext (char *msgnam
)
378 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
380 m_getfld_state_t gstate
;
382 if ((fp
= fopen (msgnam
, "r")) == NULL
)
384 gstate
= m_getfld_state_init(fp
);
386 int bufsz
= sizeof buf
;
387 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
391 * Check Content-Type field
393 if (!strcasecmp (name
, TYPE_FIELD
)) {
397 cp
= mh_xstrdup(buf
);
398 while (state
== FLDPLUS
) {
400 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
407 for (; isspace ((unsigned char) *bp
); bp
++)
412 for (bp
++, i
= 0;;) {
442 for (dp
= bp
; istoken (*dp
); dp
++)
449 if ((result
= (strcasecmp (bp
, "plain") != 0)))
452 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
455 if ((result
= !uprf (dp
, "charset")))
457 dp
+= LEN("charset");
458 while (isspace ((unsigned char) *dp
))
462 while (isspace ((unsigned char) *dp
))
465 if ((bp
= strchr(++dp
, '"')))
468 for (bp
= dp
; *bp
; bp
++)
469 if (!istoken (*bp
)) {
475 /* Default character set */
478 /* Check the character set */
479 result
= !check_charset (dp
, strlen (dp
));
481 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
492 m_getfld_state_destroy (&gstate
);
499 * Check Content-Transfer-Encoding field
501 if (!strcasecmp (name
, ENCODING_FIELD
)) {
502 cp
= mh_xstrdup(buf
);
503 while (state
== FLDPLUS
) {
505 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
508 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
510 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
513 result
= (strcasecmp (bp
, "7bit")
514 && strcasecmp (bp
, "8bit")
515 && strcasecmp (bp
, "binary"));
520 m_getfld_state_destroy (&gstate
);
527 * Just skip the rest of this header
528 * field and go to next one.
530 while (state
== FLDPLUS
) {
532 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
537 * We've passed the message header,
538 * so message is just text.
542 m_getfld_state_destroy (&gstate
);