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>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
30 * We allocate space for message names (msgs array)
31 * this number of elements at a time.
36 static struct swit switches[] = {
48 { "noserialonly", 0 },
53 #define FILESW 8 /* interface from show */
56 { "form formfile", 0 },
60 { "type content", 0 },
62 { "rcache policy", 0 },
64 { "wcache policy", 0 },
71 * switches for moreproc/mhlproc
74 { "moreproc program", -4 },
78 { "length lines", -4 },
80 { "width columns", -4 },
83 * switches for debugging
93 extern char *tmp; /* directory to place temp files */
98 extern char *cache_public;
99 extern char *cache_private;
106 extern int nomore; /* flags for moreproc/header display */
112 extern char *parts[NPARTS + 1];
113 extern char *types[NTYPES + 1];
119 /* The list of top-level contents to display */
122 #define quitser pipeser
125 CT parse_mime (char *);
128 int part_ok (CT, int);
129 int type_ok (CT, int);
130 void set_endian (void);
131 void flush_errors (void);
134 void show_all_messages (CT *);
137 void free_content (CT);
142 static RETSIGTYPE pipeser (int);
146 main (int argc, char **argv)
148 int nummsgs, maxmsgs, msgnum, *icachesw;
149 char *cp, *file = NULL, *folder = NULL;
150 char *maildir, buf[100], **argp;
151 char **arguments, **msgs;
152 struct msgs *mp = NULL;
157 setlocale(LC_ALL, "");
159 invo_name = r1bindex (argv[0], '/');
161 /* read user profile/context */
164 arguments = getarguments (invo_name, argc, argv, 1);
168 * Allocate the initial space to record message
169 * names, ranges, and sequences.
173 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
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 msgs = (char **) mh_xrealloc (msgs,
309 (size_t) (maxmsgs * sizeof(*msgs)));
311 msgs[nummsgs++] = cp;
315 /* null terminate the list of acceptable parts/types */
321 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
327 * Check if we've specified an additional profile
329 if ((cp = getenv ("MHSHOW"))) {
330 if ((fp = fopen (cp, "r"))) {
331 readconfig ((struct node **) 0, fp, cp, 0);
334 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
339 * Read the standard profile setup
341 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
342 readconfig ((struct node **) 0, fp, cp, 0);
346 /* Check for public cache location */
347 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
350 /* Check for private cache location */
351 if (!(cache_private = context_find (nmhprivcache)))
352 cache_private = ".cache";
353 cache_private = getcpy (m_maildir (cache_private));
356 * Check for storage directory. If specified,
357 * then store temporary files there. Else we
358 * store them in standard nmh directory.
360 if ((cp = context_find (nmhstorage)) && *cp)
361 tmp = concat (cp, "/", invo_name, NULL);
363 tmp = add (m_maildir (invo_name), NULL);
365 if (!context_find ("path"))
366 free (path ("./", TFOLDER));
369 adios (NULL, "cannot specify msg and file at same time!");
372 * check if message is coming from file
375 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
376 adios (NULL, "out of memory");
379 if ((ct = parse_mime (file)));
383 * message(s) are coming from a folder
386 msgs[nummsgs++] = "cur";
388 folder = getfolder (1);
389 maildir = m_maildir (folder);
391 if (chdir (maildir) == NOTOK)
392 adios (maildir, "unable to change directory to");
394 /* read folder and create message structure */
395 if (!(mp = folder_read (folder)))
396 adios (NULL, "unable to read folder %s", folder);
398 /* check for empty folder */
400 adios (NULL, "no messages in %s", folder);
402 /* parse all the message ranges/sequences and set SELECTED */
403 for (msgnum = 0; msgnum < nummsgs; msgnum++)
404 if (!m_convert (mp, msgs[msgnum]))
408 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
409 * since we will use that as a tag to know which messages
410 * to remove from the "unseen" sequence.
412 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
413 if (is_selected(mp, msgnum))
414 set_unseen (mp, msgnum);
416 seq_setprev (mp); /* set the Previous-Sequence */
417 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
419 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
420 adios (NULL, "out of memory");
424 * Parse all the SELECTED messages.
426 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
427 if (is_selected(mp, msgnum)) {
430 msgnam = m_name (msgnum);
431 if ((ct = parse_mime (msgnam)))
441 SIGNAL (SIGQUIT, quitser);
442 SIGNAL (SIGPIPE, pipeser);
445 * Get the associated umask for the relevant contents.
447 for (ctp = cts; *ctp; ctp++) {
451 if (type_ok (ct, 1) && !ct->c_umask) {
452 if (stat (ct->c_file, &st) != NOTOK)
453 ct->c_umask = ~(st.st_mode & 0777);
455 ct->c_umask = ~m_gmprot();
460 * Show the message content
462 show_all_messages (cts);
464 /* Now free all the structures for the content */
465 for (ctp = cts; *ctp; ctp++)
471 /* If reading from a folder, do some updating */
473 context_replace (pfolder, folder);/* update current folder */
474 seq_setcur (mp, mp->hghsel); /* update current message */
475 seq_save (mp); /* synchronize sequences */
476 context_save (); /* save the context file */
489 fprintf (stderr, "\n");
508 return 1; /* dead code to satisfy the compiler */