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>
23 static struct swit switches[] = {
28 #define FILESW 2 /* interface from show */
31 { "form formfile", 0 },
35 { "type content", 0 },
47 extern char *tmp; /* directory to place temp files */
56 extern char *parts[NPARTS + 1];
57 extern char *types[NTYPES + 1];
60 static enum { SHOW, NEXT, PREV } mode = SHOW;
65 #define quitser pipeser
68 CT parse_mime(char *);
73 void set_endian(void);
74 void flush_errors(void);
77 void show_all_messages(CT *);
80 void free_content(CT);
87 static void pipeser(int);
88 static void m_popen(char *);
89 static void m_pclose(void);
93 main(int argc, char **argv)
96 char *cp, *file = NULL, *folder = NULL;
97 char *maildir, buf[100], **argp;
99 struct msgs_array msgs = { 0, 0, NULL };
100 struct msgs *mp = NULL;
105 if (atexit(freects_done) != 0) {
106 adios(EX_OSERR, NULL, "atexit failed");
109 setlocale(LC_ALL, "");
110 invo_name = mhbasename(argv[0]);
111 if (mh_strcasecmp(invo_name, "next")==0) {
113 } else if (mh_strcasecmp(invo_name, "prev")==0) {
117 /* read user profile/context */
120 arguments = getarguments(invo_name, argc, argv, 1);
126 while ((cp = *argp++)) {
128 switch (smatch(++cp, switches)) {
130 ambigsw(cp, switches);
133 adios(EX_USAGE, NULL, "-%s unknown", cp);
136 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches]", invo_name, mode==SHOW ? "[msgs] " : "");
137 print_help(buf, switches, 1);
138 exit(argc == 2 ? EX_OK : EX_USAGE);
140 print_version(invo_name);
141 exit(argc == 2 ? EX_OK : EX_USAGE);
144 if (!(cp = *argp++) || *cp == '-')
145 adios(EX_USAGE, NULL, "missing argument to %s",
148 adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
153 if (!(cp = *argp++) || *cp == '-')
154 adios(EX_USAGE, NULL, "missing argument to %s",
157 adios(EX_USAGE, NULL, "too many types (starting with %s), %d max", cp, NTYPES);
163 adios(EX_USAGE, NULL, "Either call show as `%s' or use -file", invo_name);
166 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
167 adios(EX_USAGE, NULL, "missing argument to %s",
169 file = *cp == '-' ? cp : getcpy(expanddir(cp));
173 if (!(cp = *argp++) || *cp == '-')
174 adios(EX_USAGE, NULL, "missing argument to %s",
178 formsw = getcpy(etcpath(cp));
192 if (*cp == '+' || *cp == '@') {
194 adios(EX_USAGE, NULL, "only one folder at a time!");
196 folder = getcpy(expandfol(cp));
197 } else if (mode != SHOW) {
198 adios(EX_USAGE, NULL, "Either call show as `%s' or give message arguments", invo_name);
200 app_msgarg(&msgs, cp);
204 /* null terminate the list of acceptable parts/types */
210 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
215 ** Check if we've specified an additional profile
217 if ((cp = getenv("MHSHOW"))) {
218 if ((fp = fopen(cp, "r"))) {
219 readconfig((struct node **) 0, fp, cp, 0);
222 admonish("", "unable to read $MHSHOW profile (%s)",
228 ** Read the standard profile setup
230 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
231 readconfig((struct node **) 0, fp, cp, 0);
236 ** Check for storage directory. If specified,
237 ** then store temporary files there. Else we
238 ** store them in standard nmh directory.
240 if ((cp = context_find(nmhstorage)) && *cp)
241 tmp = concat(cp, "/", invo_name, NULL);
243 tmp = getcpy(toabsdir(invo_name));
245 if (file && msgs.size)
246 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
249 ** check if message is coming from file
252 if (!(cts = (CT *) mh_xcalloc((size_t) 2, sizeof(*cts))))
253 adios(EX_OSERR, NULL, "out of memory");
256 if ((ct = parse_mime(file)))
260 ** message(s) are coming from a folder
265 app_msgarg(&msgs, seq_next);
268 app_msgarg(&msgs, seq_prev);
271 app_msgarg(&msgs, seq_cur);
276 folder = getcurfol();
277 maildir = toabsdir(folder);
279 if (chdir(maildir) == NOTOK)
280 adios(EX_OSERR, maildir, "unable to change directory to");
282 /* read folder and create message structure */
283 if (!(mp = folder_read(folder)))
284 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
286 /* check for empty folder */
288 adios(EX_DATAERR, NULL, "no messages in %s", folder);
290 /* parse all the message ranges/sequences and set SELECTED */
291 for (msgnum = 0; msgnum < msgs.size; msgnum++)
292 if (!m_convert(mp, msgs.msgs[msgnum]))
296 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
297 ** since we will use that as a tag to know which messages
298 ** to remove from the "unseen" sequence.
300 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
301 if (is_selected(mp, msgnum))
302 set_unseen(mp, msgnum);
304 seq_setprev(mp); /* set the Previous-Sequence */
305 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
307 if (!(cts = (CT *) mh_xcalloc((size_t) (mp->numsel + 1),
309 adios(EX_OSERR, NULL, "out of memory");
313 ** Parse all the SELECTED messages.
315 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
316 if (is_selected(mp, msgnum)) {
319 msgnam = m_name(msgnum);
320 if ((ct = parse_mime(msgnam)))
330 SIGNAL(SIGQUIT, quitser);
331 SIGNAL(SIGPIPE, pipeser);
334 ** Get the associated umask for the relevant contents.
336 for (ctp = cts; *ctp; ctp++) {
340 if (type_ok(ct, 1) && !ct->c_umask) {
341 if (stat(ct->c_file, &st) != NOTOK)
342 ct->c_umask = ~(st.st_mode & 0777);
344 ct->c_umask = ~m_gmprot();
348 if ((ontty = isatty(fileno(stdout)))) {
349 m_popen(defaultpager);
353 ** Show the message content
355 show_all_messages(cts);
361 /* Now free all the structures for the content */
362 for (ctp = cts; *ctp; ctp++)
368 /* If reading from a folder, do some updating */
370 context_replace(curfolder, folder); /* update current folder */
371 seq_setcur(mp, mp->hghsel); /* update current message */
372 seq_save(mp); /* synchronize sequences */
373 context_save(); /* save the context file */
386 fprintf(stderr, "\n");
395 static int m_pid = NOTOK;
396 static int sd = NOTOK;
404 if ((sd = dup(fileno(stdout))) == NOTOK)
405 adios(EX_OSERR, "standard output", "unable to dup()");
407 if (pipe(pd) == NOTOK)
408 adios(EX_OSERR, "pipe", "unable to");
410 switch (m_pid = fork()) {
412 adios(EX_OSERR, "fork", "unable to");
415 SIGNAL(SIGINT, SIG_DFL);
416 SIGNAL(SIGQUIT, SIG_DFL);
419 if (pd[0] != fileno(stdin)) {
420 dup2(pd[0], fileno(stdin));
423 execlp(name, mhbasename(name), NULL);
424 fprintf(stderr, "unable to exec ");
430 if (pd[1] != fileno(stdout)) {
431 dup2(pd[1], fileno(stdout));
446 if (dup2(sd, fileno(stdout)) == NOTOK)
447 adios(EX_OSERR, "standard output", "unable to dup2()");