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
92 extern char *tmp; /* directory to place temp files */
97 extern char *cache_public;
98 extern char *cache_private;
105 extern int nomore; /* flags for moreproc/header display */
111 extern char *parts[NPARTS + 1];
112 extern char *types[NTYPES + 1];
118 /* The list of top-level contents to display */
121 #define quitser pipeser
124 CT parse_mime (char *);
127 int part_ok (CT, int);
128 int type_ok (CT, int);
129 void set_endian (void);
130 void flush_errors (void);
133 void show_all_messages (CT *);
136 void free_content (CT);
141 static RETSIGTYPE pipeser (int);
145 main (int argc, char **argv)
147 int nummsgs, maxmsgs, msgnum, *icachesw;
148 char *cp, *file = NULL, *folder = NULL;
149 char *maildir, buf[100], **argp;
150 char **arguments, **msgs;
151 struct msgs *mp = NULL;
156 setlocale(LC_ALL, "");
158 invo_name = r1bindex (argv[0], '/');
160 /* read user profile/context */
163 arguments = getarguments (invo_name, argc, argv, 1);
167 * Allocate the initial space to record message
168 * names, ranges, and sequences.
172 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
173 adios (NULL, "unable to allocate storage");
178 while ((cp = *argp++)) {
180 switch (smatch (++cp, switches)) {
182 ambigsw (cp, switches);
185 adios (NULL, "-%s unknown", cp);
188 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
190 print_help (buf, switches, 1);
193 print_version(invo_name);
197 icachesw = &rcachesw;
200 icachesw = &wcachesw;
202 if (!(cp = *argp++) || *cp == '-')
203 adios (NULL, "missing argument to %s", argp[-2]);
204 switch (*icachesw = smatch (cp, caches)) {
206 ambigsw (cp, caches);
209 adios (NULL, "%s unknown", cp);
237 if (!(cp = *argp++) || *cp == '-')
238 adios (NULL, "missing argument to %s", argp[-2]);
240 adios (NULL, "too many parts (starting with %s), %d max",
246 if (!(cp = *argp++) || *cp == '-')
247 adios (NULL, "missing argument to %s", argp[-2]);
249 adios (NULL, "too many types (starting with %s), %d max",
255 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
256 adios (NULL, "missing argument to %s", argp[-2]);
257 file = *cp == '-' ? cp : path (cp, TFILE);
261 if (!(cp = *argp++) || *cp == '-')
262 adios (NULL, "missing argument to %s", argp[-2]);
265 formsw = getcpy (etcpath (cp));
269 * Switches for moreproc/mhlproc
272 if (!(progsw = *argp++) || *progsw == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
281 if (!(cp = *argp++) || *cp == '-')
282 adios (NULL, "missing argument to %s", argp[-2]);
296 if (*cp == '+' || *cp == '@') {
298 adios (NULL, "only one folder at a time!");
300 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
303 * Check if we need to allocate more space
304 * for message names/ranges/sequences.
306 if (nummsgs >= maxmsgs) {
308 if (!(msgs = (char **) realloc (msgs,
309 (size_t) (maxmsgs * sizeof(*msgs)))))
310 adios (NULL, "unable to reallocate msgs storage");
312 msgs[nummsgs++] = cp;
316 /* null terminate the list of acceptable parts/types */
322 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
328 * Check if we've specified an additional profile
330 if ((cp = getenv ("MHSHOW"))) {
331 if ((fp = fopen (cp, "r"))) {
332 readconfig ((struct node **) 0, fp, cp, 0);
335 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
340 * Read the standard profile setup
342 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
343 readconfig ((struct node **) 0, fp, cp, 0);
347 /* Check for public cache location */
348 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
351 /* Check for private cache location */
352 if (!(cache_private = context_find (nmhprivcache)))
353 cache_private = ".cache";
354 cache_private = getcpy (m_maildir (cache_private));
357 * Check for storage directory. If specified,
358 * then store temporary files there. Else we
359 * store them in standard nmh directory.
361 if ((cp = context_find (nmhstorage)) && *cp)
362 tmp = concat (cp, "/", invo_name, NULL);
364 tmp = add (m_maildir (invo_name), NULL);
366 if (!context_find ("path"))
367 free (path ("./", TFOLDER));
370 adios (NULL, "cannot specify msg and file at same time!");
373 * check if message is coming from file
376 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
377 adios (NULL, "out of memory");
380 if ((ct = parse_mime (file)));
384 * message(s) are coming from a folder
387 msgs[nummsgs++] = "cur";
389 folder = getfolder (1);
390 maildir = m_maildir (folder);
392 if (chdir (maildir) == NOTOK)
393 adios (maildir, "unable to change directory to");
395 /* read folder and create message structure */
396 if (!(mp = folder_read (folder)))
397 adios (NULL, "unable to read folder %s", folder);
399 /* check for empty folder */
401 adios (NULL, "no messages in %s", folder);
403 /* parse all the message ranges/sequences and set SELECTED */
404 for (msgnum = 0; msgnum < nummsgs; msgnum++)
405 if (!m_convert (mp, msgs[msgnum]))
409 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
410 * since we will use that as a tag to know which messages
411 * to remove from the "unseen" sequence.
413 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
414 if (is_selected(mp, msgnum))
415 set_unseen (mp, msgnum);
417 seq_setprev (mp); /* set the Previous-Sequence */
418 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
420 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
421 adios (NULL, "out of memory");
425 * Parse all the SELECTED messages.
427 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
428 if (is_selected(mp, msgnum)) {
431 msgnam = m_name (msgnum);
432 if ((ct = parse_mime (msgnam)))
442 SIGNAL (SIGQUIT, quitser);
443 SIGNAL (SIGPIPE, pipeser);
446 * Get the associated umask for the relevant contents.
448 for (ctp = cts; *ctp; ctp++) {
452 if (type_ok (ct, 1) && !ct->c_umask) {
453 if (stat (ct->c_file, &st) != NOTOK)
454 ct->c_umask = ~(st.st_mode & 0777);
456 ct->c_umask = ~m_gmprot();
461 * Show the message content
463 show_all_messages (cts);
465 /* Now free all the structures for the content */
466 for (ctp = cts; *ctp; ctp++)
472 /* If reading from a folder, do some updating */
474 context_replace (pfolder, folder);/* update current folder */
475 seq_setcur (mp, mp->hghsel); /* update current message */
476 seq_save (mp); /* synchronize sequences */
477 context_save (); /* save the context file */
490 fprintf (stderr, "\n");
509 return 1; /* dead code to satisfy the compiler */