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 atexit(freects_done);
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);
125 /* sysexits.h EX_USAGE */
128 adios(NULL, "-%s unknown", cp);
131 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches]", invo_name, mode==SHOW ? "[msgs] " : "");
132 print_help(buf, switches, 1);
135 print_version(invo_name);
139 if (!(cp = *argp++) || *cp == '-')
140 adios(NULL, "missing argument to %s",
143 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
148 if (!(cp = *argp++) || *cp == '-')
149 adios(NULL, "missing argument to %s",
152 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
158 adios(NULL, "Either call show as `%s' or use -file", invo_name);
161 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
162 adios(NULL, "missing argument to %s",
164 file = *cp == '-' ? cp : getcpy(expanddir(cp));
168 if (!(cp = *argp++) || *cp == '-')
169 adios(NULL, "missing argument to %s",
173 formsw = getcpy(etcpath(cp));
187 if (*cp == '+' || *cp == '@') {
189 adios(NULL, "only one folder at a time!");
191 folder = getcpy(expandfol(cp));
192 } else if (mode != SHOW) {
193 adios(NULL, "Either call show as `%s' or give message arguments", invo_name);
195 app_msgarg(&msgs, cp);
199 /* null terminate the list of acceptable parts/types */
205 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
210 ** Check if we've specified an additional profile
212 if ((cp = getenv("MHSHOW"))) {
213 if ((fp = fopen(cp, "r"))) {
214 readconfig((struct node **) 0, fp, cp, 0);
217 admonish("", "unable to read $MHSHOW profile (%s)",
223 ** Read the standard profile setup
225 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
226 readconfig((struct node **) 0, fp, cp, 0);
231 ** Check for storage directory. If specified,
232 ** then store temporary files there. Else we
233 ** store them in standard nmh directory.
235 if ((cp = context_find(nmhstorage)) && *cp)
236 tmp = concat(cp, "/", invo_name, NULL);
238 tmp = getcpy(toabsdir(invo_name));
240 if (file && msgs.size)
241 adios(NULL, "cannot specify msg and file at same time!");
244 ** check if message is coming from file
247 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
248 adios(NULL, "out of memory");
251 if ((ct = parse_mime(file)))
255 ** message(s) are coming from a folder
260 app_msgarg(&msgs, seq_next);
263 app_msgarg(&msgs, seq_prev);
266 app_msgarg(&msgs, seq_cur);
271 folder = getcurfol();
272 maildir = toabsdir(folder);
274 if (chdir(maildir) == NOTOK)
275 adios(maildir, "unable to change directory to");
277 /* read folder and create message structure */
278 if (!(mp = folder_read(folder)))
279 adios(NULL, "unable to read folder %s", folder);
281 /* check for empty folder */
283 adios(NULL, "no messages in %s", folder);
285 /* parse all the message ranges/sequences and set SELECTED */
286 for (msgnum = 0; msgnum < msgs.size; msgnum++)
287 if (!m_convert(mp, msgs.msgs[msgnum]))
288 /* sysexits.h EX_USAGE */
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 */
382 fprintf(stderr, "\n");
391 static int m_pid = NOTOK;
392 static int sd = NOTOK;
400 if ((sd = dup(fileno(stdout))) == NOTOK)
401 adios("standard output", "unable to dup()");
403 if (pipe(pd) == NOTOK)
404 adios("pipe", "unable to");
406 switch (m_pid = fork()) {
408 adios("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("standard output", "unable to dup2()");