]>
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 "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "h/signals.h"
18 #include "h/fmt_scan.h"
20 #include "h/mhparse.h"
21 #include "h/mhcachesbr.h"
25 #include "sbr/m_maildir.h"
26 #include "sbr/m_popen.h"
28 #include "mhshowsbr.h"
30 #define MHSHOW_SWITCHES \
31 X("check", 0, CHECKSW) \
32 X("nocheck", 0, NCHECKSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("concat", 0, CONCATSW) \
36 X("noconcat", 0, NCONCATSW) \
37 X("textonly", 0, TEXTONLYSW) \
38 X("notextonly", 0, NTEXTONLYSW) \
39 X("inlineonly", 0, INLINESW) \
40 X("noinlineonly", 0, NINLINESW) \
41 X("file file", 0, FILESW) \
42 X("form formfile", 0, FORMSW) \
43 X("header", 0, HEADSW) \
44 X("noheader", 0, NHEADSW) \
45 X("headerform formfile", 0, HEADFORMSW) \
46 X("markform formfile", 0, MARKFORMSW) \
47 X("part number", 0, PARTSW) \
48 X("type content", 0, TYPESW) \
49 X("prefer content", 0, PREFERSW) \
50 X("noprefer", 0, NPREFERSW) \
51 X("rcache policy", 0, RCACHESW) \
52 X("wcache policy", 0, WCACHESW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
56 * switches for moreproc/mhlproc \
58 X("moreproc program", -4, PROGSW) \
59 X("nomoreproc", -3, NPROGSW) \
60 X("length lines", -4, LENSW) \
61 X("width columns", -4, WIDTHSW) \
63 * switches for debugging \
65 X("debug", -5, DEBUGSW) \
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(MHSHOW
);
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
79 #define quitser pipeser
84 static void pipeser (int);
88 main (int argc
, char **argv
)
90 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
91 char *cp
, *file
= NULL
;
92 char *maildir
, buf
[100], **argp
;
94 struct msgs_array msgs
= { 0, 0, NULL
};
95 struct msgs
*mp
= NULL
;
99 if (nmh_init(argv
[0], true, true)) { return 1; }
101 set_done(freects_done
);
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
109 while ((cp
= *argp
++)) {
111 switch (smatch (++cp
, switches
)) {
113 ambigsw (cp
, switches
);
116 die("-%s unknown", cp
);
119 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
121 print_help (buf
, switches
, 1);
124 print_version(invo_name
);
128 icachesw
= &rcachesw
;
131 icachesw
= &wcachesw
;
133 if (!(cp
= *argp
++) || *cp
== '-')
134 die("missing argument to %s", argp
[-2]);
135 switch (*icachesw
= smatch (cp
, cache_policy
)) {
137 ambigsw (cp
, cache_policy
);
140 die("%s unknown", cp
);
173 if (!(cp
= *argp
++) || *cp
== '-')
174 die("missing argument to %s", argp
[-2]);
176 die("too many parts (starting with %s), %d max",
182 if (!(cp
= *argp
++) || *cp
== '-')
183 die("missing argument to %s", argp
[-2]);
185 die("too many types (starting with %s), %d max",
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 if (npreferred
>= NPREFS
)
194 die("too many preferred types (starting with %s), %d max",
196 mime_preference
[npreferred
].type
= cp
;
197 cp
= strchr(cp
, '/');
198 if (cp
) *cp
++ = '\0';
199 mime_preference
[npreferred
++].subtype
= cp
;
207 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
208 die("missing argument to %s", argp
[-2]);
209 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
213 if (!(cp
= *argp
++) || *cp
== '-')
214 die("missing argument to %s", argp
[-2]);
216 formsw
= mh_xstrdup(etcpath(cp
));
227 if (!(headerform
= *argp
++) || *headerform
== '-')
228 die("missing argument to %s", argp
[-2]);
232 if (!(markerform
= *argp
++) || *markerform
== '-')
233 die("missing argument to %s", argp
[-2]);
237 * Switches for moreproc/mhlproc
240 if (!(progsw
= *argp
++) || *progsw
== '-')
241 die("missing argument to %s", argp
[-2]);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 die("missing argument to %s", argp
[-2]);
264 if (*cp
== '+' || *cp
== '@') {
266 die("only one folder at a time!");
267 folder
= pluspath (cp
);
269 app_msgarg(&msgs
, cp
);
272 /* null terminate the list of acceptable parts/types */
277 * If we had any specific parts or types specified, turn off text only
281 if (npart
> 0 || ntype
> 0) {
284 if (inlineonly
== -1)
289 * Check if we've specified an additional profile
291 if ((cp
= getenv ("MHSHOW"))) {
292 if ((fp
= fopen (cp
, "r"))) {
293 readconfig(NULL
, fp
, cp
, 0);
296 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
301 * Read the standard profile setup
303 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
304 readconfig(NULL
, fp
, cp
, 0);
308 /* Check for public cache location */
309 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
312 /* Check for private cache location */
313 if (!(cache_private
= context_find (nmhprivcache
)))
314 cache_private
= ".cache";
315 cache_private
= mh_xstrdup(m_maildir(cache_private
));
317 if (!context_find ("path"))
318 free (path ("./", TFOLDER
));
320 if (file
&& msgs
.size
)
321 die("cannot specify msg and file at same time!");
324 * check if message is coming from file
327 cts
= mh_xcalloc(2, sizeof *cts
);
330 if ((ct
= parse_mime (file
)))
336 * message(s) are coming from a folder
339 app_msgarg(&msgs
, "cur");
341 folder
= getfolder (1);
342 maildir
= m_maildir (folder
);
344 if (chdir (maildir
) == NOTOK
)
345 adios (maildir
, "unable to change directory to");
347 /* read folder and create message structure */
348 if (!(mp
= folder_read (folder
, 1)))
349 die("unable to read folder %s", folder
);
351 /* check for empty folder */
353 die("no messages in %s", folder
);
355 /* parse all the message ranges/sequences and set SELECTED */
356 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
357 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
361 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
362 * since we will use that as a tag to know which messages
363 * to remove from the "unseen" sequence.
365 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
366 if (is_selected(mp
, msgnum
))
367 set_unseen (mp
, msgnum
);
369 seq_setprev (mp
); /* set the Previous-Sequence */
370 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
372 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
376 * Parse all the SELECTED messages.
378 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
379 if (is_selected(mp
, msgnum
)) {
382 msgnam
= m_name (msgnum
);
383 if ((ct
= parse_mime (msgnam
)))
393 SIGNAL (SIGQUIT
, quitser
);
394 SIGNAL (SIGPIPE
, pipeser
);
397 * Get the associated umask for the relevant contents.
399 for (ctp
= cts
; *ctp
; ctp
++) {
403 if (type_ok (ct
, 1) && !ct
->c_umask
) {
404 if (stat (ct
->c_file
, &st
) != NOTOK
)
405 ct
->c_umask
= ~(st
.st_mode
& 0777);
407 ct
->c_umask
= ~m_gmprot();
411 /* If reading from a folder, do some updating */
413 context_replace (pfolder
, folder
);/* update current folder */
414 seq_setcur (mp
, mp
->hghsel
); /* update current message */
415 seq_save (mp
); /* synchronize sequences */
416 context_save (); /* save the context file */
420 m_popen(moreproc
, 0);
423 * Show the message content
425 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
427 /* Now free all the structures for the content */
428 for (ctp
= cts
; *ctp
; ctp
++)
447 fprintf (stderr
, "\n");