3 * mhn.c -- display, list, cache, or store 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>
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
29 * We allocate space for message names (msgs array)
30 * this number of elements at a time.
35 static struct swit switches[] = {
67 { "noserialonly", 0 },
80 #define FILESW 22 /* interface from show */
83 { "form formfile", 0 },
87 { "type content", 0 },
89 { "rcache policy", 0 },
91 { "wcache policy", 0 },
98 * switches for debugging
104 * switches for moreproc/mhlproc
107 { "moreproc program", -4 },
109 { "nomoreproc", -3 },
111 { "length lines", -4 },
113 { "width columns", -4 },
116 * switches for mhbuild
123 { "ebcdicsafe", -10 },
125 { "noebcdicsafe", -12 },
127 { "rfc934mode", -10 },
129 { "norfc934mode", -12 },
136 extern char *tmp; /* directory to place temp files */
141 extern char *cache_public;
142 extern char *cache_private;
149 extern int nomore; /* flags for moreproc/header display */
154 extern char *cwd; /* cache current working directory */
159 extern char *parts[NPARTS + 1];
160 extern char *types[NTYPES + 1];
166 /* The list of top-level contents to display */
170 * variables for mhbuild (mhn -build)
172 static int buildsw = 0;
173 static int ebcdicsw = 0;
174 static int rfc934sw = 0;
177 * what action to take?
179 static int cachesw = 0;
180 static int listsw = 0;
181 static int showsw = 0;
182 static int storesw = 0;
184 #define quitser pipeser
187 CT parse_mime (char *);
190 int part_ok (CT, int);
191 int type_ok (CT, int);
192 void set_endian (void);
193 void flush_errors (void);
196 void show_all_messages (CT *);
199 void list_all_messages (CT *, int, int, int, int);
202 void store_all_messages (CT *);
205 void cache_all_messages (CT *);
208 void free_content (CT);
213 static RETSIGTYPE pipeser (int);
217 main (int argc, char **argv)
219 int sizesw = 1, headsw = 1;
220 int nummsgs, maxmsgs, msgnum, *icachesw;
221 char *cp, *file = NULL, *folder = NULL;
222 char *maildir, buf[100], **argp;
223 char **arguments, **msgs;
224 struct msgs *mp = NULL;
229 setlocale(LC_ALL, "");
231 invo_name = r1bindex (argv[0], '/');
233 /* read user profile/context */
236 arguments = getarguments (invo_name, argc, argv, 1);
240 * Allocate the initial space to record message
241 * names, ranges, and sequences.
245 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
246 adios (NULL, "unable to allocate storage");
251 while ((cp = *argp++)) {
253 switch (smatch (++cp, switches)) {
255 ambigsw (cp, switches);
258 adios (NULL, "-%s unknown", cp);
261 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
263 print_help (buf, switches, 1);
266 print_version(invo_name);
284 icachesw = &rcachesw;
287 icachesw = &wcachesw;
289 if (!(cp = *argp++) || *cp == '-')
290 adios (NULL, "missing argument to %s", argp[-2]);
291 switch (*icachesw = smatch (cp, caches)) {
293 ambigsw (cp, caches);
296 adios (NULL, "%s unknown", cp);
359 if (!(cp = *argp++) || *cp == '-')
360 adios (NULL, "missing argument to %s", argp[-2]);
362 adios (NULL, "too many parts (starting with %s), %d max",
368 if (!(cp = *argp++) || *cp == '-')
369 adios (NULL, "missing argument to %s", argp[-2]);
371 adios (NULL, "too many types (starting with %s), %d max",
377 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
378 adios (NULL, "missing argument to %s", argp[-2]);
379 file = *cp == '-' ? cp : path (cp, TFILE);
383 if (!(cp = *argp++) || *cp == '-')
384 adios (NULL, "missing argument to %s", argp[-2]);
387 formsw = getcpy (etcpath (cp));
391 * Switches for moreproc/mhlproc
394 if (!(progsw = *argp++) || *progsw == '-')
395 adios (NULL, "missing argument to %s", argp[-2]);
403 if (!(cp = *argp++) || *cp == '-')
404 adios (NULL, "missing argument to %s", argp[-2]);
408 * Switches for mhbuild
440 if (*cp == '+' || *cp == '@') {
442 adios (NULL, "only one folder at a time!");
444 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
447 * Check if we need to allocate more space
448 * for message names/ranges/sequences.
450 if (nummsgs >= maxmsgs) {
452 if (!(msgs = (char **) realloc (msgs,
453 (size_t) (maxmsgs * sizeof(*msgs)))))
454 adios (NULL, "unable to reallocate msgs storage");
456 msgs[nummsgs++] = cp;
460 /* null terminate the list of acceptable parts/types */
466 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
473 * Check if we've specified an additional profile
475 if ((cp = getenv ("MHN"))) {
476 if ((fp = fopen (cp, "r"))) {
477 readconfig ((struct node **) 0, fp, cp, 0);
480 admonish ("", "unable to read $MHN profile (%s)", cp);
485 * Read the standard profile setup
487 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
488 readconfig ((struct node **) 0, fp, cp, 0);
492 /* Check for public cache location */
493 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
496 /* Check for private cache location */
497 if (!(cache_private = context_find (nmhprivcache)))
498 cache_private = ".cache";
499 cache_private = getcpy (m_maildir (cache_private));
502 * Cache the current directory before we do any chdirs()'s.
504 cwd = getcpy (pwd());
507 * Check for storage directory. If specified,
508 * then store temporary files there. Else we
509 * store them in standard nmh directory.
511 if ((cp = context_find (nmhstorage)) && *cp)
512 tmp = concat (cp, "/", invo_name, NULL);
514 tmp = add (m_maildir (invo_name), NULL);
516 if (!context_find ("path"))
517 free (path ("./", TFOLDER));
520 * Process a mhn composition file (mhn -build)
526 if (showsw || storesw || cachesw)
527 adios (NULL, "cannot use -build with -show, -store, -cache");
529 adios (NULL, "need to specify a %s composition file", invo_name);
531 adios (NULL, "only one %s composition file at a time", invo_name);
534 vec[vecp++] = "mhbuild";
537 vec[vecp++] = "-ebcdicsafe";
538 else if (ebcdicsw == -1)
539 vec[vecp++] = "-noebcdicsafe";
542 vec[vecp++] = "-rfc934mode";
543 else if (rfc934sw == -1)
544 vec[vecp++] = "-norfc934mode";
546 vec[vecp++] = msgs[0];
549 execvp ("mhbuild", vec);
550 fprintf (stderr, "unable to exec ");
555 * Process a mhn composition file (old MH style)
557 if (nummsgs == 1 && !folder && !npart && !cachesw
558 && !showsw && !storesw && !ntype && !file
559 && (cp = getenv ("mhdraft"))
560 && strcmp (cp, msgs[0]) == 0) {
566 vec[vecp++] = "mhbuild";
569 vec[vecp++] = "-ebcdicsafe";
570 else if (ebcdicsw == -1)
571 vec[vecp++] = "-noebcdicsafe";
574 vec[vecp++] = "-rfc934mode";
575 else if (rfc934sw == -1)
576 vec[vecp++] = "-norfc934mode";
581 execvp ("mhbuild", vec);
582 fprintf (stderr, "unable to exec ");
587 adios (NULL, "cannot specify msg and file at same time!");
590 * check if message is coming from file
593 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
594 adios (NULL, "out of memory");
597 if ((ct = parse_mime (file)));
601 * message(s) are coming from a folder
604 msgs[nummsgs++] = "cur";
606 folder = getfolder (1);
607 maildir = m_maildir (folder);
609 if (chdir (maildir) == NOTOK)
610 adios (maildir, "unable to change directory to");
612 /* read folder and create message structure */
613 if (!(mp = folder_read (folder)))
614 adios (NULL, "unable to read folder %s", folder);
616 /* check for empty folder */
618 adios (NULL, "no messages in %s", folder);
620 /* parse all the message ranges/sequences and set SELECTED */
621 for (msgnum = 0; msgnum < nummsgs; msgnum++)
622 if (!m_convert (mp, msgs[msgnum]))
624 seq_setprev (mp); /* set the previous-sequence */
626 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
627 adios (NULL, "out of memory");
630 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
631 if (is_selected(mp, msgnum)) {
634 msgnam = m_name (msgnum);
635 if ((ct = parse_mime (msgnam)))
645 * You can't give more than one of these flags
648 if (showsw + listsw + storesw + cachesw > 1)
649 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
651 /* If no action is specified, assume -show */
652 if (!listsw && !showsw && !storesw && !cachesw)
656 SIGNAL (SIGQUIT, quitser);
657 SIGNAL (SIGPIPE, pipeser);
660 * Get the associated umask for the relevant contents.
662 for (ctp = cts; *ctp; ctp++) {
666 if (type_ok (ct, 1) && !ct->c_umask) {
667 if (stat (ct->c_file, &st) != NOTOK)
668 ct->c_umask = ~(st.st_mode & 0777);
670 ct->c_umask = ~m_gmprot();
675 * List the message content
678 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
681 * Store the message content
684 store_all_messages (cts);
687 * Cache the message content
690 cache_all_messages (cts);
693 * Show the message content
696 show_all_messages (cts);
698 /* Now free all the structures for the content */
699 for (ctp = cts; *ctp; ctp++)
705 /* If reading from a folder, do some updating */
707 context_replace (pfolder, folder);/* update current folder */
708 seq_setcur (mp, mp->hghsel); /* update current message */
709 seq_save (mp); /* synchronize sequences */
710 context_save (); /* save the context file */
723 fprintf (stderr, "\n");
742 return 1; /* dead code to satisfy the compiler */