]>
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 <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("noprefer", 0, NPREFERSW) \
46 X("rcache policy", 0, RCACHESW) \
47 X("wcache policy", 0, WCACHESW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 * switches for moreproc/mhlproc \
53 X("moreproc program", -4, PROGSW) \
54 X("nomoreproc", -3, NPROGSW) \
55 X("length lines", -4, LENSW) \
56 X("width columns", -4, WIDTHSW) \
58 * switches for debugging \
60 X("debug", -5, DEBUGSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHSHOW
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
74 #define quitser pipeser
79 static void pipeser (int);
83 main (int argc
, char **argv
)
85 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
86 char *cp
, *file
= NULL
;
87 char *maildir
, buf
[100], **argp
;
89 struct msgs_array msgs
= { 0, 0, NULL
};
90 struct msgs
*mp
= NULL
;
94 if (nmh_init(argv
[0], 1)) { return 1; }
98 arguments
= getarguments (invo_name
, argc
, argv
, 1);
104 while ((cp
= *argp
++)) {
106 switch (smatch (++cp
, switches
)) {
108 ambigsw (cp
, switches
);
111 adios (NULL
, "-%s unknown", cp
);
114 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
123 icachesw
= &rcachesw
;
126 icachesw
= &wcachesw
;
128 if (!(cp
= *argp
++) || *cp
== '-')
129 adios (NULL
, "missing argument to %s", argp
[-2]);
130 switch (*icachesw
= smatch (cp
, cache_policy
)) {
132 ambigsw (cp
, cache_policy
);
135 adios (NULL
, "%s unknown", cp
);
168 if (!(cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
171 adios (NULL
, "too many parts (starting with %s), %d max",
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
180 adios (NULL
, "too many types (starting with %s), %d max",
186 if (!(cp
= *argp
++) || *cp
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
188 if (npreferred
>= NPREFS
)
189 adios (NULL
, "too many preferred types (starting with %s), %d max",
191 mime_preference
[npreferred
].type
= cp
;
192 cp
= strchr(cp
, '/');
193 if (cp
) *cp
++ = '\0';
194 mime_preference
[npreferred
++].subtype
= cp
;
202 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
203 adios (NULL
, "missing argument to %s", argp
[-2]);
204 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 adios (NULL
, "missing argument to %s", argp
[-2]);
211 formsw
= getcpy (etcpath (cp
));
222 if (!(headerform
= *argp
++) || *headerform
== '-')
223 adios (NULL
, "missing argument to %s", argp
[-2]);
227 if (!(markerform
= *argp
++) || *markerform
== '-')
228 adios (NULL
, "missing argument to %s", argp
[-2]);
232 * Switches for moreproc/mhlproc
235 if (!(progsw
= *argp
++) || *progsw
== '-')
236 adios (NULL
, "missing argument to %s", argp
[-2]);
244 if (!(cp
= *argp
++) || *cp
== '-')
245 adios (NULL
, "missing argument to %s", argp
[-2]);
259 if (*cp
== '+' || *cp
== '@') {
261 adios (NULL
, "only one folder at a time!");
262 folder
= pluspath (cp
);
264 app_msgarg(&msgs
, cp
);
267 /* null terminate the list of acceptable parts/types */
272 * If we had any specific parts or types specified, turn off text only
276 if (npart
> 0 || ntype
> 0) {
279 if (inlineonly
== -1)
284 * Check if we've specified an additional profile
286 if ((cp
= getenv ("MHSHOW"))) {
287 if ((fp
= fopen (cp
, "r"))) {
288 readconfig(NULL
, fp
, cp
, 0);
291 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
296 * Read the standard profile setup
298 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
299 readconfig(NULL
, fp
, cp
, 0);
303 /* Check for public cache location */
304 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
307 /* Check for private cache location */
308 if (!(cache_private
= context_find (nmhprivcache
)))
309 cache_private
= ".cache";
310 cache_private
= getcpy (m_maildir (cache_private
));
312 if (!context_find ("path"))
313 free (path ("./", TFOLDER
));
315 if (file
&& msgs
.size
)
316 adios (NULL
, "cannot specify msg and file at same time!");
319 * check if message is coming from file
322 cts
= mh_xcalloc(2, sizeof *cts
);
325 if ((ct
= parse_mime (file
)))
331 * message(s) are coming from a folder
334 app_msgarg(&msgs
, "cur");
336 folder
= getfolder (1);
337 maildir
= m_maildir (folder
);
339 if (chdir (maildir
) == NOTOK
)
340 adios (maildir
, "unable to change directory to");
342 /* read folder and create message structure */
343 if (!(mp
= folder_read (folder
, 1)))
344 adios (NULL
, "unable to read folder %s", folder
);
346 /* check for empty folder */
348 adios (NULL
, "no messages in %s", folder
);
350 /* parse all the message ranges/sequences and set SELECTED */
351 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
352 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
356 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
357 * since we will use that as a tag to know which messages
358 * to remove from the "unseen" sequence.
360 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
361 if (is_selected(mp
, msgnum
))
362 set_unseen (mp
, msgnum
);
364 seq_setprev (mp
); /* set the Previous-Sequence */
365 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
367 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
371 * Parse all the SELECTED messages.
373 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
374 if (is_selected(mp
, msgnum
)) {
377 msgnam
= m_name (msgnum
);
378 if ((ct
= parse_mime (msgnam
)))
388 SIGNAL (SIGQUIT
, quitser
);
389 SIGNAL (SIGPIPE
, pipeser
);
392 * Get the associated umask for the relevant contents.
394 for (ctp
= cts
; *ctp
; ctp
++) {
398 if (type_ok (ct
, 1) && !ct
->c_umask
) {
399 if (stat (ct
->c_file
, &st
) != NOTOK
)
400 ct
->c_umask
= ~(st
.st_mode
& 0777);
402 ct
->c_umask
= ~m_gmprot();
406 /* If reading from a folder, do some updating */
408 context_replace (pfolder
, folder
);/* update current folder */
409 seq_setcur (mp
, mp
->hghsel
); /* update current message */
410 seq_save (mp
); /* synchronize sequences */
411 context_save (); /* save the context file */
415 m_popen(moreproc
, 0);
418 * Show the message content
420 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
422 /* Now free all the structures for the content */
423 for (ctp
= cts
; *ctp
; ctp
++)
442 fprintf (stderr
, "\n");