]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
bad75b36fe30ec5994637bf86b6dfa89009c5c31
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/context_find.h"
10 #include "sbr/readconfig.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
17 #include "h/signals.h"
21 #include "h/fmt_scan.h"
23 #include "h/mhparse.h"
24 #include "h/mhcachesbr.h"
28 #include "sbr/m_maildir.h"
29 #include "sbr/m_popen.h"
31 #include "mhshowsbr.h"
33 #define MHSHOW_SWITCHES \
34 X("check", 0, CHECKSW) \
35 X("nocheck", 0, NCHECKSW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("concat", 0, CONCATSW) \
39 X("noconcat", 0, NCONCATSW) \
40 X("textonly", 0, TEXTONLYSW) \
41 X("notextonly", 0, NTEXTONLYSW) \
42 X("inlineonly", 0, INLINESW) \
43 X("noinlineonly", 0, NINLINESW) \
44 X("file file", 0, FILESW) \
45 X("form formfile", 0, FORMSW) \
46 X("header", 0, HEADSW) \
47 X("noheader", 0, NHEADSW) \
48 X("headerform formfile", 0, HEADFORMSW) \
49 X("markform formfile", 0, MARKFORMSW) \
50 X("part number", 0, PARTSW) \
51 X("type content", 0, TYPESW) \
52 X("prefer content", 0, PREFERSW) \
53 X("noprefer", 0, NPREFERSW) \
54 X("rcache policy", 0, RCACHESW) \
55 X("wcache policy", 0, WCACHESW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
59 * switches for moreproc/mhlproc \
61 X("moreproc program", -4, PROGSW) \
62 X("nomoreproc", -3, NPROGSW) \
63 X("length lines", -4, LENSW) \
64 X("width columns", -4, WIDTHSW) \
66 * switches for debugging \
68 X("debug", -5, DEBUGSW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(MHSHOW
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
82 #define quitser pipeser
87 static void pipeser (int);
91 main (int argc
, char **argv
)
93 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
94 char *cp
, *file
= NULL
;
95 char *maildir
, buf
[100], **argp
;
97 struct msgs_array msgs
= { 0, 0, NULL
};
98 struct msgs
*mp
= NULL
;
102 if (nmh_init(argv
[0], true, true)) { return 1; }
104 set_done(freects_done
);
106 arguments
= getarguments (invo_name
, argc
, argv
, 1);
112 while ((cp
= *argp
++)) {
114 switch (smatch (++cp
, switches
)) {
116 ambigsw (cp
, switches
);
119 die("-%s unknown", cp
);
122 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
124 print_help (buf
, switches
, 1);
127 print_version(invo_name
);
131 icachesw
= &rcachesw
;
134 icachesw
= &wcachesw
;
136 if (!(cp
= *argp
++) || *cp
== '-')
137 die("missing argument to %s", argp
[-2]);
138 switch (*icachesw
= smatch (cp
, cache_policy
)) {
140 ambigsw (cp
, cache_policy
);
143 die("%s unknown", cp
);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 die("missing argument to %s", argp
[-2]);
179 die("too many parts (starting with %s), %d max",
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
188 die("too many types (starting with %s), %d max",
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
196 if (npreferred
>= NPREFS
)
197 die("too many preferred types (starting with %s), %d max",
199 mime_preference
[npreferred
].type
= cp
;
200 cp
= strchr(cp
, '/');
201 if (cp
) *cp
++ = '\0';
202 mime_preference
[npreferred
++].subtype
= cp
;
210 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
211 die("missing argument to %s", argp
[-2]);
212 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
216 if (!(cp
= *argp
++) || *cp
== '-')
217 die("missing argument to %s", argp
[-2]);
219 formsw
= mh_xstrdup(etcpath(cp
));
230 if (!(headerform
= *argp
++) || *headerform
== '-')
231 die("missing argument to %s", argp
[-2]);
235 if (!(markerform
= *argp
++) || *markerform
== '-')
236 die("missing argument to %s", argp
[-2]);
240 * Switches for moreproc/mhlproc
243 if (!(progsw
= *argp
++) || *progsw
== '-')
244 die("missing argument to %s", argp
[-2]);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 die("missing argument to %s", argp
[-2]);
267 if (*cp
== '+' || *cp
== '@') {
269 die("only one folder at a time!");
270 folder
= pluspath (cp
);
272 app_msgarg(&msgs
, cp
);
275 /* null terminate the list of acceptable parts/types */
280 * If we had any specific parts or types specified, turn off text only
284 if (npart
> 0 || ntype
> 0) {
287 if (inlineonly
== -1)
292 * Check if we've specified an additional profile
294 if ((cp
= getenv ("MHSHOW"))) {
295 if ((fp
= fopen (cp
, "r"))) {
296 readconfig(NULL
, fp
, cp
, 0);
299 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
304 * Read the standard profile setup
306 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
307 readconfig(NULL
, fp
, cp
, 0);
311 /* Check for public cache location */
312 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
315 /* Check for private cache location */
316 if (!(cache_private
= context_find (nmhprivcache
)))
317 cache_private
= ".cache";
318 cache_private
= mh_xstrdup(m_maildir(cache_private
));
320 if (!context_find ("path"))
321 free (path ("./", TFOLDER
));
323 if (file
&& msgs
.size
)
324 die("cannot specify msg and file at same time!");
327 * check if message is coming from file
330 cts
= mh_xcalloc(2, sizeof *cts
);
333 if ((ct
= parse_mime (file
)))
339 * message(s) are coming from a folder
342 app_msgarg(&msgs
, "cur");
344 folder
= getfolder (1);
345 maildir
= m_maildir (folder
);
347 if (chdir (maildir
) == NOTOK
)
348 adios (maildir
, "unable to change directory to");
350 /* read folder and create message structure */
351 if (!(mp
= folder_read (folder
, 1)))
352 die("unable to read folder %s", folder
);
354 /* check for empty folder */
356 die("no messages in %s", folder
);
358 /* parse all the message ranges/sequences and set SELECTED */
359 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
360 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
364 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
365 * since we will use that as a tag to know which messages
366 * to remove from the "unseen" sequence.
368 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
369 if (is_selected(mp
, msgnum
))
370 set_unseen (mp
, msgnum
);
372 seq_setprev (mp
); /* set the Previous-Sequence */
373 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
375 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
379 * Parse all the SELECTED messages.
381 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
382 if (is_selected(mp
, msgnum
)) {
385 msgnam
= m_name (msgnum
);
386 if ((ct
= parse_mime (msgnam
)))
396 SIGNAL (SIGQUIT
, quitser
);
397 SIGNAL (SIGPIPE
, pipeser
);
400 * Get the associated umask for the relevant contents.
402 for (ctp
= cts
; *ctp
; ctp
++) {
406 if (type_ok (ct
, 1) && !ct
->c_umask
) {
407 if (stat (ct
->c_file
, &st
) != NOTOK
)
408 ct
->c_umask
= ~(st
.st_mode
& 0777);
410 ct
->c_umask
= ~m_gmprot();
414 /* If reading from a folder, do some updating */
416 context_replace (pfolder
, folder
);/* update current folder */
417 seq_setcur (mp
, mp
->hghsel
); /* update current message */
418 seq_save (mp
); /* synchronize sequences */
419 context_save (); /* save the context file */
423 m_popen(moreproc
, 0);
426 * Show the message content
428 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
430 /* Now free all the structures for the content */
431 for (ctp
= cts
; *ctp
; ctp
++)
450 fprintf (stderr
, "\n");