]>
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("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 */
96 extern char *cwd
; /* cache current working directory */
101 extern char *parts
[NPARTS
+ 1];
102 extern char *types
[NTYPES
+ 1];
109 * variables for mhbuild (mhn -build)
111 static int buildsw
= 0;
112 static int rfc934sw
= 0;
115 * what action to take?
117 static int cachesw
= 0;
118 static int listsw
= 0;
119 static int showsw
= 0;
120 static int storesw
= 0;
122 #define quitser pipeser
125 CT
parse_mime (char *);
128 int part_ok (CT
, int);
129 int type_ok (CT
, int);
130 void flush_errors (void);
133 void show_all_messages (CT
*);
136 void list_all_messages (CT
*, int, int, int, int);
139 void store_all_messages (CT
*);
142 void cache_all_messages (CT
*);
146 void freects_done (int) NORETURN
;
151 static void pipeser (int);
155 main (int argc
, char **argv
)
157 int sizesw
= 1, headsw
= 1;
158 int msgnum
, *icachesw
;
159 char *cp
, *file
= NULL
, *folder
= NULL
;
160 char *maildir
, buf
[100], **argp
;
162 struct msgs_array msgs
= { 0, 0, NULL
};
163 struct msgs
*mp
= NULL
;
167 if (nmh_init(argv
[0], 1)) { return 1; }
171 arguments
= getarguments (invo_name
, argc
, argv
, 1);
177 while ((cp
= *argp
++)) {
179 switch (smatch (++cp
, switches
)) {
181 ambigsw (cp
, switches
);
184 adios (NULL
, "-%s unknown", cp
);
187 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
189 print_help (buf
, switches
, 1);
192 print_version(invo_name
);
210 icachesw
= &rcachesw
;
213 icachesw
= &wcachesw
;
215 if (!(cp
= *argp
++) || *cp
== '-')
216 adios (NULL
, "missing argument to %s", argp
[-2]);
217 switch (*icachesw
= smatch (cp
, caches
)) {
219 ambigsw (cp
, caches
);
222 adios (NULL
, "%s unknown", cp
);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 adios (NULL
, "missing argument to %s", argp
[-2]);
288 adios (NULL
, "too many parts (starting with %s), %d max",
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
297 adios (NULL
, "too many types (starting with %s), %d max",
303 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
304 adios (NULL
, "missing argument to %s", argp
[-2]);
305 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
309 if (!(cp
= *argp
++) || *cp
== '-')
310 adios (NULL
, "missing argument to %s", argp
[-2]);
313 formsw
= getcpy (etcpath (cp
));
317 * Switches for moreproc/mhlproc
320 if (!(progsw
= *argp
++) || *progsw
== '-')
321 adios (NULL
, "missing argument to %s", argp
[-2]);
329 if (!(cp
= *argp
++) || *cp
== '-')
330 adios (NULL
, "missing argument to %s", argp
[-2]);
334 * Switches for mhbuild
360 if (*cp
== '+' || *cp
== '@') {
362 adios (NULL
, "only one folder at a time!");
364 folder
= pluspath (cp
);
366 app_msgarg(&msgs
, cp
);
369 /* null terminate the list of acceptable parts/types */
374 * Check if we've specified an additional profile
376 if ((cp
= getenv ("MHN"))) {
377 if ((fp
= fopen (cp
, "r"))) {
378 readconfig ((struct node
**) 0, fp
, cp
, 0);
381 admonish ("", "unable to read $MHN profile (%s)", cp
);
386 * Read the standard profile setup
388 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
389 readconfig ((struct node
**) 0, fp
, cp
, 0);
393 /* Check for public cache location */
394 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
397 /* Check for private cache location */
398 if (!(cache_private
= context_find (nmhprivcache
)))
399 cache_private
= ".cache";
400 cache_private
= getcpy (m_maildir (cache_private
));
403 * Cache the current directory before we do any chdirs()'s.
405 cwd
= getcpy (pwd());
407 if (!context_find ("path"))
408 free (path ("./", TFOLDER
));
411 * Process a mhn composition file (mhn -build)
417 if (showsw
|| storesw
|| cachesw
)
418 adios (NULL
, "cannot use -build with -show, -store, -cache");
420 adios (NULL
, "need to specify a %s composition file", invo_name
);
422 adios (NULL
, "only one %s composition file at a time", invo_name
);
425 vec
[vecp
++] = "mhbuild";
428 vec
[vecp
++] = "-rfc934mode";
429 else if (rfc934sw
== -1)
430 vec
[vecp
++] = "-norfc934mode";
432 vec
[vecp
++] = msgs
.msgs
[0];
435 execvp ("mhbuild", vec
);
436 fprintf (stderr
, "unable to exec ");
441 * Process a mhn composition file (old MH style)
443 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
444 && !showsw
&& !storesw
&& !ntype
&& !file
445 && (cp
= getenv ("mhdraft"))
446 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
452 vec
[vecp
++] = "mhbuild";
455 vec
[vecp
++] = "-rfc934mode";
456 else if (rfc934sw
== -1)
457 vec
[vecp
++] = "-norfc934mode";
462 execvp ("mhbuild", vec
);
463 fprintf (stderr
, "unable to exec ");
467 if (file
&& msgs
.size
)
468 adios (NULL
, "cannot specify msg and file at same time!");
471 * check if message is coming from file
474 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
475 adios (NULL
, "out of memory");
478 if ((ct
= parse_mime (file
)))
482 * message(s) are coming from a folder
485 app_msgarg(&msgs
, "cur");
487 folder
= getfolder (1);
488 maildir
= m_maildir (folder
);
490 if (chdir (maildir
) == NOTOK
)
491 adios (maildir
, "unable to change directory to");
493 /* read folder and create message structure */
494 if (!(mp
= folder_read (folder
, 1)))
495 adios (NULL
, "unable to read folder %s", folder
);
497 /* check for empty folder */
499 adios (NULL
, "no messages in %s", folder
);
501 /* parse all the message ranges/sequences and set SELECTED */
502 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
503 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
505 seq_setprev (mp
); /* set the previous-sequence */
507 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
508 adios (NULL
, "out of memory");
511 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
512 if (is_selected(mp
, msgnum
)) {
515 msgnam
= m_name (msgnum
);
516 if ((ct
= parse_mime (msgnam
)))
526 * You can't give more than one of these flags
529 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
530 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
532 /* If no action is specified, assume -show */
533 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
537 SIGNAL (SIGQUIT
, quitser
);
538 SIGNAL (SIGPIPE
, pipeser
);
541 * Get the associated umask for the relevant contents.
543 for (ctp
= cts
; *ctp
; ctp
++) {
547 if (type_ok (ct
, 1) && !ct
->c_umask
) {
548 if (stat (ct
->c_file
, &st
) != NOTOK
)
549 ct
->c_umask
= ~(st
.st_mode
& 0777);
551 ct
->c_umask
= ~m_gmprot();
556 * List the message content
559 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
562 * Store the message content
565 store_all_messages (cts
);
568 * Cache the message content
571 cache_all_messages (cts
);
574 * Show the message content
577 show_all_messages (cts
);
579 /* Now free all the structures for the content */
580 for (ctp
= cts
; *ctp
; ctp
++)
586 /* If reading from a folder, do some updating */
588 context_replace (pfolder
, folder
);/* update current folder */
589 seq_setcur (mp
, mp
->hghsel
); /* update current message */
590 seq_save (mp
); /* synchronize sequences */
591 context_save (); /* save the context file */
604 fprintf (stderr
, "\n");