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 : mh_xstrdup(expanddir(cp));
173 if (!(cp = *argp++) || *cp == '-')
174 adios(EX_USAGE, NULL, "missing argument to %s",
178 formsw = mh_xstrdup(etcpath(cp));
192 if (*cp == '+' || *cp == '@') {
194 adios(EX_USAGE, NULL, "only one folder at a time!");
196 folder = mh_xstrdup(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 = mh_xstrdup(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 cts = mh_xcalloc(2, sizeof(*cts));
255 if ((ct = parse_mime(file)))
259 ** message(s) are coming from a folder
264 app_msgarg(&msgs, seq_next);
267 app_msgarg(&msgs, seq_prev);
270 app_msgarg(&msgs, seq_cur);
275 folder = getcurfol();
276 maildir = toabsdir(folder);
278 if (chdir(maildir) == NOTOK)
279 adios(EX_OSERR, maildir, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp = folder_read(folder)))
283 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
285 /* check for empty folder */
287 adios(EX_DATAERR, NULL, "no messages in %s", folder);
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum = 0; msgnum < msgs.size; msgnum++)
291 if (!m_convert(mp, msgs.msgs[msgnum]))
295 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
296 ** since we will use that as a tag to know which messages
297 ** to remove from the "unseen" sequence.
299 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
300 if (is_selected(mp, msgnum))
301 set_unseen(mp, msgnum);
303 seq_setprev(mp); /* set the Previous-Sequence */
304 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
306 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
310 ** Parse all the SELECTED messages.
312 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
313 if (is_selected(mp, msgnum)) {
316 msgnam = m_name(msgnum);
317 if ((ct = parse_mime(msgnam)))
327 SIGNAL(SIGQUIT, quitser);
328 SIGNAL(SIGPIPE, pipeser);
331 ** Get the associated umask for the relevant contents.
333 for (ctp = cts; *ctp; ctp++) {
337 if (type_ok(ct, 1) && !ct->c_umask) {
338 if (stat(ct->c_file, &st) != NOTOK)
339 ct->c_umask = ~(st.st_mode & 0777);
341 ct->c_umask = ~m_gmprot();
345 if ((ontty = isatty(fileno(stdout)))) {
346 m_popen(defaultpager);
350 ** Show the message content
352 show_all_messages(cts);
358 /* Now free all the structures for the content */
359 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 */
382 fprintf(stderr, "\n");
391 static int m_pid = NOTOK;
392 static int sd = NOTOK;
400 if ((sd = dup(fileno(stdout))) == NOTOK)
401 adios(EX_OSERR, "standard output", "unable to dup()");
403 if (pipe(pd) == NOTOK)
404 adios(EX_OSERR, "pipe", "unable to");
406 switch (m_pid = fork()) {
408 adios(EX_OSERR, "fork", "unable to");
411 SIGNAL(SIGINT, SIG_DFL);
412 SIGNAL(SIGQUIT, SIG_DFL);
415 if (pd[0] != fileno(stdin)) {
416 dup2(pd[0], fileno(stdin));
419 execlp(name, mhbasename(name), NULL);
420 fprintf(stderr, "unable to exec ");
426 if (pd[1] != fileno(stdout)) {
427 dup2(pd[1], fileno(stdout));
442 if (dup2(sd, fileno(stdout)) == NOTOK)
443 adios(EX_OSERR, "standard output", "unable to dup2()");