3 * mhshow.c -- display the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
27 static struct swit switches[] = {
39 { "noserialonly", 0 },
44 #define FILESW 8 /* interface from show */
47 { "form formfile", 0 },
51 { "type content", 0 },
53 { "rcache policy", 0 },
55 { "wcache policy", 0 },
62 * switches for moreproc/mhlproc
65 { "moreproc program", -4 },
69 { "length lines", -4 },
71 { "width columns", -4 },
74 * switches for debugging
83 extern char *tmp; /* directory to place temp files */
88 extern char *cache_public;
89 extern char *cache_private;
96 extern int nomore; /* flags for moreproc/header display */
102 extern char *parts[NPARTS + 1];
103 extern char *types[NTYPES + 1];
109 #define quitser pipeser
112 CT parse_mime (char *);
115 int part_ok (CT, int);
116 int type_ok (CT, int);
117 void set_endian (void);
118 void flush_errors (void);
121 void show_all_messages (CT *);
124 void free_content (CT);
126 void freects_done (int) NORETURN;
131 static RETSIGTYPE pipeser (int);
135 main (int argc, char **argv)
137 int msgnum, *icachesw;
138 char *cp, *file = NULL, *folder = NULL;
139 char *maildir, buf[100], **argp;
141 struct msgs_array msgs = { 0, 0, NULL };
142 struct msgs *mp = NULL;
149 setlocale(LC_ALL, "");
151 invo_name = r1bindex (argv[0], '/');
153 /* read user profile/context */
156 arguments = getarguments (invo_name, argc, argv, 1);
162 while ((cp = *argp++)) {
164 switch (smatch (++cp, switches)) {
166 ambigsw (cp, switches);
169 adios (NULL, "-%s unknown", cp);
172 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
174 print_help (buf, switches, 1);
177 print_version(invo_name);
181 icachesw = &rcachesw;
184 icachesw = &wcachesw;
186 if (!(cp = *argp++) || *cp == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
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", argp[-2]);
224 adios (NULL, "too many parts (starting with %s), %d max",
230 if (!(cp = *argp++) || *cp == '-')
231 adios (NULL, "missing argument to %s", argp[-2]);
233 adios (NULL, "too many types (starting with %s), %d max",
239 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
240 adios (NULL, "missing argument to %s", argp[-2]);
241 file = *cp == '-' ? cp : path (cp, TFILE);
245 if (!(cp = *argp++) || *cp == '-')
246 adios (NULL, "missing argument to %s", argp[-2]);
249 formsw = getcpy (etcpath (cp));
253 * Switches for moreproc/mhlproc
256 if (!(progsw = *argp++) || *progsw == '-')
257 adios (NULL, "missing argument to %s", argp[-2]);
265 if (!(cp = *argp++) || *cp == '-')
266 adios (NULL, "missing argument to %s", argp[-2]);
280 if (*cp == '+' || *cp == '@') {
282 adios (NULL, "only one folder at a time!");
284 folder = pluspath (cp);
286 app_msgarg(&msgs, cp);
289 /* null terminate the list of acceptable parts/types */
295 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
301 * Check if we've specified an additional profile
303 if ((cp = getenv ("MHSHOW"))) {
304 if ((fp = fopen (cp, "r"))) {
305 readconfig ((struct node **) 0, fp, cp, 0);
308 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
313 * Read the standard profile setup
315 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
316 readconfig ((struct node **) 0, fp, cp, 0);
320 /* Check for public cache location */
321 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
324 /* Check for private cache location */
325 if (!(cache_private = context_find (nmhprivcache)))
326 cache_private = ".cache";
327 cache_private = getcpy (m_maildir (cache_private));
330 * Check for storage directory. If specified,
331 * then store temporary files there. Else we
332 * store them in standard nmh directory.
334 if ((cp = context_find (nmhstorage)) && *cp)
335 tmp = concat (cp, "/", invo_name, NULL);
337 tmp = add (m_maildir (invo_name), NULL);
339 if (!context_find ("path"))
340 free (path ("./", TFOLDER));
342 if (file && msgs.size)
343 adios (NULL, "cannot specify msg and file at same time!");
346 * check if message is coming from file
349 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
350 adios (NULL, "out of memory");
353 if ((ct = parse_mime (file)));
357 * message(s) are coming from a folder
360 app_msgarg(&msgs, "cur");
362 folder = getfolder (1);
363 maildir = m_maildir (folder);
365 if (chdir (maildir) == NOTOK)
366 adios (maildir, "unable to change directory to");
368 /* read folder and create message structure */
369 if (!(mp = folder_read (folder)))
370 adios (NULL, "unable to read folder %s", folder);
372 /* check for empty folder */
374 adios (NULL, "no messages in %s", folder);
376 /* parse all the message ranges/sequences and set SELECTED */
377 for (msgnum = 0; msgnum < msgs.size; msgnum++)
378 if (!m_convert (mp, msgs.msgs[msgnum]))
382 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
383 * since we will use that as a tag to know which messages
384 * to remove from the "unseen" sequence.
386 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
387 if (is_selected(mp, msgnum))
388 set_unseen (mp, msgnum);
390 seq_setprev (mp); /* set the Previous-Sequence */
391 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
393 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
394 adios (NULL, "out of memory");
398 * Parse all the SELECTED messages.
400 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
401 if (is_selected(mp, msgnum)) {
404 msgnam = m_name (msgnum);
405 if ((ct = parse_mime (msgnam)))
415 SIGNAL (SIGQUIT, quitser);
416 SIGNAL (SIGPIPE, pipeser);
419 * Get the associated umask for the relevant contents.
421 for (ctp = cts; *ctp; ctp++) {
425 if (type_ok (ct, 1) && !ct->c_umask) {
426 if (stat (ct->c_file, &st) != NOTOK)
427 ct->c_umask = ~(st.st_mode & 0777);
429 ct->c_umask = ~m_gmprot();
434 * Show the message content
436 show_all_messages (cts);
438 /* Now free all the structures for the content */
439 for (ctp = cts; *ctp; ctp++)
445 /* If reading from a folder, do some updating */
447 context_replace (pfolder, folder);/* update current folder */
448 seq_setcur (mp, mp->hghsel); /* update current message */
449 seq_save (mp); /* synchronize sequences */
450 context_save (); /* save the context file */
464 fprintf (stderr, "\n");