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>
29 static struct swit switches[] = {
41 { "noserialonly", 0 },
46 #define FILESW 8 /* interface from show */
49 { "form formfile", 0 },
53 { "type content", 0 },
55 { "rcache policy", 0 },
57 { "wcache policy", 0 },
64 * switches for moreproc/mhlproc
67 { "moreproc program", -4 },
71 { "length lines", -4 },
73 { "width columns", -4 },
76 * switches for debugging
86 extern char *tmp; /* directory to place temp files */
91 extern char *cache_public;
92 extern char *cache_private;
99 extern int nomore; /* flags for moreproc/header display */
105 extern char *parts[NPARTS + 1];
106 extern char *types[NTYPES + 1];
112 #define quitser pipeser
115 CT parse_mime (char *);
118 int part_ok (CT, int);
119 int type_ok (CT, int);
120 void set_endian (void);
121 void flush_errors (void);
124 void show_all_messages (CT *);
127 void free_content (CT);
129 void freects_done (int) NORETURN;
134 static RETSIGTYPE pipeser (int);
138 main (int argc, char **argv)
140 int msgnum, *icachesw;
141 char *cp, *file = NULL, *folder = NULL;
142 char *maildir, buf[100], **argp;
144 struct msgs_array msgs = { 0, 0, NULL };
145 struct msgs *mp = NULL;
152 setlocale(LC_ALL, "");
154 invo_name = r1bindex (argv[0], '/');
156 /* read user profile/context */
159 arguments = getarguments (invo_name, argc, argv, 1);
165 while ((cp = *argp++)) {
167 switch (smatch (++cp, switches)) {
169 ambigsw (cp, switches);
172 adios (NULL, "-%s unknown", cp);
175 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
177 print_help (buf, switches, 1);
180 print_version(invo_name);
184 icachesw = &rcachesw;
187 icachesw = &wcachesw;
189 if (!(cp = *argp++) || *cp == '-')
190 adios (NULL, "missing argument to %s", argp[-2]);
191 switch (*icachesw = smatch (cp, caches)) {
193 ambigsw (cp, caches);
196 adios (NULL, "%s unknown", cp);
224 if (!(cp = *argp++) || *cp == '-')
225 adios (NULL, "missing argument to %s", argp[-2]);
227 adios (NULL, "too many parts (starting with %s), %d max",
233 if (!(cp = *argp++) || *cp == '-')
234 adios (NULL, "missing argument to %s", argp[-2]);
236 adios (NULL, "too many types (starting with %s), %d max",
242 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
243 adios (NULL, "missing argument to %s", argp[-2]);
244 file = *cp == '-' ? cp : path (cp, TFILE);
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
252 formsw = getcpy (etcpath (cp));
256 * Switches for moreproc/mhlproc
259 if (!(progsw = *argp++) || *progsw == '-')
260 adios (NULL, "missing argument to %s", argp[-2]);
268 if (!(cp = *argp++) || *cp == '-')
269 adios (NULL, "missing argument to %s", argp[-2]);
283 if (*cp == '+' || *cp == '@') {
285 adios (NULL, "only one folder at a time!");
287 folder = pluspath (cp);
289 app_msgarg(&msgs, cp);
292 /* null terminate the list of acceptable parts/types */
298 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
304 * Check if we've specified an additional profile
306 if ((cp = getenv ("MHSHOW"))) {
307 if ((fp = fopen (cp, "r"))) {
308 readconfig ((struct node **) 0, fp, cp, 0);
311 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
316 * Read the standard profile setup
318 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
319 readconfig ((struct node **) 0, fp, cp, 0);
323 /* Check for public cache location */
324 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
327 /* Check for private cache location */
328 if (!(cache_private = context_find (nmhprivcache)))
329 cache_private = ".cache";
330 cache_private = getcpy (m_maildir (cache_private));
333 * Check for storage directory. If specified,
334 * then store temporary files there. Else we
335 * store them in standard nmh directory.
337 if ((cp = context_find (nmhstorage)) && *cp)
338 tmp = concat (cp, "/", invo_name, NULL);
340 tmp = add (m_maildir (invo_name), NULL);
342 if (!context_find ("path"))
343 free (path ("./", TFOLDER));
345 if (file && msgs.size)
346 adios (NULL, "cannot specify msg and file at same time!");
349 * check if message is coming from file
352 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
353 adios (NULL, "out of memory");
356 if ((ct = parse_mime (file)));
360 * message(s) are coming from a folder
363 app_msgarg(&msgs, "cur");
365 folder = getfolder (1);
366 maildir = m_maildir (folder);
368 if (chdir (maildir) == NOTOK)
369 adios (maildir, "unable to change directory to");
371 /* read folder and create message structure */
372 if (!(mp = folder_read (folder)))
373 adios (NULL, "unable to read folder %s", folder);
375 /* check for empty folder */
377 adios (NULL, "no messages in %s", folder);
379 /* parse all the message ranges/sequences and set SELECTED */
380 for (msgnum = 0; msgnum < msgs.size; msgnum++)
381 if (!m_convert (mp, msgs.msgs[msgnum]))
385 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
386 * since we will use that as a tag to know which messages
387 * to remove from the "unseen" sequence.
389 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
390 if (is_selected(mp, msgnum))
391 set_unseen (mp, msgnum);
393 seq_setprev (mp); /* set the Previous-Sequence */
394 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
396 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
397 adios (NULL, "out of memory");
401 * Parse all the SELECTED messages.
403 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
404 if (is_selected(mp, msgnum)) {
407 msgnam = m_name (msgnum);
408 if ((ct = parse_mime (msgnam)))
418 SIGNAL (SIGQUIT, quitser);
419 SIGNAL (SIGPIPE, pipeser);
422 * Get the associated umask for the relevant contents.
424 for (ctp = cts; *ctp; ctp++) {
428 if (type_ok (ct, 1) && !ct->c_umask) {
429 if (stat (ct->c_file, &st) != NOTOK)
430 ct->c_umask = ~(st.st_mode & 0777);
432 ct->c_umask = ~m_gmprot();
437 * Show the message content
439 show_all_messages (cts);
441 /* Now free all the structures for the content */
442 for (ctp = cts; *ctp; ctp++)
448 /* If reading from a folder, do some updating */
450 context_replace (pfolder, folder);/* update current folder */
451 seq_setcur (mp, mp->hghsel); /* update current message */
452 seq_save (mp); /* synchronize sequences */
453 context_save (); /* save the context file */
467 fprintf (stderr, "\n");