]>
diplodocus.org Git - nmh/blob - uip/mhn.c
c0b7e7816c7a4e2eb58d569bed82c7cba0b63169
3 * mhn.c -- display, list, cache, or store the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHN_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
24 X("cache", 0, CACHESW) \
25 X("nocache", 0, NCACHESW) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("headers", 0, HEADSW) \
29 X("noheaders", 0, NHEADSW) \
30 X("list", 0, LISTSW) \
31 X("nolist", 0, NLISTSW) \
32 X("pause", 0, PAUSESW) \
33 X("nopause", 0, NPAUSESW) \
34 X("realsize", 0, SIZESW) \
35 X("norealsize", 0, NSIZESW) \
36 X("serialonly", 0, SERIALSW) \
37 X("noserialonly", 0, NSERIALSW) \
38 X("show", 0, SHOWSW) \
39 X("noshow", 0, NSHOWSW) \
40 X("store", 0, STORESW) \
41 X("nostore", 0, NSTORESW) \
42 X("verbose", 0, VERBSW) \
43 X("noverbose", 0, NVERBSW) \
44 X("file file", 0, FILESW) \
45 X("form formfile", 0, FORMSW) \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
55 X("debug", -5, DEBUGSW) \
57 * switches for moreproc/mhlproc \
59 X("moreproc program", -4, PROGSW) \
60 X("nomoreproc", -3, NPROGSW) \
61 X("length lines", -4, LENSW) \
62 X("width columns", -4, WIDTHSW) \
64 * switches for mhbuild \
66 X("build", -5, BUILDSW) \
67 X("nobuild", -7, NBUILDSW) \
68 X("rfc934mode", -10, RFC934SW) \
69 X("norfc934mode", -12, NRFC934SW) \
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(MHN
);
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(MHN
, switches
);
83 extern char *cache_public
;
84 extern char *cache_private
;
91 extern int nomore
; /* flags for moreproc/header display */
97 extern char *parts
[NPARTS
+ 1];
98 extern char *types
[NTYPES
+ 1];
105 * variables for mhbuild (mhn -build)
107 static int buildsw
= 0;
108 static int rfc934sw
= 0;
111 * what action to take?
113 static int cachesw
= 0;
114 static int listsw
= 0;
115 static int showsw
= 0;
116 static int storesw
= 0;
118 #define quitser pipeser
121 CT
parse_mime (char *);
124 int part_ok (CT
, int);
125 int type_ok (CT
, int);
126 void flush_errors (void);
129 void show_all_messages (CT
*);
132 typedef struct mhstoreinfo
*mhstoreinfo_t
;
133 mhstoreinfo_t
mhstoreinfo_create(CT
*, char *, const char *, int, int);
134 void mhstoreinfo_free(mhstoreinfo_t
);
135 void store_all_messages (mhstoreinfo_t
);
138 void cache_all_messages (CT
*);
142 void freects_done (int) NORETURN
;
147 static void pipeser (int);
151 main (int argc
, char **argv
)
153 int sizesw
= 1, headsw
= 1, autosw
= 0;
154 int msgnum
, *icachesw
;
155 char *cp
, *file
= NULL
, *folder
= NULL
;
156 char *maildir
, buf
[100], **argp
;
159 struct msgs_array msgs
= { 0, 0, NULL
};
160 struct msgs
*mp
= NULL
;
165 if (nmh_init(argv
[0], 1)) { return 1; }
169 arguments
= getarguments (invo_name
, argc
, argv
, 1);
175 while ((cp
= *argp
++)) {
177 switch (smatch (++cp
, switches
)) {
179 ambigsw (cp
, switches
);
182 adios (NULL
, "-%s unknown", cp
);
185 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
187 print_help (buf
, switches
, 1);
190 print_version(invo_name
);
208 icachesw
= &rcachesw
;
211 icachesw
= &wcachesw
;
213 if (!(cp
= *argp
++) || *cp
== '-')
214 adios (NULL
, "missing argument to %s", argp
[-2]);
215 switch (*icachesw
= smatch (cp
, caches
)) {
217 ambigsw (cp
, caches
);
220 adios (NULL
, "%s unknown", cp
);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 adios (NULL
, "missing argument to %s", argp
[-2]);
286 adios (NULL
, "too many parts (starting with %s), %d max",
292 if (!(cp
= *argp
++) || *cp
== '-')
293 adios (NULL
, "missing argument to %s", argp
[-2]);
295 adios (NULL
, "too many types (starting with %s), %d max",
301 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
302 adios (NULL
, "missing argument to %s", argp
[-2]);
303 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
307 if (!(cp
= *argp
++) || *cp
== '-')
308 adios (NULL
, "missing argument to %s", argp
[-2]);
311 formsw
= getcpy (etcpath (cp
));
315 * Switches for moreproc/mhlproc
318 if (!(progsw
= *argp
++) || *progsw
== '-')
319 adios (NULL
, "missing argument to %s", argp
[-2]);
327 if (!(cp
= *argp
++) || *cp
== '-')
328 adios (NULL
, "missing argument to %s", argp
[-2]);
332 * Switches for mhbuild
358 if (*cp
== '+' || *cp
== '@') {
360 adios (NULL
, "only one folder at a time!");
362 folder
= pluspath (cp
);
364 app_msgarg(&msgs
, cp
);
367 /* null terminate the list of acceptable parts/types */
372 * Check if we've specified an additional profile
374 if ((cp
= getenv ("MHN"))) {
375 if ((fp
= fopen (cp
, "r"))) {
376 readconfig ((struct node
**) 0, fp
, cp
, 0);
379 admonish ("", "unable to read $MHN profile (%s)", cp
);
384 * Read the standard profile setup
386 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
387 readconfig ((struct node
**) 0, fp
, cp
, 0);
391 /* Check for public cache location */
392 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
395 /* Check for private cache location */
396 if (!(cache_private
= context_find (nmhprivcache
)))
397 cache_private
= ".cache";
398 cache_private
= getcpy (m_maildir (cache_private
));
401 * Cache the current directory before we do any chdirs()'s.
403 cwd
= add(pwd(), NULL
);
405 if (!context_find ("path"))
406 free (path ("./", TFOLDER
));
409 * Process a mhn composition file (mhn -build)
415 if (showsw
|| storesw
|| cachesw
)
416 adios (NULL
, "cannot use -build with -show, -store, -cache");
418 adios (NULL
, "need to specify a %s composition file", invo_name
);
420 adios (NULL
, "only one %s composition file at a time", invo_name
);
423 vec
[vecp
++] = "mhbuild";
426 vec
[vecp
++] = "-rfc934mode";
427 else if (rfc934sw
== -1)
428 vec
[vecp
++] = "-norfc934mode";
430 vec
[vecp
++] = msgs
.msgs
[0];
433 execvp ("mhbuild", vec
);
434 fprintf (stderr
, "unable to exec ");
439 * Process a mhn composition file (old MH style)
441 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
442 && !showsw
&& !storesw
&& !ntype
&& !file
443 && (cp
= getenv ("mhdraft"))
444 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
450 vec
[vecp
++] = "mhbuild";
453 vec
[vecp
++] = "-rfc934mode";
454 else if (rfc934sw
== -1)
455 vec
[vecp
++] = "-norfc934mode";
460 execvp ("mhbuild", vec
);
461 fprintf (stderr
, "unable to exec ");
465 if (file
&& msgs
.size
)
466 adios (NULL
, "cannot specify msg and file at same time!");
469 * check if message is coming from file
472 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
473 adios (NULL
, "out of memory");
476 if ((ct
= parse_mime (file
)))
480 * message(s) are coming from a folder
483 app_msgarg(&msgs
, "cur");
485 folder
= getfolder (1);
486 maildir
= m_maildir (folder
);
488 if (chdir (maildir
) == NOTOK
)
489 adios (maildir
, "unable to change directory to");
491 /* read folder and create message structure */
492 if (!(mp
= folder_read (folder
, 1)))
493 adios (NULL
, "unable to read folder %s", folder
);
495 /* check for empty folder */
497 adios (NULL
, "no messages in %s", folder
);
499 /* parse all the message ranges/sequences and set SELECTED */
500 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
501 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
503 seq_setprev (mp
); /* set the previous-sequence */
505 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
506 adios (NULL
, "out of memory");
509 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
510 if (is_selected(mp
, msgnum
)) {
513 msgnam
= m_name (msgnum
);
514 if ((ct
= parse_mime (msgnam
)))
524 * You can't give more than one of these flags
527 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
528 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
530 /* If no action is specified, assume -show */
531 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
535 SIGNAL (SIGQUIT
, quitser
);
536 SIGNAL (SIGPIPE
, pipeser
);
539 * Get the associated umask for the relevant contents.
541 for (ctp
= cts
; *ctp
; ctp
++) {
545 if (type_ok (ct
, 1) && !ct
->c_umask
) {
546 if (stat (ct
->c_file
, &st
) != NOTOK
)
547 ct
->c_umask
= ~(st
.st_mode
& 0777);
549 ct
->c_umask
= ~m_gmprot();
554 * List the message content
557 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
560 * Store the message content
563 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
564 store_all_messages (info
);
565 mhstoreinfo_free (info
);
569 * Cache the message content
572 cache_all_messages (cts
);
575 * Show the message content
578 show_all_messages (cts
);
580 /* Now free all the structures for the content */
581 for (ctp
= cts
; *ctp
; ctp
++)
587 /* If reading from a folder, do some updating */
589 context_replace (pfolder
, folder
);/* update current folder */
590 seq_setcur (mp
, mp
->hghsel
); /* update current message */
591 seq_save (mp
); /* synchronize sequences */
592 context_save (); /* save the context file */
605 fprintf (stderr
, "\n");