]>
diplodocus.org Git - nmh/blob - uip/mhn.c
3 * mhn.c -- display, list, cache, or store the contents of MIME messages
10 #include <h/signals.h>
14 #include <zotnet/mts/mts.h>
15 #include <zotnet/tws/tws.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
25 * We allocate space for message names (msgs array)
26 * this number of elements at a time.
31 static struct swit switches
[] = {
63 { "noserialonly", 0 },
76 #define FILESW 22 /* interface from show */
79 { "form formfile", 0 },
83 { "type content", 0 },
85 { "rcache policy", 0 },
87 { "wcache policy", 0 },
94 * switches for debugging
100 * switches for moreproc/mhlproc
103 { "moreproc program", -4 },
105 { "nomoreproc", -3 },
107 { "length lines", -4 },
109 { "width columns", -4 },
112 * switches for mhbuild
119 { "ebcdicsafe", -10 },
121 { "noebcdicsafe", -12 },
123 { "rfc934mode", -10 },
125 { "norfc934mode", -12 },
134 extern char *tmp
; /* directory to place temp files */
139 extern char *cache_public
;
140 extern char *cache_private
;
147 extern int nomore
; /* flags for moreproc/header display */
152 extern char *cwd
; /* cache current working directory */
157 extern char *parts
[NPARTS
+ 1];
158 extern char *types
[NTYPES
+ 1];
164 /* The list of top-level contents to display */
168 * variables for mhbuild (mhn -build)
170 static int buildsw
= 0;
171 static int ebcdicsw
= 0;
172 static int rfc934sw
= 0;
175 * what action to take?
177 static int cachesw
= 0;
178 static int listsw
= 0;
179 static int showsw
= 0;
180 static int storesw
= 0;
182 #define quitser pipeser
185 CT
parse_mime (char *);
188 int part_ok (CT
, int);
189 int type_ok (CT
, int);
190 void set_endian (void);
191 void flush_errors (void);
194 void show_all_messages (CT
*);
197 void list_all_messages (CT
*, int, int, int, int);
200 void store_all_messages (CT
*);
203 void cache_all_messages (CT
*);
206 void free_content (CT
);
211 static RETSIGTYPE
pipeser (int);
215 main (int argc
, char **argv
)
217 int sizesw
= 1, headsw
= 1;
218 int nummsgs
, maxmsgs
, msgnum
, *icachesw
;
219 char *cp
, *file
= NULL
, *folder
= NULL
;
220 char *maildir
, buf
[100], **argp
;
221 char **arguments
, **msgs
;
222 struct msgs
*mp
= NULL
;
227 setlocale(LC_ALL
, "");
229 invo_name
= r1bindex (argv
[0], '/');
231 /* read user profile/context */
234 arguments
= getarguments (invo_name
, argc
, argv
, 1);
238 * Allocate the initial space to record message
239 * names, ranges, and sequences.
243 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
244 adios (NULL
, "unable to allocate storage");
249 while ((cp
= *argp
++)) {
251 switch (smatch (++cp
, switches
)) {
253 ambigsw (cp
, switches
);
256 adios (NULL
, "-%s unknown", cp
);
259 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
261 print_help (buf
, switches
, 1);
264 print_version(invo_name
);
282 icachesw
= &rcachesw
;
285 icachesw
= &wcachesw
;
287 if (!(cp
= *argp
++) || *cp
== '-')
288 adios (NULL
, "missing argument to %s", argp
[-2]);
289 switch (*icachesw
= smatch (cp
, caches
)) {
291 ambigsw (cp
, caches
);
294 adios (NULL
, "%s unknown", cp
);
357 if (!(cp
= *argp
++) || *cp
== '-')
358 adios (NULL
, "missing argument to %s", argp
[-2]);
360 adios (NULL
, "too many parts (starting with %s), %d max",
366 if (!(cp
= *argp
++) || *cp
== '-')
367 adios (NULL
, "missing argument to %s", argp
[-2]);
369 adios (NULL
, "too many types (starting with %s), %d max",
375 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
376 adios (NULL
, "missing argument to %s", argp
[-2]);
377 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
381 if (!(cp
= *argp
++) || *cp
== '-')
382 adios (NULL
, "missing argument to %s", argp
[-2]);
385 formsw
= getcpy (etcpath (cp
));
389 * Switches for moreproc/mhlproc
392 if (!(progsw
= *argp
++) || *progsw
== '-')
393 adios (NULL
, "missing argument to %s", argp
[-2]);
401 if (!(cp
= *argp
++) || *cp
== '-')
402 adios (NULL
, "missing argument to %s", argp
[-2]);
406 * Switches for mhbuild
438 if (*cp
== '+' || *cp
== '@') {
440 adios (NULL
, "only one folder at a time!");
442 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
445 * Check if we need to allocate more space
446 * for message names/ranges/sequences.
448 if (nummsgs
>= maxmsgs
) {
450 if (!(msgs
= (char **) realloc (msgs
,
451 (size_t) (maxmsgs
* sizeof(*msgs
)))))
452 adios (NULL
, "unable to reallocate msgs storage");
454 msgs
[nummsgs
++] = cp
;
458 /* null terminate the list of acceptable parts/types */
464 if ((cp
= getenv ("MM_NOASK")) && !strcmp (cp
, "1")) {
471 * Check if we've specified an additional profile
473 if ((cp
= getenv ("MHN"))) {
474 if ((fp
= fopen (cp
, "r"))) {
475 readconfig ((struct node
**) 0, fp
, cp
, 0);
478 admonish ("", "unable to read $MHN profile (%s)", cp
);
483 * Read the standard profile setup
485 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
486 readconfig ((struct node
**) 0, fp
, cp
, 0);
490 /* Check for public cache location */
491 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
494 /* Check for private cache location */
495 if (!(cache_private
= context_find (nmhprivcache
)))
496 cache_private
= ".cache";
497 cache_private
= getcpy (m_maildir (cache_private
));
500 * Cache the current directory before we do any chdirs()'s.
502 cwd
= getcpy (pwd());
505 * Check for storage directory. If specified,
506 * then store temporary files there. Else we
507 * store them in standard nmh directory.
509 if ((cp
= context_find (nmhstorage
)) && *cp
)
510 tmp
= concat (cp
, "/", invo_name
, NULL
);
512 tmp
= add (m_maildir (invo_name
), NULL
);
514 if (!context_find ("path"))
515 free (path ("./", TFOLDER
));
518 * Process a mhn composition file (mhn -build)
524 if (showsw
|| storesw
|| cachesw
)
525 adios (NULL
, "cannot use -build with -show, -store, -cache");
527 adios (NULL
, "need to specify a %s composition file", invo_name
);
529 adios (NULL
, "only one %s composition file at a time", invo_name
);
532 vec
[vecp
++] = "mhbuild";
535 vec
[vecp
++] = "-ebcdicsafe";
536 else if (ebcdicsw
== -1)
537 vec
[vecp
++] = "-noebcdicsafe";
540 vec
[vecp
++] = "-rfc934mode";
541 else if (rfc934sw
== -1)
542 vec
[vecp
++] = "-norfc934mode";
544 vec
[vecp
++] = msgs
[0];
547 execvp ("mhbuild", vec
);
548 fprintf (stderr
, "unable to exec ");
553 * Process a mhn composition file (old MH style)
555 if (nummsgs
== 1 && !folder
&& !npart
&& !cachesw
556 && !showsw
&& !storesw
&& !ntype
&& !file
557 && (cp
= getenv ("mhdraft"))
558 && strcmp (cp
, msgs
[0]) == 0) {
564 vec
[vecp
++] = "mhbuild";
567 vec
[vecp
++] = "-ebcdicsafe";
568 else if (ebcdicsw
== -1)
569 vec
[vecp
++] = "-noebcdicsafe";
572 vec
[vecp
++] = "-rfc934mode";
573 else if (rfc934sw
== -1)
574 vec
[vecp
++] = "-norfc934mode";
579 execvp ("mhbuild", vec
);
580 fprintf (stderr
, "unable to exec ");
585 adios (NULL
, "cannot specify msg and file at same time!");
588 * check if message is coming from file
591 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
592 adios (NULL
, "out of memory");
595 if ((ct
= parse_mime (file
)));
599 * message(s) are coming from a folder
602 msgs
[nummsgs
++] = "cur";
604 folder
= getfolder (1);
605 maildir
= m_maildir (folder
);
607 if (chdir (maildir
) == NOTOK
)
608 adios (maildir
, "unable to change directory to");
610 /* read folder and create message structure */
611 if (!(mp
= folder_read (folder
)))
612 adios (NULL
, "unable to read folder %s", folder
);
614 /* check for empty folder */
616 adios (NULL
, "no messages in %s", folder
);
618 /* parse all the message ranges/sequences and set SELECTED */
619 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
620 if (!m_convert (mp
, msgs
[msgnum
]))
622 seq_setprev (mp
); /* set the previous-sequence */
624 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
625 adios (NULL
, "out of memory");
628 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
629 if (is_selected(mp
, msgnum
)) {
632 msgnam
= m_name (msgnum
);
633 if ((ct
= parse_mime (msgnam
)))
643 * You can't give more than one of these flags
646 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
647 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
649 /* If no action is specified, assume -show */
650 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
654 SIGNAL (SIGQUIT
, quitser
);
655 SIGNAL (SIGPIPE
, pipeser
);
658 * Get the associated umask for the relevant contents.
660 for (ctp
= cts
; *ctp
; ctp
++) {
664 if (type_ok (ct
, 1) && !ct
->c_umask
) {
665 if (stat (ct
->c_file
, &st
) != NOTOK
)
666 ct
->c_umask
= ~(st
.st_mode
& 0777);
668 ct
->c_umask
= ~m_gmprot();
673 * List the message content
676 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
679 * Store the message content
682 store_all_messages (cts
);
685 * Cache the message content
688 cache_all_messages (cts
);
691 * Show the message content
694 show_all_messages (cts
);
696 /* Now free all the structures for the content */
697 for (ctp
= cts
; *ctp
; ctp
++)
703 /* If reading from a folder, do some updating */
705 context_replace (pfolder
, folder
);/* update current folder */
706 seq_setcur (mp
, mp
->hghsel
); /* update current message */
707 seq_save (mp
); /* synchronize sequences */
708 context_save (); /* save the context file */
721 fprintf (stderr
, "\n");
740 return 1; /* dead code to satisfy the compiler */