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>
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
29 * We allocate space for message names (msgs array)
30 * this number of elements at a time.
35 static struct swit switches[] = {
52 #define FILESW 8 /* interface from show */
57 { "type content", 0 },
59 { "rcache policy", 0 },
61 { "wcache policy", 0 },
68 * switches for debugging
78 extern char *tmp; /* directory to place temp files */
83 extern char *cache_public;
84 extern char *cache_private;
89 extern char *parts[NPARTS + 1];
90 extern char *types[NTYPES + 1];
94 * This is currently needed to keep mhparse happy.
95 * This needs to be changed.
102 /* The list of top-level contents to display */
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 list_all_messages (CT *, int, int, int, int);
120 void free_content (CT);
125 static RETSIGTYPE pipeser (int);
129 main (int argc, char **argv)
131 int sizesw = 1, headsw = 1;
132 int nummsgs, maxmsgs, msgnum, *icachesw;
133 char *cp, *file = NULL, *folder = NULL;
134 char *maildir, buf[100], **argp;
135 char **arguments, **msgs;
136 struct msgs *mp = NULL;
140 setlocale(LC_ALL, "");
142 invo_name = r1bindex (argv[0], '/');
144 /* read user profile/context */
147 arguments = getarguments (invo_name, argc, argv, 1);
151 * Allocate the initial space to record message
152 * names, ranges, and sequences.
156 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
157 adios (NULL, "unable to allocate storage");
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 if (!(msgs = (char **) realloc (msgs,
268 (size_t) (maxmsgs * sizeof(*msgs)))))
269 adios (NULL, "unable to reallocate msgs storage");
271 msgs[nummsgs++] = cp;
275 /* null terminate the list of acceptable parts/types */
281 /* Check for public cache location */
282 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
285 /* Check for private cache location */
286 if (!(cache_private = context_find (nmhprivcache)))
287 cache_private = ".cache";
288 cache_private = getcpy (m_maildir (cache_private));
291 * Check for storage directory. If specified,
292 * then store temporary files there. Else we
293 * store them in standard nmh directory.
295 if ((cp = context_find (nmhstorage)) && *cp)
296 tmp = concat (cp, "/", invo_name, NULL);
298 tmp = add (m_maildir (invo_name), NULL);
300 if (!context_find ("path"))
301 free (path ("./", TFOLDER));
304 adios (NULL, "cannot specify msg and file at same time!");
307 * check if message is coming from file
310 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
311 adios (NULL, "out of memory");
314 if ((ct = parse_mime (file)));
318 * message(s) are coming from a folder
321 msgs[nummsgs++] = "cur";
323 folder = getfolder (1);
324 maildir = m_maildir (folder);
326 if (chdir (maildir) == NOTOK)
327 adios (maildir, "unable to change directory to");
329 /* read folder and create message structure */
330 if (!(mp = folder_read (folder)))
331 adios (NULL, "unable to read folder %s", folder);
333 /* check for empty folder */
335 adios (NULL, "no messages in %s", folder);
337 /* parse all the message ranges/sequences and set SELECTED */
338 for (msgnum = 0; msgnum < nummsgs; msgnum++)
339 if (!m_convert (mp, msgs[msgnum]))
341 seq_setprev (mp); /* set the previous-sequence */
343 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
344 adios (NULL, "out of memory");
347 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
348 if (is_selected(mp, msgnum)) {
351 msgnam = m_name (msgnum);
352 if ((ct = parse_mime (msgnam)))
362 SIGNAL (SIGQUIT, quitser);
363 SIGNAL (SIGPIPE, pipeser);
366 * Get the associated umask for the relevant contents.
368 for (ctp = cts; *ctp; ctp++) {
372 if (type_ok (ct, 1) && !ct->c_umask) {
373 if (stat (ct->c_file, &st) != NOTOK)
374 ct->c_umask = ~(st.st_mode & 0777);
376 ct->c_umask = ~m_gmprot();
381 * List the message content
383 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
385 /* Now free all the structures for the content */
386 for (ctp = cts; *ctp; ctp++)
392 /* If reading from a folder, do some updating */
394 context_replace (pfolder, folder);/* update current folder */
395 seq_setcur (mp, mp->hghsel); /* update current message */
396 seq_save (mp); /* synchronize sequences */
397 context_save (); /* save the context file */
410 fprintf (stderr, "\n");
429 return 1; /* dead code to satisfy the compiler */