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 /* The list of top-level contents to display */
115 #define quitser pipeser
118 CT parse_mime (char *);
121 int part_ok (CT, int);
122 int type_ok (CT, int);
123 void set_endian (void);
124 void flush_errors (void);
127 void show_all_messages (CT *);
130 void free_content (CT);
135 static RETSIGTYPE pipeser (int);
136 static int freectp_done (int);
140 main (int argc, char **argv)
142 int msgnum, *icachesw;
143 char *cp, *file = NULL, *folder = NULL;
144 char *maildir, buf[100], **argp;
146 struct msgs_array msgs = { 0, 0, NULL };
147 struct msgs *mp = NULL;
154 setlocale(LC_ALL, "");
156 invo_name = r1bindex (argv[0], '/');
158 /* read user profile/context */
161 arguments = getarguments (invo_name, argc, argv, 1);
167 while ((cp = *argp++)) {
169 switch (smatch (++cp, switches)) {
171 ambigsw (cp, switches);
174 adios (NULL, "-%s unknown", cp);
177 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
179 print_help (buf, switches, 1);
182 print_version(invo_name);
186 icachesw = &rcachesw;
189 icachesw = &wcachesw;
191 if (!(cp = *argp++) || *cp == '-')
192 adios (NULL, "missing argument to %s", argp[-2]);
193 switch (*icachesw = smatch (cp, caches)) {
195 ambigsw (cp, caches);
198 adios (NULL, "%s unknown", cp);
226 if (!(cp = *argp++) || *cp == '-')
227 adios (NULL, "missing argument to %s", argp[-2]);
229 adios (NULL, "too many parts (starting with %s), %d max",
235 if (!(cp = *argp++) || *cp == '-')
236 adios (NULL, "missing argument to %s", argp[-2]);
238 adios (NULL, "too many types (starting with %s), %d max",
244 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
245 adios (NULL, "missing argument to %s", argp[-2]);
246 file = *cp == '-' ? cp : path (cp, TFILE);
250 if (!(cp = *argp++) || *cp == '-')
251 adios (NULL, "missing argument to %s", argp[-2]);
254 formsw = getcpy (etcpath (cp));
258 * Switches for moreproc/mhlproc
261 if (!(progsw = *argp++) || *progsw == '-')
262 adios (NULL, "missing argument to %s", argp[-2]);
270 if (!(cp = *argp++) || *cp == '-')
271 adios (NULL, "missing argument to %s", argp[-2]);
285 if (*cp == '+' || *cp == '@') {
287 adios (NULL, "only one folder at a time!");
289 folder = pluspath (cp);
291 app_msgarg(&msgs, cp);
294 /* null terminate the list of acceptable parts/types */
300 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
306 * Check if we've specified an additional profile
308 if ((cp = getenv ("MHSHOW"))) {
309 if ((fp = fopen (cp, "r"))) {
310 readconfig ((struct node **) 0, fp, cp, 0);
313 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
318 * Read the standard profile setup
320 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
321 readconfig ((struct node **) 0, fp, cp, 0);
325 /* Check for public cache location */
326 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
329 /* Check for private cache location */
330 if (!(cache_private = context_find (nmhprivcache)))
331 cache_private = ".cache";
332 cache_private = getcpy (m_maildir (cache_private));
335 * Check for storage directory. If specified,
336 * then store temporary files there. Else we
337 * store them in standard nmh directory.
339 if ((cp = context_find (nmhstorage)) && *cp)
340 tmp = concat (cp, "/", invo_name, NULL);
342 tmp = add (m_maildir (invo_name), NULL);
344 if (!context_find ("path"))
345 free (path ("./", TFOLDER));
347 if (file && msgs.size)
348 adios (NULL, "cannot specify msg and file at same time!");
351 * check if message is coming from file
354 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
355 adios (NULL, "out of memory");
358 if ((ct = parse_mime (file)));
362 * message(s) are coming from a folder
365 app_msgarg(&msgs, "cur");
367 folder = getfolder (1);
368 maildir = m_maildir (folder);
370 if (chdir (maildir) == NOTOK)
371 adios (maildir, "unable to change directory to");
373 /* read folder and create message structure */
374 if (!(mp = folder_read (folder)))
375 adios (NULL, "unable to read folder %s", folder);
377 /* check for empty folder */
379 adios (NULL, "no messages in %s", folder);
381 /* parse all the message ranges/sequences and set SELECTED */
382 for (msgnum = 0; msgnum < msgs.size; msgnum++)
383 if (!m_convert (mp, msgs.msgs[msgnum]))
387 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
388 * since we will use that as a tag to know which messages
389 * to remove from the "unseen" sequence.
391 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
392 if (is_selected(mp, msgnum))
393 set_unseen (mp, msgnum);
395 seq_setprev (mp); /* set the Previous-Sequence */
396 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
398 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
399 adios (NULL, "out of memory");
403 * Parse all the SELECTED messages.
405 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
406 if (is_selected(mp, msgnum)) {
409 msgnam = m_name (msgnum);
410 if ((ct = parse_mime (msgnam)))
420 SIGNAL (SIGQUIT, quitser);
421 SIGNAL (SIGPIPE, pipeser);
424 * Get the associated umask for the relevant contents.
426 for (ctp = cts; *ctp; ctp++) {
430 if (type_ok (ct, 1) && !ct->c_umask) {
431 if (stat (ct->c_file, &st) != NOTOK)
432 ct->c_umask = ~(st.st_mode & 0777);
434 ct->c_umask = ~m_gmprot();
439 * Show the message content
441 show_all_messages (cts);
443 /* Now free all the structures for the content */
444 for (ctp = cts; *ctp; ctp++)
450 /* If reading from a folder, do some updating */
452 context_replace (pfolder, folder);/* update current folder */
453 seq_setcur (mp, mp->hghsel); /* update current message */
454 seq_save (mp); /* synchronize sequences */
455 context_save (); /* save the context file */
468 fprintf (stderr, "\n");
478 freectp_done (int status)
487 return 1; /* dead code to satisfy the compiler */