]>
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", 0, CONCATSW) \
36 X("noconcat", 0, NCONCATSW) \
38 * switches for mhshow \
40 X("part number", 0, PARTSW) \
41 X("type content", 0, TYPESW) \
42 X("prefer content", 0, PREFERSW) \
43 X("markform file", 0, MARKFORMSW) \
44 X("rcache policy", 0, RCACHESW) \
45 X("wcache policy", 0, WCACHESW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(SHOW
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
58 static int is_nontext(char *);
66 main (int argc
, char **argv
)
68 int draftsw
= 0, headersw
= 1;
69 int nshow
= 0, checkmime
= 1, mime
= 0;
70 int isdf
= 0, mode
= SHOW
, msgnum
;
71 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
72 char buf
[BUFSIZ
], **argp
, **arguments
;
73 struct msgs
*mp
= NULL
;
74 struct msgs_array msgs
= { 0, 0, NULL
};
75 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
77 if (nmh_init(argv
[0], 1)) { return 1; }
79 if (!strcasecmp (invo_name
, "next")) {
81 } else if (!strcasecmp (invo_name
, "prev")) {
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
96 goto non_mhl_switches
;
102 /* mhl can't handle these, so keep them separate. */
103 app_msgarg(&non_mhl_vec
, --cp
);
109 app_msgarg(&vec
, --cp
);
113 snprintf (buf
, sizeof(buf
),
114 "%s [+folder] %s[switches] [switches for showproc]",
115 invo_name
, mode
== SHOW
? "[msgs] ": "");
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
124 adios (NULL
, "only one file at a time!");
130 "usage: %s [+folder] [switches] [switches for showproc]",
136 adios (NULL
, "only one file at a time!");
137 if (!(cp
= *argp
++) || *cp
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
139 file
= path (cp
, TFILE
);
143 app_msgarg(&vec
, --cp
);
144 if (!(cp
= *argp
++) || *cp
== '-')
145 adios (NULL
, "missing argument to %s", argp
[-2]);
146 app_msgarg(&vec
, getcpy (etcpath(cp
)));
159 app_msgarg(&vec
, --cp
);
160 if (!(cp
= *argp
++) || *cp
== '-')
161 adios (NULL
, "missing argument to %s", argp
[-2]);
162 app_msgarg(&vec
, cp
);
166 if (!(showproc
= *argp
++) || *showproc
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
175 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
187 if (*cp
== '+' || *cp
== '@') {
189 adios (NULL
, "only one folder at a time!");
191 folder
= pluspath (cp
);
196 app_msgarg(&msgs
, cp
);
200 if (!context_find ("path"))
201 free (path ("./", TFOLDER
));
203 if (draftsw
|| file
) {
205 adios (NULL
, "only one file at a time!");
207 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
209 app_msgarg(&vec
, file
);
217 app_msgarg(&msgs
, "next");
220 app_msgarg(&msgs
, "prev");
223 app_msgarg(&msgs
, "cur");
229 folder
= getfolder (1);
230 maildir
= m_maildir (folder
);
232 if (chdir (maildir
) == NOTOK
)
233 adios (maildir
, "unable to change directory to");
235 /* read folder and create message structure */
236 if (!(mp
= folder_read (folder
, 1)))
237 adios (NULL
, "unable to read folder %s", folder
);
239 /* check for empty folder */
241 adios (NULL
, "no messages in %s", folder
);
243 /* parse all the message ranges/sequences and set SELECTED */
244 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
245 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
249 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
250 * since we will use that as a tag to know which messages
251 * to remove from the "unseen" sequence.
253 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
254 if (is_selected(mp
, msgnum
))
255 set_unseen (mp
, msgnum
);
257 seq_setprev (mp
); /* set the Previous-Sequence */
258 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
260 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
261 if (is_selected(mp
, msgnum
))
262 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
264 seq_setcur (mp
, mp
->hghsel
); /* update current message */
265 seq_save (mp
); /* synchronize sequences */
266 context_replace (pfolder
, folder
); /* update current folder */
267 context_save (); /* save the context file */
272 * Decide which "proc" to use
277 /* check if any messages are non-text MIME messages */
278 if (! mime
&& checkmime
) {
279 if (!draftsw
&& !file
) {
280 /* loop through selected messages and check for MIME */
281 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
282 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
287 /* check the file or draft for MIME */
288 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
300 if (folder
&& !draftsw
&& !file
)
301 m_putenv ("mhfolder", folder
);
303 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
305 if (headersw
&& vec
.size
== 1)
306 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
308 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
310 if (headersw
&& vec
.size
== 1)
311 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
313 /* If "mhl", then run it internally */
314 argsplit_insert(&vec
, "mhl", &program
);
315 app_msgarg(&vec
, NULL
);
316 mhl (vec
.size
, vec
.msgs
);
323 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
324 if (draftsw
|| file
) {
325 /* Insert the switch before the filename. */
326 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
327 vec
.msgs
[vec
.size
- 2] = *mp
;
329 app_msgarg(&vec
, *mp
);
333 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
334 /* Add "-file" if showing file or draft, */
335 if (draftsw
|| file
) {
336 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
337 vec
.msgs
[vec
.size
- 2] = "-file";
339 /* and add -show for backward compatibility */
340 app_msgarg(&vec
, "-show");
341 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
342 /* If "mhshow", add "-file" if showing file or draft. */
343 if (draftsw
|| file
) {
344 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
345 vec
.msgs
[vec
.size
- 2] = "-file";
348 if (headersw
&& vec
.size
== 1)
349 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
355 argsplit_insert(&vec
, proc
, &program
);
356 app_msgarg(&vec
, NULL
);
357 execvp (program
, vec
.msgs
);
358 adios (proc
, "unable to exec");
359 return 0; /* dead code to satisfy the compiler */
364 * Check if a message or file contains any non-text parts
367 is_nontext (char *msgnam
)
371 char buf
[BUFSIZ
], name
[NAMESZ
];
373 m_getfld_state_t gstate
= 0;
375 if ((fp
= fopen (msgnam
, "r")) == NULL
)
379 int bufsz
= sizeof buf
;
380 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
384 * Check Content-Type field
386 if (!strcasecmp (name
, TYPE_FIELD
)) {
390 cp
= add (buf
, NULL
);
391 while (state
== FLDPLUS
) {
393 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
400 for (; isspace ((unsigned char) *bp
); bp
++)
405 for (bp
++, i
= 0;;) {
435 for (dp
= bp
; istoken (*dp
); dp
++)
442 if ((result
= (strcasecmp (bp
, "plain") != 0)))
445 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
448 if ((result
= !uprf (dp
, "charset")))
450 dp
+= sizeof("charset") - 1;
451 while (isspace ((unsigned char) *dp
))
455 while (isspace ((unsigned char) *dp
))
458 if ((bp
= strchr(++dp
, '"')))
461 for (bp
= dp
; *bp
; bp
++)
462 if (!istoken (*bp
)) {
468 /* Default character set */
471 /* Check the character set */
472 result
= !check_charset (dp
, strlen (dp
));
474 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
485 m_getfld_state_destroy (&gstate
);
492 * Check Content-Transfer-Encoding field
494 if (!strcasecmp (name
, ENCODING_FIELD
)) {
495 cp
= add (buf
, NULL
);
496 while (state
== FLDPLUS
) {
498 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
501 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
503 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
506 result
= (strcasecmp (bp
, "7bit")
507 && strcasecmp (bp
, "8bit")
508 && strcasecmp (bp
, "binary"));
513 m_getfld_state_destroy (&gstate
);
520 * Just skip the rest of this header
521 * field and go to next one.
523 while (state
== FLDPLUS
) {
525 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
530 * We've passed the message header,
531 * so message is just text.
535 m_getfld_state_destroy (&gstate
);