2 * mhshow.c -- display the contents of MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
38 { "noserialonly", 0 },
43 #define FILESW 8 /* interface from show */
46 { "form formfile", 0 },
50 { "type content", 0 },
52 { "rcache policy", 0 },
54 { "wcache policy", 0 },
61 * switches for moreproc/mhlproc
64 { "moreproc program", -4 },
68 { "length lines", -4 },
70 { "width columns", -4 },
73 * switches for debugging
82 extern char *tmp; /* directory to place temp files */
87 extern char *cache_public;
88 extern char *cache_private;
95 extern int nomore; /* flags for moreproc/header display */
101 extern char *parts[NPARTS + 1];
102 extern char *types[NTYPES + 1];
108 #define quitser pipeser
111 CT parse_mime (char *);
114 int part_ok (CT, int);
115 int type_ok (CT, int);
116 void set_endian (void);
117 void flush_errors (void);
120 void show_all_messages (CT *);
123 void free_content (CT);
125 void freects_done (int) NORETURN;
130 static RETSIGTYPE pipeser (int);
134 main (int argc, char **argv)
136 int msgnum, *icachesw;
137 char *cp, *file = NULL, *folder = NULL;
138 char *maildir, buf[100], **argp;
140 struct msgs_array msgs = { 0, 0, NULL };
141 struct msgs *mp = NULL;
148 setlocale(LC_ALL, "");
150 invo_name = r1bindex (argv[0], '/');
152 /* read user profile/context */
155 arguments = getarguments (invo_name, argc, argv, 1);
161 while ((cp = *argp++)) {
163 switch (smatch (++cp, switches)) {
165 ambigsw (cp, switches);
168 adios (NULL, "-%s unknown", cp);
171 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
173 print_help (buf, switches, 1);
176 print_version(invo_name);
180 icachesw = &rcachesw;
183 icachesw = &wcachesw;
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 switch (*icachesw = smatch (cp, caches)) {
189 ambigsw (cp, caches);
192 adios (NULL, "%s unknown", cp);
220 if (!(cp = *argp++) || *cp == '-')
221 adios (NULL, "missing argument to %s", argp[-2]);
223 adios (NULL, "too many parts (starting with %s), %d max",
229 if (!(cp = *argp++) || *cp == '-')
230 adios (NULL, "missing argument to %s", argp[-2]);
232 adios (NULL, "too many types (starting with %s), %d max",
238 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
239 adios (NULL, "missing argument to %s", argp[-2]);
240 file = *cp == '-' ? cp : path (cp, TFILE);
244 if (!(cp = *argp++) || *cp == '-')
245 adios (NULL, "missing argument to %s", argp[-2]);
248 formsw = getcpy (etcpath (cp));
252 * Switches for moreproc/mhlproc
255 if (!(progsw = *argp++) || *progsw == '-')
256 adios (NULL, "missing argument to %s", argp[-2]);
264 if (!(cp = *argp++) || *cp == '-')
265 adios (NULL, "missing argument to %s", argp[-2]);
279 if (*cp == '+' || *cp == '@') {
281 adios (NULL, "only one folder at a time!");
283 folder = pluspath (cp);
285 app_msgarg(&msgs, cp);
288 /* null terminate the list of acceptable parts/types */
294 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
300 * Check if we've specified an additional profile
302 if ((cp = getenv ("MHSHOW"))) {
303 if ((fp = fopen (cp, "r"))) {
304 readconfig ((struct node **) 0, fp, cp, 0);
307 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
312 * Read the standard profile setup
314 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
315 readconfig ((struct node **) 0, fp, cp, 0);
319 /* Check for public cache location */
320 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
323 /* Check for private cache location */
324 if (!(cache_private = context_find (nmhprivcache)))
325 cache_private = ".cache";
326 cache_private = getcpy (m_maildir (cache_private));
329 * Check for storage directory. If specified,
330 * then store temporary files there. Else we
331 * store them in standard nmh directory.
333 if ((cp = context_find (nmhstorage)) && *cp)
334 tmp = concat (cp, "/", invo_name, NULL);
336 tmp = add (m_maildir (invo_name), NULL);
338 if (!context_find ("path"))
339 free (path ("./", TFOLDER));
341 if (file && msgs.size)
342 adios (NULL, "cannot specify msg and file at same time!");
345 * check if message is coming from file
348 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
349 adios (NULL, "out of memory");
352 if ((ct = parse_mime (file)));
356 * message(s) are coming from a folder
359 app_msgarg(&msgs, "cur");
361 folder = getfolder (1);
362 maildir = m_maildir (folder);
364 if (chdir (maildir) == NOTOK)
365 adios (maildir, "unable to change directory to");
367 /* read folder and create message structure */
368 if (!(mp = folder_read (folder)))
369 adios (NULL, "unable to read folder %s", folder);
371 /* check for empty folder */
373 adios (NULL, "no messages in %s", folder);
375 /* parse all the message ranges/sequences and set SELECTED */
376 for (msgnum = 0; msgnum < msgs.size; msgnum++)
377 if (!m_convert (mp, msgs.msgs[msgnum]))
381 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
382 * since we will use that as a tag to know which messages
383 * to remove from the "unseen" sequence.
385 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
386 if (is_selected(mp, msgnum))
387 set_unseen (mp, msgnum);
389 seq_setprev (mp); /* set the Previous-Sequence */
390 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
392 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
393 adios (NULL, "out of memory");
397 * Parse all the SELECTED messages.
399 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
400 if (is_selected(mp, msgnum)) {
403 msgnam = m_name (msgnum);
404 if ((ct = parse_mime (msgnam)))
414 SIGNAL (SIGQUIT, quitser);
415 SIGNAL (SIGPIPE, pipeser);
418 * Get the associated umask for the relevant contents.
420 for (ctp = cts; *ctp; ctp++) {
424 if (type_ok (ct, 1) && !ct->c_umask) {
425 if (stat (ct->c_file, &st) != NOTOK)
426 ct->c_umask = ~(st.st_mode & 0777);
428 ct->c_umask = ~m_gmprot();
433 * Show the message content
435 show_all_messages (cts);
437 /* Now free all the structures for the content */
438 for (ctp = cts; *ctp; ctp++)
444 /* If reading from a folder, do some updating */
446 context_replace (pfolder, folder); /* update current folder */
447 seq_setcur (mp, mp->hghsel); /* update current message */
448 seq_save (mp); /* synchronize sequences */
449 context_save (); /* save the context file */
463 fprintf (stderr, "\n");