3 * mhshow.c -- display the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 static struct swit switches[] = {
35 { "noserialonly", 0 },
40 #define FILESW 8 /* interface from show */
43 { "form formfile", 0 },
47 { "type content", 0 },
49 { "rcache policy", 0 },
51 { "wcache policy", 0 },
58 * switches for moreproc/mhlproc
61 { "moreproc program", -4 },
65 { "length lines", -4 },
67 { "width columns", -4 },
70 * switches for debugging
79 extern char *tmp; /* directory to place temp files */
84 extern char *cache_public;
85 extern char *cache_private;
92 extern int nomore; /* flags for moreproc/header display */
98 extern char *parts[NPARTS + 1];
99 extern char *types[NTYPES + 1];
105 #define quitser pipeser
108 CT parse_mime (char *);
111 int part_ok (CT, int);
112 int type_ok (CT, int);
113 void set_endian (void);
114 void flush_errors (void);
117 void show_all_messages (CT *);
120 void free_content (CT);
122 void freects_done (int) NORETURN;
127 static void pipeser (int);
131 main (int argc, char **argv)
133 int msgnum, *icachesw;
134 char *cp, *file = NULL, *folder = NULL;
135 char *maildir, buf[100], **argp;
137 struct msgs_array msgs = { 0, 0, NULL };
138 struct msgs *mp = NULL;
145 setlocale(LC_ALL, "");
147 invo_name = r1bindex (argv[0], '/');
149 /* read user profile/context */
152 arguments = getarguments (invo_name, argc, argv, 1);
158 while ((cp = *argp++)) {
160 switch (smatch (++cp, switches)) {
162 ambigsw (cp, switches);
165 adios (NULL, "-%s unknown", cp);
168 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
170 print_help (buf, switches, 1);
173 print_version(invo_name);
177 icachesw = &rcachesw;
180 icachesw = &wcachesw;
182 if (!(cp = *argp++) || *cp == '-')
183 adios (NULL, "missing argument to %s", argp[-2]);
184 switch (*icachesw = smatch (cp, caches)) {
186 ambigsw (cp, caches);
189 adios (NULL, "%s unknown", cp);
217 if (!(cp = *argp++) || *cp == '-')
218 adios (NULL, "missing argument to %s", argp[-2]);
220 adios (NULL, "too many parts (starting with %s), %d max",
226 if (!(cp = *argp++) || *cp == '-')
227 adios (NULL, "missing argument to %s", argp[-2]);
229 adios (NULL, "too many types (starting with %s), %d max",
235 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
236 adios (NULL, "missing argument to %s", argp[-2]);
237 file = *cp == '-' ? cp : path (cp, TFILE);
241 if (!(cp = *argp++) || *cp == '-')
242 adios (NULL, "missing argument to %s", argp[-2]);
245 formsw = getcpy (etcpath (cp));
249 * Switches for moreproc/mhlproc
252 if (!(progsw = *argp++) || *progsw == '-')
253 adios (NULL, "missing argument to %s", argp[-2]);
261 if (!(cp = *argp++) || *cp == '-')
262 adios (NULL, "missing argument to %s", argp[-2]);
276 if (*cp == '+' || *cp == '@') {
278 adios (NULL, "only one folder at a time!");
280 folder = pluspath (cp);
282 app_msgarg(&msgs, cp);
285 /* null terminate the list of acceptable parts/types */
291 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
297 * Check if we've specified an additional profile
299 if ((cp = getenv ("MHSHOW"))) {
300 if ((fp = fopen (cp, "r"))) {
301 readconfig ((struct node **) 0, fp, cp, 0);
304 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
309 * Read the standard profile setup
311 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
312 readconfig ((struct node **) 0, fp, cp, 0);
316 /* Check for public cache location */
317 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
320 /* Check for private cache location */
321 if (!(cache_private = context_find (nmhprivcache)))
322 cache_private = ".cache";
323 cache_private = getcpy (m_maildir (cache_private));
326 * Check for storage directory. If specified,
327 * then store temporary files there. Else we
328 * store them in standard nmh directory.
330 if ((cp = context_find (nmhstorage)) && *cp)
331 tmp = concat (cp, "/", invo_name, NULL);
333 tmp = add (m_maildir (invo_name), NULL);
335 if (!context_find ("path"))
336 free (path ("./", TFOLDER));
338 if (file && msgs.size)
339 adios (NULL, "cannot specify msg and file at same time!");
342 * check if message is coming from file
345 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
346 adios (NULL, "out of memory");
349 if ((ct = parse_mime (file)))
353 * message(s) are coming from a folder
356 app_msgarg(&msgs, "cur");
358 folder = getfolder (1);
359 maildir = m_maildir (folder);
361 if (chdir (maildir) == NOTOK)
362 adios (maildir, "unable to change directory to");
364 /* read folder and create message structure */
365 if (!(mp = folder_read (folder)))
366 adios (NULL, "unable to read folder %s", folder);
368 /* check for empty folder */
370 adios (NULL, "no messages in %s", folder);
372 /* parse all the message ranges/sequences and set SELECTED */
373 for (msgnum = 0; msgnum < msgs.size; msgnum++)
374 if (!m_convert (mp, msgs.msgs[msgnum]))
378 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
379 * since we will use that as a tag to know which messages
380 * to remove from the "unseen" sequence.
382 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
383 if (is_selected(mp, msgnum))
384 set_unseen (mp, msgnum);
386 seq_setprev (mp); /* set the Previous-Sequence */
387 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
389 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
390 adios (NULL, "out of memory");
394 * Parse all the SELECTED messages.
396 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
397 if (is_selected(mp, msgnum)) {
400 msgnam = m_name (msgnum);
401 if ((ct = parse_mime (msgnam)))
411 SIGNAL (SIGQUIT, quitser);
412 SIGNAL (SIGPIPE, pipeser);
415 * Get the associated umask for the relevant contents.
417 for (ctp = cts; *ctp; ctp++) {
421 if (type_ok (ct, 1) && !ct->c_umask) {
422 if (stat (ct->c_file, &st) != NOTOK)
423 ct->c_umask = ~(st.st_mode & 0777);
425 ct->c_umask = ~m_gmprot();
430 * Show the message content
432 show_all_messages (cts);
434 /* Now free all the structures for the content */
435 for (ctp = cts; *ctp; ctp++)
441 /* If reading from a folder, do some updating */
443 context_replace (pfolder, folder);/* update current folder */
444 seq_setcur (mp, mp->hghsel); /* update current message */
445 seq_save (mp); /* synchronize sequences */
446 context_save (); /* save the context file */
460 fprintf (stderr, "\n");