]>
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.
9 #include "sbr/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/readconfig.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
20 #include "h/signals.h"
24 #include "h/fmt_scan.h"
26 #include "h/mhparse.h"
27 #include "h/mhcachesbr.h"
31 #include "sbr/m_maildir.h"
32 #include "sbr/m_popen.h"
34 #include "mhshowsbr.h"
36 #define MHSHOW_SWITCHES \
37 X("check", 0, CHECKSW) \
38 X("nocheck", 0, NCHECKSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("concat", 0, CONCATSW) \
42 X("noconcat", 0, NCONCATSW) \
43 X("textonly", 0, TEXTONLYSW) \
44 X("notextonly", 0, NTEXTONLYSW) \
45 X("inlineonly", 0, INLINESW) \
46 X("noinlineonly", 0, NINLINESW) \
47 X("file file", 0, FILESW) \
48 X("form formfile", 0, FORMSW) \
49 X("header", 0, HEADSW) \
50 X("noheader", 0, NHEADSW) \
51 X("headerform formfile", 0, HEADFORMSW) \
52 X("markform formfile", 0, MARKFORMSW) \
53 X("part number", 0, PARTSW) \
54 X("type content", 0, TYPESW) \
55 X("prefer content", 0, PREFERSW) \
56 X("noprefer", 0, NPREFERSW) \
57 X("rcache policy", 0, RCACHESW) \
58 X("wcache policy", 0, WCACHESW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
62 * switches for moreproc/mhlproc \
64 X("moreproc program", -4, PROGSW) \
65 X("nomoreproc", -3, NPROGSW) \
66 X("length lines", -4, LENSW) \
67 X("width columns", -4, WIDTHSW) \
69 * switches for debugging \
71 X("debug", -5, DEBUGSW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(MHSHOW
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
85 #define quitser pipeser
90 static void pipeser (int);
94 main (int argc
, char **argv
)
96 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
97 char *cp
, *file
= NULL
;
98 char *maildir
, buf
[100], **argp
;
100 struct msgs_array msgs
= { 0, 0, NULL
};
101 struct msgs
*mp
= NULL
;
105 if (nmh_init(argv
[0], true, true)) { return 1; }
107 set_done(freects_done
);
109 arguments
= getarguments (invo_name
, argc
, argv
, 1);
115 while ((cp
= *argp
++)) {
117 switch (smatch (++cp
, switches
)) {
119 ambigsw (cp
, switches
);
122 die("-%s unknown", cp
);
125 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
127 print_help (buf
, switches
, 1);
130 print_version(invo_name
);
134 icachesw
= &rcachesw
;
137 icachesw
= &wcachesw
;
139 if (!(cp
= *argp
++) || *cp
== '-')
140 die("missing argument to %s", argp
[-2]);
141 switch (*icachesw
= smatch (cp
, cache_policy
)) {
143 ambigsw (cp
, cache_policy
);
146 die("%s unknown", cp
);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
182 die("too many parts (starting with %s), %d max",
188 if (!(cp
= *argp
++) || *cp
== '-')
189 die("missing argument to %s", argp
[-2]);
191 die("too many types (starting with %s), %d max",
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
199 if (npreferred
>= NPREFS
)
200 die("too many preferred types (starting with %s), %d max",
202 mime_preference
[npreferred
].type
= cp
;
203 cp
= strchr(cp
, '/');
204 if (cp
) *cp
++ = '\0';
205 mime_preference
[npreferred
++].subtype
= cp
;
213 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
214 die("missing argument to %s", argp
[-2]);
215 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
219 if (!(cp
= *argp
++) || *cp
== '-')
220 die("missing argument to %s", argp
[-2]);
222 formsw
= mh_xstrdup(etcpath(cp
));
233 if (!(headerform
= *argp
++) || *headerform
== '-')
234 die("missing argument to %s", argp
[-2]);
238 if (!(markerform
= *argp
++) || *markerform
== '-')
239 die("missing argument to %s", argp
[-2]);
243 * Switches for moreproc/mhlproc
246 if (!(progsw
= *argp
++) || *progsw
== '-')
247 die("missing argument to %s", argp
[-2]);
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
270 if (*cp
== '+' || *cp
== '@') {
272 die("only one folder at a time!");
273 folder
= pluspath (cp
);
275 app_msgarg(&msgs
, cp
);
278 /* null terminate the list of acceptable parts/types */
283 * If we had any specific parts or types specified, turn off text only
287 if (npart
> 0 || ntype
> 0) {
290 if (inlineonly
== -1)
295 * Check if we've specified an additional profile
297 if ((cp
= getenv ("MHSHOW"))) {
298 if ((fp
= fopen (cp
, "r"))) {
299 readconfig(NULL
, fp
, cp
, 0);
302 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
307 * Read the standard profile setup
309 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
310 readconfig(NULL
, fp
, cp
, 0);
314 /* Check for public cache location */
315 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
318 /* Check for private cache location */
319 if (!(cache_private
= context_find (nmhprivcache
)))
320 cache_private
= ".cache";
321 cache_private
= mh_xstrdup(m_maildir(cache_private
));
323 if (!context_find ("path"))
324 free (path ("./", TFOLDER
));
326 if (file
&& msgs
.size
)
327 die("cannot specify msg and file at same time!");
330 * check if message is coming from file
333 cts
= mh_xcalloc(2, sizeof *cts
);
336 if ((ct
= parse_mime (file
)))
342 * message(s) are coming from a folder
345 app_msgarg(&msgs
, "cur");
347 folder
= getfolder (1);
348 maildir
= m_maildir (folder
);
350 if (chdir (maildir
) == NOTOK
)
351 adios (maildir
, "unable to change directory to");
353 /* read folder and create message structure */
354 if (!(mp
= folder_read (folder
, 1)))
355 die("unable to read folder %s", folder
);
357 /* check for empty folder */
359 die("no messages in %s", folder
);
361 /* parse all the message ranges/sequences and set SELECTED */
362 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
363 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
367 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
368 * since we will use that as a tag to know which messages
369 * to remove from the "unseen" sequence.
371 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
372 if (is_selected(mp
, msgnum
))
373 set_unseen (mp
, msgnum
);
375 seq_setprev (mp
); /* set the Previous-Sequence */
376 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
378 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
382 * Parse all the SELECTED messages.
384 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
385 if (is_selected(mp
, msgnum
)) {
388 msgnam
= m_name (msgnum
);
389 if ((ct
= parse_mime (msgnam
)))
399 SIGNAL (SIGQUIT
, quitser
);
400 SIGNAL (SIGPIPE
, pipeser
);
403 * Get the associated umask for the relevant contents.
405 for (ctp
= cts
; *ctp
; ctp
++) {
409 if (type_ok (ct
, 1) && !ct
->c_umask
) {
410 if (stat (ct
->c_file
, &st
) != NOTOK
)
411 ct
->c_umask
= ~(st
.st_mode
& 0777);
413 ct
->c_umask
= ~m_gmprot();
417 /* If reading from a folder, do some updating */
419 context_replace (pfolder
, folder
);/* update current folder */
420 seq_setcur (mp
, mp
->hghsel
); /* update current message */
421 seq_save (mp
); /* synchronize sequences */
422 context_save (); /* save the context file */
426 m_popen(moreproc
, 0);
429 * Show the message content
431 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
433 /* Now free all the structures for the content */
434 for (ctp
= cts
; *ctp
; ctp
++)
453 fprintf (stderr
, "\n");