3 * mhlist.c -- list the contents of MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
30 * We allocate space for message names (msgs array)
31 * this number of elements at a time.
36 static struct swit switches[] = {
53 #define FILESW 8 /* interface from show */
58 { "type content", 0 },
60 { "rcache policy", 0 },
62 { "wcache policy", 0 },
69 * switches for debugging
79 extern char *tmp; /* directory to place temp files */
84 extern char *cache_public;
85 extern char *cache_private;
90 extern char *parts[NPARTS + 1];
91 extern char *types[NTYPES + 1];
95 * This is currently needed to keep mhparse happy.
96 * This needs to be changed.
103 /* The list of top-level contents to display */
106 #define quitser pipeser
109 CT parse_mime (char *);
112 int part_ok (CT, int);
113 int type_ok (CT, int);
114 void set_endian (void);
115 void flush_errors (void);
118 void list_all_messages (CT *, int, int, int, int);
121 void free_content (CT);
126 static RETSIGTYPE pipeser (int);
130 main (int argc, char **argv)
132 int sizesw = 1, headsw = 1;
133 int nummsgs, maxmsgs, msgnum, *icachesw;
134 char *cp, *file = NULL, *folder = NULL;
135 char *maildir, buf[100], **argp;
136 char **arguments, **msgs;
137 struct msgs *mp = NULL;
141 setlocale(LC_ALL, "");
143 invo_name = r1bindex (argv[0], '/');
145 /* read user profile/context */
148 arguments = getarguments (invo_name, argc, argv, 1);
152 * Allocate the initial space to record message
153 * names, ranges, and sequences.
157 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
162 while ((cp = *argp++)) {
164 switch (smatch (++cp, switches)) {
166 ambigsw (cp, switches);
169 adios (NULL, "-%s unknown", cp);
172 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
174 print_help (buf, switches, 1);
177 print_version(invo_name);
181 icachesw = &rcachesw;
184 icachesw = &wcachesw;
186 if (!(cp = *argp++) || *cp == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
188 switch (*icachesw = smatch (cp, caches)) {
190 ambigsw (cp, caches);
193 adios (NULL, "%s unknown", cp);
221 if (!(cp = *argp++) || *cp == '-')
222 adios (NULL, "missing argument to %s", argp[-2]);
224 adios (NULL, "too many parts (starting with %s), %d max",
230 if (!(cp = *argp++) || *cp == '-')
231 adios (NULL, "missing argument to %s", argp[-2]);
233 adios (NULL, "too many types (starting with %s), %d max",
239 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
240 adios (NULL, "missing argument to %s", argp[-2]);
241 file = *cp == '-' ? cp : path (cp, TFILE);
255 if (*cp == '+' || *cp == '@') {
257 adios (NULL, "only one folder at a time!");
259 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
262 * Check if we need to allocate more space
263 * for message names/ranges/sequences.
265 if (nummsgs >= maxmsgs) {
267 msgs = (char **) mh_xrealloc (msgs,
268 (size_t) (maxmsgs * sizeof(*msgs)));
270 msgs[nummsgs++] = cp;
274 /* null terminate the list of acceptable parts/types */
280 /* Check for public cache location */
281 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
284 /* Check for private cache location */
285 if (!(cache_private = context_find (nmhprivcache)))
286 cache_private = ".cache";
287 cache_private = getcpy (m_maildir (cache_private));
290 * Check for storage directory. If specified,
291 * then store temporary files there. Else we
292 * store them in standard nmh directory.
294 if ((cp = context_find (nmhstorage)) && *cp)
295 tmp = concat (cp, "/", invo_name, NULL);
297 tmp = add (m_maildir (invo_name), NULL);
299 if (!context_find ("path"))
300 free (path ("./", TFOLDER));
303 adios (NULL, "cannot specify msg and file at same time!");
306 * check if message is coming from file
309 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
310 adios (NULL, "out of memory");
313 if ((ct = parse_mime (file)));
317 * message(s) are coming from a folder
320 msgs[nummsgs++] = "cur";
322 folder = getfolder (1);
323 maildir = m_maildir (folder);
325 if (chdir (maildir) == NOTOK)
326 adios (maildir, "unable to change directory to");
328 /* read folder and create message structure */
329 if (!(mp = folder_read (folder)))
330 adios (NULL, "unable to read folder %s", folder);
332 /* check for empty folder */
334 adios (NULL, "no messages in %s", folder);
336 /* parse all the message ranges/sequences and set SELECTED */
337 for (msgnum = 0; msgnum < nummsgs; msgnum++)
338 if (!m_convert (mp, msgs[msgnum]))
340 seq_setprev (mp); /* set the previous-sequence */
342 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
343 adios (NULL, "out of memory");
346 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
347 if (is_selected(mp, msgnum)) {
350 msgnam = m_name (msgnum);
351 if ((ct = parse_mime (msgnam)))
361 SIGNAL (SIGQUIT, quitser);
362 SIGNAL (SIGPIPE, pipeser);
365 * Get the associated umask for the relevant contents.
367 for (ctp = cts; *ctp; ctp++) {
371 if (type_ok (ct, 1) && !ct->c_umask) {
372 if (stat (ct->c_file, &st) != NOTOK)
373 ct->c_umask = ~(st.st_mode & 0777);
375 ct->c_umask = ~m_gmprot();
380 * List the message content
382 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
384 /* Now free all the structures for the content */
385 for (ctp = cts; *ctp; ctp++)
391 /* If reading from a folder, do some updating */
393 context_replace (pfolder, folder);/* update current folder */
394 seq_setcur (mp, mp->hghsel); /* update current message */
395 seq_save (mp); /* synchronize sequences */
396 context_save (); /* save the context file */
409 fprintf (stderr, "\n");
428 return 1; /* dead code to satisfy the compiler */