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
85 extern char *tmp; /* directory to place temp files */
90 extern char *cache_public;
91 extern char *cache_private;
98 extern int nomore; /* flags for moreproc/header display */
104 extern char *parts[NPARTS + 1];
105 extern char *types[NTYPES + 1];
111 #define quitser pipeser
114 CT parse_mime (char *);
117 int part_ok (CT, int);
118 int type_ok (CT, int);
119 void set_endian (void);
120 void flush_errors (void);
123 void show_all_messages (CT *);
126 void free_content (CT);
128 void freects_done (int) NORETURN;
133 static RETSIGTYPE pipeser (int);
137 main (int argc, char **argv)
139 int msgnum, *icachesw;
140 char *cp, *file = NULL, *folder = NULL;
141 char *maildir, buf[100], **argp;
143 struct msgs_array msgs = { 0, 0, NULL };
144 struct msgs *mp = NULL;
151 setlocale(LC_ALL, "");
153 invo_name = r1bindex (argv[0], '/');
155 /* read user profile/context */
158 arguments = getarguments (invo_name, argc, argv, 1);
164 while ((cp = *argp++)) {
166 switch (smatch (++cp, switches)) {
168 ambigsw (cp, switches);
171 adios (NULL, "-%s unknown", cp);
174 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
176 print_help (buf, switches, 1);
179 print_version(invo_name);
183 icachesw = &rcachesw;
186 icachesw = &wcachesw;
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 switch (*icachesw = smatch (cp, caches)) {
192 ambigsw (cp, caches);
195 adios (NULL, "%s unknown", cp);
223 if (!(cp = *argp++) || *cp == '-')
224 adios (NULL, "missing argument to %s", argp[-2]);
226 adios (NULL, "too many parts (starting with %s), %d max",
232 if (!(cp = *argp++) || *cp == '-')
233 adios (NULL, "missing argument to %s", argp[-2]);
235 adios (NULL, "too many types (starting with %s), %d max",
241 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
242 adios (NULL, "missing argument to %s", argp[-2]);
243 file = *cp == '-' ? cp : path (cp, TFILE);
247 if (!(cp = *argp++) || *cp == '-')
248 adios (NULL, "missing argument to %s", argp[-2]);
251 formsw = getcpy (etcpath (cp));
255 * Switches for moreproc/mhlproc
258 if (!(progsw = *argp++) || *progsw == '-')
259 adios (NULL, "missing argument to %s", argp[-2]);
267 if (!(cp = *argp++) || *cp == '-')
268 adios (NULL, "missing argument to %s", argp[-2]);
282 if (*cp == '+' || *cp == '@') {
284 adios (NULL, "only one folder at a time!");
286 folder = pluspath (cp);
288 app_msgarg(&msgs, cp);
291 /* null terminate the list of acceptable parts/types */
297 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
303 * Check if we've specified an additional profile
305 if ((cp = getenv ("MHSHOW"))) {
306 if ((fp = fopen (cp, "r"))) {
307 readconfig ((struct node **) 0, fp, cp, 0);
310 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
315 * Read the standard profile setup
317 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
318 readconfig ((struct node **) 0, fp, cp, 0);
322 /* Check for public cache location */
323 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
326 /* Check for private cache location */
327 if (!(cache_private = context_find (nmhprivcache)))
328 cache_private = ".cache";
329 cache_private = getcpy (m_maildir (cache_private));
332 * Check for storage directory. If specified,
333 * then store temporary files there. Else we
334 * store them in standard nmh directory.
336 if ((cp = context_find (nmhstorage)) && *cp)
337 tmp = concat (cp, "/", invo_name, NULL);
339 tmp = add (m_maildir (invo_name), NULL);
341 if (!context_find ("path"))
342 free (path ("./", TFOLDER));
344 if (file && msgs.size)
345 adios (NULL, "cannot specify msg and file at same time!");
348 * check if message is coming from file
351 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
352 adios (NULL, "out of memory");
355 if ((ct = parse_mime (file)));
359 * message(s) are coming from a folder
362 app_msgarg(&msgs, "cur");
364 folder = getfolder (1);
365 maildir = m_maildir (folder);
367 if (chdir (maildir) == NOTOK)
368 adios (maildir, "unable to change directory to");
370 /* read folder and create message structure */
371 if (!(mp = folder_read (folder)))
372 adios (NULL, "unable to read folder %s", folder);
374 /* check for empty folder */
376 adios (NULL, "no messages in %s", folder);
378 /* parse all the message ranges/sequences and set SELECTED */
379 for (msgnum = 0; msgnum < msgs.size; msgnum++)
380 if (!m_convert (mp, msgs.msgs[msgnum]))
384 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
385 * since we will use that as a tag to know which messages
386 * to remove from the "unseen" sequence.
388 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
389 if (is_selected(mp, msgnum))
390 set_unseen (mp, msgnum);
392 seq_setprev (mp); /* set the Previous-Sequence */
393 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
395 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
396 adios (NULL, "out of memory");
400 * Parse all the SELECTED messages.
402 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
403 if (is_selected(mp, msgnum)) {
406 msgnam = m_name (msgnum);
407 if ((ct = parse_mime (msgnam)))
417 SIGNAL (SIGQUIT, quitser);
418 SIGNAL (SIGPIPE, pipeser);
421 * Get the associated umask for the relevant contents.
423 for (ctp = cts; *ctp; ctp++) {
427 if (type_ok (ct, 1) && !ct->c_umask) {
428 if (stat (ct->c_file, &st) != NOTOK)
429 ct->c_umask = ~(st.st_mode & 0777);
431 ct->c_umask = ~m_gmprot();
436 * Show the message content
438 show_all_messages (cts);
440 /* Now free all the structures for the content */
441 for (ctp = cts; *ctp; ctp++)
447 /* If reading from a folder, do some updating */
449 context_replace (pfolder, folder);/* update current folder */
450 seq_setcur (mp, mp->hghsel); /* update current message */
451 seq_save (mp); /* synchronize sequences */
452 context_save (); /* save the context file */
466 fprintf (stderr, "\n");