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;
103 setlocale(LC_ALL, "");
104 invo_name = mhbasename(argv[0]);
105 if (mh_strcasecmp(invo_name, "next")==0) {
107 } else if (mh_strcasecmp(invo_name, "prev")==0) {
111 /* read user profile/context */
114 arguments = getarguments(invo_name, argc, argv, 1);
120 while ((cp = *argp++)) {
122 switch (smatch(++cp, switches)) {
124 ambigsw(cp, switches);
127 adios(NULL, "-%s unknown", cp);
130 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches]", invo_name, mode==SHOW ? "[msgs] " : "");
131 print_help(buf, switches, 1);
134 print_version(invo_name);
138 if (!(cp = *argp++) || *cp == '-')
139 adios(NULL, "missing argument to %s",
142 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
147 if (!(cp = *argp++) || *cp == '-')
148 adios(NULL, "missing argument to %s",
151 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
157 adios(NULL, "Either call show as `%s' or use -file", invo_name);
160 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
161 adios(NULL, "missing argument to %s",
163 file = *cp == '-' ? cp : getcpy(expanddir(cp));
167 if (!(cp = *argp++) || *cp == '-')
168 adios(NULL, "missing argument to %s",
172 formsw = getcpy(etcpath(cp));
186 if (*cp == '+' || *cp == '@') {
188 adios(NULL, "only one folder at a time!");
190 folder = getcpy(expandfol(cp));
191 } else if (mode != SHOW) {
192 adios(NULL, "Either call show as `%s' or give message arguments", invo_name);
194 app_msgarg(&msgs, cp);
198 /* null terminate the list of acceptable parts/types */
204 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
209 ** Check if we've specified an additional profile
211 if ((cp = getenv("MHSHOW"))) {
212 if ((fp = fopen(cp, "r"))) {
213 readconfig((struct node **) 0, fp, cp, 0);
216 admonish("", "unable to read $MHSHOW profile (%s)",
222 ** Read the standard profile setup
224 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
225 readconfig((struct node **) 0, fp, cp, 0);
230 ** Check for storage directory. If specified,
231 ** then store temporary files there. Else we
232 ** store them in standard nmh directory.
234 if ((cp = context_find(nmhstorage)) && *cp)
235 tmp = concat(cp, "/", invo_name, NULL);
237 tmp = getcpy(toabsdir(invo_name));
239 if (file && msgs.size)
240 adios(NULL, "cannot specify msg and file at same time!");
243 ** check if message is coming from file
246 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
247 adios(NULL, "out of memory");
250 if ((ct = parse_mime(file)))
254 ** message(s) are coming from a folder
259 app_msgarg(&msgs, seq_next);
262 app_msgarg(&msgs, seq_prev);
265 app_msgarg(&msgs, seq_cur);
270 folder = getcurfol();
271 maildir = toabsdir(folder);
273 if (chdir(maildir) == NOTOK)
274 adios(maildir, "unable to change directory to");
276 /* read folder and create message structure */
277 if (!(mp = folder_read(folder)))
278 adios(NULL, "unable to read folder %s", folder);
280 /* check for empty folder */
282 adios(NULL, "no messages in %s", folder);
284 /* parse all the message ranges/sequences and set SELECTED */
285 for (msgnum = 0; msgnum < msgs.size; msgnum++)
286 if (!m_convert(mp, msgs.msgs[msgnum]))
290 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
291 ** since we will use that as a tag to know which messages
292 ** to remove from the "unseen" sequence.
294 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
295 if (is_selected(mp, msgnum))
296 set_unseen(mp, msgnum);
298 seq_setprev(mp); /* set the Previous-Sequence */
299 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
301 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
303 adios(NULL, "out of memory");
307 ** Parse all the SELECTED messages.
309 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
310 if (is_selected(mp, msgnum)) {
313 msgnam = m_name(msgnum);
314 if ((ct = parse_mime(msgnam)))
324 SIGNAL(SIGQUIT, quitser);
325 SIGNAL(SIGPIPE, pipeser);
328 ** Get the associated umask for the relevant contents.
330 for (ctp = cts; *ctp; ctp++) {
334 if (type_ok(ct, 1) && !ct->c_umask) {
335 if (stat(ct->c_file, &st) != NOTOK)
336 ct->c_umask = ~(st.st_mode & 0777);
338 ct->c_umask = ~m_gmprot();
342 if ((ontty = isatty(fileno(stdout)))) {
343 m_popen(defaultpager);
347 ** Show the message content
349 show_all_messages(cts);
355 /* Now free all the structures for the content */
356 for (ctp = cts; *ctp; ctp++)
362 /* If reading from a folder, do some updating */
364 context_replace(curfolder, folder); /* update current folder */
365 seq_setcur(mp, mp->hghsel); /* update current message */
366 seq_save(mp); /* synchronize sequences */
367 context_save(); /* save the context file */
381 fprintf(stderr, "\n");
390 static int m_pid = NOTOK;
391 static int sd = NOTOK;
399 if ((sd = dup(fileno(stdout))) == NOTOK)
400 adios("standard output", "unable to dup()");
402 if (pipe(pd) == NOTOK)
403 adios("pipe", "unable to");
405 switch (m_pid = fork()) {
407 adios("fork", "unable to");
410 SIGNAL(SIGINT, SIG_DFL);
411 SIGNAL(SIGQUIT, SIG_DFL);
414 if (pd[0] != fileno(stdin)) {
415 dup2(pd[0], fileno(stdin));
418 execlp(name, mhbasename(name), NULL);
419 fprintf(stderr, "unable to exec ");
425 if (pd[1] != fileno(stdout)) {
426 dup2(pd[1], fileno(stdout));
441 if (dup2(sd, fileno(stdout)) == NOTOK)
442 adios("standard output", "unable to dup2()");