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 },
138 extern char *tmp; /* directory to place temp files */
143 extern char *cache_public;
144 extern char *cache_private;
151 extern int nomore; /* flags for moreproc/header display */
156 extern char *cwd; /* cache current working directory */
161 extern char *parts[NPARTS + 1];
162 extern char *types[NTYPES + 1];
168 /* The list of top-level contents to display */
172 * variables for mhbuild (mhn -build)
174 static int buildsw = 0;
175 static int ebcdicsw = 0;
176 static int rfc934sw = 0;
179 * what action to take?
181 static int cachesw = 0;
182 static int listsw = 0;
183 static int showsw = 0;
184 static int storesw = 0;
186 #define quitser pipeser
189 CT parse_mime (char *);
192 int part_ok (CT, int);
193 int type_ok (CT, int);
194 void set_endian (void);
195 void flush_errors (void);
198 void show_all_messages (CT *);
201 void list_all_messages (CT *, int, int, int, int);
204 void store_all_messages (CT *);
207 void cache_all_messages (CT *);
210 void free_content (CT);
215 static RETSIGTYPE pipeser (int);
219 main (int argc, char **argv)
221 int sizesw = 1, headsw = 1;
222 int nummsgs, maxmsgs, msgnum, *icachesw;
223 char *cp, *file = NULL, *folder = NULL;
224 char *maildir, buf[100], **argp;
225 char **arguments, **msgs;
226 struct msgs *mp = NULL;
231 setlocale(LC_ALL, "");
233 invo_name = r1bindex (argv[0], '/');
235 /* read user profile/context */
238 arguments = getarguments (invo_name, argc, argv, 1);
242 * Allocate the initial space to record message
243 * names, ranges, and sequences.
247 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
248 adios (NULL, "unable to allocate storage");
253 while ((cp = *argp++)) {
255 switch (smatch (++cp, switches)) {
257 ambigsw (cp, switches);
260 adios (NULL, "-%s unknown", cp);
263 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
265 print_help (buf, switches, 1);
268 print_version(invo_name);
286 icachesw = &rcachesw;
289 icachesw = &wcachesw;
291 if (!(cp = *argp++) || *cp == '-')
292 adios (NULL, "missing argument to %s", argp[-2]);
293 switch (*icachesw = smatch (cp, caches)) {
295 ambigsw (cp, caches);
298 adios (NULL, "%s unknown", cp);
361 if (!(cp = *argp++) || *cp == '-')
362 adios (NULL, "missing argument to %s", argp[-2]);
364 adios (NULL, "too many parts (starting with %s), %d max",
370 if (!(cp = *argp++) || *cp == '-')
371 adios (NULL, "missing argument to %s", argp[-2]);
373 adios (NULL, "too many types (starting with %s), %d max",
379 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
380 adios (NULL, "missing argument to %s", argp[-2]);
381 file = *cp == '-' ? cp : path (cp, TFILE);
385 if (!(cp = *argp++) || *cp == '-')
386 adios (NULL, "missing argument to %s", argp[-2]);
389 formsw = getcpy (etcpath (cp));
393 * Switches for moreproc/mhlproc
396 if (!(progsw = *argp++) || *progsw == '-')
397 adios (NULL, "missing argument to %s", argp[-2]);
405 if (!(cp = *argp++) || *cp == '-')
406 adios (NULL, "missing argument to %s", argp[-2]);
410 * Switches for mhbuild
442 if (*cp == '+' || *cp == '@') {
444 adios (NULL, "only one folder at a time!");
446 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
449 * Check if we need to allocate more space
450 * for message names/ranges/sequences.
452 if (nummsgs >= maxmsgs) {
454 if (!(msgs = (char **) realloc (msgs,
455 (size_t) (maxmsgs * sizeof(*msgs)))))
456 adios (NULL, "unable to reallocate msgs storage");
458 msgs[nummsgs++] = cp;
462 /* null terminate the list of acceptable parts/types */
468 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
475 * Check if we've specified an additional profile
477 if ((cp = getenv ("MHN"))) {
478 if ((fp = fopen (cp, "r"))) {
479 readconfig ((struct node **) 0, fp, cp, 0);
482 admonish ("", "unable to read $MHN profile (%s)", cp);
487 * Read the standard profile setup
489 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
490 readconfig ((struct node **) 0, fp, cp, 0);
494 /* Check for public cache location */
495 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
498 /* Check for private cache location */
499 if (!(cache_private = context_find (nmhprivcache)))
500 cache_private = ".cache";
501 cache_private = getcpy (m_maildir (cache_private));
504 * Cache the current directory before we do any chdirs()'s.
506 cwd = getcpy (pwd());
509 * Check for storage directory. If specified,
510 * then store temporary files there. Else we
511 * store them in standard nmh directory.
513 if ((cp = context_find (nmhstorage)) && *cp)
514 tmp = concat (cp, "/", invo_name, NULL);
516 tmp = add (m_maildir (invo_name), NULL);
518 if (!context_find ("path"))
519 free (path ("./", TFOLDER));
522 * Process a mhn composition file (mhn -build)
528 if (showsw || storesw || cachesw)
529 adios (NULL, "cannot use -build with -show, -store, -cache");
531 adios (NULL, "need to specify a %s composition file", invo_name);
533 adios (NULL, "only one %s composition file at a time", invo_name);
536 vec[vecp++] = "mhbuild";
539 vec[vecp++] = "-ebcdicsafe";
540 else if (ebcdicsw == -1)
541 vec[vecp++] = "-noebcdicsafe";
544 vec[vecp++] = "-rfc934mode";
545 else if (rfc934sw == -1)
546 vec[vecp++] = "-norfc934mode";
548 vec[vecp++] = msgs[0];
551 execvp ("mhbuild", vec);
552 fprintf (stderr, "unable to exec ");
557 * Process a mhn composition file (old MH style)
559 if (nummsgs == 1 && !folder && !npart && !cachesw
560 && !showsw && !storesw && !ntype && !file
561 && (cp = getenv ("mhdraft"))
562 && strcmp (cp, msgs[0]) == 0) {
568 vec[vecp++] = "mhbuild";
571 vec[vecp++] = "-ebcdicsafe";
572 else if (ebcdicsw == -1)
573 vec[vecp++] = "-noebcdicsafe";
576 vec[vecp++] = "-rfc934mode";
577 else if (rfc934sw == -1)
578 vec[vecp++] = "-norfc934mode";
583 execvp ("mhbuild", vec);
584 fprintf (stderr, "unable to exec ");
589 adios (NULL, "cannot specify msg and file at same time!");
592 * check if message is coming from file
595 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
596 adios (NULL, "out of memory");
599 if ((ct = parse_mime (file)));
603 * message(s) are coming from a folder
606 msgs[nummsgs++] = "cur";
608 folder = getfolder (1);
609 maildir = m_maildir (folder);
611 if (chdir (maildir) == NOTOK)
612 adios (maildir, "unable to change directory to");
614 /* read folder and create message structure */
615 if (!(mp = folder_read (folder)))
616 adios (NULL, "unable to read folder %s", folder);
618 /* check for empty folder */
620 adios (NULL, "no messages in %s", folder);
622 /* parse all the message ranges/sequences and set SELECTED */
623 for (msgnum = 0; msgnum < nummsgs; msgnum++)
624 if (!m_convert (mp, msgs[msgnum]))
626 seq_setprev (mp); /* set the previous-sequence */
628 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
629 adios (NULL, "out of memory");
632 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
633 if (is_selected(mp, msgnum)) {
636 msgnam = m_name (msgnum);
637 if ((ct = parse_mime (msgnam)))
647 * You can't give more than one of these flags
650 if (showsw + listsw + storesw + cachesw > 1)
651 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
653 /* If no action is specified, assume -show */
654 if (!listsw && !showsw && !storesw && !cachesw)
658 SIGNAL (SIGQUIT, quitser);
659 SIGNAL (SIGPIPE, pipeser);
662 * Get the associated umask for the relevant contents.
664 for (ctp = cts; *ctp; ctp++) {
668 if (type_ok (ct, 1) && !ct->c_umask) {
669 if (stat (ct->c_file, &st) != NOTOK)
670 ct->c_umask = ~(st.st_mode & 0777);
672 ct->c_umask = ~m_gmprot();
677 * List the message content
680 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
683 * Store the message content
686 store_all_messages (cts);
689 * Cache the message content
692 cache_all_messages (cts);
695 * Show the message content
698 show_all_messages (cts);
700 /* Now free all the structures for the content */
701 for (ctp = cts; *ctp; ctp++)
707 /* If reading from a folder, do some updating */
709 context_replace (pfolder, folder);/* update current folder */
710 seq_setcur (mp, mp->hghsel); /* update current message */
711 seq_save (mp); /* synchronize sequences */
712 context_save (); /* save the context file */
725 fprintf (stderr, "\n");
744 return 1; /* dead code to satisfy the compiler */