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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
25 static struct swit switches[] = {
33 { "noserialonly", 0 },
38 #define FILESW 6 /* interface from show */
41 { "form formfile", 0 },
45 { "type content", 0 },
47 { "rcache policy", 0 },
49 { "wcache policy", 0 },
56 ** switches for moreproc/mhlproc
59 { "moreproc program", -4 },
63 { "length lines", -4 },
65 { "width columns", -4 },
68 ** switches for debugging
77 extern char *tmp; /* directory to place temp files */
82 extern char *cache_public;
83 extern char *cache_private;
89 extern int nomore; /* flags for moreproc/header display */
95 extern char *parts[NPARTS + 1];
96 extern char *types[NTYPES + 1];
102 #define quitser pipeser
105 CT parse_mime(char *);
108 int part_ok(CT, int);
109 int type_ok(CT, int);
110 void set_endian(void);
111 void flush_errors(void);
114 void show_all_messages(CT *);
117 void free_content(CT);
119 void freects_done(int) NORETURN;
124 static RETSIGTYPE pipeser(int);
128 main(int argc, char **argv)
130 int msgnum, *icachesw;
131 char *cp, *file = NULL, *folder = NULL;
132 char *maildir, buf[100], **argp;
134 struct msgs_array msgs = { 0, 0, NULL };
135 struct msgs *mp = NULL;
142 setlocale(LC_ALL, "");
144 invo_name = mhbasename(argv[0]);
146 /* read user profile/context */
149 arguments = getarguments(invo_name, argc, argv, 1);
155 while ((cp = *argp++)) {
157 switch (smatch(++cp, switches)) {
159 ambigsw(cp, switches);
162 adios(NULL, "-%s unknown", cp);
165 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
166 print_help(buf, switches, 1);
169 print_version(invo_name);
173 icachesw = &rcachesw;
176 icachesw = &wcachesw;
178 if (!(cp = *argp++) || *cp == '-')
179 adios(NULL, "missing argument to %s",
181 switch (*icachesw = smatch(cp, caches)) {
186 adios(NULL, "%s unknown", cp);
207 if (!(cp = *argp++) || *cp == '-')
208 adios(NULL, "missing argument to %s",
211 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
216 if (!(cp = *argp++) || *cp == '-')
217 adios(NULL, "missing argument to %s",
220 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
225 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
226 adios(NULL, "missing argument to %s",
228 file = *cp == '-' ? cp : getcpy(expanddir(cp));
232 if (!(cp = *argp++) || *cp == '-')
233 adios(NULL, "missing argument to %s",
237 formsw = getcpy(etcpath(cp));
241 ** Switches for moreproc/mhlproc
244 if (!(progsw = *argp++) || *progsw == '-')
245 adios(NULL, "missing argument to %s",
254 if (!(cp = *argp++) || *cp == '-')
255 adios(NULL, "missing argument to %s",
270 if (*cp == '+' || *cp == '@') {
272 adios(NULL, "only one folder at a time!");
274 folder = getcpy(expandfol(cp));
276 app_msgarg(&msgs, cp);
279 /* null terminate the list of acceptable parts/types */
285 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
290 ** Check if we've specified an additional profile
292 if ((cp = getenv("MHSHOW"))) {
293 if ((fp = fopen(cp, "r"))) {
294 readconfig((struct node **) 0, fp, cp, 0);
297 admonish("", "unable to read $MHSHOW profile (%s)",
303 ** Read the standard profile setup
305 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
306 readconfig((struct node **) 0, fp, cp, 0);
310 /* Check for public cache location */
311 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
314 /* Check for private cache location */
315 if (!(cache_private = context_find(nmhprivcache)))
316 cache_private = ".cache";
317 cache_private = getcpy(toabsdir(cache_private));
320 ** Check for storage directory. If specified,
321 ** then store temporary files there. Else we
322 ** store them in standard nmh directory.
324 if ((cp = context_find(nmhstorage)) && *cp)
325 tmp = concat(cp, "/", invo_name, NULL);
327 tmp = getcpy(toabsdir(invo_name));
329 if (file && msgs.size)
330 adios(NULL, "cannot specify msg and file at same time!");
333 ** check if message is coming from file
336 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
337 adios(NULL, "out of memory");
340 if ((ct = parse_mime(file)));
344 ** message(s) are coming from a folder
347 app_msgarg(&msgs, seq_cur);
349 folder = getcurfol();
350 maildir = toabsdir(folder);
352 if (chdir(maildir) == NOTOK)
353 adios(maildir, "unable to change directory to");
355 /* read folder and create message structure */
356 if (!(mp = folder_read(folder)))
357 adios(NULL, "unable to read folder %s", folder);
359 /* check for empty folder */
361 adios(NULL, "no messages in %s", folder);
363 /* parse all the message ranges/sequences and set SELECTED */
364 for (msgnum = 0; msgnum < msgs.size; msgnum++)
365 if (!m_convert(mp, msgs.msgs[msgnum]))
369 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
370 ** since we will use that as a tag to know which messages
371 ** to remove from the "unseen" sequence.
373 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
374 if (is_selected(mp, msgnum))
375 set_unseen(mp, msgnum);
377 seq_setprev(mp); /* set the Previous-Sequence */
378 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
380 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
382 adios(NULL, "out of memory");
386 ** Parse all the SELECTED messages.
388 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
389 if (is_selected(mp, msgnum)) {
392 msgnam = m_name(msgnum);
393 if ((ct = parse_mime(msgnam)))
403 SIGNAL(SIGQUIT, quitser);
404 SIGNAL(SIGPIPE, pipeser);
407 ** Get the associated umask for the relevant contents.
409 for (ctp = cts; *ctp; ctp++) {
413 if (type_ok(ct, 1) && !ct->c_umask) {
414 if (stat(ct->c_file, &st) != NOTOK)
415 ct->c_umask = ~(st.st_mode & 0777);
417 ct->c_umask = ~m_gmprot();
422 ** Show the message content
424 show_all_messages(cts);
426 /* Now free all the structures for the content */
427 for (ctp = cts; *ctp; ctp++)
433 /* If reading from a folder, do some updating */
435 context_replace(curfolder, folder); /* update current folder */
436 seq_setcur(mp, mp->hghsel); /* update current message */
437 seq_save(mp); /* synchronize sequences */
438 context_save(); /* save the context file */
452 fprintf(stderr, "\n");