]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
a39622f9542708c43f29e88299ab0f2504c08894
1 /* mhshow.c -- display the contents of MIME 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/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/readconfig.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
26 #include "h/signals.h"
30 #include "h/fmt_scan.h"
32 #include "h/mhparse.h"
33 #include "h/mhcachesbr.h"
37 #include "sbr/m_maildir.h"
38 #include "sbr/m_popen.h"
40 #include "mhshowsbr.h"
42 #define MHSHOW_SWITCHES \
43 X("check", 0, CHECKSW) \
44 X("nocheck", 0, NCHECKSW) \
45 X("verbose", 0, VERBSW) \
46 X("noverbose", 0, NVERBSW) \
47 X("concat", 0, CONCATSW) \
48 X("noconcat", 0, NCONCATSW) \
49 X("textonly", 0, TEXTONLYSW) \
50 X("notextonly", 0, NTEXTONLYSW) \
51 X("inlineonly", 0, INLINESW) \
52 X("noinlineonly", 0, NINLINESW) \
53 X("file file", 0, FILESW) \
54 X("form formfile", 0, FORMSW) \
55 X("header", 0, HEADSW) \
56 X("noheader", 0, NHEADSW) \
57 X("headerform formfile", 0, HEADFORMSW) \
58 X("markform formfile", 0, MARKFORMSW) \
59 X("part number", 0, PARTSW) \
60 X("type content", 0, TYPESW) \
61 X("prefer content", 0, PREFERSW) \
62 X("noprefer", 0, NPREFERSW) \
63 X("rcache policy", 0, RCACHESW) \
64 X("wcache policy", 0, WCACHESW) \
65 X("version", 0, VERSIONSW) \
66 X("help", 0, HELPSW) \
68 * switches for moreproc/mhlproc \
70 X("moreproc program", -4, PROGSW) \
71 X("nomoreproc", -3, NPROGSW) \
72 X("length lines", -4, LENSW) \
73 X("width columns", -4, WIDTHSW) \
75 * switches for debugging \
77 X("debug", -5, DEBUGSW) \
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(MHSHOW
);
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
91 #define quitser pipeser
96 static void pipeser (int);
100 main (int argc
, char **argv
)
102 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
103 char *cp
, *file
= NULL
;
104 char *maildir
, buf
[100], **argp
;
106 struct msgs_array msgs
= { 0, 0, NULL
};
107 struct msgs
*mp
= NULL
;
111 if (nmh_init(argv
[0], true, true)) { return 1; }
113 set_done(freects_done
);
115 arguments
= getarguments (invo_name
, argc
, argv
, 1);
121 while ((cp
= *argp
++)) {
123 switch (smatch (++cp
, switches
)) {
125 ambigsw (cp
, switches
);
128 die("-%s unknown", cp
);
131 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
133 print_help (buf
, switches
, 1);
136 print_version(invo_name
);
140 icachesw
= &rcachesw
;
143 icachesw
= &wcachesw
;
145 if (!(cp
= *argp
++) || *cp
== '-')
146 die("missing argument to %s", argp
[-2]);
147 switch (*icachesw
= smatch (cp
, cache_policy
)) {
149 ambigsw (cp
, cache_policy
);
152 die("%s unknown", cp
);
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
188 die("too many parts (starting with %s), %d max",
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
197 die("too many types (starting with %s), %d max",
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
205 if (npreferred
>= NPREFS
)
206 die("too many preferred types (starting with %s), %d max",
208 mime_preference
[npreferred
].type
= cp
;
209 cp
= strchr(cp
, '/');
210 if (cp
) *cp
++ = '\0';
211 mime_preference
[npreferred
++].subtype
= cp
;
219 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
220 die("missing argument to %s", argp
[-2]);
221 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
225 if (!(cp
= *argp
++) || *cp
== '-')
226 die("missing argument to %s", argp
[-2]);
228 formsw
= mh_xstrdup(etcpath(cp
));
239 if (!(headerform
= *argp
++) || *headerform
== '-')
240 die("missing argument to %s", argp
[-2]);
244 if (!(markerform
= *argp
++) || *markerform
== '-')
245 die("missing argument to %s", argp
[-2]);
249 * Switches for moreproc/mhlproc
252 if (!(progsw
= *argp
++) || *progsw
== '-')
253 die("missing argument to %s", argp
[-2]);
261 if (!(cp
= *argp
++) || *cp
== '-')
262 die("missing argument to %s", argp
[-2]);
276 if (*cp
== '+' || *cp
== '@') {
278 die("only one folder at a time!");
279 folder
= pluspath (cp
);
281 app_msgarg(&msgs
, cp
);
284 /* null terminate the list of acceptable parts/types */
289 * If we had any specific parts or types specified, turn off text only
293 if (npart
> 0 || ntype
> 0) {
296 if (inlineonly
== -1)
301 * Check if we've specified an additional profile
303 if ((cp
= getenv ("MHSHOW"))) {
304 if ((fp
= fopen (cp
, "r"))) {
305 readconfig(NULL
, fp
, cp
, 0);
308 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
313 * Read the standard profile setup
315 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
316 readconfig(NULL
, fp
, cp
, 0);
320 /* Check for public cache location */
321 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
324 /* Check for private cache location */
325 if (!(cache_private
= context_find (nmhprivcache
)))
326 cache_private
= ".cache";
327 cache_private
= mh_xstrdup(m_maildir(cache_private
));
329 if (!context_find ("path"))
330 free (path ("./", TFOLDER
));
332 if (file
&& msgs
.size
)
333 die("cannot specify msg and file at same time!");
336 * check if message is coming from file
339 cts
= mh_xcalloc(2, sizeof *cts
);
342 if ((ct
= parse_mime (file
)))
348 * message(s) are coming from a folder
351 app_msgarg(&msgs
, "cur");
353 folder
= getfolder (1);
354 maildir
= m_maildir (folder
);
356 if (chdir (maildir
) == NOTOK
)
357 adios (maildir
, "unable to change directory to");
359 /* read folder and create message structure */
360 if (!(mp
= folder_read (folder
, 1)))
361 die("unable to read folder %s", folder
);
363 /* check for empty folder */
365 die("no messages in %s", folder
);
367 /* parse all the message ranges/sequences and set SELECTED */
368 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
369 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
373 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
374 * since we will use that as a tag to know which messages
375 * to remove from the "unseen" sequence.
377 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
378 if (is_selected(mp
, msgnum
))
379 set_unseen (mp
, msgnum
);
381 seq_setprev (mp
); /* set the Previous-Sequence */
382 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
384 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
388 * Parse all the SELECTED messages.
390 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
391 if (is_selected(mp
, msgnum
)) {
394 msgnam
= m_name (msgnum
);
395 if ((ct
= parse_mime (msgnam
)))
405 SIGNAL (SIGQUIT
, quitser
);
406 SIGNAL (SIGPIPE
, pipeser
);
409 * Get the associated umask for the relevant contents.
411 for (ctp
= cts
; *ctp
; ctp
++) {
415 if (type_ok (ct
, 1) && !ct
->c_umask
) {
416 if (stat (ct
->c_file
, &st
) != NOTOK
)
417 ct
->c_umask
= ~(st
.st_mode
& 0777);
419 ct
->c_umask
= ~m_gmprot();
423 /* If reading from a folder, do some updating */
425 context_replace (pfolder
, folder
);/* update current folder */
426 seq_setcur (mp
, mp
->hghsel
); /* update current message */
427 seq_save (mp
); /* synchronize sequences */
428 context_save (); /* save the context file */
432 m_popen(moreproc
, 0);
435 * Show the message content
437 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
439 /* Now free all the structures for the content */
440 for (ctp
= cts
; *ctp
; ctp
++)
459 fprintf (stderr
, "\n");