2 ** mhshow.c -- display the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
38 { "noserialonly", 0 },
43 #define FILESW 8 /* interface from show */
46 { "form formfile", 0 },
50 { "type content", 0 },
52 { "rcache policy", 0 },
54 { "wcache policy", 0 },
61 ** switches for moreproc/mhlproc
64 { "moreproc program", -4 },
68 { "length lines", -4 },
70 { "width columns", -4 },
73 ** switches for debugging
82 extern char *tmp; /* directory to place temp files */
87 extern char *cache_public;
88 extern char *cache_private;
95 extern int nomore; /* flags for moreproc/header display */
101 extern char *parts[NPARTS + 1];
102 extern char *types[NTYPES + 1];
108 #define quitser pipeser
111 CT parse_mime (char *);
114 int part_ok (CT, int);
115 int type_ok (CT, int);
116 void set_endian (void);
117 void flush_errors (void);
120 void show_all_messages (CT *);
123 void free_content (CT);
125 void freects_done (int) NORETURN;
130 static RETSIGTYPE pipeser (int);
134 main (int argc, char **argv)
136 int msgnum, *icachesw;
137 char *cp, *file = NULL, *folder = NULL;
138 char *maildir, buf[100], **argp;
140 struct msgs_array msgs = { 0, 0, NULL };
141 struct msgs *mp = NULL;
148 setlocale(LC_ALL, "");
150 invo_name = r1bindex (argv[0], '/');
152 /* read user profile/context */
155 arguments = getarguments (invo_name, argc, argv, 1);
161 while ((cp = *argp++)) {
163 switch (smatch (++cp, switches)) {
165 ambigsw (cp, switches);
168 adios (NULL, "-%s unknown", cp);
171 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
173 print_help (buf, switches, 1);
176 print_version(invo_name);
180 icachesw = &rcachesw;
183 icachesw = &wcachesw;
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s",
188 switch (*icachesw = smatch (cp, caches)) {
190 ambigsw (cp, caches);
193 adios (NULL, "%s unknown", cp);
221 if (!(cp = *argp++) || *cp == '-')
222 adios (NULL, "missing argument to %s",
225 adios (NULL, "too many parts (starting with %s), %d max",
231 if (!(cp = *argp++) || *cp == '-')
232 adios (NULL, "missing argument to %s",
235 adios (NULL, "too many types (starting with %s), %d max",
241 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
242 adios (NULL, "missing argument to %s",
244 file = *cp == '-' ? cp : path (cp, TFILE);
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s",
253 formsw = getcpy (etcpath (cp));
257 ** Switches for moreproc/mhlproc
260 if (!(progsw = *argp++) || *progsw == '-')
261 adios (NULL, "missing argument to %s",
270 if (!(cp = *argp++) || *cp == '-')
271 adios (NULL, "missing argument to %s",
286 if (*cp == '+' || *cp == '@') {
288 adios (NULL, "only one folder at a time!");
290 folder = pluspath (cp);
292 app_msgarg(&msgs, cp);
295 /* null terminate the list of acceptable parts/types */
301 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
307 ** Check if we've specified an additional profile
309 if ((cp = getenv ("MHSHOW"))) {
310 if ((fp = fopen (cp, "r"))) {
311 readconfig ((struct node **) 0, fp, cp, 0);
314 admonish ("", "unable to read $MHSHOW profile (%s)",
320 ** Read the standard profile setup
322 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
323 readconfig ((struct node **) 0, fp, cp, 0);
327 /* Check for public cache location */
328 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
331 /* Check for private cache location */
332 if (!(cache_private = context_find (nmhprivcache)))
333 cache_private = ".cache";
334 cache_private = getcpy (m_maildir (cache_private));
337 ** Check for storage directory. If specified,
338 ** then store temporary files there. Else we
339 ** store them in standard nmh directory.
341 if ((cp = context_find (nmhstorage)) && *cp)
342 tmp = concat (cp, "/", invo_name, NULL);
344 tmp = add (m_maildir (invo_name), NULL);
346 if (!context_find ("path"))
347 free (path ("./", TFOLDER));
349 if (file && msgs.size)
350 adios (NULL, "cannot specify msg and file at same time!");
353 ** check if message is coming from file
356 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
357 adios (NULL, "out of memory");
360 if ((ct = parse_mime (file)));
364 ** message(s) are coming from a folder
367 app_msgarg(&msgs, "cur");
369 folder = getfolder (1);
370 maildir = m_maildir (folder);
372 if (chdir (maildir) == NOTOK)
373 adios (maildir, "unable to change directory to");
375 /* read folder and create message structure */
376 if (!(mp = folder_read (folder)))
377 adios (NULL, "unable to read folder %s", folder);
379 /* check for empty folder */
381 adios (NULL, "no messages in %s", folder);
383 /* parse all the message ranges/sequences and set SELECTED */
384 for (msgnum = 0; msgnum < msgs.size; msgnum++)
385 if (!m_convert (mp, msgs.msgs[msgnum]))
389 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
390 ** since we will use that as a tag to know which messages
391 ** to remove from the "unseen" sequence.
393 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
394 if (is_selected(mp, msgnum))
395 set_unseen (mp, msgnum);
397 seq_setprev (mp); /* set the Previous-Sequence */
398 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
400 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1),
402 adios (NULL, "out of memory");
406 ** Parse all the SELECTED messages.
408 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
409 if (is_selected(mp, msgnum)) {
412 msgnam = m_name (msgnum);
413 if ((ct = parse_mime (msgnam)))
423 SIGNAL (SIGQUIT, quitser);
424 SIGNAL (SIGPIPE, pipeser);
427 ** Get the associated umask for the relevant contents.
429 for (ctp = cts; *ctp; ctp++) {
433 if (type_ok (ct, 1) && !ct->c_umask) {
434 if (stat (ct->c_file, &st) != NOTOK)
435 ct->c_umask = ~(st.st_mode & 0777);
437 ct->c_umask = ~m_gmprot();
442 ** Show the message content
444 show_all_messages (cts);
446 /* Now free all the structures for the content */
447 for (ctp = cts; *ctp; ctp++)
453 /* If reading from a folder, do some updating */
455 context_replace (pfolder, folder); /* update current folder */
456 seq_setcur (mp, mp->hghsel); /* update current message */
457 seq_save (mp); /* synchronize sequences */
458 context_save (); /* save the context file */
472 fprintf (stderr, "\n");