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 },
45 char *version=VERSION;
48 extern char *tmp; /* directory to place temp files */
57 extern char *parts[NPARTS + 1];
58 extern char *types[NTYPES + 1];
61 static enum { SHOW, NEXT, PREV } mode = SHOW;
66 #define quitser pipeser
69 CT parse_mime(char *);
74 void set_endian(void);
75 void flush_errors(void);
78 void show_all_messages(CT *);
81 void free_content(CT);
88 static void pipeser(int);
89 static void m_popen(char *);
90 static void m_pclose(void);
94 main(int argc, char **argv)
97 char *cp, *file = NULL, *folder = NULL;
98 char *maildir, buf[100], **argp;
100 struct msgs_array msgs = { 0, 0, NULL };
101 struct msgs *mp = NULL;
106 if (atexit(freects_done) != 0) {
107 adios(EX_OSERR, NULL, "atexit failed");
110 setlocale(LC_ALL, "");
111 invo_name = mhbasename(argv[0]);
112 if (mh_strcasecmp(invo_name, "next")==0) {
114 } else if (mh_strcasecmp(invo_name, "prev")==0) {
118 /* read user profile/context */
121 arguments = getarguments(invo_name, argc, argv, 1);
127 while ((cp = *argp++)) {
129 switch (smatch(++cp, switches)) {
131 ambigsw(cp, switches);
134 adios(EX_USAGE, NULL, "-%s unknown", cp);
137 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches]", invo_name, mode==SHOW ? "[msgs] " : "");
138 print_help(buf, switches, 1);
139 exit(argc == 2 ? EX_OK : EX_USAGE);
141 print_version(invo_name);
142 exit(argc == 2 ? EX_OK : EX_USAGE);
145 if (!(cp = *argp++) || *cp == '-')
146 adios(EX_USAGE, NULL, "missing argument to %s",
149 adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
154 if (!(cp = *argp++) || *cp == '-')
155 adios(EX_USAGE, NULL, "missing argument to %s",
158 adios(EX_USAGE, NULL, "too many types (starting with %s), %d max", cp, NTYPES);
164 adios(EX_USAGE, NULL, "Either call show as `%s' or use -file", invo_name);
167 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
168 adios(EX_USAGE, NULL, "missing argument to %s",
170 file = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
174 if (!(cp = *argp++) || *cp == '-')
175 adios(EX_USAGE, NULL, "missing argument to %s",
179 formsw = mh_xstrdup(etcpath(cp));
193 if (*cp == '+' || *cp == '@') {
195 adios(EX_USAGE, NULL, "only one folder at a time!");
197 folder = mh_xstrdup(expandfol(cp));
198 } else if (mode != SHOW) {
199 adios(EX_USAGE, NULL, "Either call show as `%s' or give message arguments", invo_name);
201 app_msgarg(&msgs, cp);
205 /* null terminate the list of acceptable parts/types */
211 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
216 ** Check if we've specified an additional profile
218 if ((cp = getenv("MHSHOW"))) {
219 if ((fp = fopen(cp, "r"))) {
220 readconfig((struct node **) 0, fp, cp, 0);
223 admonish("", "unable to read $MHSHOW profile (%s)",
229 ** Read the standard profile setup
231 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
232 readconfig((struct node **) 0, fp, cp, 0);
237 ** Check for storage directory. If specified,
238 ** then store temporary files there. Else we
239 ** store them in standard nmh directory.
241 if ((cp = context_find(nmhstorage)) && *cp)
242 tmp = concat(cp, "/", invo_name, NULL);
244 tmp = mh_xstrdup(toabsdir(invo_name));
246 if (file && msgs.size)
247 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
250 ** check if message is coming from file
253 cts = mh_xcalloc(2, sizeof(*cts));
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 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
308 ** Parse all the SELECTED messages.
310 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
311 if (is_selected(mp, msgnum)) {
314 msgnam = m_name(msgnum);
315 if ((ct = parse_mime(msgnam)))
325 SIGNAL(SIGQUIT, quitser);
326 SIGNAL(SIGPIPE, pipeser);
329 ** Get the associated umask for the relevant contents.
331 for (ctp = cts; *ctp; ctp++) {
335 if (type_ok(ct, 1) && !ct->c_umask) {
336 if (stat(ct->c_file, &st) != NOTOK)
337 ct->c_umask = ~(st.st_mode & 0777);
339 ct->c_umask = ~m_gmprot();
343 if ((ontty = isatty(fileno(stdout)))) {
344 m_popen(defaultpager);
348 ** Show the message content
350 show_all_messages(cts);
356 /* Now free all the structures for the content */
357 for (ctp = cts; *ctp; ctp++)
362 /* If reading from a folder, do some updating */
365 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
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()");