]>
diplodocus.org Git - nmh/blob - uip/mhn.c
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("realsize", 0, SIZESW) \
33 X("norealsize", 0, NSIZESW) \
34 X("show", 0, SHOWSW) \
35 X("noshow", 0, NSHOWSW) \
36 X("store", 0, STORESW) \
37 X("nostore", 0, NSTORESW) \
38 X("verbose", 0, VERBSW) \
39 X("noverbose", 0, NVERBSW) \
40 X("file file", 0, FILESW) \
41 X("form formfile", 0, FORMSW) \
42 X("part number", 0, PARTSW) \
43 X("type content", 0, TYPESW) \
44 X("rcache policy", 0, RCACHESW) \
45 X("wcache policy", 0, WCACHESW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
51 X("debug", -5, DEBUGSW) \
53 * switches for moreproc/mhlproc \
55 X("moreproc program", -4, PROGSW) \
56 X("nomoreproc", -3, NPROGSW) \
57 X("length lines", -4, LENSW) \
58 X("width columns", -4, WIDTHSW) \
60 * switches for mhbuild \
62 X("build", -5, BUILDSW) \
63 X("nobuild", -7, NBUILDSW) \
64 X("rfc934mode", 0, RFC934SW) \
65 X("norfc934mode", 0, NRFC934SW) \
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(MHN
);
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(MHN
, switches
);
79 extern char *cache_public
;
80 extern char *cache_private
;
85 extern int nomore
; /* flags for moreproc/header display */
91 extern char *parts
[NPARTS
+ 1];
92 extern char *types
[NTYPES
+ 1];
99 * variables for mhbuild (mhn -build)
101 static int buildsw
= 0;
102 static int rfc934sw
= 0;
105 * what action to take?
107 static int cachesw
= 0;
108 static int listsw
= 0;
109 static int showsw
= 0;
110 static int storesw
= 0;
112 #define quitser pipeser
115 CT
parse_mime (char *);
119 int type_ok (CT
, int);
120 void flush_errors (void);
123 void cache_all_messages (CT
*);
127 void freects_done (int) NORETURN
;
132 static void pipeser (int);
136 main (int argc
, char **argv
)
138 int sizesw
= 1, headsw
= 1, autosw
= 0;
139 int msgnum
, *icachesw
;
140 char *cp
, *file
= NULL
, *folder
= NULL
;
141 char *maildir
, buf
[100], **argp
;
144 struct msgs_array msgs
= { 0, 0, NULL
};
145 struct msgs
*mp
= NULL
;
150 if (nmh_init(argv
[0], 1)) { return 1; }
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
160 while ((cp
= *argp
++)) {
162 switch (smatch (++cp
, switches
)) {
164 ambigsw (cp
, switches
);
167 adios (NULL
, "-%s unknown", cp
);
170 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
172 print_help (buf
, switches
, 1);
175 print_version(invo_name
);
193 icachesw
= &rcachesw
;
196 icachesw
= &wcachesw
;
198 if (!(cp
= *argp
++) || *cp
== '-')
199 adios (NULL
, "missing argument to %s", argp
[-2]);
200 switch (*icachesw
= smatch (cp
, caches
)) {
202 ambigsw (cp
, caches
);
205 adios (NULL
, "%s unknown", cp
);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 adios (NULL
, "missing argument to %s", argp
[-2]);
257 adios (NULL
, "too many parts (starting with %s), %d max",
263 if (!(cp
= *argp
++) || *cp
== '-')
264 adios (NULL
, "missing argument to %s", argp
[-2]);
266 adios (NULL
, "too many types (starting with %s), %d max",
272 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
273 adios (NULL
, "missing argument to %s", argp
[-2]);
274 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 adios (NULL
, "missing argument to %s", argp
[-2]);
281 formsw
= getcpy (etcpath (cp
));
285 * Switches for moreproc/mhlproc
288 if (!(progsw
= *argp
++) || *progsw
== '-')
289 adios (NULL
, "missing argument to %s", argp
[-2]);
297 if (!(cp
= *argp
++) || *cp
== '-')
298 adios (NULL
, "missing argument to %s", argp
[-2]);
302 * Switches for mhbuild
328 if (*cp
== '+' || *cp
== '@') {
330 adios (NULL
, "only one folder at a time!");
332 folder
= pluspath (cp
);
334 app_msgarg(&msgs
, cp
);
337 /* null terminate the list of acceptable parts/types */
342 * Check if we've specified an additional profile
344 if ((cp
= getenv ("MHN"))) {
345 if ((fp
= fopen (cp
, "r"))) {
346 readconfig ((struct node
**) 0, fp
, cp
, 0);
349 admonish ("", "unable to read $MHN profile (%s)", cp
);
354 * Read the standard profile setup
356 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
357 readconfig ((struct node
**) 0, fp
, cp
, 0);
361 /* Check for public cache location */
362 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
365 /* Check for private cache location */
366 if (!(cache_private
= context_find (nmhprivcache
)))
367 cache_private
= ".cache";
368 cache_private
= getcpy (m_maildir (cache_private
));
371 * Cache the current directory before we do any chdirs()'s.
373 cwd
= add(pwd(), NULL
);
375 if (!context_find ("path"))
376 free (path ("./", TFOLDER
));
379 * Process a mhn composition file (mhn -build)
385 if (showsw
|| storesw
|| cachesw
)
386 adios (NULL
, "cannot use -build with -show, -store, -cache");
388 adios (NULL
, "need to specify a %s composition file", invo_name
);
390 adios (NULL
, "only one %s composition file at a time", invo_name
);
393 vec
[vecp
++] = "mhbuild";
396 vec
[vecp
++] = "-rfc934mode";
397 else if (rfc934sw
== -1)
398 vec
[vecp
++] = "-norfc934mode";
400 vec
[vecp
++] = msgs
.msgs
[0];
403 execvp ("mhbuild", vec
);
404 fprintf (stderr
, "unable to exec ");
409 * Process a mhn composition file (old MH style)
411 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
412 && !showsw
&& !storesw
&& !ntype
&& !file
413 && (cp
= getenv ("mhdraft"))
414 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
420 vec
[vecp
++] = "mhbuild";
423 vec
[vecp
++] = "-rfc934mode";
424 else if (rfc934sw
== -1)
425 vec
[vecp
++] = "-norfc934mode";
430 execvp ("mhbuild", vec
);
431 fprintf (stderr
, "unable to exec ");
435 if (file
&& msgs
.size
)
436 adios (NULL
, "cannot specify msg and file at same time!");
439 * check if message is coming from file
442 cts
= mh_xcalloc(2, sizeof *cts
);
445 if ((ct
= parse_mime (file
)))
449 * message(s) are coming from a folder
452 app_msgarg(&msgs
, "cur");
454 folder
= getfolder (1);
455 maildir
= m_maildir (folder
);
457 if (chdir (maildir
) == NOTOK
)
458 adios (maildir
, "unable to change directory to");
460 /* read folder and create message structure */
461 if (!(mp
= folder_read (folder
, 1)))
462 adios (NULL
, "unable to read folder %s", folder
);
464 /* check for empty folder */
466 adios (NULL
, "no messages in %s", folder
);
468 /* parse all the message ranges/sequences and set SELECTED */
469 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
470 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
472 seq_setprev (mp
); /* set the previous-sequence */
474 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
477 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
478 if (is_selected(mp
, msgnum
)) {
481 msgnam
= m_name (msgnum
);
482 if ((ct
= parse_mime (msgnam
)))
492 * You can't give more than one of these flags
495 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
496 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
498 /* If no action is specified, assume -show */
499 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
503 SIGNAL (SIGQUIT
, quitser
);
504 SIGNAL (SIGPIPE
, pipeser
);
507 * Get the associated umask for the relevant contents.
509 for (ctp
= cts
; *ctp
; ctp
++) {
513 if (type_ok (ct
, 1) && !ct
->c_umask
) {
514 if (stat (ct
->c_file
, &st
) != NOTOK
)
515 ct
->c_umask
= ~(st
.st_mode
& 0777);
517 ct
->c_umask
= ~m_gmprot();
522 * List the message content
525 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
528 * Store the message content
531 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
532 store_all_messages (info
);
533 mhstoreinfo_free (info
);
536 /* If reading from a folder, do some updating */
538 context_replace (pfolder
, folder
);/* update current folder */
539 seq_setcur (mp
, mp
->hghsel
); /* update current message */
540 seq_save (mp
); /* synchronize sequences */
541 context_save (); /* save the context file */
545 * Cache the message content
548 cache_all_messages (cts
);
551 * Show the message content
554 show_all_messages (cts
, 0, 0, 0);
556 /* Now free all the structures for the content */
557 for (ctp
= cts
; *ctp
; ctp
++)
573 fprintf (stderr
, "\n");