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);
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;
101 if (atexit(freects_done) != 0) {
102 adios(NULL, "atexit failed");
105 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);
127 /* sysexits.h EX_USAGE */
130 adios(NULL, "-%s unknown", cp);
133 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches]", invo_name, mode==SHOW ? "[msgs] " : "");
134 print_help(buf, switches, 1);
137 print_version(invo_name);
141 if (!(cp = *argp++) || *cp == '-')
142 adios(NULL, "missing argument to %s",
145 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
150 if (!(cp = *argp++) || *cp == '-')
151 adios(NULL, "missing argument to %s",
154 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
160 adios(NULL, "Either call show as `%s' or use -file", invo_name);
163 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
164 adios(NULL, "missing argument to %s",
166 file = *cp == '-' ? cp : getcpy(expanddir(cp));
170 if (!(cp = *argp++) || *cp == '-')
171 adios(NULL, "missing argument to %s",
175 formsw = getcpy(etcpath(cp));
189 if (*cp == '+' || *cp == '@') {
191 adios(NULL, "only one folder at a time!");
193 folder = getcpy(expandfol(cp));
194 } else if (mode != SHOW) {
195 adios(NULL, "Either call show as `%s' or give message arguments", invo_name);
197 app_msgarg(&msgs, cp);
201 /* null terminate the list of acceptable parts/types */
207 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
212 ** Check if we've specified an additional profile
214 if ((cp = getenv("MHSHOW"))) {
215 if ((fp = fopen(cp, "r"))) {
216 readconfig((struct node **) 0, fp, cp, 0);
219 admonish("", "unable to read $MHSHOW profile (%s)",
225 ** Read the standard profile setup
227 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
228 readconfig((struct node **) 0, fp, cp, 0);
233 ** Check for storage directory. If specified,
234 ** then store temporary files there. Else we
235 ** store them in standard nmh directory.
237 if ((cp = context_find(nmhstorage)) && *cp)
238 tmp = concat(cp, "/", invo_name, NULL);
240 tmp = getcpy(toabsdir(invo_name));
242 if (file && msgs.size)
243 adios(NULL, "cannot specify msg and file at same time!");
246 ** check if message is coming from file
249 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
250 adios(NULL, "out of memory");
253 if ((ct = parse_mime(file)))
257 ** message(s) are coming from a folder
262 app_msgarg(&msgs, seq_next);
265 app_msgarg(&msgs, seq_prev);
268 app_msgarg(&msgs, seq_cur);
273 folder = getcurfol();
274 maildir = toabsdir(folder);
276 if (chdir(maildir) == NOTOK)
277 adios(maildir, "unable to change directory to");
279 /* read folder and create message structure */
280 if (!(mp = folder_read(folder)))
281 adios(NULL, "unable to read folder %s", folder);
283 /* check for empty folder */
285 adios(NULL, "no messages in %s", folder);
287 /* parse all the message ranges/sequences and set SELECTED */
288 for (msgnum = 0; msgnum < msgs.size; msgnum++)
289 if (!m_convert(mp, msgs.msgs[msgnum]))
290 /* sysexits.h EX_USAGE */
294 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
295 ** since we will use that as a tag to know which messages
296 ** to remove from the "unseen" sequence.
298 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
299 if (is_selected(mp, msgnum))
300 set_unseen(mp, msgnum);
302 seq_setprev(mp); /* set the Previous-Sequence */
303 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
305 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
307 adios(NULL, "out of memory");
311 ** Parse all the SELECTED messages.
313 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
314 if (is_selected(mp, msgnum)) {
317 msgnam = m_name(msgnum);
318 if ((ct = parse_mime(msgnam)))
328 SIGNAL(SIGQUIT, quitser);
329 SIGNAL(SIGPIPE, pipeser);
332 ** Get the associated umask for the relevant contents.
334 for (ctp = cts; *ctp; ctp++) {
338 if (type_ok(ct, 1) && !ct->c_umask) {
339 if (stat(ct->c_file, &st) != NOTOK)
340 ct->c_umask = ~(st.st_mode & 0777);
342 ct->c_umask = ~m_gmprot();
346 if ((ontty = isatty(fileno(stdout)))) {
347 m_popen(defaultpager);
351 ** Show the message content
353 show_all_messages(cts);
359 /* Now free all the structures for the content */
360 for (ctp = cts; *ctp; ctp++)
366 /* If reading from a folder, do some updating */
368 context_replace(curfolder, folder); /* update current folder */
369 seq_setcur(mp, mp->hghsel); /* update current message */
370 seq_save(mp); /* synchronize sequences */
371 context_save(); /* save the context file */
384 fprintf(stderr, "\n");
393 static int m_pid = NOTOK;
394 static int sd = NOTOK;
402 if ((sd = dup(fileno(stdout))) == NOTOK)
403 adios("standard output", "unable to dup()");
405 if (pipe(pd) == NOTOK)
406 adios("pipe", "unable to");
408 switch (m_pid = fork()) {
410 adios("fork", "unable to");
413 SIGNAL(SIGINT, SIG_DFL);
414 SIGNAL(SIGQUIT, SIG_DFL);
417 if (pd[0] != fileno(stdin)) {
418 dup2(pd[0], fileno(stdin));
421 execlp(name, mhbasename(name), NULL);
422 fprintf(stderr, "unable to exec ");
428 if (pd[1] != fileno(stdout)) {
429 dup2(pd[1], fileno(stdout));
444 if (dup2(sd, fileno(stdout)) == NOTOK)
445 adios("standard output", "unable to dup2()");