3 * mhshow.c -- display the contents of MIME messages
10 #include <h/signals.h>
14 #include <zotnet/mts/mts.h>
15 #include <zotnet/tws/tws.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
25 * We allocate space for message names (msgs array)
26 * this number of elements at a time.
31 static struct swit switches[] = {
43 { "noserialonly", 0 },
48 #define FILESW 8 /* interface from show */
51 { "form formfile", 0 },
55 { "type content", 0 },
57 { "rcache policy", 0 },
59 { "wcache policy", 0 },
66 * switches for moreproc/mhlproc
69 { "moreproc program", -4 },
73 { "length lines", -4 },
75 { "width columns", -4 },
78 * switches for debugging
90 extern char *tmp; /* directory to place temp files */
95 extern char *cache_public;
96 extern char *cache_private;
103 extern int nomore; /* flags for moreproc/header display */
109 extern char *parts[NPARTS + 1];
110 extern char *types[NTYPES + 1];
116 /* The list of top-level contents to display */
119 #define quitser pipeser
122 CT parse_mime (char *);
125 int part_ok (CT, int);
126 int type_ok (CT, int);
127 void set_endian (void);
128 void flush_errors (void);
131 void show_all_messages (CT *);
134 void free_content (CT);
139 static RETSIGTYPE pipeser (int);
143 main (int argc, char **argv)
145 int nummsgs, maxmsgs, msgnum, *icachesw;
146 char *cp, *file = NULL, *folder = NULL;
147 char *maildir, buf[100], **argp;
148 char **arguments, **msgs;
149 struct msgs *mp = NULL;
154 setlocale(LC_ALL, "");
156 invo_name = r1bindex (argv[0], '/');
158 /* read user profile/context */
161 arguments = getarguments (invo_name, argc, argv, 1);
165 * Allocate the initial space to record message
166 * names, ranges, and sequences.
170 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
171 adios (NULL, "unable to allocate storage");
176 while ((cp = *argp++)) {
178 switch (smatch (++cp, switches)) {
180 ambigsw (cp, switches);
183 adios (NULL, "-%s unknown", cp);
186 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
188 print_help (buf, switches, 1);
191 print_version(invo_name);
195 icachesw = &rcachesw;
198 icachesw = &wcachesw;
200 if (!(cp = *argp++) || *cp == '-')
201 adios (NULL, "missing argument to %s", argp[-2]);
202 switch (*icachesw = smatch (cp, caches)) {
204 ambigsw (cp, caches);
207 adios (NULL, "%s unknown", cp);
235 if (!(cp = *argp++) || *cp == '-')
236 adios (NULL, "missing argument to %s", argp[-2]);
238 adios (NULL, "too many parts (starting with %s), %d max",
244 if (!(cp = *argp++) || *cp == '-')
245 adios (NULL, "missing argument to %s", argp[-2]);
247 adios (NULL, "too many types (starting with %s), %d max",
253 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
254 adios (NULL, "missing argument to %s", argp[-2]);
255 file = *cp == '-' ? cp : path (cp, TFILE);
259 if (!(cp = *argp++) || *cp == '-')
260 adios (NULL, "missing argument to %s", argp[-2]);
263 formsw = getcpy (etcpath (cp));
267 * Switches for moreproc/mhlproc
270 if (!(progsw = *argp++) || *progsw == '-')
271 adios (NULL, "missing argument to %s", argp[-2]);
279 if (!(cp = *argp++) || *cp == '-')
280 adios (NULL, "missing argument to %s", argp[-2]);
294 if (*cp == '+' || *cp == '@') {
296 adios (NULL, "only one folder at a time!");
298 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
301 * Check if we need to allocate more space
302 * for message names/ranges/sequences.
304 if (nummsgs >= maxmsgs) {
306 if (!(msgs = (char **) realloc (msgs,
307 (size_t) (maxmsgs * sizeof(*msgs)))))
308 adios (NULL, "unable to reallocate msgs storage");
310 msgs[nummsgs++] = cp;
314 /* null terminate the list of acceptable parts/types */
320 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
326 * Check if we've specified an additional profile
328 if ((cp = getenv ("MHSHOW"))) {
329 if ((fp = fopen (cp, "r"))) {
330 readconfig ((struct node **) 0, fp, cp, 0);
333 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
338 * Read the standard profile setup
340 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
341 readconfig ((struct node **) 0, fp, cp, 0);
345 /* Check for public cache location */
346 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
349 /* Check for private cache location */
350 if (!(cache_private = context_find (nmhprivcache)))
351 cache_private = ".cache";
352 cache_private = getcpy (m_maildir (cache_private));
355 * Check for storage directory. If specified,
356 * then store temporary files there. Else we
357 * store them in standard nmh directory.
359 if ((cp = context_find (nmhstorage)) && *cp)
360 tmp = concat (cp, "/", invo_name, NULL);
362 tmp = add (m_maildir (invo_name), NULL);
364 if (!context_find ("path"))
365 free (path ("./", TFOLDER));
368 adios (NULL, "cannot specify msg and file at same time!");
371 * check if message is coming from file
374 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
375 adios (NULL, "out of memory");
378 if ((ct = parse_mime (file)));
382 * message(s) are coming from a folder
385 msgs[nummsgs++] = "cur";
387 folder = getfolder (1);
388 maildir = m_maildir (folder);
390 if (chdir (maildir) == NOTOK)
391 adios (maildir, "unable to change directory to");
393 /* read folder and create message structure */
394 if (!(mp = folder_read (folder)))
395 adios (NULL, "unable to read folder %s", folder);
397 /* check for empty folder */
399 adios (NULL, "no messages in %s", folder);
401 /* parse all the message ranges/sequences and set SELECTED */
402 for (msgnum = 0; msgnum < nummsgs; msgnum++)
403 if (!m_convert (mp, msgs[msgnum]))
407 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
408 * since we will use that as a tag to know which messages
409 * to remove from the "unseen" sequence.
411 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
412 if (is_selected(mp, msgnum))
413 set_unseen (mp, msgnum);
415 seq_setprev (mp); /* set the Previous-Sequence */
416 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
418 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
419 adios (NULL, "out of memory");
423 * Parse all the SELECTED messages.
425 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
426 if (is_selected(mp, msgnum)) {
429 msgnam = m_name (msgnum);
430 if ((ct = parse_mime (msgnam)))
440 SIGNAL (SIGQUIT, quitser);
441 SIGNAL (SIGPIPE, pipeser);
444 * Get the associated umask for the relevant contents.
446 for (ctp = cts; *ctp; ctp++) {
450 if (type_ok (ct, 1) && !ct->c_umask) {
451 if (stat (ct->c_file, &st) != NOTOK)
452 ct->c_umask = ~(st.st_mode & 0777);
454 ct->c_umask = ~m_gmprot();
459 * Show the message content
461 show_all_messages (cts);
463 /* Now free all the structures for the content */
464 for (ctp = cts; *ctp; ctp++)
470 /* If reading from a folder, do some updating */
472 context_replace (pfolder, folder);/* update current folder */
473 seq_setcur (mp, mp->hghsel); /* update current message */
474 seq_save (mp); /* synchronize sequences */
475 context_save (); /* save the context file */
488 fprintf (stderr, "\n");
507 return 1; /* dead code to satisfy the compiler */