]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
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.
10 #include <h/signals.h>
14 #include <h/fmt_scan.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_popen.h"
24 #include "mhshowsbr.h"
26 #define MHSHOW_SWITCHES \
27 X("check", 0, CHECKSW) \
28 X("nocheck", 0, NCHECKSW) \
29 X("verbose", 0, VERBSW) \
30 X("noverbose", 0, NVERBSW) \
31 X("concat", 0, CONCATSW) \
32 X("noconcat", 0, NCONCATSW) \
33 X("textonly", 0, TEXTONLYSW) \
34 X("notextonly", 0, NTEXTONLYSW) \
35 X("inlineonly", 0, INLINESW) \
36 X("noinlineonly", 0, NINLINESW) \
37 X("file file", 0, FILESW) \
38 X("form formfile", 0, FORMSW) \
39 X("header", 0, HEADSW) \
40 X("noheader", 0, NHEADSW) \
41 X("headerform formfile", 0, HEADFORMSW) \
42 X("markform formfile", 0, MARKFORMSW) \
43 X("part number", 0, PARTSW) \
44 X("type content", 0, TYPESW) \
45 X("prefer content", 0, PREFERSW) \
46 X("noprefer", 0, NPREFERSW) \
47 X("rcache policy", 0, RCACHESW) \
48 X("wcache policy", 0, WCACHESW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 * switches for moreproc/mhlproc \
54 X("moreproc program", -4, PROGSW) \
55 X("nomoreproc", -3, NPROGSW) \
56 X("length lines", -4, LENSW) \
57 X("width columns", -4, WIDTHSW) \
59 * switches for debugging \
61 X("debug", -5, DEBUGSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(MHSHOW
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
75 #define quitser pipeser
80 static void pipeser (int);
84 main (int argc
, char **argv
)
86 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
87 char *cp
, *file
= NULL
;
88 char *maildir
, buf
[100], **argp
;
90 struct msgs_array msgs
= { 0, 0, NULL
};
91 struct msgs
*mp
= NULL
;
95 if (nmh_init(argv
[0], true, true)) { return 1; }
97 set_done(freects_done
);
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
105 while ((cp
= *argp
++)) {
107 switch (smatch (++cp
, switches
)) {
109 ambigsw (cp
, switches
);
112 die("-%s unknown", cp
);
115 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
117 print_help (buf
, switches
, 1);
120 print_version(invo_name
);
124 icachesw
= &rcachesw
;
127 icachesw
= &wcachesw
;
129 if (!(cp
= *argp
++) || *cp
== '-')
130 die("missing argument to %s", argp
[-2]);
131 switch (*icachesw
= smatch (cp
, cache_policy
)) {
133 ambigsw (cp
, cache_policy
);
136 die("%s unknown", cp
);
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
172 die("too many parts (starting with %s), %d max",
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
181 die("too many types (starting with %s), %d max",
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 if (npreferred
>= NPREFS
)
190 die("too many preferred types (starting with %s), %d max",
192 mime_preference
[npreferred
].type
= cp
;
193 cp
= strchr(cp
, '/');
194 if (cp
) *cp
++ = '\0';
195 mime_preference
[npreferred
++].subtype
= cp
;
203 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
204 die("missing argument to %s", argp
[-2]);
205 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
209 if (!(cp
= *argp
++) || *cp
== '-')
210 die("missing argument to %s", argp
[-2]);
212 formsw
= mh_xstrdup(etcpath(cp
));
223 if (!(headerform
= *argp
++) || *headerform
== '-')
224 die("missing argument to %s", argp
[-2]);
228 if (!(markerform
= *argp
++) || *markerform
== '-')
229 die("missing argument to %s", argp
[-2]);
233 * Switches for moreproc/mhlproc
236 if (!(progsw
= *argp
++) || *progsw
== '-')
237 die("missing argument to %s", argp
[-2]);
245 if (!(cp
= *argp
++) || *cp
== '-')
246 die("missing argument to %s", argp
[-2]);
260 if (*cp
== '+' || *cp
== '@') {
262 die("only one folder at a time!");
263 folder
= pluspath (cp
);
265 app_msgarg(&msgs
, cp
);
268 /* null terminate the list of acceptable parts/types */
273 * If we had any specific parts or types specified, turn off text only
277 if (npart
> 0 || ntype
> 0) {
280 if (inlineonly
== -1)
285 * Check if we've specified an additional profile
287 if ((cp
= getenv ("MHSHOW"))) {
288 if ((fp
= fopen (cp
, "r"))) {
289 readconfig(NULL
, fp
, cp
, 0);
292 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
297 * Read the standard profile setup
299 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
300 readconfig(NULL
, fp
, cp
, 0);
304 /* Check for public cache location */
305 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
308 /* Check for private cache location */
309 if (!(cache_private
= context_find (nmhprivcache
)))
310 cache_private
= ".cache";
311 cache_private
= mh_xstrdup(m_maildir(cache_private
));
313 if (!context_find ("path"))
314 free (path ("./", TFOLDER
));
316 if (file
&& msgs
.size
)
317 die("cannot specify msg and file at same time!");
320 * check if message is coming from file
323 cts
= mh_xcalloc(2, sizeof *cts
);
326 if ((ct
= parse_mime (file
)))
332 * message(s) are coming from a folder
335 app_msgarg(&msgs
, "cur");
337 folder
= getfolder (1);
338 maildir
= m_maildir (folder
);
340 if (chdir (maildir
) == NOTOK
)
341 adios (maildir
, "unable to change directory to");
343 /* read folder and create message structure */
344 if (!(mp
= folder_read (folder
, 1)))
345 die("unable to read folder %s", folder
);
347 /* check for empty folder */
349 die("no messages in %s", folder
);
351 /* parse all the message ranges/sequences and set SELECTED */
352 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
353 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
357 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
358 * since we will use that as a tag to know which messages
359 * to remove from the "unseen" sequence.
361 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
362 if (is_selected(mp
, msgnum
))
363 set_unseen (mp
, msgnum
);
365 seq_setprev (mp
); /* set the Previous-Sequence */
366 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
368 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
372 * Parse all the SELECTED messages.
374 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
375 if (is_selected(mp
, msgnum
)) {
378 msgnam
= m_name (msgnum
);
379 if ((ct
= parse_mime (msgnam
)))
389 SIGNAL (SIGQUIT
, quitser
);
390 SIGNAL (SIGPIPE
, pipeser
);
393 * Get the associated umask for the relevant contents.
395 for (ctp
= cts
; *ctp
; ctp
++) {
399 if (type_ok (ct
, 1) && !ct
->c_umask
) {
400 if (stat (ct
->c_file
, &st
) != NOTOK
)
401 ct
->c_umask
= ~(st
.st_mode
& 0777);
403 ct
->c_umask
= ~m_gmprot();
407 /* If reading from a folder, do some updating */
409 context_replace (pfolder
, folder
);/* update current folder */
410 seq_setcur (mp
, mp
->hghsel
); /* update current message */
411 seq_save (mp
); /* synchronize sequences */
412 context_save (); /* save the context file */
416 m_popen(moreproc
, 0);
419 * Show the message content
421 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
423 /* Now free all the structures for the content */
424 for (ctp
= cts
; *ctp
; ctp
++)
443 fprintf (stderr
, "\n");