]>
diplodocus.org Git - nmh/blob - uip/mhn.c
7f85ae55e9627f5c629145cb64fb74d3396112b2
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("show", 0, SHOWSW) \
37 X("noshow", 0, NSHOWSW) \
38 X("store", 0, STORESW) \
39 X("nostore", 0, NSTORESW) \
40 X("verbose", 0, VERBSW) \
41 X("noverbose", 0, NVERBSW) \
42 X("file file", 0, FILESW) \
43 X("form formfile", 0, FORMSW) \
44 X("part number", 0, PARTSW) \
45 X("type content", 0, TYPESW) \
46 X("rcache policy", 0, RCACHESW) \
47 X("wcache policy", 0, WCACHESW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
53 X("debug", -5, DEBUGSW) \
55 * switches for moreproc/mhlproc \
57 X("moreproc program", -4, PROGSW) \
58 X("nomoreproc", -3, NPROGSW) \
59 X("length lines", -4, LENSW) \
60 X("width columns", -4, WIDTHSW) \
62 * switches for mhbuild \
64 X("build", -5, BUILDSW) \
65 X("nobuild", -7, NBUILDSW) \
66 X("rfc934mode", -10, RFC934SW) \
67 X("norfc934mode", -12, NRFC934SW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(MHN
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(MHN
, switches
);
81 extern char *cache_public
;
82 extern char *cache_private
;
88 extern int nomore
; /* flags for moreproc/header display */
94 extern char *parts
[NPARTS
+ 1];
95 extern char *types
[NTYPES
+ 1];
102 * variables for mhbuild (mhn -build)
104 static int buildsw
= 0;
105 static int rfc934sw
= 0;
108 * what action to take?
110 static int cachesw
= 0;
111 static int listsw
= 0;
112 static int showsw
= 0;
113 static int storesw
= 0;
115 #define quitser pipeser
118 CT
parse_mime (char *);
121 int part_ok (CT
, int);
122 int type_ok (CT
, int);
123 void flush_errors (void);
126 void show_all_messages (CT
*);
129 typedef struct mhstoreinfo
*mhstoreinfo_t
;
130 mhstoreinfo_t
mhstoreinfo_create(CT
*, char *, const char *, int, int);
131 void mhstoreinfo_free(mhstoreinfo_t
);
132 void store_all_messages (mhstoreinfo_t
);
135 void cache_all_messages (CT
*);
139 void freects_done (int) NORETURN
;
144 static void pipeser (int);
148 main (int argc
, char **argv
)
150 int sizesw
= 1, headsw
= 1, autosw
= 0;
151 int msgnum
, *icachesw
;
152 char *cp
, *file
= NULL
, *folder
= NULL
;
153 char *maildir
, buf
[100], **argp
;
156 struct msgs_array msgs
= { 0, 0, NULL
};
157 struct msgs
*mp
= NULL
;
162 if (nmh_init(argv
[0], 1)) { return 1; }
166 arguments
= getarguments (invo_name
, argc
, argv
, 1);
172 while ((cp
= *argp
++)) {
174 switch (smatch (++cp
, switches
)) {
176 ambigsw (cp
, switches
);
179 adios (NULL
, "-%s unknown", cp
);
182 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
184 print_help (buf
, switches
, 1);
187 print_version(invo_name
);
205 icachesw
= &rcachesw
;
208 icachesw
= &wcachesw
;
210 if (!(cp
= *argp
++) || *cp
== '-')
211 adios (NULL
, "missing argument to %s", argp
[-2]);
212 switch (*icachesw
= smatch (cp
, caches
)) {
214 ambigsw (cp
, caches
);
217 adios (NULL
, "%s unknown", cp
);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 adios (NULL
, "missing argument to %s", argp
[-2]);
276 adios (NULL
, "too many parts (starting with %s), %d max",
282 if (!(cp
= *argp
++) || *cp
== '-')
283 adios (NULL
, "missing argument to %s", argp
[-2]);
285 adios (NULL
, "too many types (starting with %s), %d max",
291 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
292 adios (NULL
, "missing argument to %s", argp
[-2]);
293 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
297 if (!(cp
= *argp
++) || *cp
== '-')
298 adios (NULL
, "missing argument to %s", argp
[-2]);
301 formsw
= getcpy (etcpath (cp
));
305 * Switches for moreproc/mhlproc
308 if (!(progsw
= *argp
++) || *progsw
== '-')
309 adios (NULL
, "missing argument to %s", argp
[-2]);
317 if (!(cp
= *argp
++) || *cp
== '-')
318 adios (NULL
, "missing argument to %s", argp
[-2]);
322 * Switches for mhbuild
348 if (*cp
== '+' || *cp
== '@') {
350 adios (NULL
, "only one folder at a time!");
352 folder
= pluspath (cp
);
354 app_msgarg(&msgs
, cp
);
357 /* null terminate the list of acceptable parts/types */
362 * Check if we've specified an additional profile
364 if ((cp
= getenv ("MHN"))) {
365 if ((fp
= fopen (cp
, "r"))) {
366 readconfig ((struct node
**) 0, fp
, cp
, 0);
369 admonish ("", "unable to read $MHN profile (%s)", cp
);
374 * Read the standard profile setup
376 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
377 readconfig ((struct node
**) 0, fp
, cp
, 0);
381 /* Check for public cache location */
382 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
385 /* Check for private cache location */
386 if (!(cache_private
= context_find (nmhprivcache
)))
387 cache_private
= ".cache";
388 cache_private
= getcpy (m_maildir (cache_private
));
391 * Cache the current directory before we do any chdirs()'s.
393 cwd
= add(pwd(), NULL
);
395 if (!context_find ("path"))
396 free (path ("./", TFOLDER
));
399 * Process a mhn composition file (mhn -build)
405 if (showsw
|| storesw
|| cachesw
)
406 adios (NULL
, "cannot use -build with -show, -store, -cache");
408 adios (NULL
, "need to specify a %s composition file", invo_name
);
410 adios (NULL
, "only one %s composition file at a time", invo_name
);
413 vec
[vecp
++] = "mhbuild";
416 vec
[vecp
++] = "-rfc934mode";
417 else if (rfc934sw
== -1)
418 vec
[vecp
++] = "-norfc934mode";
420 vec
[vecp
++] = msgs
.msgs
[0];
423 execvp ("mhbuild", vec
);
424 fprintf (stderr
, "unable to exec ");
429 * Process a mhn composition file (old MH style)
431 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
432 && !showsw
&& !storesw
&& !ntype
&& !file
433 && (cp
= getenv ("mhdraft"))
434 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
440 vec
[vecp
++] = "mhbuild";
443 vec
[vecp
++] = "-rfc934mode";
444 else if (rfc934sw
== -1)
445 vec
[vecp
++] = "-norfc934mode";
450 execvp ("mhbuild", vec
);
451 fprintf (stderr
, "unable to exec ");
455 if (file
&& msgs
.size
)
456 adios (NULL
, "cannot specify msg and file at same time!");
459 * check if message is coming from file
462 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
463 adios (NULL
, "out of memory");
466 if ((ct
= parse_mime (file
)))
470 * message(s) are coming from a folder
473 app_msgarg(&msgs
, "cur");
475 folder
= getfolder (1);
476 maildir
= m_maildir (folder
);
478 if (chdir (maildir
) == NOTOK
)
479 adios (maildir
, "unable to change directory to");
481 /* read folder and create message structure */
482 if (!(mp
= folder_read (folder
, 1)))
483 adios (NULL
, "unable to read folder %s", folder
);
485 /* check for empty folder */
487 adios (NULL
, "no messages in %s", folder
);
489 /* parse all the message ranges/sequences and set SELECTED */
490 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
491 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
493 seq_setprev (mp
); /* set the previous-sequence */
495 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
496 adios (NULL
, "out of memory");
499 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
500 if (is_selected(mp
, msgnum
)) {
503 msgnam
= m_name (msgnum
);
504 if ((ct
= parse_mime (msgnam
)))
514 * You can't give more than one of these flags
517 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
518 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
520 /* If no action is specified, assume -show */
521 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
525 SIGNAL (SIGQUIT
, quitser
);
526 SIGNAL (SIGPIPE
, pipeser
);
529 * Get the associated umask for the relevant contents.
531 for (ctp
= cts
; *ctp
; ctp
++) {
535 if (type_ok (ct
, 1) && !ct
->c_umask
) {
536 if (stat (ct
->c_file
, &st
) != NOTOK
)
537 ct
->c_umask
= ~(st
.st_mode
& 0777);
539 ct
->c_umask
= ~m_gmprot();
544 * List the message content
547 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
550 * Store the message content
553 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
554 store_all_messages (info
);
555 mhstoreinfo_free (info
);
559 * Cache the message content
562 cache_all_messages (cts
);
565 * Show the message content
568 show_all_messages (cts
);
570 /* Now free all the structures for the content */
571 for (ctp
= cts
; *ctp
; ctp
++)
577 /* If reading from a folder, do some updating */
579 context_replace (pfolder
, folder
);/* update current folder */
580 seq_setcur (mp
, mp
->hghsel
); /* update current message */
581 seq_save (mp
); /* synchronize sequences */
582 context_save (); /* save the context file */
595 fprintf (stderr
, "\n");