2 ** mhshow.c -- display the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
38 { "noserialonly", 0 },
43 #define FILESW 8 /* interface from show */
46 { "form formfile", 0 },
50 { "type content", 0 },
52 { "rcache policy", 0 },
54 { "wcache policy", 0 },
61 ** switches for moreproc/mhlproc
64 { "moreproc program", -4 },
68 { "length lines", -4 },
70 { "width columns", -4 },
73 ** switches for debugging
82 extern char *tmp; /* directory to place temp files */
87 extern char *cache_public;
88 extern char *cache_private;
95 extern int nomore; /* flags for moreproc/header display */
101 extern char *parts[NPARTS + 1];
102 extern char *types[NTYPES + 1];
108 #define quitser pipeser
111 CT parse_mime(char *);
114 int part_ok(CT, int);
115 int type_ok(CT, int);
116 void set_endian(void);
117 void flush_errors(void);
120 void show_all_messages(CT *);
123 void free_content(CT);
125 void freects_done(int) NORETURN;
130 static RETSIGTYPE pipeser(int);
134 main(int argc, char **argv)
136 int msgnum, *icachesw;
137 char *cp, *file = NULL, *folder = NULL;
138 char *maildir, buf[100], **argp;
140 struct msgs_array msgs = { 0, 0, NULL };
141 struct msgs *mp = NULL;
148 setlocale(LC_ALL, "");
150 invo_name = mhbasename(argv[0]);
152 /* read user profile/context */
155 arguments = getarguments(invo_name, argc, argv, 1);
161 while ((cp = *argp++)) {
163 switch (smatch(++cp, switches)) {
165 ambigsw(cp, switches);
168 adios(NULL, "-%s unknown", cp);
171 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
172 print_help(buf, switches, 1);
175 print_version(invo_name);
179 icachesw = &rcachesw;
182 icachesw = &wcachesw;
184 if (!(cp = *argp++) || *cp == '-')
185 adios(NULL, "missing argument to %s",
187 switch (*icachesw = smatch(cp, caches)) {
192 adios(NULL, "%s unknown", cp);
220 if (!(cp = *argp++) || *cp == '-')
221 adios(NULL, "missing argument to %s",
224 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
229 if (!(cp = *argp++) || *cp == '-')
230 adios(NULL, "missing argument to %s",
233 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
238 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
239 adios(NULL, "missing argument to %s",
241 file = *cp == '-' ? cp : getcpy(expanddir(cp));
245 if (!(cp = *argp++) || *cp == '-')
246 adios(NULL, "missing argument to %s",
250 formsw = getcpy(etcpath(cp));
254 ** Switches for moreproc/mhlproc
257 if (!(progsw = *argp++) || *progsw == '-')
258 adios(NULL, "missing argument to %s",
267 if (!(cp = *argp++) || *cp == '-')
268 adios(NULL, "missing argument to %s",
283 if (*cp == '+' || *cp == '@') {
285 adios(NULL, "only one folder at a time!");
287 folder = getcpy(expandfol(cp));
289 app_msgarg(&msgs, cp);
292 /* null terminate the list of acceptable parts/types */
298 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
304 ** Check if we've specified an additional profile
306 if ((cp = getenv("MHSHOW"))) {
307 if ((fp = fopen(cp, "r"))) {
308 readconfig((struct node **) 0, fp, cp, 0);
311 admonish("", "unable to read $MHSHOW profile (%s)",
317 ** Read the standard profile setup
319 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
320 readconfig((struct node **) 0, fp, cp, 0);
324 /* Check for public cache location */
325 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
328 /* Check for private cache location */
329 if (!(cache_private = context_find(nmhprivcache)))
330 cache_private = ".cache";
331 cache_private = getcpy(toabsdir(cache_private));
334 ** Check for storage directory. If specified,
335 ** then store temporary files there. Else we
336 ** store them in standard nmh directory.
338 if ((cp = context_find(nmhstorage)) && *cp)
339 tmp = concat(cp, "/", invo_name, NULL);
341 tmp = getcpy(toabsdir(invo_name));
343 if (file && msgs.size)
344 adios(NULL, "cannot specify msg and file at same time!");
347 ** check if message is coming from file
350 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
351 adios(NULL, "out of memory");
354 if ((ct = parse_mime(file)));
358 ** message(s) are coming from a folder
361 app_msgarg(&msgs, seq_cur);
363 folder = getcurfol();
364 maildir = toabsdir(folder);
366 if (chdir(maildir) == NOTOK)
367 adios(maildir, "unable to change directory to");
369 /* read folder and create message structure */
370 if (!(mp = folder_read(folder)))
371 adios(NULL, "unable to read folder %s", folder);
373 /* check for empty folder */
375 adios(NULL, "no messages in %s", folder);
377 /* parse all the message ranges/sequences and set SELECTED */
378 for (msgnum = 0; msgnum < msgs.size; msgnum++)
379 if (!m_convert(mp, msgs.msgs[msgnum]))
383 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
384 ** since we will use that as a tag to know which messages
385 ** to remove from the "unseen" sequence.
387 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
388 if (is_selected(mp, msgnum))
389 set_unseen(mp, msgnum);
391 seq_setprev(mp); /* set the Previous-Sequence */
392 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
394 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
396 adios(NULL, "out of memory");
400 ** Parse all the SELECTED messages.
402 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
403 if (is_selected(mp, msgnum)) {
406 msgnam = m_name(msgnum);
407 if ((ct = parse_mime(msgnam)))
417 SIGNAL(SIGQUIT, quitser);
418 SIGNAL(SIGPIPE, pipeser);
421 ** Get the associated umask for the relevant contents.
423 for (ctp = cts; *ctp; ctp++) {
427 if (type_ok(ct, 1) && !ct->c_umask) {
428 if (stat(ct->c_file, &st) != NOTOK)
429 ct->c_umask = ~(st.st_mode & 0777);
431 ct->c_umask = ~m_gmprot();
436 ** Show the message content
438 show_all_messages(cts);
440 /* Now free all the structures for the content */
441 for (ctp = cts; *ctp; ctp++)
447 /* If reading from a folder, do some updating */
449 context_replace(curfolder, folder); /* update current folder */
450 seq_setcur(mp, mp->hghsel); /* update current message */
451 seq_save(mp); /* synchronize sequences */
452 context_save(); /* save the context file */
466 fprintf(stderr, "\n");