]>
diplodocus.org Git - nmh/blob - uip/show.c
3 * show.c -- show/list messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 #define SHOW_SWITCHES \
15 X("checkmime", 0, CHECKMIMESW) \
16 X("nocheckmime", 0, NOCHECKMIMESW) \
17 X("header", 0, HEADSW) \
18 X("noheader", 0, NHEADSW) \
19 X("form formfile", 0, FORMSW) \
20 X("moreproc program", 0, PROGSW) \
21 X("nomoreproc", 0, NPROGSW) \
22 X("length lines", 0, LENSW) \
23 X("width columns", 0, WIDTHSW) \
24 X("showproc program", 0, SHOWSW) \
25 X("showmimeproc program", 0, SHOWMIMESW) \
26 X("noshowproc", 0, NSHOWSW) \
27 X("draft", 0, DRFTSW) \
28 X("file file", -4, FILESW) /* interface from showfile */ \
29 X("fmtproc program", 0, FMTPROCSW) \
30 X("nofmtproc", 0, NFMTPROCSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 * switches for mhlproc \
36 X("concat", -6, CONCATSW) \
37 X("noconcat", -8, NCONCATSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(SHOW
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
50 static int is_nontext(char *);
58 main (int argc
, char **argv
)
60 int draftsw
= 0, headersw
= 1;
61 int nshow
= 0, checkmime
= 1, mime
= 0;
62 int isdf
= 0, mode
= SHOW
, msgnum
;
63 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
64 char buf
[BUFSIZ
], **argp
, **arguments
;
65 struct msgs
*mp
= NULL
;
66 struct msgs_array msgs
= { 0, 0, NULL
};
67 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
69 if (nmh_init(argv
[0], 1)) { return 1; }
71 if (!strcasecmp (invo_name
, "next")) {
73 } else if (!strcasecmp (invo_name
, "prev")) {
76 arguments
= getarguments (invo_name
, argc
, argv
, 1);
79 while ((cp
= *argp
++)) {
81 switch (smatch (++cp
, switches
)) {
83 ambigsw (cp
, switches
);
88 /* mhl can't handle these, so keep them separate. */
89 app_msgarg(&non_mhl_vec
, --cp
);
95 app_msgarg(&vec
, --cp
);
99 snprintf (buf
, sizeof(buf
),
100 "%s [+folder] %s[switches] [switches for showproc]",
101 invo_name
, mode
== SHOW
? "[msgs] ": "");
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
110 adios (NULL
, "only one file at a time!");
116 "usage: %s [+folder] [switches] [switches for showproc]",
122 adios (NULL
, "only one file at a time!");
123 if (!(cp
= *argp
++) || *cp
== '-')
124 adios (NULL
, "missing argument to %s", argp
[-2]);
125 file
= path (cp
, TFILE
);
136 app_msgarg(&vec
, --cp
);
137 if (!(cp
= *argp
++) || *cp
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
139 app_msgarg(&vec
, getcpy (etcpath(cp
)));
146 app_msgarg(&vec
, --cp
);
147 if (!(cp
= *argp
++) || *cp
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
149 app_msgarg(&vec
, cp
);
153 if (!(showproc
= *argp
++) || *showproc
== '-')
154 adios (NULL
, "missing argument to %s", argp
[-2]);
162 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
174 if (*cp
== '+' || *cp
== '@') {
176 adios (NULL
, "only one folder at a time!");
178 folder
= pluspath (cp
);
183 app_msgarg(&msgs
, cp
);
187 if (!context_find ("path"))
188 free (path ("./", TFOLDER
));
190 if (draftsw
|| file
) {
192 adios (NULL
, "only one file at a time!");
194 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
196 app_msgarg(&vec
, file
);
203 app_msgarg(&msgs
, "next");
206 app_msgarg(&msgs
, "prev");
209 app_msgarg(&msgs
, "cur");
215 folder
= getfolder (1);
216 maildir
= m_maildir (folder
);
218 if (chdir (maildir
) == NOTOK
)
219 adios (maildir
, "unable to change directory to");
221 /* read folder and create message structure */
222 if (!(mp
= folder_read (folder
, 1)))
223 adios (NULL
, "unable to read folder %s", folder
);
225 /* check for empty folder */
227 adios (NULL
, "no messages in %s", folder
);
229 /* parse all the message ranges/sequences and set SELECTED */
230 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
231 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
235 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
236 * since we will use that as a tag to know which messages
237 * to remove from the "unseen" sequence.
239 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
240 if (is_selected(mp
, msgnum
))
241 set_unseen (mp
, msgnum
);
243 seq_setprev (mp
); /* set the Previous-Sequence */
244 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
246 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
247 if (is_selected(mp
, msgnum
))
248 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
250 seq_setcur (mp
, mp
->hghsel
); /* update current message */
251 seq_save (mp
); /* synchronize sequences */
252 context_replace (pfolder
, folder
); /* update current folder */
253 context_save (); /* save the context file */
255 if (headersw
&& vec
.size
== 1)
256 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
262 * Decide which "proc" to use
267 /* check if any messages are non-text MIME messages */
268 if (! mime
&& checkmime
) {
269 if (!draftsw
&& !file
) {
270 /* loop through selected messages and check for MIME */
271 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
272 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
277 /* check the file or draft for MIME */
278 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
290 if (folder
&& !draftsw
&& !file
)
291 m_putenv ("mhfolder", folder
);
293 if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
294 /* If "mhl", then run it internally */
295 argsplit_insert(&vec
, "mhl", &program
);
296 app_msgarg(&vec
, NULL
);
297 mhl (vec
.size
, vec
.msgs
);
303 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
304 if (draftsw
|| file
) {
305 /* Insert the switch before the filename. */
306 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
307 vec
.msgs
[vec
.size
- 2] = *mp
;
309 app_msgarg(&vec
, *mp
);
313 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
314 /* Add "-file" if showing file or draft, */
315 if (draftsw
|| file
) {
316 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
317 vec
.msgs
[vec
.size
- 2] = "-file";
319 /* and add -show for backward compatibility */
320 app_msgarg(&vec
, "-show");
321 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
322 /* If "mhshow", add "-file" if showing file or draft. */
323 if (draftsw
|| file
) {
324 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
325 vec
.msgs
[vec
.size
- 2] = "-file";
330 argsplit_insert(&vec
, proc
, &program
);
331 app_msgarg(&vec
, NULL
);
332 execvp (program
, vec
.msgs
);
333 adios (proc
, "unable to exec");
334 return 0; /* dead code to satisfy the compiler */
339 * Check if a message or file contains any non-text parts
342 is_nontext (char *msgnam
)
346 char buf
[BUFSIZ
], name
[NAMESZ
];
348 m_getfld_state_t gstate
= 0;
350 if ((fp
= fopen (msgnam
, "r")) == NULL
)
354 int bufsz
= sizeof buf
;
355 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
359 * Check Content-Type field
361 if (!strcasecmp (name
, TYPE_FIELD
)) {
365 cp
= add (buf
, NULL
);
366 while (state
== FLDPLUS
) {
368 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
375 for (; isspace ((unsigned char) *bp
); bp
++)
380 for (bp
++, i
= 0;;) {
410 for (dp
= bp
; istoken (*dp
); dp
++)
417 if ((result
= (strcasecmp (bp
, "plain") != 0)))
420 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
423 if ((result
= !uprf (dp
, "charset")))
425 dp
+= sizeof("charset") - 1;
426 while (isspace ((unsigned char) *dp
))
430 while (isspace ((unsigned char) *dp
))
433 if ((bp
= strchr(++dp
, '"')))
436 for (bp
= dp
; *bp
; bp
++)
437 if (!istoken (*bp
)) {
443 /* Default character set */
446 /* Check the character set */
447 result
= !check_charset (dp
, strlen (dp
));
449 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
460 m_getfld_state_destroy (&gstate
);
467 * Check Content-Transfer-Encoding field
469 if (!strcasecmp (name
, ENCODING_FIELD
)) {
470 cp
= add (buf
, NULL
);
471 while (state
== FLDPLUS
) {
473 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
476 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
478 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
481 result
= (strcasecmp (bp
, "7bit")
482 && strcasecmp (bp
, "8bit")
483 && strcasecmp (bp
, "binary"));
488 m_getfld_state_destroy (&gstate
);
495 * Just skip the rest of this header
496 * field and go to next one.
498 while (state
== FLDPLUS
) {
500 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
505 * We've passed the message header,
506 * so message is just text.
510 m_getfld_state_destroy (&gstate
);