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[] = {
34 { "noserialonly", 0 },
39 #define FILESW 6 /* interface from show */
42 { "form formfile", 0 },
46 { "type content", 0 },
48 { "rcache policy", 0 },
50 { "wcache policy", 0 },
57 ** switches for moreproc/mhlproc
60 { "moreproc program", -4 },
64 { "length lines", -4 },
66 { "width columns", -4 },
69 ** switches for debugging
78 extern char *tmp; /* directory to place temp files */
83 extern char *cache_public;
84 extern char *cache_private;
90 extern int nomore; /* flags for moreproc/header display */
96 extern char *parts[NPARTS + 1];
97 extern char *types[NTYPES + 1];
103 #define quitser pipeser
106 CT parse_mime(char *);
109 int part_ok(CT, int);
110 int type_ok(CT, int);
111 void set_endian(void);
112 void flush_errors(void);
115 void show_all_messages(CT *);
118 void free_content(CT);
120 void freects_done(int) NORETURN;
125 static RETSIGTYPE pipeser(int);
129 main(int argc, char **argv)
131 int msgnum, *icachesw;
132 char *cp, *file = NULL, *folder = NULL;
133 char *maildir, buf[100], **argp;
135 struct msgs_array msgs = { 0, 0, NULL };
136 struct msgs *mp = NULL;
143 setlocale(LC_ALL, "");
145 invo_name = mhbasename(argv[0]);
147 /* read user profile/context */
150 arguments = getarguments(invo_name, argc, argv, 1);
156 while ((cp = *argp++)) {
158 switch (smatch(++cp, switches)) {
160 ambigsw(cp, switches);
163 adios(NULL, "-%s unknown", cp);
166 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
167 print_help(buf, switches, 1);
170 print_version(invo_name);
174 icachesw = &rcachesw;
177 icachesw = &wcachesw;
179 if (!(cp = *argp++) || *cp == '-')
180 adios(NULL, "missing argument to %s",
182 switch (*icachesw = smatch(cp, caches)) {
187 adios(NULL, "%s unknown", cp);
208 if (!(cp = *argp++) || *cp == '-')
209 adios(NULL, "missing argument to %s",
212 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
217 if (!(cp = *argp++) || *cp == '-')
218 adios(NULL, "missing argument to %s",
221 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
226 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
227 adios(NULL, "missing argument to %s",
229 file = *cp == '-' ? cp : getcpy(expanddir(cp));
233 if (!(cp = *argp++) || *cp == '-')
234 adios(NULL, "missing argument to %s",
238 formsw = getcpy(etcpath(cp));
242 ** Switches for moreproc/mhlproc
245 if (!(progsw = *argp++) || *progsw == '-')
246 adios(NULL, "missing argument to %s",
255 if (!(cp = *argp++) || *cp == '-')
256 adios(NULL, "missing argument to %s",
271 if (*cp == '+' || *cp == '@') {
273 adios(NULL, "only one folder at a time!");
275 folder = getcpy(expandfol(cp));
277 app_msgarg(&msgs, cp);
280 /* null terminate the list of acceptable parts/types */
286 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
291 ** Check if we've specified an additional profile
293 if ((cp = getenv("MHSHOW"))) {
294 if ((fp = fopen(cp, "r"))) {
295 readconfig((struct node **) 0, fp, cp, 0);
298 admonish("", "unable to read $MHSHOW profile (%s)",
304 ** Read the standard profile setup
306 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
307 readconfig((struct node **) 0, fp, cp, 0);
311 /* Check for public cache location */
312 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
315 /* Check for private cache location */
316 if (!(cache_private = context_find(nmhprivcache)))
317 cache_private = ".cache";
318 cache_private = getcpy(toabsdir(cache_private));
321 ** Check for storage directory. If specified,
322 ** then store temporary files there. Else we
323 ** store them in standard nmh directory.
325 if ((cp = context_find(nmhstorage)) && *cp)
326 tmp = concat(cp, "/", invo_name, NULL);
328 tmp = getcpy(toabsdir(invo_name));
330 if (file && msgs.size)
331 adios(NULL, "cannot specify msg and file at same time!");
334 ** check if message is coming from file
337 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
338 adios(NULL, "out of memory");
341 if ((ct = parse_mime(file)));
345 ** message(s) are coming from a folder
348 app_msgarg(&msgs, seq_cur);
350 folder = getcurfol();
351 maildir = toabsdir(folder);
353 if (chdir(maildir) == NOTOK)
354 adios(maildir, "unable to change directory to");
356 /* read folder and create message structure */
357 if (!(mp = folder_read(folder)))
358 adios(NULL, "unable to read folder %s", folder);
360 /* check for empty folder */
362 adios(NULL, "no messages in %s", folder);
364 /* parse all the message ranges/sequences and set SELECTED */
365 for (msgnum = 0; msgnum < msgs.size; msgnum++)
366 if (!m_convert(mp, msgs.msgs[msgnum]))
370 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
371 ** since we will use that as a tag to know which messages
372 ** to remove from the "unseen" sequence.
374 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
375 if (is_selected(mp, msgnum))
376 set_unseen(mp, msgnum);
378 seq_setprev(mp); /* set the Previous-Sequence */
379 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
381 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
383 adios(NULL, "out of memory");
387 ** Parse all the SELECTED messages.
389 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
390 if (is_selected(mp, msgnum)) {
393 msgnam = m_name(msgnum);
394 if ((ct = parse_mime(msgnam)))
404 SIGNAL(SIGQUIT, quitser);
405 SIGNAL(SIGPIPE, pipeser);
408 ** Get the associated umask for the relevant contents.
410 for (ctp = cts; *ctp; ctp++) {
414 if (type_ok(ct, 1) && !ct->c_umask) {
415 if (stat(ct->c_file, &st) != NOTOK)
416 ct->c_umask = ~(st.st_mode & 0777);
418 ct->c_umask = ~m_gmprot();
423 ** Show the message content
425 show_all_messages(cts);
427 /* Now free all the structures for the content */
428 for (ctp = cts; *ctp; ctp++)
434 /* If reading from a folder, do some updating */
436 context_replace(curfolder, folder); /* update current folder */
437 seq_setcur(mp, mp->hghsel); /* update current message */
438 seq_save(mp); /* synchronize sequences */
439 context_save(); /* save the context file */
453 fprintf(stderr, "\n");