]>
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
};
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 /* Use showmimeproc if one of these switches was
89 specified because mhl doesn't understand them. */
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
, '/'), "mhn") == 0) {
294 /* Add "-file" if showing file or draft, */
295 if (draftsw
|| file
) {
296 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
297 vec
.msgs
[vec
.size
- 2] = "-file";
299 /* and add -show for backward compatibility */
300 app_msgarg(&vec
, "-show");
301 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
302 /* If "mhshow", add "-file" if showing file or draft. */
303 if (draftsw
|| file
) {
304 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
305 vec
.msgs
[vec
.size
- 2] = "-file";
307 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
308 /* If "mhl", then run it internally */
309 argsplit_insert(&vec
, "mhl", &program
);
310 app_msgarg(&vec
, NULL
);
311 mhl (vec
.size
, vec
.msgs
);
315 argsplit_insert(&vec
, proc
, &program
);
316 app_msgarg(&vec
, NULL
);
317 execvp (program
, vec
.msgs
);
318 adios (proc
, "unable to exec");
319 return 0; /* dead code to satisfy the compiler */
324 * Check if a message or file contains any non-text parts
327 is_nontext (char *msgnam
)
331 char buf
[BUFSIZ
], name
[NAMESZ
];
333 m_getfld_state_t gstate
= 0;
335 if ((fp
= fopen (msgnam
, "r")) == NULL
)
339 int bufsz
= sizeof buf
;
340 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
344 * Check Content-Type field
346 if (!strcasecmp (name
, TYPE_FIELD
)) {
350 cp
= add (buf
, NULL
);
351 while (state
== FLDPLUS
) {
353 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
360 for (; isspace ((unsigned char) *bp
); bp
++)
365 for (bp
++, i
= 0;;) {
395 for (dp
= bp
; istoken (*dp
); dp
++)
402 if ((result
= (strcasecmp (bp
, "plain") != 0)))
405 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
408 if ((result
= !uprf (dp
, "charset")))
410 dp
+= sizeof("charset") - 1;
411 while (isspace ((unsigned char) *dp
))
415 while (isspace ((unsigned char) *dp
))
418 if ((bp
= strchr(++dp
, '"')))
421 for (bp
= dp
; *bp
; bp
++)
422 if (!istoken (*bp
)) {
428 /* Default character set */
431 /* Check the character set */
432 result
= !check_charset (dp
, strlen (dp
));
434 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
445 m_getfld_state_destroy (&gstate
);
452 * Check Content-Transfer-Encoding field
454 if (!strcasecmp (name
, ENCODING_FIELD
)) {
455 cp
= add (buf
, NULL
);
456 while (state
== FLDPLUS
) {
458 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
461 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
463 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
466 result
= (strcasecmp (bp
, "7bit")
467 && strcasecmp (bp
, "8bit")
468 && strcasecmp (bp
, "binary"));
473 m_getfld_state_destroy (&gstate
);
480 * Just skip the rest of this header
481 * field and go to next one.
483 while (state
== FLDPLUS
) {
485 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
490 * We've passed the message header,
491 * so message is just text.
495 m_getfld_state_destroy (&gstate
);