]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
9367f00803fc94456a7ca346df852af4c1ba5553
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/getarguments.h"
10 #include "sbr/seq_setunseen.h"
11 #include "sbr/seq_setprev.h"
12 #include "sbr/seq_setcur.h"
13 #include "sbr/seq_save.h"
14 #include "sbr/smatch.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_find.h"
21 #include "sbr/readconfig.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
28 #include "h/signals.h"
32 #include "h/fmt_scan.h"
34 #include "h/mhparse.h"
35 #include "h/mhcachesbr.h"
39 #include "sbr/m_maildir.h"
40 #include "sbr/m_popen.h"
42 #include "mhshowsbr.h"
44 #define MHSHOW_SWITCHES \
45 X("check", 0, CHECKSW) \
46 X("nocheck", 0, NCHECKSW) \
47 X("verbose", 0, VERBSW) \
48 X("noverbose", 0, NVERBSW) \
49 X("concat", 0, CONCATSW) \
50 X("noconcat", 0, NCONCATSW) \
51 X("textonly", 0, TEXTONLYSW) \
52 X("notextonly", 0, NTEXTONLYSW) \
53 X("inlineonly", 0, INLINESW) \
54 X("noinlineonly", 0, NINLINESW) \
55 X("file file", 0, FILESW) \
56 X("form formfile", 0, FORMSW) \
57 X("header", 0, HEADSW) \
58 X("noheader", 0, NHEADSW) \
59 X("headerform formfile", 0, HEADFORMSW) \
60 X("markform formfile", 0, MARKFORMSW) \
61 X("part number", 0, PARTSW) \
62 X("type content", 0, TYPESW) \
63 X("prefer content", 0, PREFERSW) \
64 X("noprefer", 0, NPREFERSW) \
65 X("rcache policy", 0, RCACHESW) \
66 X("wcache policy", 0, WCACHESW) \
67 X("version", 0, VERSIONSW) \
68 X("help", 0, HELPSW) \
70 * switches for moreproc/mhlproc \
72 X("moreproc program", -4, PROGSW) \
73 X("nomoreproc", -3, NPROGSW) \
74 X("length lines", -4, LENSW) \
75 X("width columns", -4, WIDTHSW) \
77 * switches for debugging \
79 X("debug", -5, DEBUGSW) \
81 #define X(sw, minchars, id) id,
82 DEFINE_SWITCH_ENUM(MHSHOW
);
85 #define X(sw, minchars, id) { sw, minchars, id },
86 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
93 #define quitser pipeser
98 static void pipeser (int);
102 main (int argc
, char **argv
)
104 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
105 char *cp
, *file
= NULL
;
106 char *maildir
, buf
[100], **argp
;
108 struct msgs_array msgs
= { 0, 0, NULL
};
109 struct msgs
*mp
= NULL
;
113 if (nmh_init(argv
[0], true, true)) { return 1; }
115 set_done(freects_done
);
117 arguments
= getarguments (invo_name
, argc
, argv
, 1);
123 while ((cp
= *argp
++)) {
125 switch (smatch (++cp
, switches
)) {
127 ambigsw (cp
, switches
);
130 die("-%s unknown", cp
);
133 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
135 print_help (buf
, switches
, 1);
138 print_version(invo_name
);
142 icachesw
= &rcachesw
;
145 icachesw
= &wcachesw
;
147 if (!(cp
= *argp
++) || *cp
== '-')
148 die("missing argument to %s", argp
[-2]);
149 switch (*icachesw
= smatch (cp
, cache_policy
)) {
151 ambigsw (cp
, cache_policy
);
154 die("%s unknown", cp
);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
190 die("too many parts (starting with %s), %d max",
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
199 die("too many types (starting with %s), %d max",
205 if (!(cp
= *argp
++) || *cp
== '-')
206 die("missing argument to %s", argp
[-2]);
207 if (npreferred
>= NPREFS
)
208 die("too many preferred types (starting with %s), %d max",
210 mime_preference
[npreferred
].type
= cp
;
211 cp
= strchr(cp
, '/');
212 if (cp
) *cp
++ = '\0';
213 mime_preference
[npreferred
++].subtype
= cp
;
221 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
222 die("missing argument to %s", argp
[-2]);
223 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
227 if (!(cp
= *argp
++) || *cp
== '-')
228 die("missing argument to %s", argp
[-2]);
230 formsw
= mh_xstrdup(etcpath(cp
));
241 if (!(headerform
= *argp
++) || *headerform
== '-')
242 die("missing argument to %s", argp
[-2]);
246 if (!(markerform
= *argp
++) || *markerform
== '-')
247 die("missing argument to %s", argp
[-2]);
251 * Switches for moreproc/mhlproc
254 if (!(progsw
= *argp
++) || *progsw
== '-')
255 die("missing argument to %s", argp
[-2]);
263 if (!(cp
= *argp
++) || *cp
== '-')
264 die("missing argument to %s", argp
[-2]);
278 if (*cp
== '+' || *cp
== '@') {
280 die("only one folder at a time!");
281 folder
= pluspath (cp
);
283 app_msgarg(&msgs
, cp
);
286 /* null terminate the list of acceptable parts/types */
291 * If we had any specific parts or types specified, turn off text only
295 if (npart
> 0 || ntype
> 0) {
298 if (inlineonly
== -1)
303 * Check if we've specified an additional profile
305 if ((cp
= getenv ("MHSHOW"))) {
306 if ((fp
= fopen (cp
, "r"))) {
307 readconfig(NULL
, fp
, cp
, 0);
310 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
315 * Read the standard profile setup
317 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
318 readconfig(NULL
, fp
, cp
, 0);
322 /* Check for public cache location */
323 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
326 /* Check for private cache location */
327 if (!(cache_private
= context_find (nmhprivcache
)))
328 cache_private
= ".cache";
329 cache_private
= mh_xstrdup(m_maildir(cache_private
));
331 if (!context_find ("path"))
332 free (path ("./", TFOLDER
));
334 if (file
&& msgs
.size
)
335 die("cannot specify msg and file at same time!");
338 * check if message is coming from file
341 cts
= mh_xcalloc(2, sizeof *cts
);
344 if ((ct
= parse_mime (file
)))
350 * message(s) are coming from a folder
353 app_msgarg(&msgs
, "cur");
355 folder
= getfolder (1);
356 maildir
= m_maildir (folder
);
358 if (chdir (maildir
) == NOTOK
)
359 adios (maildir
, "unable to change directory to");
361 /* read folder and create message structure */
362 if (!(mp
= folder_read (folder
, 1)))
363 die("unable to read folder %s", folder
);
365 /* check for empty folder */
367 die("no messages in %s", folder
);
369 /* parse all the message ranges/sequences and set SELECTED */
370 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
371 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
375 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
376 * since we will use that as a tag to know which messages
377 * to remove from the "unseen" sequence.
379 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
380 if (is_selected(mp
, msgnum
))
381 set_unseen (mp
, msgnum
);
383 seq_setprev (mp
); /* set the Previous-Sequence */
384 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
386 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
390 * Parse all the SELECTED messages.
392 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
393 if (is_selected(mp
, msgnum
)) {
396 msgnam
= m_name (msgnum
);
397 if ((ct
= parse_mime (msgnam
)))
407 SIGNAL (SIGQUIT
, quitser
);
408 SIGNAL (SIGPIPE
, pipeser
);
411 * Get the associated umask for the relevant contents.
413 for (ctp
= cts
; *ctp
; ctp
++) {
417 if (type_ok (ct
, 1) && !ct
->c_umask
) {
418 if (stat (ct
->c_file
, &st
) != NOTOK
)
419 ct
->c_umask
= ~(st
.st_mode
& 0777);
421 ct
->c_umask
= ~m_gmprot();
425 /* If reading from a folder, do some updating */
427 context_replace (pfolder
, folder
);/* update current folder */
428 seq_setcur (mp
, mp
->hghsel
); /* update current message */
429 seq_save (mp
); /* synchronize sequences */
430 context_save (); /* save the context file */
434 m_popen(moreproc
, 0);
437 * Show the message content
439 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
441 /* Now free all the structures for the content */
442 for (ctp
= cts
; *ctp
; ctp
++)
461 fprintf (stderr
, "\n");