]>
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/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setunseen.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/getfolder.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_find.h"
23 #include "sbr/readconfig.h"
24 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/error.h"
30 #include "h/signals.h"
34 #include "h/fmt_scan.h"
36 #include "h/mhparse.h"
37 #include "h/mhcachesbr.h"
41 #include "sbr/m_maildir.h"
42 #include "sbr/m_popen.h"
44 #include "mhshowsbr.h"
46 #define MHSHOW_SWITCHES \
47 X("check", 0, CHECKSW) \
48 X("nocheck", 0, NCHECKSW) \
49 X("verbose", 0, VERBSW) \
50 X("noverbose", 0, NVERBSW) \
51 X("concat", 0, CONCATSW) \
52 X("noconcat", 0, NCONCATSW) \
53 X("textonly", 0, TEXTONLYSW) \
54 X("notextonly", 0, NTEXTONLYSW) \
55 X("inlineonly", 0, INLINESW) \
56 X("noinlineonly", 0, NINLINESW) \
57 X("file file", 0, FILESW) \
58 X("form formfile", 0, FORMSW) \
59 X("header", 0, HEADSW) \
60 X("noheader", 0, NHEADSW) \
61 X("headerform formfile", 0, HEADFORMSW) \
62 X("markform formfile", 0, MARKFORMSW) \
63 X("part number", 0, PARTSW) \
64 X("type content", 0, TYPESW) \
65 X("prefer content", 0, PREFERSW) \
66 X("noprefer", 0, NPREFERSW) \
67 X("rcache policy", 0, RCACHESW) \
68 X("wcache policy", 0, WCACHESW) \
69 X("version", 0, VERSIONSW) \
70 X("help", 0, HELPSW) \
72 * switches for moreproc/mhlproc \
74 X("moreproc program", -4, PROGSW) \
75 X("nomoreproc", -3, NPROGSW) \
76 X("length lines", -4, LENSW) \
77 X("width columns", -4, WIDTHSW) \
79 * switches for debugging \
81 X("debug", -5, DEBUGSW) \
83 #define X(sw, minchars, id) id,
84 DEFINE_SWITCH_ENUM(MHSHOW
);
87 #define X(sw, minchars, id) { sw, minchars, id },
88 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
95 #define quitser pipeser
100 static void pipeser (int);
104 main (int argc
, char **argv
)
106 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
107 char *cp
, *file
= NULL
;
108 char *maildir
, buf
[100], **argp
;
110 struct msgs_array msgs
= { 0, 0, NULL
};
111 struct msgs
*mp
= NULL
;
115 if (nmh_init(argv
[0], true, true)) { return 1; }
117 set_done(freects_done
);
119 arguments
= getarguments (invo_name
, argc
, argv
, 1);
125 while ((cp
= *argp
++)) {
127 switch (smatch (++cp
, switches
)) {
129 ambigsw (cp
, switches
);
132 die("-%s unknown", cp
);
135 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
137 print_help (buf
, switches
, 1);
140 print_version(invo_name
);
144 icachesw
= &rcachesw
;
147 icachesw
= &wcachesw
;
149 if (!(cp
= *argp
++) || *cp
== '-')
150 die("missing argument to %s", argp
[-2]);
151 switch (*icachesw
= smatch (cp
, cache_policy
)) {
153 ambigsw (cp
, cache_policy
);
156 die("%s unknown", cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
192 die("too many parts (starting with %s), %d max",
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
201 die("too many types (starting with %s), %d max",
207 if (!(cp
= *argp
++) || *cp
== '-')
208 die("missing argument to %s", argp
[-2]);
209 if (npreferred
>= NPREFS
)
210 die("too many preferred types (starting with %s), %d max",
212 mime_preference
[npreferred
].type
= cp
;
213 cp
= strchr(cp
, '/');
214 if (cp
) *cp
++ = '\0';
215 mime_preference
[npreferred
++].subtype
= cp
;
223 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
224 die("missing argument to %s", argp
[-2]);
225 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
229 if (!(cp
= *argp
++) || *cp
== '-')
230 die("missing argument to %s", argp
[-2]);
232 formsw
= mh_xstrdup(etcpath(cp
));
243 if (!(headerform
= *argp
++) || *headerform
== '-')
244 die("missing argument to %s", argp
[-2]);
248 if (!(markerform
= *argp
++) || *markerform
== '-')
249 die("missing argument to %s", argp
[-2]);
253 * Switches for moreproc/mhlproc
256 if (!(progsw
= *argp
++) || *progsw
== '-')
257 die("missing argument to %s", argp
[-2]);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 die("missing argument to %s", argp
[-2]);
280 if (*cp
== '+' || *cp
== '@') {
282 die("only one folder at a time!");
283 folder
= pluspath (cp
);
285 app_msgarg(&msgs
, cp
);
288 /* null terminate the list of acceptable parts/types */
293 * If we had any specific parts or types specified, turn off text only
297 if (npart
> 0 || ntype
> 0) {
300 if (inlineonly
== -1)
305 * Check if we've specified an additional profile
307 if ((cp
= getenv ("MHSHOW"))) {
308 if ((fp
= fopen (cp
, "r"))) {
309 readconfig(NULL
, fp
, cp
, 0);
312 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
317 * Read the standard profile setup
319 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
320 readconfig(NULL
, fp
, cp
, 0);
324 /* Check for public cache location */
325 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
328 /* Check for private cache location */
329 if (!(cache_private
= context_find (nmhprivcache
)))
330 cache_private
= ".cache";
331 cache_private
= mh_xstrdup(m_maildir(cache_private
));
333 if (!context_find ("path"))
334 free (path ("./", TFOLDER
));
336 if (file
&& msgs
.size
)
337 die("cannot specify msg and file at same time!");
340 * check if message is coming from file
343 cts
= mh_xcalloc(2, sizeof *cts
);
346 if ((ct
= parse_mime (file
)))
352 * message(s) are coming from a folder
355 app_msgarg(&msgs
, "cur");
357 folder
= getfolder (1);
358 maildir
= m_maildir (folder
);
360 if (chdir (maildir
) == NOTOK
)
361 adios (maildir
, "unable to change directory to");
363 /* read folder and create message structure */
364 if (!(mp
= folder_read (folder
, 1)))
365 die("unable to read folder %s", folder
);
367 /* check for empty folder */
369 die("no messages in %s", folder
);
371 /* parse all the message ranges/sequences and set SELECTED */
372 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
373 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
377 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
378 * since we will use that as a tag to know which messages
379 * to remove from the "unseen" sequence.
381 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
382 if (is_selected(mp
, msgnum
))
383 set_unseen (mp
, msgnum
);
385 seq_setprev (mp
); /* set the Previous-Sequence */
386 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
388 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
392 * Parse all the SELECTED messages.
394 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
395 if (is_selected(mp
, msgnum
)) {
398 msgnam
= m_name (msgnum
);
399 if ((ct
= parse_mime (msgnam
)))
409 SIGNAL (SIGQUIT
, quitser
);
410 SIGNAL (SIGPIPE
, pipeser
);
413 * Get the associated umask for the relevant contents.
415 for (ctp
= cts
; *ctp
; ctp
++) {
419 if (type_ok (ct
, 1) && !ct
->c_umask
) {
420 if (stat (ct
->c_file
, &st
) != NOTOK
)
421 ct
->c_umask
= ~(st
.st_mode
& 0777);
423 ct
->c_umask
= ~m_gmprot();
427 /* If reading from a folder, do some updating */
429 context_replace (pfolder
, folder
);/* update current folder */
430 seq_setcur (mp
, mp
->hghsel
); /* update current message */
431 seq_save (mp
); /* synchronize sequences */
432 context_save (); /* save the context file */
436 m_popen(moreproc
, 0);
439 * Show the message content
441 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
443 /* Now free all the structures for the content */
444 for (ctp
= cts
; *ctp
; ctp
++)
463 fprintf (stderr
, "\n");