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];
59 #define quitser pipeser
62 CT parse_mime(char *);
67 void set_endian(void);
68 void flush_errors(void);
71 void show_all_messages(CT *);
74 void free_content(CT);
76 void freects_done(int) NORETURN;
81 static void pipeser(int);
82 static void m_popen(char *);
83 static void m_pclose(void);
87 main(int argc, char **argv)
90 char *cp, *file = NULL, *folder = NULL;
91 char *maildir, buf[100], **argp;
93 struct msgs_array msgs = { 0, 0, NULL };
94 struct msgs *mp = NULL;
102 setlocale(LC_ALL, "");
104 invo_name = mhbasename(argv[0]);
106 /* read user profile/context */
109 arguments = getarguments(invo_name, argc, argv, 1);
115 while ((cp = *argp++)) {
117 switch (smatch(++cp, switches)) {
119 ambigsw(cp, switches);
122 adios(NULL, "-%s unknown", cp);
125 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
126 print_help(buf, switches, 1);
129 print_version(invo_name);
133 if (!(cp = *argp++) || *cp == '-')
134 adios(NULL, "missing argument to %s",
137 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
142 if (!(cp = *argp++) || *cp == '-')
143 adios(NULL, "missing argument to %s",
146 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
151 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
152 adios(NULL, "missing argument to %s",
154 file = *cp == '-' ? cp : getcpy(expanddir(cp));
158 if (!(cp = *argp++) || *cp == '-')
159 adios(NULL, "missing argument to %s",
163 formsw = getcpy(etcpath(cp));
177 if (*cp == '+' || *cp == '@') {
179 adios(NULL, "only one folder at a time!");
181 folder = getcpy(expandfol(cp));
183 app_msgarg(&msgs, cp);
186 /* null terminate the list of acceptable parts/types */
192 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
197 ** Check if we've specified an additional profile
199 if ((cp = getenv("MHSHOW"))) {
200 if ((fp = fopen(cp, "r"))) {
201 readconfig((struct node **) 0, fp, cp, 0);
204 admonish("", "unable to read $MHSHOW profile (%s)",
210 ** Read the standard profile setup
212 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
213 readconfig((struct node **) 0, fp, cp, 0);
218 ** Check for storage directory. If specified,
219 ** then store temporary files there. Else we
220 ** store them in standard nmh directory.
222 if ((cp = context_find(nmhstorage)) && *cp)
223 tmp = concat(cp, "/", invo_name, NULL);
225 tmp = getcpy(toabsdir(invo_name));
227 if (file && msgs.size)
228 adios(NULL, "cannot specify msg and file at same time!");
231 ** check if message is coming from file
234 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
235 adios(NULL, "out of memory");
238 if ((ct = parse_mime(file)))
242 ** message(s) are coming from a folder
245 app_msgarg(&msgs, seq_cur);
247 folder = getcurfol();
248 maildir = toabsdir(folder);
250 if (chdir(maildir) == NOTOK)
251 adios(maildir, "unable to change directory to");
253 /* read folder and create message structure */
254 if (!(mp = folder_read(folder)))
255 adios(NULL, "unable to read folder %s", folder);
257 /* check for empty folder */
259 adios(NULL, "no messages in %s", folder);
261 /* parse all the message ranges/sequences and set SELECTED */
262 for (msgnum = 0; msgnum < msgs.size; msgnum++)
263 if (!m_convert(mp, msgs.msgs[msgnum]))
267 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
268 ** since we will use that as a tag to know which messages
269 ** to remove from the "unseen" sequence.
271 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
272 if (is_selected(mp, msgnum))
273 set_unseen(mp, msgnum);
275 seq_setprev(mp); /* set the Previous-Sequence */
276 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
278 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
280 adios(NULL, "out of memory");
284 ** Parse all the SELECTED messages.
286 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
287 if (is_selected(mp, msgnum)) {
290 msgnam = m_name(msgnum);
291 if ((ct = parse_mime(msgnam)))
301 SIGNAL(SIGQUIT, quitser);
302 SIGNAL(SIGPIPE, pipeser);
305 ** Get the associated umask for the relevant contents.
307 for (ctp = cts; *ctp; ctp++) {
311 if (type_ok(ct, 1) && !ct->c_umask) {
312 if (stat(ct->c_file, &st) != NOTOK)
313 ct->c_umask = ~(st.st_mode & 0777);
315 ct->c_umask = ~m_gmprot();
319 if ((ontty = isatty(fileno(stdout)))) {
320 m_popen(defaultpager);
324 ** Show the message content
326 show_all_messages(cts);
332 /* Now free all the structures for the content */
333 for (ctp = cts; *ctp; ctp++)
339 /* If reading from a folder, do some updating */
341 context_replace(curfolder, folder); /* update current folder */
342 seq_setcur(mp, mp->hghsel); /* update current message */
343 seq_save(mp); /* synchronize sequences */
344 context_save(); /* save the context file */
358 fprintf(stderr, "\n");
367 static int m_pid = NOTOK;
368 static int sd = NOTOK;
376 if ((sd = dup(fileno(stdout))) == NOTOK)
377 adios("standard output", "unable to dup()");
379 if (pipe(pd) == NOTOK)
380 adios("pipe", "unable to");
382 switch (m_pid = fork()) {
384 adios("fork", "unable to");
387 SIGNAL(SIGINT, SIG_DFL);
388 SIGNAL(SIGQUIT, SIG_DFL);
391 if (pd[0] != fileno(stdin)) {
392 dup2(pd[0], fileno(stdin));
395 execlp(name, mhbasename(name), NULL);
396 fprintf(stderr, "unable to exec ");
402 if (pd[1] != fileno(stdout)) {
403 dup2(pd[1], fileno(stdout));
418 if (dup2(sd, fileno(stdout)) == NOTOK)
419 adios("standard output", "unable to dup2()");