]>
diplodocus.org Git - nmh/blob - uip/mhn.c
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>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
30 * We allocate space for message names (msgs array)
31 * this number of elements at a time.
36 static struct swit switches
[] = {
68 { "noserialonly", 0 },
81 #define FILESW 22 /* interface from show */
84 { "form formfile", 0 },
88 { "type content", 0 },
90 { "rcache policy", 0 },
92 { "wcache policy", 0 },
99 * switches for debugging
105 * switches for moreproc/mhlproc
108 { "moreproc program", -4 },
110 { "nomoreproc", -3 },
112 { "length lines", -4 },
114 { "width columns", -4 },
117 * switches for mhbuild
124 { "ebcdicsafe", -10 },
126 { "noebcdicsafe", -12 },
128 { "rfc934mode", -10 },
130 { "norfc934mode", -12 },
137 extern char *tmp
; /* directory to place temp files */
142 extern char *cache_public
;
143 extern char *cache_private
;
150 extern int nomore
; /* flags for moreproc/header display */
155 extern char *cwd
; /* cache current working directory */
160 extern char *parts
[NPARTS
+ 1];
161 extern char *types
[NTYPES
+ 1];
167 /* The list of top-level contents to display */
171 * variables for mhbuild (mhn -build)
173 static int buildsw
= 0;
174 static int ebcdicsw
= 0;
175 static int rfc934sw
= 0;
178 * what action to take?
180 static int cachesw
= 0;
181 static int listsw
= 0;
182 static int showsw
= 0;
183 static int storesw
= 0;
185 #define quitser pipeser
188 CT
parse_mime (char *);
191 int part_ok (CT
, int);
192 int type_ok (CT
, int);
193 void set_endian (void);
194 void flush_errors (void);
197 void show_all_messages (CT
*);
200 void list_all_messages (CT
*, int, int, int, int);
203 void store_all_messages (CT
*);
206 void cache_all_messages (CT
*);
209 void free_content (CT
);
214 static RETSIGTYPE
pipeser (int);
218 main (int argc
, char **argv
)
220 int sizesw
= 1, headsw
= 1;
221 int nummsgs
, maxmsgs
, msgnum
, *icachesw
;
222 char *cp
, *file
= NULL
, *folder
= NULL
;
223 char *maildir
, buf
[100], **argp
;
224 char **arguments
, **msgs
;
225 struct msgs
*mp
= NULL
;
230 setlocale(LC_ALL
, "");
232 invo_name
= r1bindex (argv
[0], '/');
234 /* read user profile/context */
237 arguments
= getarguments (invo_name
, argc
, argv
, 1);
241 * Allocate the initial space to record message
242 * names, ranges, and sequences.
246 msgs
= (char **) mh_xmalloc ((size_t) (maxmsgs
* sizeof(*msgs
)));
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 msgs
= (char **) mh_xrealloc (msgs
,
453 (size_t) (maxmsgs
* sizeof(*msgs
)));
455 msgs
[nummsgs
++] = cp
;
459 /* null terminate the list of acceptable parts/types */
465 if ((cp
= getenv ("MM_NOASK")) && !strcmp (cp
, "1")) {
472 * Check if we've specified an additional profile
474 if ((cp
= getenv ("MHN"))) {
475 if ((fp
= fopen (cp
, "r"))) {
476 readconfig ((struct node
**) 0, fp
, cp
, 0);
479 admonish ("", "unable to read $MHN profile (%s)", cp
);
484 * Read the standard profile setup
486 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
487 readconfig ((struct node
**) 0, fp
, cp
, 0);
491 /* Check for public cache location */
492 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
495 /* Check for private cache location */
496 if (!(cache_private
= context_find (nmhprivcache
)))
497 cache_private
= ".cache";
498 cache_private
= getcpy (m_maildir (cache_private
));
501 * Cache the current directory before we do any chdirs()'s.
503 cwd
= getcpy (pwd());
506 * Check for storage directory. If specified,
507 * then store temporary files there. Else we
508 * store them in standard nmh directory.
510 if ((cp
= context_find (nmhstorage
)) && *cp
)
511 tmp
= concat (cp
, "/", invo_name
, NULL
);
513 tmp
= add (m_maildir (invo_name
), NULL
);
515 if (!context_find ("path"))
516 free (path ("./", TFOLDER
));
519 * Process a mhn composition file (mhn -build)
525 if (showsw
|| storesw
|| cachesw
)
526 adios (NULL
, "cannot use -build with -show, -store, -cache");
528 adios (NULL
, "need to specify a %s composition file", invo_name
);
530 adios (NULL
, "only one %s composition file at a time", invo_name
);
533 vec
[vecp
++] = "mhbuild";
536 vec
[vecp
++] = "-ebcdicsafe";
537 else if (ebcdicsw
== -1)
538 vec
[vecp
++] = "-noebcdicsafe";
541 vec
[vecp
++] = "-rfc934mode";
542 else if (rfc934sw
== -1)
543 vec
[vecp
++] = "-norfc934mode";
545 vec
[vecp
++] = msgs
[0];
548 execvp ("mhbuild", vec
);
549 fprintf (stderr
, "unable to exec ");
554 * Process a mhn composition file (old MH style)
556 if (nummsgs
== 1 && !folder
&& !npart
&& !cachesw
557 && !showsw
&& !storesw
&& !ntype
&& !file
558 && (cp
= getenv ("mhdraft"))
559 && strcmp (cp
, msgs
[0]) == 0) {
565 vec
[vecp
++] = "mhbuild";
568 vec
[vecp
++] = "-ebcdicsafe";
569 else if (ebcdicsw
== -1)
570 vec
[vecp
++] = "-noebcdicsafe";
573 vec
[vecp
++] = "-rfc934mode";
574 else if (rfc934sw
== -1)
575 vec
[vecp
++] = "-norfc934mode";
580 execvp ("mhbuild", vec
);
581 fprintf (stderr
, "unable to exec ");
586 adios (NULL
, "cannot specify msg and file at same time!");
589 * check if message is coming from file
592 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
593 adios (NULL
, "out of memory");
596 if ((ct
= parse_mime (file
)));
600 * message(s) are coming from a folder
603 msgs
[nummsgs
++] = "cur";
605 folder
= getfolder (1);
606 maildir
= m_maildir (folder
);
608 if (chdir (maildir
) == NOTOK
)
609 adios (maildir
, "unable to change directory to");
611 /* read folder and create message structure */
612 if (!(mp
= folder_read (folder
)))
613 adios (NULL
, "unable to read folder %s", folder
);
615 /* check for empty folder */
617 adios (NULL
, "no messages in %s", folder
);
619 /* parse all the message ranges/sequences and set SELECTED */
620 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
621 if (!m_convert (mp
, msgs
[msgnum
]))
623 seq_setprev (mp
); /* set the previous-sequence */
625 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
626 adios (NULL
, "out of memory");
629 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
630 if (is_selected(mp
, msgnum
)) {
633 msgnam
= m_name (msgnum
);
634 if ((ct
= parse_mime (msgnam
)))
644 * You can't give more than one of these flags
647 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
648 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
650 /* If no action is specified, assume -show */
651 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
655 SIGNAL (SIGQUIT
, quitser
);
656 SIGNAL (SIGPIPE
, pipeser
);
659 * Get the associated umask for the relevant contents.
661 for (ctp
= cts
; *ctp
; ctp
++) {
665 if (type_ok (ct
, 1) && !ct
->c_umask
) {
666 if (stat (ct
->c_file
, &st
) != NOTOK
)
667 ct
->c_umask
= ~(st
.st_mode
& 0777);
669 ct
->c_umask
= ~m_gmprot();
674 * List the message content
677 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
680 * Store the message content
683 store_all_messages (cts
);
686 * Cache the message content
689 cache_all_messages (cts
);
692 * Show the message content
695 show_all_messages (cts
);
697 /* Now free all the structures for the content */
698 for (ctp
= cts
; *ctp
; ctp
++)
704 /* If reading from a folder, do some updating */
706 context_replace (pfolder
, folder
);/* update current folder */
707 seq_setcur (mp
, mp
->hghsel
); /* update current message */
708 seq_save (mp
); /* synchronize sequences */
709 context_save (); /* save the context file */
722 fprintf (stderr
, "\n");
741 return 1; /* dead code to satisfy the compiler */