]>
diplodocus.org Git - nmh/blob - uip/show.c
9aa7dd816baac236576f167317c50460f99e2cfd
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/context_find.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
19 #include "sbr/m_maildir.h"
21 #define SHOW_SWITCHES \
22 X("checkmime", 0, CHECKMIMESW) \
23 X("nocheckmime", 0, NOCHECKMIMESW) \
24 X("header", 0, HEADSW) \
25 X("noheader", 0, NHEADSW) \
26 X("form formfile", 0, FORMSW) \
27 X("moreproc program", 0, PROGSW) \
28 X("nomoreproc", 0, NPROGSW) \
29 X("length lines", 0, LENSW) \
30 X("width columns", 0, WIDTHSW) \
31 X("showproc program", 0, SHOWSW) \
32 X("showmimeproc program", 0, SHOWMIMESW) \
33 X("noshowproc", 0, NSHOWSW) \
34 X("draft", 0, DRFTSW) \
35 X("file file", -4, FILESW) /* interface from showfile */ \
36 X("fmtproc program", 0, FMTPROCSW) \
37 X("nofmtproc", 0, NFMTPROCSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 * switches for mhlproc \
43 X("concat", 0, CONCATSW) \
44 X("noconcat", 0, NCONCATSW) \
46 * switches for mhshow \
48 X("part number", 0, PARTSW) \
49 X("type content", 0, TYPESW) \
50 X("prefer content", 0, PREFERSW) \
51 X("markform file", 0, MARKFORMSW) \
52 X("rcache policy", 0, RCACHESW) \
53 X("wcache policy", 0, WCACHESW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(SHOW
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
66 static int is_nontext(char *);
74 main (int argc
, char **argv
)
79 bool checkmime
= true;
81 int isdf
= 0, mode
= SHOW
, msgnum
;
82 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
83 char buf
[BUFSIZ
], **argp
, **arguments
;
84 struct msgs
*mp
= NULL
;
85 struct msgs_array msgs
= { 0, 0, NULL
};
86 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
88 if (nmh_init(argv
[0], true, true)) { return 1; }
90 if (!strcasecmp (invo_name
, "next")) {
92 } else if (!strcasecmp (invo_name
, "prev")) {
95 arguments
= getarguments (invo_name
, argc
, argv
, 1);
98 while ((cp
= *argp
++)) {
100 switch (smatch (++cp
, switches
)) {
102 ambigsw (cp
, switches
);
107 goto non_mhl_switches
;
114 /* mhl can't handle these, so keep them separate. */
115 app_msgarg(&non_mhl_vec
, --cp
);
121 app_msgarg(&vec
, --cp
);
125 snprintf (buf
, sizeof(buf
),
126 "%s [+folder] %s[switches] [switches for showproc]",
127 invo_name
, mode
== SHOW
? "[msgs] ": "");
128 print_help (buf
, switches
, 1);
131 print_version(invo_name
);
136 die("only one file at a time!");
141 die( "usage: %s [+folder] [switches] [switches for showproc]",
147 die("only one file at a time!");
148 if (!(cp
= *argp
++) || *cp
== '-')
149 die("missing argument to %s", argp
[-2]);
150 file
= path (cp
, TFILE
);
154 app_msgarg(&vec
, --cp
);
155 if (!(cp
= *argp
++) || *cp
== '-')
156 die("missing argument to %s", argp
[-2]);
157 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
170 app_msgarg(&vec
, --cp
);
171 if (!(cp
= *argp
++) || *cp
== '-')
172 die("missing argument to %s", argp
[-2]);
173 app_msgarg(&vec
, cp
);
177 if (!(showproc
= *argp
++) || *showproc
== '-')
178 die("missing argument to %s", argp
[-2]);
186 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
187 die("missing argument to %s", argp
[-2]);
198 if (*cp
== '+' || *cp
== '@') {
200 die("only one folder at a time!");
201 folder
= pluspath (cp
);
205 app_msgarg(&msgs
, cp
);
209 if (!context_find ("path"))
210 free (path ("./", TFOLDER
));
212 if (draftsw
|| file
) {
214 die("only one file at a time!");
216 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
218 app_msgarg(&vec
, file
);
226 app_msgarg(&msgs
, "next");
229 app_msgarg(&msgs
, "prev");
232 app_msgarg(&msgs
, "cur");
238 folder
= getfolder (1);
239 maildir
= m_maildir (folder
);
241 if (chdir (maildir
) == NOTOK
)
242 adios (maildir
, "unable to change directory to");
244 /* read folder and create message structure */
245 if (!(mp
= folder_read (folder
, 1)))
246 die("unable to read folder %s", folder
);
248 /* check for empty folder */
250 die("no messages in %s", folder
);
252 /* parse all the message ranges/sequences and set SELECTED */
253 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
254 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
258 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
259 * since we will use that as a tag to know which messages
260 * to remove from the "unseen" sequence.
262 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
263 if (is_selected(mp
, msgnum
))
264 set_unseen (mp
, msgnum
);
266 seq_setprev (mp
); /* set the Previous-Sequence */
267 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
269 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
270 if (is_selected(mp
, msgnum
))
271 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
273 seq_setcur (mp
, mp
->hghsel
); /* update current message */
274 seq_save (mp
); /* synchronize sequences */
275 context_replace (pfolder
, folder
); /* update current folder */
276 context_save (); /* save the context file */
281 * Decide which "proc" to use
286 /* check if any messages are non-text MIME messages */
287 if (! mime
&& checkmime
) {
288 if (!draftsw
&& !file
) {
289 /* loop through selected messages and check for MIME */
290 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
291 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
296 /* check the file or draft for MIME */
297 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
309 if (folder
&& !draftsw
&& !file
)
310 setenv("mhfolder", folder
, 1);
312 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
314 if (headersw
&& vec
.size
== 1)
315 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
317 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
319 if (headersw
&& vec
.size
== 1)
320 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
322 /* If "mhl", then run it internally */
323 argsplit_insert(&vec
, "mhl", &program
);
324 app_msgarg(&vec
, NULL
);
325 mhl (vec
.size
, vec
.msgs
);
332 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
333 if (draftsw
|| file
) {
334 /* Insert the switch before the filename. */
335 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
336 vec
.msgs
[vec
.size
- 2] = *mp
;
338 app_msgarg(&vec
, *mp
);
342 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
343 /* Add "-file" if showing file or draft, */
344 if (draftsw
|| file
) {
345 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
346 vec
.msgs
[vec
.size
- 2] = "-file";
348 /* and add -show for backward compatibility */
349 app_msgarg(&vec
, "-show");
350 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
351 /* If "mhshow", add "-file" if showing file or draft. */
352 if (draftsw
|| file
) {
353 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
354 vec
.msgs
[vec
.size
- 2] = "-file";
357 if (headersw
&& vec
.size
== 1)
358 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
364 argsplit_insert(&vec
, proc
, &program
);
365 app_msgarg(&vec
, NULL
);
366 execvp (program
, vec
.msgs
);
367 adios (proc
, "unable to exec");
368 return 0; /* dead code to satisfy the compiler */
373 * Check if a message or file contains any non-text parts
376 is_nontext (char *msgnam
)
380 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
382 m_getfld_state_t gstate
;
384 if ((fp
= fopen (msgnam
, "r")) == NULL
)
386 gstate
= m_getfld_state_init(fp
);
388 int bufsz
= sizeof buf
;
389 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
393 * Check Content-Type field
395 if (!strcasecmp (name
, TYPE_FIELD
)) {
399 cp
= mh_xstrdup(buf
);
400 while (state
== FLDPLUS
) {
402 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
409 for (; isspace ((unsigned char) *bp
); bp
++)
414 for (bp
++, i
= 0;;) {
444 for (dp
= bp
; istoken (*dp
); dp
++)
451 if ((result
= (strcasecmp (bp
, "plain") != 0)))
454 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
457 if ((result
= !uprf (dp
, "charset")))
459 dp
+= LEN("charset");
460 while (isspace ((unsigned char) *dp
))
464 while (isspace ((unsigned char) *dp
))
467 if ((bp
= strchr(++dp
, '"')))
470 for (bp
= dp
; *bp
; bp
++)
471 if (!istoken (*bp
)) {
477 /* Default character set */
480 /* Check the character set */
481 result
= !check_charset (dp
, strlen (dp
));
483 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
494 m_getfld_state_destroy (&gstate
);
501 * Check Content-Transfer-Encoding field
503 if (!strcasecmp (name
, ENCODING_FIELD
)) {
504 cp
= mh_xstrdup(buf
);
505 while (state
== FLDPLUS
) {
507 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
510 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
512 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
515 result
= (strcasecmp (bp
, "7bit")
516 && strcasecmp (bp
, "8bit")
517 && strcasecmp (bp
, "binary"));
522 m_getfld_state_destroy (&gstate
);
529 * Just skip the rest of this header
530 * field and go to next one.
532 while (state
== FLDPLUS
) {
534 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
539 * We've passed the message header,
540 * so message is just text.
544 m_getfld_state_destroy (&gstate
);