]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
8c604ad8689dc774903e821fe7a6f9bd8fe1e262
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>
20 #include "../sbr/m_maildir.h"
21 #include "../sbr/m_popen.h"
23 #include "mhshowsbr.h"
25 #define MHSHOW_SWITCHES \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("concat", 0, CONCATSW) \
31 X("noconcat", 0, NCONCATSW) \
32 X("textonly", 0, TEXTONLYSW) \
33 X("notextonly", 0, NTEXTONLYSW) \
34 X("inlineonly", 0, INLINESW) \
35 X("noinlineonly", 0, NINLINESW) \
36 X("file file", 0, FILESW) \
37 X("form formfile", 0, FORMSW) \
38 X("header", 0, HEADSW) \
39 X("noheader", 0, NHEADSW) \
40 X("headerform formfile", 0, HEADFORMSW) \
41 X("markform formfile", 0, MARKFORMSW) \
42 X("part number", 0, PARTSW) \
43 X("type content", 0, TYPESW) \
44 X("prefer content", 0, PREFERSW) \
45 X("rcache policy", 0, RCACHESW) \
46 X("wcache policy", 0, WCACHESW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
50 * switches for moreproc/mhlproc \
52 X("moreproc program", -4, PROGSW) \
53 X("nomoreproc", -3, NPROGSW) \
54 X("length lines", -4, LENSW) \
55 X("width columns", -4, WIDTHSW) \
57 * switches for debugging \
59 X("debug", -5, DEBUGSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MHSHOW
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
73 #define quitser pipeser
78 static void pipeser (int);
82 main (int argc
, char **argv
)
84 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
85 char *cp
, *file
= NULL
;
86 char *maildir
, buf
[100], **argp
;
88 struct msgs_array msgs
= { 0, 0, NULL
};
89 struct msgs
*mp
= NULL
;
93 if (nmh_init(argv
[0], 1)) { return 1; }
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
103 while ((cp
= *argp
++)) {
105 switch (smatch (++cp
, switches
)) {
107 ambigsw (cp
, switches
);
110 adios (NULL
, "-%s unknown", cp
);
113 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
115 print_help (buf
, switches
, 1);
118 print_version(invo_name
);
122 icachesw
= &rcachesw
;
125 icachesw
= &wcachesw
;
127 if (!(cp
= *argp
++) || *cp
== '-')
128 adios (NULL
, "missing argument to %s", argp
[-2]);
129 switch (*icachesw
= smatch (cp
, cache_policy
)) {
131 ambigsw (cp
, cache_policy
);
134 adios (NULL
, "%s unknown", cp
);
167 if (!(cp
= *argp
++) || *cp
== '-')
168 adios (NULL
, "missing argument to %s", argp
[-2]);
170 adios (NULL
, "too many parts (starting with %s), %d max",
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
179 adios (NULL
, "too many types (starting with %s), %d max",
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
187 if (npreferred
>= NPREFS
)
188 adios (NULL
, "too many preferred types (starting with %s), %d max",
190 preferred_types
[npreferred
] = cp
;
191 cp
= strchr(cp
, '/');
192 if (cp
) *cp
++ = '\0';
193 preferred_subtypes
[npreferred
++] = cp
;
197 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
198 adios (NULL
, "missing argument to %s", argp
[-2]);
199 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 adios (NULL
, "missing argument to %s", argp
[-2]);
206 formsw
= getcpy (etcpath (cp
));
217 if (!(headerform
= *argp
++) || *headerform
== '-')
218 adios (NULL
, "missing argument to %s", argp
[-2]);
222 if (!(markerform
= *argp
++) || *markerform
== '-')
223 adios (NULL
, "missing argument to %s", argp
[-2]);
227 * Switches for moreproc/mhlproc
230 if (!(progsw
= *argp
++) || *progsw
== '-')
231 adios (NULL
, "missing argument to %s", argp
[-2]);
239 if (!(cp
= *argp
++) || *cp
== '-')
240 adios (NULL
, "missing argument to %s", argp
[-2]);
254 if (*cp
== '+' || *cp
== '@') {
256 adios (NULL
, "only one folder at a time!");
257 folder
= pluspath (cp
);
259 app_msgarg(&msgs
, cp
);
262 /* null terminate the list of acceptable parts/types */
267 * If we had any specific parts or types specified, turn off text only
271 if (npart
> 0 || ntype
> 0) {
274 if (inlineonly
== -1)
279 * Check if we've specified an additional profile
281 if ((cp
= getenv ("MHSHOW"))) {
282 if ((fp
= fopen (cp
, "r"))) {
283 readconfig(NULL
, fp
, cp
, 0);
286 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
291 * Read the standard profile setup
293 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
294 readconfig(NULL
, fp
, cp
, 0);
298 /* Check for public cache location */
299 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
302 /* Check for private cache location */
303 if (!(cache_private
= context_find (nmhprivcache
)))
304 cache_private
= ".cache";
305 cache_private
= getcpy (m_maildir (cache_private
));
307 if (!context_find ("path"))
308 free (path ("./", TFOLDER
));
310 if (file
&& msgs
.size
)
311 adios (NULL
, "cannot specify msg and file at same time!");
314 * check if message is coming from file
317 cts
= mh_xcalloc(2, sizeof *cts
);
320 if ((ct
= parse_mime (file
)))
326 * message(s) are coming from a folder
329 app_msgarg(&msgs
, "cur");
331 folder
= getfolder (1);
332 maildir
= m_maildir (folder
);
334 if (chdir (maildir
) == NOTOK
)
335 adios (maildir
, "unable to change directory to");
337 /* read folder and create message structure */
338 if (!(mp
= folder_read (folder
, 1)))
339 adios (NULL
, "unable to read folder %s", folder
);
341 /* check for empty folder */
343 adios (NULL
, "no messages in %s", folder
);
345 /* parse all the message ranges/sequences and set SELECTED */
346 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
347 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
351 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
352 * since we will use that as a tag to know which messages
353 * to remove from the "unseen" sequence.
355 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
356 if (is_selected(mp
, msgnum
))
357 set_unseen (mp
, msgnum
);
359 seq_setprev (mp
); /* set the Previous-Sequence */
360 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
362 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
366 * Parse all the SELECTED messages.
368 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
369 if (is_selected(mp
, msgnum
)) {
372 msgnam
= m_name (msgnum
);
373 if ((ct
= parse_mime (msgnam
)))
383 SIGNAL (SIGQUIT
, quitser
);
384 SIGNAL (SIGPIPE
, pipeser
);
387 * Get the associated umask for the relevant contents.
389 for (ctp
= cts
; *ctp
; ctp
++) {
393 if (type_ok (ct
, 1) && !ct
->c_umask
) {
394 if (stat (ct
->c_file
, &st
) != NOTOK
)
395 ct
->c_umask
= ~(st
.st_mode
& 0777);
397 ct
->c_umask
= ~m_gmprot();
401 /* If reading from a folder, do some updating */
403 context_replace (pfolder
, folder
);/* update current folder */
404 seq_setcur (mp
, mp
->hghsel
); /* update current message */
405 seq_save (mp
); /* synchronize sequences */
406 context_save (); /* save the context file */
410 m_popen(moreproc
, 0);
413 * Show the message content
415 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
417 /* Now free all the structures for the content */
418 for (ctp
= cts
; *ctp
; ctp
++)
437 fprintf (stderr
, "\n");