3 * mhshow.c -- display the contents of MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
29 * We allocate space for message names (msgs array)
30 * this number of elements at a time.
35 static struct swit switches[] = {
47 { "noserialonly", 0 },
52 #define FILESW 8 /* interface from show */
55 { "form formfile", 0 },
59 { "type content", 0 },
61 { "rcache policy", 0 },
63 { "wcache policy", 0 },
70 * switches for moreproc/mhlproc
73 { "moreproc program", -4 },
77 { "length lines", -4 },
79 { "width columns", -4 },
82 * switches for debugging
94 extern char *tmp; /* directory to place temp files */
99 extern char *cache_public;
100 extern char *cache_private;
107 extern int nomore; /* flags for moreproc/header display */
113 extern char *parts[NPARTS + 1];
114 extern char *types[NTYPES + 1];
120 /* The list of top-level contents to display */
123 #define quitser pipeser
126 CT parse_mime (char *);
129 int part_ok (CT, int);
130 int type_ok (CT, int);
131 void set_endian (void);
132 void flush_errors (void);
135 void show_all_messages (CT *);
138 void free_content (CT);
143 static RETSIGTYPE pipeser (int);
147 main (int argc, char **argv)
149 int nummsgs, maxmsgs, msgnum, *icachesw;
150 char *cp, *file = NULL, *folder = NULL;
151 char *maildir, buf[100], **argp;
152 char **arguments, **msgs;
153 struct msgs *mp = NULL;
158 setlocale(LC_ALL, "");
160 invo_name = r1bindex (argv[0], '/');
162 /* read user profile/context */
165 arguments = getarguments (invo_name, argc, argv, 1);
169 * Allocate the initial space to record message
170 * names, ranges, and sequences.
174 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
175 adios (NULL, "unable to allocate storage");
180 while ((cp = *argp++)) {
182 switch (smatch (++cp, switches)) {
184 ambigsw (cp, switches);
187 adios (NULL, "-%s unknown", cp);
190 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
192 print_help (buf, switches, 1);
195 print_version(invo_name);
199 icachesw = &rcachesw;
202 icachesw = &wcachesw;
204 if (!(cp = *argp++) || *cp == '-')
205 adios (NULL, "missing argument to %s", argp[-2]);
206 switch (*icachesw = smatch (cp, caches)) {
208 ambigsw (cp, caches);
211 adios (NULL, "%s unknown", cp);
239 if (!(cp = *argp++) || *cp == '-')
240 adios (NULL, "missing argument to %s", argp[-2]);
242 adios (NULL, "too many parts (starting with %s), %d max",
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
251 adios (NULL, "too many types (starting with %s), %d max",
257 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
258 adios (NULL, "missing argument to %s", argp[-2]);
259 file = *cp == '-' ? cp : path (cp, TFILE);
263 if (!(cp = *argp++) || *cp == '-')
264 adios (NULL, "missing argument to %s", argp[-2]);
267 formsw = getcpy (etcpath (cp));
271 * Switches for moreproc/mhlproc
274 if (!(progsw = *argp++) || *progsw == '-')
275 adios (NULL, "missing argument to %s", argp[-2]);
283 if (!(cp = *argp++) || *cp == '-')
284 adios (NULL, "missing argument to %s", argp[-2]);
298 if (*cp == '+' || *cp == '@') {
300 adios (NULL, "only one folder at a time!");
302 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
305 * Check if we need to allocate more space
306 * for message names/ranges/sequences.
308 if (nummsgs >= maxmsgs) {
310 if (!(msgs = (char **) realloc (msgs,
311 (size_t) (maxmsgs * sizeof(*msgs)))))
312 adios (NULL, "unable to reallocate msgs storage");
314 msgs[nummsgs++] = cp;
318 /* null terminate the list of acceptable parts/types */
324 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
330 * Check if we've specified an additional profile
332 if ((cp = getenv ("MHSHOW"))) {
333 if ((fp = fopen (cp, "r"))) {
334 readconfig ((struct node **) 0, fp, cp, 0);
337 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
342 * Read the standard profile setup
344 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
345 readconfig ((struct node **) 0, fp, cp, 0);
349 /* Check for public cache location */
350 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
353 /* Check for private cache location */
354 if (!(cache_private = context_find (nmhprivcache)))
355 cache_private = ".cache";
356 cache_private = getcpy (m_maildir (cache_private));
359 * Check for storage directory. If specified,
360 * then store temporary files there. Else we
361 * store them in standard nmh directory.
363 if ((cp = context_find (nmhstorage)) && *cp)
364 tmp = concat (cp, "/", invo_name, NULL);
366 tmp = add (m_maildir (invo_name), NULL);
368 if (!context_find ("path"))
369 free (path ("./", TFOLDER));
372 adios (NULL, "cannot specify msg and file at same time!");
375 * check if message is coming from file
378 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
379 adios (NULL, "out of memory");
382 if ((ct = parse_mime (file)));
386 * message(s) are coming from a folder
389 msgs[nummsgs++] = "cur";
391 folder = getfolder (1);
392 maildir = m_maildir (folder);
394 if (chdir (maildir) == NOTOK)
395 adios (maildir, "unable to change directory to");
397 /* read folder and create message structure */
398 if (!(mp = folder_read (folder)))
399 adios (NULL, "unable to read folder %s", folder);
401 /* check for empty folder */
403 adios (NULL, "no messages in %s", folder);
405 /* parse all the message ranges/sequences and set SELECTED */
406 for (msgnum = 0; msgnum < nummsgs; msgnum++)
407 if (!m_convert (mp, msgs[msgnum]))
411 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
412 * since we will use that as a tag to know which messages
413 * to remove from the "unseen" sequence.
415 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
416 if (is_selected(mp, msgnum))
417 set_unseen (mp, msgnum);
419 seq_setprev (mp); /* set the Previous-Sequence */
420 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
422 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
423 adios (NULL, "out of memory");
427 * Parse all the SELECTED messages.
429 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
430 if (is_selected(mp, msgnum)) {
433 msgnam = m_name (msgnum);
434 if ((ct = parse_mime (msgnam)))
444 SIGNAL (SIGQUIT, quitser);
445 SIGNAL (SIGPIPE, pipeser);
448 * Get the associated umask for the relevant contents.
450 for (ctp = cts; *ctp; ctp++) {
454 if (type_ok (ct, 1) && !ct->c_umask) {
455 if (stat (ct->c_file, &st) != NOTOK)
456 ct->c_umask = ~(st.st_mode & 0777);
458 ct->c_umask = ~m_gmprot();
463 * Show the message content
465 show_all_messages (cts);
467 /* Now free all the structures for the content */
468 for (ctp = cts; *ctp; ctp++)
474 /* If reading from a folder, do some updating */
476 context_replace (pfolder, folder);/* update current folder */
477 seq_setcur (mp, mp->hghsel); /* update current message */
478 seq_save (mp); /* synchronize sequences */
479 context_save (); /* save the context file */
492 fprintf (stderr, "\n");
511 return 1; /* dead code to satisfy the compiler */