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 flush_errors (void);
116 void show_all_messages (CT *);
119 void free_content (CT);
121 void freects_done (int) NORETURN;
126 static void pipeser (int);
130 main (int argc, char **argv)
132 int msgnum, *icachesw;
133 char *cp, *file = NULL, *folder = NULL;
134 char *maildir, buf[100], **argp;
136 struct msgs_array msgs = { 0, 0, NULL };
137 struct msgs *mp = NULL;
144 setlocale(LC_ALL, "");
146 invo_name = r1bindex (argv[0], '/');
148 /* read user profile/context */
151 arguments = getarguments (invo_name, argc, argv, 1);
157 while ((cp = *argp++)) {
159 switch (smatch (++cp, switches)) {
161 ambigsw (cp, switches);
164 adios (NULL, "-%s unknown", cp);
167 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
169 print_help (buf, switches, 1);
172 print_version(invo_name);
176 icachesw = &rcachesw;
179 icachesw = &wcachesw;
181 if (!(cp = *argp++) || *cp == '-')
182 adios (NULL, "missing argument to %s", argp[-2]);
183 switch (*icachesw = smatch (cp, caches)) {
185 ambigsw (cp, caches);
188 adios (NULL, "%s unknown", cp);
216 if (!(cp = *argp++) || *cp == '-')
217 adios (NULL, "missing argument to %s", argp[-2]);
219 adios (NULL, "too many parts (starting with %s), %d max",
225 if (!(cp = *argp++) || *cp == '-')
226 adios (NULL, "missing argument to %s", argp[-2]);
228 adios (NULL, "too many types (starting with %s), %d max",
234 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
235 adios (NULL, "missing argument to %s", argp[-2]);
236 file = *cp == '-' ? cp : path (cp, TFILE);
240 if (!(cp = *argp++) || *cp == '-')
241 adios (NULL, "missing argument to %s", argp[-2]);
244 formsw = getcpy (etcpath (cp));
248 * Switches for moreproc/mhlproc
251 if (!(progsw = *argp++) || *progsw == '-')
252 adios (NULL, "missing argument to %s", argp[-2]);
260 if (!(cp = *argp++) || *cp == '-')
261 adios (NULL, "missing argument to %s", argp[-2]);
275 if (*cp == '+' || *cp == '@') {
277 adios (NULL, "only one folder at a time!");
279 folder = pluspath (cp);
281 app_msgarg(&msgs, cp);
284 /* null terminate the list of acceptable parts/types */
290 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
296 * Check if we've specified an additional profile
298 if ((cp = getenv ("MHSHOW"))) {
299 if ((fp = fopen (cp, "r"))) {
300 readconfig ((struct node **) 0, fp, cp, 0);
303 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
308 * Read the standard profile setup
310 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
311 readconfig ((struct node **) 0, fp, cp, 0);
315 /* Check for public cache location */
316 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
319 /* Check for private cache location */
320 if (!(cache_private = context_find (nmhprivcache)))
321 cache_private = ".cache";
322 cache_private = getcpy (m_maildir (cache_private));
325 * Check for storage directory. If specified,
326 * then store temporary files there. Else we
327 * store them in standard nmh directory.
329 if ((cp = context_find (nmhstorage)) && *cp)
330 tmp = concat (cp, "/", invo_name, NULL);
332 tmp = add (m_maildir (invo_name), NULL);
334 if (!context_find ("path"))
335 free (path ("./", TFOLDER));
337 if (file && msgs.size)
338 adios (NULL, "cannot specify msg and file at same time!");
341 * check if message is coming from file
344 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
345 adios (NULL, "out of memory");
348 if ((ct = parse_mime (file)))
352 * message(s) are coming from a folder
355 app_msgarg(&msgs, "cur");
357 folder = getfolder (1);
358 maildir = m_maildir (folder);
360 if (chdir (maildir) == NOTOK)
361 adios (maildir, "unable to change directory to");
363 /* read folder and create message structure */
364 if (!(mp = folder_read (folder)))
365 adios (NULL, "unable to read folder %s", folder);
367 /* check for empty folder */
369 adios (NULL, "no messages in %s", folder);
371 /* parse all the message ranges/sequences and set SELECTED */
372 for (msgnum = 0; msgnum < msgs.size; msgnum++)
373 if (!m_convert (mp, msgs.msgs[msgnum]))
377 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
378 * since we will use that as a tag to know which messages
379 * to remove from the "unseen" sequence.
381 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
382 if (is_selected(mp, msgnum))
383 set_unseen (mp, msgnum);
385 seq_setprev (mp); /* set the Previous-Sequence */
386 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
388 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
389 adios (NULL, "out of memory");
393 * Parse all the SELECTED messages.
395 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
396 if (is_selected(mp, msgnum)) {
399 msgnam = m_name (msgnum);
400 if ((ct = parse_mime (msgnam)))
410 SIGNAL (SIGQUIT, quitser);
411 SIGNAL (SIGPIPE, pipeser);
414 * Get the associated umask for the relevant contents.
416 for (ctp = cts; *ctp; ctp++) {
420 if (type_ok (ct, 1) && !ct->c_umask) {
421 if (stat (ct->c_file, &st) != NOTOK)
422 ct->c_umask = ~(st.st_mode & 0777);
424 ct->c_umask = ~m_gmprot();
429 * Show the message content
431 show_all_messages (cts);
433 /* Now free all the structures for the content */
434 for (ctp = cts; *ctp; ctp++)
440 /* If reading from a folder, do some updating */
442 context_replace (pfolder, folder);/* update current folder */
443 seq_setcur (mp, mp->hghsel); /* update current message */
444 seq_save (mp); /* synchronize sequences */
445 context_save (); /* save the context file */
459 fprintf (stderr, "\n");