]>
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", -6, CONCATSW) \
36 X("noconcat", -8, NCONCATSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(SHOW
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
49 static int is_nontext(char *);
57 main (int argc
, char **argv
)
59 int draftsw
= 0, headersw
= 1;
60 int nshow
= 0, checkmime
= 1, mime
= 0;
61 int isdf
= 0, mode
= SHOW
, msgnum
;
62 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
63 char buf
[BUFSIZ
], **argp
, **arguments
;
64 struct msgs
*mp
= NULL
;
65 struct msgs_array msgs
= { 0, 0, NULL
};
66 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
68 if (nmh_init(argv
[0], 1)) { return 1; }
70 if (!strcasecmp (invo_name
, "next")) {
72 } else if (!strcasecmp (invo_name
, "prev")) {
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
78 while ((cp
= *argp
++)) {
80 switch (smatch (++cp
, switches
)) {
82 ambigsw (cp
, switches
);
87 goto non_mhl_switches
;
93 /* mhl can't handle these, so keep them separate. */
94 app_msgarg(&non_mhl_vec
, --cp
);
100 app_msgarg(&vec
, --cp
);
104 snprintf (buf
, sizeof(buf
),
105 "%s [+folder] %s[switches] [switches for showproc]",
106 invo_name
, mode
== SHOW
? "[msgs] ": "");
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
115 adios (NULL
, "only one file at a time!");
121 "usage: %s [+folder] [switches] [switches for showproc]",
127 adios (NULL
, "only one file at a time!");
128 if (!(cp
= *argp
++) || *cp
== '-')
129 adios (NULL
, "missing argument to %s", argp
[-2]);
130 file
= path (cp
, TFILE
);
134 app_msgarg(&vec
, --cp
);
135 if (!(cp
= *argp
++) || *cp
== '-')
136 adios (NULL
, "missing argument to %s", argp
[-2]);
137 app_msgarg(&vec
, getcpy (etcpath(cp
)));
144 app_msgarg(&vec
, --cp
);
145 if (!(cp
= *argp
++) || *cp
== '-')
146 adios (NULL
, "missing argument to %s", argp
[-2]);
147 app_msgarg(&vec
, cp
);
151 if (!(showproc
= *argp
++) || *showproc
== '-')
152 adios (NULL
, "missing argument to %s", argp
[-2]);
160 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
161 adios (NULL
, "missing argument to %s", argp
[-2]);
172 if (*cp
== '+' || *cp
== '@') {
174 adios (NULL
, "only one folder at a time!");
176 folder
= pluspath (cp
);
181 app_msgarg(&msgs
, cp
);
185 if (!context_find ("path"))
186 free (path ("./", TFOLDER
));
188 if (draftsw
|| file
) {
190 adios (NULL
, "only one file at a time!");
192 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
194 app_msgarg(&vec
, file
);
202 app_msgarg(&msgs
, "next");
205 app_msgarg(&msgs
, "prev");
208 app_msgarg(&msgs
, "cur");
214 folder
= getfolder (1);
215 maildir
= m_maildir (folder
);
217 if (chdir (maildir
) == NOTOK
)
218 adios (maildir
, "unable to change directory to");
220 /* read folder and create message structure */
221 if (!(mp
= folder_read (folder
, 1)))
222 adios (NULL
, "unable to read folder %s", folder
);
224 /* check for empty folder */
226 adios (NULL
, "no messages in %s", folder
);
228 /* parse all the message ranges/sequences and set SELECTED */
229 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
230 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
234 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
235 * since we will use that as a tag to know which messages
236 * to remove from the "unseen" sequence.
238 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
239 if (is_selected(mp
, msgnum
))
240 set_unseen (mp
, msgnum
);
242 seq_setprev (mp
); /* set the Previous-Sequence */
243 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
245 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
246 if (is_selected(mp
, msgnum
))
247 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
249 seq_setcur (mp
, mp
->hghsel
); /* update current message */
250 seq_save (mp
); /* synchronize sequences */
251 context_replace (pfolder
, folder
); /* update current folder */
252 context_save (); /* save the context file */
257 * Decide which "proc" to use
262 /* check if any messages are non-text MIME messages */
263 if (! mime
&& checkmime
) {
264 if (!draftsw
&& !file
) {
265 /* loop through selected messages and check for MIME */
266 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
267 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
272 /* check the file or draft for MIME */
273 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
285 if (folder
&& !draftsw
&& !file
)
286 m_putenv ("mhfolder", folder
);
288 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
290 if (headersw
&& vec
.size
== 1)
291 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
293 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
295 if (headersw
&& vec
.size
== 1)
296 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
298 /* If "mhl", then run it internally */
299 argsplit_insert(&vec
, "mhl", &program
);
300 app_msgarg(&vec
, NULL
);
301 mhl (vec
.size
, vec
.msgs
);
308 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
309 if (draftsw
|| file
) {
310 /* Insert the switch before the filename. */
311 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
312 vec
.msgs
[vec
.size
- 2] = *mp
;
314 app_msgarg(&vec
, *mp
);
318 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
319 /* Add "-file" if showing file or draft, */
320 if (draftsw
|| file
) {
321 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
322 vec
.msgs
[vec
.size
- 2] = "-file";
324 /* and add -show for backward compatibility */
325 app_msgarg(&vec
, "-show");
326 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
327 /* If "mhshow", add "-file" if showing file or draft. */
328 if (draftsw
|| file
) {
329 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
330 vec
.msgs
[vec
.size
- 2] = "-file";
333 if (headersw
&& vec
.size
== 1)
334 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
340 argsplit_insert(&vec
, proc
, &program
);
341 app_msgarg(&vec
, NULL
);
342 execvp (program
, vec
.msgs
);
343 adios (proc
, "unable to exec");
344 return 0; /* dead code to satisfy the compiler */
349 * Check if a message or file contains any non-text parts
352 is_nontext (char *msgnam
)
356 char buf
[BUFSIZ
], name
[NAMESZ
];
358 m_getfld_state_t gstate
= 0;
360 if ((fp
= fopen (msgnam
, "r")) == NULL
)
364 int bufsz
= sizeof buf
;
365 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
369 * Check Content-Type field
371 if (!strcasecmp (name
, TYPE_FIELD
)) {
375 cp
= add (buf
, NULL
);
376 while (state
== FLDPLUS
) {
378 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
385 for (; isspace ((unsigned char) *bp
); bp
++)
390 for (bp
++, i
= 0;;) {
420 for (dp
= bp
; istoken (*dp
); dp
++)
427 if ((result
= (strcasecmp (bp
, "plain") != 0)))
430 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
433 if ((result
= !uprf (dp
, "charset")))
435 dp
+= sizeof("charset") - 1;
436 while (isspace ((unsigned char) *dp
))
440 while (isspace ((unsigned char) *dp
))
443 if ((bp
= strchr(++dp
, '"')))
446 for (bp
= dp
; *bp
; bp
++)
447 if (!istoken (*bp
)) {
453 /* Default character set */
456 /* Check the character set */
457 result
= !check_charset (dp
, strlen (dp
));
459 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
470 m_getfld_state_destroy (&gstate
);
477 * Check Content-Transfer-Encoding field
479 if (!strcasecmp (name
, ENCODING_FIELD
)) {
480 cp
= add (buf
, NULL
);
481 while (state
== FLDPLUS
) {
483 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
486 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
488 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
491 result
= (strcasecmp (bp
, "7bit")
492 && strcasecmp (bp
, "8bit")
493 && strcasecmp (bp
, "binary"));
498 m_getfld_state_destroy (&gstate
);
505 * Just skip the rest of this header
506 * field and go to next one.
508 while (state
== FLDPLUS
) {
510 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
515 * We've passed the message header,
516 * so message is just text.
520 m_getfld_state_destroy (&gstate
);