]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
fcf045228821c704eba0051f0767debab4c5abdf
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/readconfig.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
16 #include "h/signals.h"
20 #include "h/fmt_scan.h"
22 #include "h/mhparse.h"
23 #include "h/mhcachesbr.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_popen.h"
30 #include "mhshowsbr.h"
32 #define MHSHOW_SWITCHES \
33 X("check", 0, CHECKSW) \
34 X("nocheck", 0, NCHECKSW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("concat", 0, CONCATSW) \
38 X("noconcat", 0, NCONCATSW) \
39 X("textonly", 0, TEXTONLYSW) \
40 X("notextonly", 0, NTEXTONLYSW) \
41 X("inlineonly", 0, INLINESW) \
42 X("noinlineonly", 0, NINLINESW) \
43 X("file file", 0, FILESW) \
44 X("form formfile", 0, FORMSW) \
45 X("header", 0, HEADSW) \
46 X("noheader", 0, NHEADSW) \
47 X("headerform formfile", 0, HEADFORMSW) \
48 X("markform formfile", 0, MARKFORMSW) \
49 X("part number", 0, PARTSW) \
50 X("type content", 0, TYPESW) \
51 X("prefer content", 0, PREFERSW) \
52 X("noprefer", 0, NPREFERSW) \
53 X("rcache policy", 0, RCACHESW) \
54 X("wcache policy", 0, WCACHESW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
58 * switches for moreproc/mhlproc \
60 X("moreproc program", -4, PROGSW) \
61 X("nomoreproc", -3, NPROGSW) \
62 X("length lines", -4, LENSW) \
63 X("width columns", -4, WIDTHSW) \
65 * switches for debugging \
67 X("debug", -5, DEBUGSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(MHSHOW
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
81 #define quitser pipeser
86 static void pipeser (int);
90 main (int argc
, char **argv
)
92 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
93 char *cp
, *file
= NULL
;
94 char *maildir
, buf
[100], **argp
;
96 struct msgs_array msgs
= { 0, 0, NULL
};
97 struct msgs
*mp
= NULL
;
101 if (nmh_init(argv
[0], true, true)) { return 1; }
103 set_done(freects_done
);
105 arguments
= getarguments (invo_name
, argc
, argv
, 1);
111 while ((cp
= *argp
++)) {
113 switch (smatch (++cp
, switches
)) {
115 ambigsw (cp
, switches
);
118 die("-%s unknown", cp
);
121 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
123 print_help (buf
, switches
, 1);
126 print_version(invo_name
);
130 icachesw
= &rcachesw
;
133 icachesw
= &wcachesw
;
135 if (!(cp
= *argp
++) || *cp
== '-')
136 die("missing argument to %s", argp
[-2]);
137 switch (*icachesw
= smatch (cp
, cache_policy
)) {
139 ambigsw (cp
, cache_policy
);
142 die("%s unknown", cp
);
175 if (!(cp
= *argp
++) || *cp
== '-')
176 die("missing argument to %s", argp
[-2]);
178 die("too many parts (starting with %s), %d max",
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
187 die("too many types (starting with %s), %d max",
193 if (!(cp
= *argp
++) || *cp
== '-')
194 die("missing argument to %s", argp
[-2]);
195 if (npreferred
>= NPREFS
)
196 die("too many preferred types (starting with %s), %d max",
198 mime_preference
[npreferred
].type
= cp
;
199 cp
= strchr(cp
, '/');
200 if (cp
) *cp
++ = '\0';
201 mime_preference
[npreferred
++].subtype
= cp
;
209 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
210 die("missing argument to %s", argp
[-2]);
211 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
215 if (!(cp
= *argp
++) || *cp
== '-')
216 die("missing argument to %s", argp
[-2]);
218 formsw
= mh_xstrdup(etcpath(cp
));
229 if (!(headerform
= *argp
++) || *headerform
== '-')
230 die("missing argument to %s", argp
[-2]);
234 if (!(markerform
= *argp
++) || *markerform
== '-')
235 die("missing argument to %s", argp
[-2]);
239 * Switches for moreproc/mhlproc
242 if (!(progsw
= *argp
++) || *progsw
== '-')
243 die("missing argument to %s", argp
[-2]);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
266 if (*cp
== '+' || *cp
== '@') {
268 die("only one folder at a time!");
269 folder
= pluspath (cp
);
271 app_msgarg(&msgs
, cp
);
274 /* null terminate the list of acceptable parts/types */
279 * If we had any specific parts or types specified, turn off text only
283 if (npart
> 0 || ntype
> 0) {
286 if (inlineonly
== -1)
291 * Check if we've specified an additional profile
293 if ((cp
= getenv ("MHSHOW"))) {
294 if ((fp
= fopen (cp
, "r"))) {
295 readconfig(NULL
, fp
, cp
, 0);
298 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
303 * Read the standard profile setup
305 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
306 readconfig(NULL
, fp
, cp
, 0);
310 /* Check for public cache location */
311 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
314 /* Check for private cache location */
315 if (!(cache_private
= context_find (nmhprivcache
)))
316 cache_private
= ".cache";
317 cache_private
= mh_xstrdup(m_maildir(cache_private
));
319 if (!context_find ("path"))
320 free (path ("./", TFOLDER
));
322 if (file
&& msgs
.size
)
323 die("cannot specify msg and file at same time!");
326 * check if message is coming from file
329 cts
= mh_xcalloc(2, sizeof *cts
);
332 if ((ct
= parse_mime (file
)))
338 * message(s) are coming from a folder
341 app_msgarg(&msgs
, "cur");
343 folder
= getfolder (1);
344 maildir
= m_maildir (folder
);
346 if (chdir (maildir
) == NOTOK
)
347 adios (maildir
, "unable to change directory to");
349 /* read folder and create message structure */
350 if (!(mp
= folder_read (folder
, 1)))
351 die("unable to read folder %s", folder
);
353 /* check for empty folder */
355 die("no messages in %s", folder
);
357 /* parse all the message ranges/sequences and set SELECTED */
358 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
359 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
363 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
364 * since we will use that as a tag to know which messages
365 * to remove from the "unseen" sequence.
367 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
368 if (is_selected(mp
, msgnum
))
369 set_unseen (mp
, msgnum
);
371 seq_setprev (mp
); /* set the Previous-Sequence */
372 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
374 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
378 * Parse all the SELECTED messages.
380 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
381 if (is_selected(mp
, msgnum
)) {
384 msgnam
= m_name (msgnum
);
385 if ((ct
= parse_mime (msgnam
)))
395 SIGNAL (SIGQUIT
, quitser
);
396 SIGNAL (SIGPIPE
, pipeser
);
399 * Get the associated umask for the relevant contents.
401 for (ctp
= cts
; *ctp
; ctp
++) {
405 if (type_ok (ct
, 1) && !ct
->c_umask
) {
406 if (stat (ct
->c_file
, &st
) != NOTOK
)
407 ct
->c_umask
= ~(st
.st_mode
& 0777);
409 ct
->c_umask
= ~m_gmprot();
413 /* If reading from a folder, do some updating */
415 context_replace (pfolder
, folder
);/* update current folder */
416 seq_setcur (mp
, mp
->hghsel
); /* update current message */
417 seq_save (mp
); /* synchronize sequences */
418 context_save (); /* save the context file */
422 m_popen(moreproc
, 0);
425 * Show the message content
427 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
429 /* Now free all the structures for the content */
430 for (ctp
= cts
; *ctp
; ctp
++)
449 fprintf (stderr
, "\n");