]>
diplodocus.org Git - nmh/blob - uip/show.c
08ddcf1499076024bb6849cd601e7b510c1c0ce8
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/arglist.h"
12 #include "sbr/error.h"
16 #include "sbr/m_maildir.h"
18 #define SHOW_SWITCHES \
19 X("checkmime", 0, CHECKMIMESW) \
20 X("nocheckmime", 0, NOCHECKMIMESW) \
21 X("header", 0, HEADSW) \
22 X("noheader", 0, NHEADSW) \
23 X("form formfile", 0, FORMSW) \
24 X("moreproc program", 0, PROGSW) \
25 X("nomoreproc", 0, NPROGSW) \
26 X("length lines", 0, LENSW) \
27 X("width columns", 0, WIDTHSW) \
28 X("showproc program", 0, SHOWSW) \
29 X("showmimeproc program", 0, SHOWMIMESW) \
30 X("noshowproc", 0, NSHOWSW) \
31 X("draft", 0, DRFTSW) \
32 X("file file", -4, FILESW) /* interface from showfile */ \
33 X("fmtproc program", 0, FMTPROCSW) \
34 X("nofmtproc", 0, NFMTPROCSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 * switches for mhlproc \
40 X("concat", 0, CONCATSW) \
41 X("noconcat", 0, NCONCATSW) \
43 * switches for mhshow \
45 X("part number", 0, PARTSW) \
46 X("type content", 0, TYPESW) \
47 X("prefer content", 0, PREFERSW) \
48 X("markform file", 0, MARKFORMSW) \
49 X("rcache policy", 0, RCACHESW) \
50 X("wcache policy", 0, WCACHESW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(SHOW
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
63 static int is_nontext(char *);
71 main (int argc
, char **argv
)
76 bool checkmime
= true;
78 int isdf
= 0, mode
= SHOW
, msgnum
;
79 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
80 char buf
[BUFSIZ
], **argp
, **arguments
;
81 struct msgs
*mp
= NULL
;
82 struct msgs_array msgs
= { 0, 0, NULL
};
83 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
85 if (nmh_init(argv
[0], true, true)) { return 1; }
87 if (!strcasecmp (invo_name
, "next")) {
89 } else if (!strcasecmp (invo_name
, "prev")) {
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
104 goto non_mhl_switches
;
111 /* mhl can't handle these, so keep them separate. */
112 app_msgarg(&non_mhl_vec
, --cp
);
118 app_msgarg(&vec
, --cp
);
122 snprintf (buf
, sizeof(buf
),
123 "%s [+folder] %s[switches] [switches for showproc]",
124 invo_name
, mode
== SHOW
? "[msgs] ": "");
125 print_help (buf
, switches
, 1);
128 print_version(invo_name
);
133 die("only one file at a time!");
138 die( "usage: %s [+folder] [switches] [switches for showproc]",
144 die("only one file at a time!");
145 if (!(cp
= *argp
++) || *cp
== '-')
146 die("missing argument to %s", argp
[-2]);
147 file
= path (cp
, TFILE
);
151 app_msgarg(&vec
, --cp
);
152 if (!(cp
= *argp
++) || *cp
== '-')
153 die("missing argument to %s", argp
[-2]);
154 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
167 app_msgarg(&vec
, --cp
);
168 if (!(cp
= *argp
++) || *cp
== '-')
169 die("missing argument to %s", argp
[-2]);
170 app_msgarg(&vec
, cp
);
174 if (!(showproc
= *argp
++) || *showproc
== '-')
175 die("missing argument to %s", argp
[-2]);
183 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
184 die("missing argument to %s", argp
[-2]);
195 if (*cp
== '+' || *cp
== '@') {
197 die("only one folder at a time!");
198 folder
= pluspath (cp
);
202 app_msgarg(&msgs
, cp
);
206 if (!context_find ("path"))
207 free (path ("./", TFOLDER
));
209 if (draftsw
|| file
) {
211 die("only one file at a time!");
213 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
215 app_msgarg(&vec
, file
);
223 app_msgarg(&msgs
, "next");
226 app_msgarg(&msgs
, "prev");
229 app_msgarg(&msgs
, "cur");
235 folder
= getfolder (1);
236 maildir
= m_maildir (folder
);
238 if (chdir (maildir
) == NOTOK
)
239 adios (maildir
, "unable to change directory to");
241 /* read folder and create message structure */
242 if (!(mp
= folder_read (folder
, 1)))
243 die("unable to read folder %s", folder
);
245 /* check for empty folder */
247 die("no messages in %s", folder
);
249 /* parse all the message ranges/sequences and set SELECTED */
250 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
251 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
255 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
256 * since we will use that as a tag to know which messages
257 * to remove from the "unseen" sequence.
259 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
260 if (is_selected(mp
, msgnum
))
261 set_unseen (mp
, msgnum
);
263 seq_setprev (mp
); /* set the Previous-Sequence */
264 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
266 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
267 if (is_selected(mp
, msgnum
))
268 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
270 seq_setcur (mp
, mp
->hghsel
); /* update current message */
271 seq_save (mp
); /* synchronize sequences */
272 context_replace (pfolder
, folder
); /* update current folder */
273 context_save (); /* save the context file */
278 * Decide which "proc" to use
283 /* check if any messages are non-text MIME messages */
284 if (! mime
&& checkmime
) {
285 if (!draftsw
&& !file
) {
286 /* loop through selected messages and check for MIME */
287 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
288 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
293 /* check the file or draft for MIME */
294 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
306 if (folder
&& !draftsw
&& !file
)
307 setenv("mhfolder", folder
, 1);
309 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
311 if (headersw
&& vec
.size
== 1)
312 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
314 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
316 if (headersw
&& vec
.size
== 1)
317 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
319 /* If "mhl", then run it internally */
320 argsplit_insert(&vec
, "mhl", &program
);
321 app_msgarg(&vec
, NULL
);
322 mhl (vec
.size
, vec
.msgs
);
329 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
330 if (draftsw
|| file
) {
331 /* Insert the switch before the filename. */
332 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
333 vec
.msgs
[vec
.size
- 2] = *mp
;
335 app_msgarg(&vec
, *mp
);
339 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
340 /* Add "-file" if showing file or draft, */
341 if (draftsw
|| file
) {
342 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
343 vec
.msgs
[vec
.size
- 2] = "-file";
345 /* and add -show for backward compatibility */
346 app_msgarg(&vec
, "-show");
347 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
348 /* If "mhshow", add "-file" if showing file or draft. */
349 if (draftsw
|| file
) {
350 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
351 vec
.msgs
[vec
.size
- 2] = "-file";
354 if (headersw
&& vec
.size
== 1)
355 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
361 argsplit_insert(&vec
, proc
, &program
);
362 app_msgarg(&vec
, NULL
);
363 execvp (program
, vec
.msgs
);
364 adios (proc
, "unable to exec");
365 return 0; /* dead code to satisfy the compiler */
370 * Check if a message or file contains any non-text parts
373 is_nontext (char *msgnam
)
377 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
379 m_getfld_state_t gstate
;
381 if ((fp
= fopen (msgnam
, "r")) == NULL
)
383 gstate
= m_getfld_state_init(fp
);
385 int bufsz
= sizeof buf
;
386 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
390 * Check Content-Type field
392 if (!strcasecmp (name
, TYPE_FIELD
)) {
396 cp
= mh_xstrdup(buf
);
397 while (state
== FLDPLUS
) {
399 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
406 for (; isspace ((unsigned char) *bp
); bp
++)
411 for (bp
++, i
= 0;;) {
441 for (dp
= bp
; istoken (*dp
); dp
++)
448 if ((result
= (strcasecmp (bp
, "plain") != 0)))
451 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
454 if ((result
= !uprf (dp
, "charset")))
456 dp
+= LEN("charset");
457 while (isspace ((unsigned char) *dp
))
461 while (isspace ((unsigned char) *dp
))
464 if ((bp
= strchr(++dp
, '"')))
467 for (bp
= dp
; *bp
; bp
++)
468 if (!istoken (*bp
)) {
474 /* Default character set */
477 /* Check the character set */
478 result
= !check_charset (dp
, strlen (dp
));
480 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
491 m_getfld_state_destroy (&gstate
);
498 * Check Content-Transfer-Encoding field
500 if (!strcasecmp (name
, ENCODING_FIELD
)) {
501 cp
= mh_xstrdup(buf
);
502 while (state
== FLDPLUS
) {
504 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
507 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
509 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
512 result
= (strcasecmp (bp
, "7bit")
513 && strcasecmp (bp
, "8bit")
514 && strcasecmp (bp
, "binary"));
519 m_getfld_state_destroy (&gstate
);
526 * Just skip the rest of this header
527 * field and go to next one.
529 while (state
== FLDPLUS
) {
531 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
536 * We've passed the message header,
537 * so message is just text.
541 m_getfld_state_destroy (&gstate
);