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 */
291 * Check if we've specified an additional profile
293 if ((cp = getenv ("MHSHOW"))) {
294 if ((fp = fopen (cp, "r"))) {
295 readconfig ((struct node **) 0, fp, cp, 0);
298 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
303 * Read the standard profile setup
305 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
306 readconfig ((struct node **) 0, fp, cp, 0);
310 /* Check for public cache location */
311 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
314 /* Check for private cache location */
315 if (!(cache_private = context_find (nmhprivcache)))
316 cache_private = ".cache";
317 cache_private = getcpy (m_maildir (cache_private));
320 * Check for storage directory. If specified,
321 * then store temporary files there. Else we
322 * store them in standard nmh directory.
324 if ((cp = context_find (nmhstorage)) && *cp)
325 tmp = concat (cp, "/", invo_name, NULL);
327 tmp = add (m_maildir (invo_name), NULL);
329 if (!context_find ("path"))
330 free (path ("./", TFOLDER));
332 if (file && msgs.size)
333 adios (NULL, "cannot specify msg and file at same time!");
336 * check if message is coming from file
339 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
340 adios (NULL, "out of memory");
343 if ((ct = parse_mime (file)))
347 * message(s) are coming from a folder
350 app_msgarg(&msgs, "cur");
352 folder = getfolder (1);
353 maildir = m_maildir (folder);
355 if (chdir (maildir) == NOTOK)
356 adios (maildir, "unable to change directory to");
358 /* read folder and create message structure */
359 if (!(mp = folder_read (folder)))
360 adios (NULL, "unable to read folder %s", folder);
362 /* check for empty folder */
364 adios (NULL, "no messages in %s", folder);
366 /* parse all the message ranges/sequences and set SELECTED */
367 for (msgnum = 0; msgnum < msgs.size; msgnum++)
368 if (!m_convert (mp, msgs.msgs[msgnum]))
372 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
373 * since we will use that as a tag to know which messages
374 * to remove from the "unseen" sequence.
376 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
377 if (is_selected(mp, msgnum))
378 set_unseen (mp, msgnum);
380 seq_setprev (mp); /* set the Previous-Sequence */
381 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
383 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
384 adios (NULL, "out of memory");
388 * Parse all the SELECTED messages.
390 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
391 if (is_selected(mp, msgnum)) {
394 msgnam = m_name (msgnum);
395 if ((ct = parse_mime (msgnam)))
405 SIGNAL (SIGQUIT, quitser);
406 SIGNAL (SIGPIPE, pipeser);
409 * Get the associated umask for the relevant contents.
411 for (ctp = cts; *ctp; ctp++) {
415 if (type_ok (ct, 1) && !ct->c_umask) {
416 if (stat (ct->c_file, &st) != NOTOK)
417 ct->c_umask = ~(st.st_mode & 0777);
419 ct->c_umask = ~m_gmprot();
423 /* If reading from a folder, do some updating */
425 context_replace (pfolder, folder);/* update current folder */
426 seq_setcur (mp, mp->hghsel); /* update current message */
427 seq_save (mp); /* synchronize sequences */
428 context_save (); /* save the context file */
432 * Show the message content
434 show_all_messages (cts);
436 /* Now free all the structures for the content */
437 for (ctp = cts; *ctp; ctp++)
454 fprintf (stderr, "\n");