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>
16 #include <h/mhparse.h>
19 static struct swit switches[] = {
24 #define FILESW 2 /* interface from show */
27 { "form formfile", 0 },
31 { "type content", 0 },
43 extern char *tmp; /* directory to place temp files */
52 extern char *parts[NPARTS + 1];
53 extern char *types[NTYPES + 1];
56 static enum { SHOW, NEXT, PREV } mode = SHOW;
61 #define quitser pipeser
64 CT parse_mime(char *);
69 void set_endian(void);
70 void flush_errors(void);
73 void show_all_messages(CT *);
76 void free_content(CT);
78 void freects_done(int) NORETURN;
83 static void pipeser(int);
84 static void m_popen(char *);
85 static void m_pclose(void);
89 main(int argc, char **argv)
92 char *cp, *file = NULL, *folder = NULL;
93 char *maildir, buf[100], **argp;
95 struct msgs_array msgs = { 0, 0, NULL };
96 struct msgs *mp = NULL;
104 setlocale(LC_ALL, "");
106 invo_name = mhbasename(argv[0]);
107 if (mh_strcasecmp(invo_name, "next")==0) {
109 } else if (mh_strcasecmp(invo_name, "prev")==0) {
113 /* read user profile/context */
116 arguments = getarguments(invo_name, argc, argv, 1);
122 while ((cp = *argp++)) {
124 switch (smatch(++cp, switches)) {
126 ambigsw(cp, switches);
129 adios(NULL, "-%s unknown", cp);
132 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches]", invo_name, mode==SHOW ? "[msgs] " : "");
133 print_help(buf, switches, 1);
136 print_version(invo_name);
140 if (!(cp = *argp++) || *cp == '-')
141 adios(NULL, "missing argument to %s",
144 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
149 if (!(cp = *argp++) || *cp == '-')
150 adios(NULL, "missing argument to %s",
153 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
159 adios(NULL, "Either call show as `%s' or use -file", invo_name);
162 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
163 adios(NULL, "missing argument to %s",
165 file = *cp == '-' ? cp : getcpy(expanddir(cp));
169 if (!(cp = *argp++) || *cp == '-')
170 adios(NULL, "missing argument to %s",
174 formsw = getcpy(etcpath(cp));
188 if (*cp == '+' || *cp == '@') {
190 adios(NULL, "only one folder at a time!");
192 folder = getcpy(expandfol(cp));
193 } else if (mode != SHOW) {
194 adios(NULL, "Either call show as `%s' or give message arguments", invo_name);
196 app_msgarg(&msgs, cp);
200 /* null terminate the list of acceptable parts/types */
206 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
211 ** Check if we've specified an additional profile
213 if ((cp = getenv("MHSHOW"))) {
214 if ((fp = fopen(cp, "r"))) {
215 readconfig((struct node **) 0, fp, cp, 0);
218 admonish("", "unable to read $MHSHOW profile (%s)",
224 ** Read the standard profile setup
226 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
227 readconfig((struct node **) 0, fp, cp, 0);
232 ** Check for storage directory. If specified,
233 ** then store temporary files there. Else we
234 ** store them in standard nmh directory.
236 if ((cp = context_find(nmhstorage)) && *cp)
237 tmp = concat(cp, "/", invo_name, NULL);
239 tmp = getcpy(toabsdir(invo_name));
241 if (file && msgs.size)
242 adios(NULL, "cannot specify msg and file at same time!");
245 ** check if message is coming from file
248 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
249 adios(NULL, "out of memory");
252 if ((ct = parse_mime(file)))
256 ** message(s) are coming from a folder
261 app_msgarg(&msgs, seq_next);
264 app_msgarg(&msgs, seq_prev);
267 app_msgarg(&msgs, seq_cur);
272 folder = getcurfol();
273 maildir = toabsdir(folder);
275 if (chdir(maildir) == NOTOK)
276 adios(maildir, "unable to change directory to");
278 /* read folder and create message structure */
279 if (!(mp = folder_read(folder)))
280 adios(NULL, "unable to read folder %s", folder);
282 /* check for empty folder */
284 adios(NULL, "no messages in %s", folder);
286 /* parse all the message ranges/sequences and set SELECTED */
287 for (msgnum = 0; msgnum < msgs.size; msgnum++)
288 if (!m_convert(mp, msgs.msgs[msgnum]))
292 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
293 ** since we will use that as a tag to know which messages
294 ** to remove from the "unseen" sequence.
296 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
297 if (is_selected(mp, msgnum))
298 set_unseen(mp, msgnum);
300 seq_setprev(mp); /* set the Previous-Sequence */
301 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
303 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
305 adios(NULL, "out of memory");
309 ** Parse all the SELECTED messages.
311 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
312 if (is_selected(mp, msgnum)) {
315 msgnam = m_name(msgnum);
316 if ((ct = parse_mime(msgnam)))
326 SIGNAL(SIGQUIT, quitser);
327 SIGNAL(SIGPIPE, pipeser);
330 ** Get the associated umask for the relevant contents.
332 for (ctp = cts; *ctp; ctp++) {
336 if (type_ok(ct, 1) && !ct->c_umask) {
337 if (stat(ct->c_file, &st) != NOTOK)
338 ct->c_umask = ~(st.st_mode & 0777);
340 ct->c_umask = ~m_gmprot();
344 if ((ontty = isatty(fileno(stdout)))) {
345 m_popen(defaultpager);
349 ** Show the message content
351 show_all_messages(cts);
357 /* Now free all the structures for the content */
358 for (ctp = cts; *ctp; ctp++)
364 /* If reading from a folder, do some updating */
366 context_replace(curfolder, folder); /* update current folder */
367 seq_setcur(mp, mp->hghsel); /* update current message */
368 seq_save(mp); /* synchronize sequences */
369 context_save(); /* save the context file */
383 fprintf(stderr, "\n");
392 static int m_pid = NOTOK;
393 static int sd = NOTOK;
401 if ((sd = dup(fileno(stdout))) == NOTOK)
402 adios("standard output", "unable to dup()");
404 if (pipe(pd) == NOTOK)
405 adios("pipe", "unable to");
407 switch (m_pid = fork()) {
409 adios("fork", "unable to");
412 SIGNAL(SIGINT, SIG_DFL);
413 SIGNAL(SIGQUIT, SIG_DFL);
416 if (pd[0] != fileno(stdin)) {
417 dup2(pd[0], fileno(stdin));
420 execlp(name, mhbasename(name), NULL);
421 fprintf(stderr, "unable to exec ");
427 if (pd[1] != fileno(stdout)) {
428 dup2(pd[1], fileno(stdout));
443 if (dup2(sd, fileno(stdout)) == NOTOK)
444 adios("standard output", "unable to dup2()");