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 },
55 ** switches for moreproc/mhlproc
58 { "moreproc program", -4 },
60 { "width columns", -4 },
62 ** switches for debugging
71 extern char *tmp; /* directory to place temp files */
76 extern char *cache_public;
77 extern char *cache_private;
88 extern char *parts[NPARTS + 1];
89 extern char *types[NTYPES + 1];
95 #define quitser pipeser
98 CT parse_mime(char *);
101 int part_ok(CT, int);
102 int type_ok(CT, int);
103 void set_endian(void);
104 void flush_errors(void);
107 void show_all_messages(CT *);
110 void free_content(CT);
112 void freects_done(int) NORETURN;
117 static RETSIGTYPE pipeser(int);
121 main(int argc, char **argv)
123 int msgnum, *icachesw;
124 char *cp, *file = NULL, *folder = NULL;
125 char *maildir, buf[100], **argp;
127 struct msgs_array msgs = { 0, 0, NULL };
128 struct msgs *mp = NULL;
135 setlocale(LC_ALL, "");
137 invo_name = mhbasename(argv[0]);
139 /* read user profile/context */
142 arguments = getarguments(invo_name, argc, argv, 1);
148 while ((cp = *argp++)) {
150 switch (smatch(++cp, switches)) {
152 ambigsw(cp, switches);
155 adios(NULL, "-%s unknown", cp);
158 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
159 print_help(buf, switches, 1);
162 print_version(invo_name);
166 icachesw = &rcachesw;
169 icachesw = &wcachesw;
171 if (!(cp = *argp++) || *cp == '-')
172 adios(NULL, "missing argument to %s",
174 switch (*icachesw = smatch(cp, caches)) {
179 adios(NULL, "%s unknown", cp);
200 if (!(cp = *argp++) || *cp == '-')
201 adios(NULL, "missing argument to %s",
204 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
209 if (!(cp = *argp++) || *cp == '-')
210 adios(NULL, "missing argument to %s",
213 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
218 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
219 adios(NULL, "missing argument to %s",
221 file = *cp == '-' ? cp : getcpy(expanddir(cp));
225 if (!(cp = *argp++) || *cp == '-')
226 adios(NULL, "missing argument to %s",
230 formsw = getcpy(etcpath(cp));
234 ** Switches for moreproc/mhlproc
237 if (!(progsw = *argp++) || *progsw == '-')
238 adios(NULL, "missing argument to %s",
243 if (!(cp = *argp++) || *cp == '-')
244 adios(NULL, "missing argument to %s",
259 if (*cp == '+' || *cp == '@') {
261 adios(NULL, "only one folder at a time!");
263 folder = getcpy(expandfol(cp));
265 app_msgarg(&msgs, cp);
268 /* null terminate the list of acceptable parts/types */
274 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
279 ** Check if we've specified an additional profile
281 if ((cp = getenv("MHSHOW"))) {
282 if ((fp = fopen(cp, "r"))) {
283 readconfig((struct node **) 0, fp, cp, 0);
286 admonish("", "unable to read $MHSHOW profile (%s)",
292 ** Read the standard profile setup
294 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
295 readconfig((struct node **) 0, fp, cp, 0);
299 /* Check for public cache location */
300 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
303 /* Check for private cache location */
304 if (!(cache_private = context_find(nmhprivcache)))
305 cache_private = ".cache";
306 cache_private = getcpy(toabsdir(cache_private));
309 ** Check for storage directory. If specified,
310 ** then store temporary files there. Else we
311 ** store them in standard nmh directory.
313 if ((cp = context_find(nmhstorage)) && *cp)
314 tmp = concat(cp, "/", invo_name, NULL);
316 tmp = getcpy(toabsdir(invo_name));
318 if (file && msgs.size)
319 adios(NULL, "cannot specify msg and file at same time!");
322 ** check if message is coming from file
325 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
326 adios(NULL, "out of memory");
329 if ((ct = parse_mime(file)));
333 ** message(s) are coming from a folder
336 app_msgarg(&msgs, seq_cur);
338 folder = getcurfol();
339 maildir = toabsdir(folder);
341 if (chdir(maildir) == NOTOK)
342 adios(maildir, "unable to change directory to");
344 /* read folder and create message structure */
345 if (!(mp = folder_read(folder)))
346 adios(NULL, "unable to read folder %s", folder);
348 /* check for empty folder */
350 adios(NULL, "no messages in %s", folder);
352 /* parse all the message ranges/sequences and set SELECTED */
353 for (msgnum = 0; msgnum < msgs.size; msgnum++)
354 if (!m_convert(mp, msgs.msgs[msgnum]))
358 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
359 ** since we will use that as a tag to know which messages
360 ** to remove from the "unseen" sequence.
362 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
363 if (is_selected(mp, msgnum))
364 set_unseen(mp, msgnum);
366 seq_setprev(mp); /* set the Previous-Sequence */
367 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
369 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
371 adios(NULL, "out of memory");
375 ** Parse all the SELECTED messages.
377 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
378 if (is_selected(mp, msgnum)) {
381 msgnam = m_name(msgnum);
382 if ((ct = parse_mime(msgnam)))
392 SIGNAL(SIGQUIT, quitser);
393 SIGNAL(SIGPIPE, pipeser);
396 ** Get the associated umask for the relevant contents.
398 for (ctp = cts; *ctp; ctp++) {
402 if (type_ok(ct, 1) && !ct->c_umask) {
403 if (stat(ct->c_file, &st) != NOTOK)
404 ct->c_umask = ~(st.st_mode & 0777);
406 ct->c_umask = ~m_gmprot();
411 ** Show the message content
413 show_all_messages(cts);
415 /* Now free all the structures for the content */
416 for (ctp = cts; *ctp; ctp++)
422 /* If reading from a folder, do some updating */
424 context_replace(curfolder, folder); /* update current folder */
425 seq_setcur(mp, mp->hghsel); /* update current message */
426 seq_save(mp); /* synchronize sequences */
427 context_save(); /* save the context file */
441 fprintf(stderr, "\n");