]>
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>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
19 #define MHSHOW_SWITCHES \
20 X("check", 0, CHECKSW) \
21 X("nocheck", 0, NCHECKSW) \
22 X("verbose", 0, VERBSW) \
23 X("noverbose", 0, NVERBSW) \
24 X("concat", 0, CONCATSW) \
25 X("noconcat", 0, NCONCATSW) \
26 X("textonly", 0, TEXTONLYSW) \
27 X("notextonly", 0, NTEXTONLYSW) \
28 X("inlineonly", 0, INLINESW) \
29 X("noinlineonly", 0, NINLINESW) \
30 X("file file", 0, FILESW) \
31 X("form formfile", 0, FORMSW) \
32 X("header", 0, HEADSW) \
33 X("noheader", 0, NHEADSW) \
34 X("headerform formfile", 0, HEADFORMSW) \
35 X("markform formfile", 0, MARKFORMSW) \
36 X("part number", 0, PARTSW) \
37 X("type content", 0, TYPESW) \
38 X("prefer content", 0, PREFERSW) \
39 X("rcache policy", 0, RCACHESW) \
40 X("wcache policy", 0, WCACHESW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 * switches for moreproc/mhlproc \
46 X("moreproc program", -4, PROGSW) \
47 X("nomoreproc", -3, NPROGSW) \
48 X("length lines", -4, LENSW) \
49 X("width columns", -4, WIDTHSW) \
51 * switches for debugging \
53 X("debug", -5, DEBUGSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(MHSHOW
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
67 extern char *cache_public
;
68 extern char *cache_private
;
72 extern int nomore
; /* flags for moreproc/header display */
75 extern char *headerform
;
76 extern char *markerform
;
82 extern char *parts
[NPARTS
+ 1];
83 extern char *types
[NTYPES
+ 1];
87 extern char *preferred_types
[];
88 extern char *preferred_subtypes
[];
89 extern int npreferred
;
94 #define quitser pipeser
97 CT
parse_mime (char *);
101 int type_ok (CT
, int);
102 void flush_errors (void);
106 void freects_done (int) NORETURN
;
111 static void pipeser (int);
115 main (int argc
, char **argv
)
117 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
118 char *cp
, *file
= NULL
;
119 char *maildir
, buf
[100], **argp
;
121 struct msgs_array msgs
= { 0, 0, NULL
};
122 struct msgs
*mp
= NULL
;
126 if (nmh_init(argv
[0], 1)) { return 1; }
130 arguments
= getarguments (invo_name
, argc
, argv
, 1);
136 while ((cp
= *argp
++)) {
138 switch (smatch (++cp
, switches
)) {
140 ambigsw (cp
, switches
);
143 adios (NULL
, "-%s unknown", cp
);
146 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
148 print_help (buf
, switches
, 1);
151 print_version(invo_name
);
155 icachesw
= &rcachesw
;
158 icachesw
= &wcachesw
;
160 if (!(cp
= *argp
++) || *cp
== '-')
161 adios (NULL
, "missing argument to %s", argp
[-2]);
162 switch (*icachesw
= smatch (cp
, caches
)) {
164 ambigsw (cp
, caches
);
167 adios (NULL
, "%s unknown", cp
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 adios (NULL
, "missing argument to %s", argp
[-2]);
203 adios (NULL
, "too many parts (starting with %s), %d max",
209 if (!(cp
= *argp
++) || *cp
== '-')
210 adios (NULL
, "missing argument to %s", argp
[-2]);
212 adios (NULL
, "too many types (starting with %s), %d max",
218 if (!(cp
= *argp
++) || *cp
== '-')
219 adios (NULL
, "missing argument to %s", argp
[-2]);
220 if (npreferred
>= NPREFS
)
221 adios (NULL
, "too many preferred types (starting with %s), %d max",
223 preferred_types
[npreferred
] = cp
;
224 cp
= strchr(cp
, '/');
225 if (cp
) *cp
++ = '\0';
226 preferred_subtypes
[npreferred
++] = cp
;
230 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
231 adios (NULL
, "missing argument to %s", argp
[-2]);
232 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
239 formsw
= getcpy (etcpath (cp
));
250 if (!(headerform
= *argp
++) || *headerform
== '-')
251 adios (NULL
, "missing argument to %s", argp
[-2]);
255 if (!(markerform
= *argp
++) || *markerform
== '-')
256 adios (NULL
, "missing argument to %s", argp
[-2]);
260 * Switches for moreproc/mhlproc
263 if (!(progsw
= *argp
++) || *progsw
== '-')
264 adios (NULL
, "missing argument to %s", argp
[-2]);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 adios (NULL
, "missing argument to %s", argp
[-2]);
287 if (*cp
== '+' || *cp
== '@') {
289 adios (NULL
, "only one folder at a time!");
291 folder
= pluspath (cp
);
293 app_msgarg(&msgs
, cp
);
296 /* null terminate the list of acceptable parts/types */
301 * If we had any specific parts or types specified, turn off text only
305 if (npart
> 0 || ntype
> 0) {
308 if (inlineonly
== -1)
313 * Check if we've specified an additional profile
315 if ((cp
= getenv ("MHSHOW"))) {
316 if ((fp
= fopen (cp
, "r"))) {
317 readconfig ((struct node
**) 0, fp
, cp
, 0);
320 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
325 * Read the standard profile setup
327 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
328 readconfig ((struct node
**) 0, fp
, cp
, 0);
332 /* Check for public cache location */
333 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
336 /* Check for private cache location */
337 if (!(cache_private
= context_find (nmhprivcache
)))
338 cache_private
= ".cache";
339 cache_private
= getcpy (m_maildir (cache_private
));
341 if (!context_find ("path"))
342 free (path ("./", TFOLDER
));
344 if (file
&& msgs
.size
)
345 adios (NULL
, "cannot specify msg and file at same time!");
348 * check if message is coming from file
351 cts
= mh_xcalloc(2, sizeof *cts
);
354 if ((ct
= parse_mime (file
)))
360 * message(s) are coming from a folder
363 app_msgarg(&msgs
, "cur");
365 folder
= getfolder (1);
366 maildir
= m_maildir (folder
);
368 if (chdir (maildir
) == NOTOK
)
369 adios (maildir
, "unable to change directory to");
371 /* read folder and create message structure */
372 if (!(mp
= folder_read (folder
, 1)))
373 adios (NULL
, "unable to read folder %s", folder
);
375 /* check for empty folder */
377 adios (NULL
, "no messages in %s", folder
);
379 /* parse all the message ranges/sequences and set SELECTED */
380 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
381 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
385 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
386 * since we will use that as a tag to know which messages
387 * to remove from the "unseen" sequence.
389 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
390 if (is_selected(mp
, msgnum
))
391 set_unseen (mp
, msgnum
);
393 seq_setprev (mp
); /* set the Previous-Sequence */
394 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
396 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
400 * Parse all the SELECTED messages.
402 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
403 if (is_selected(mp
, msgnum
)) {
406 msgnam
= m_name (msgnum
);
407 if ((ct
= parse_mime (msgnam
)))
417 SIGNAL (SIGQUIT
, quitser
);
418 SIGNAL (SIGPIPE
, pipeser
);
421 * Get the associated umask for the relevant contents.
423 for (ctp
= cts
; *ctp
; ctp
++) {
427 if (type_ok (ct
, 1) && !ct
->c_umask
) {
428 if (stat (ct
->c_file
, &st
) != NOTOK
)
429 ct
->c_umask
= ~(st
.st_mode
& 0777);
431 ct
->c_umask
= ~m_gmprot();
435 /* If reading from a folder, do some updating */
437 context_replace (pfolder
, folder
);/* update current folder */
438 seq_setcur (mp
, mp
->hghsel
); /* update current message */
439 seq_save (mp
); /* synchronize sequences */
440 context_save (); /* save the context file */
444 m_popen(moreproc
, 0);
447 * Show the message content
449 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
451 /* Now free all the structures for the content */
452 for (ctp
= cts
; *ctp
; ctp
++)
471 fprintf (stderr
, "\n");