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
80 extern char *tmp; /* directory to place temp files */
85 extern char *cache_public;
86 extern char *cache_private;
91 extern char *parts[NPARTS + 1];
92 extern char *types[NTYPES + 1];
96 * This is currently needed to keep mhparse happy.
97 * This needs to be changed.
104 /* The list of top-level contents to display */
107 #define quitser pipeser
110 CT parse_mime (char *);
113 int part_ok (CT, int);
114 int type_ok (CT, int);
115 void set_endian (void);
116 void flush_errors (void);
119 void list_all_messages (CT *, int, int, int, int);
122 void free_content (CT);
127 static RETSIGTYPE pipeser (int);
131 main (int argc, char **argv)
133 int sizesw = 1, headsw = 1;
134 int nummsgs, maxmsgs, msgnum, *icachesw;
135 char *cp, *file = NULL, *folder = NULL;
136 char *maildir, buf[100], **argp;
137 char **arguments, **msgs;
138 struct msgs *mp = NULL;
142 setlocale(LC_ALL, "");
144 invo_name = r1bindex (argv[0], '/');
146 /* read user profile/context */
149 arguments = getarguments (invo_name, argc, argv, 1);
153 * Allocate the initial space to record message
154 * names, ranges, and sequences.
158 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
159 adios (NULL, "unable to allocate storage");
164 while ((cp = *argp++)) {
166 switch (smatch (++cp, switches)) {
168 ambigsw (cp, switches);
171 adios (NULL, "-%s unknown", cp);
174 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
176 print_help (buf, switches, 1);
179 print_version(invo_name);
183 icachesw = &rcachesw;
186 icachesw = &wcachesw;
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 switch (*icachesw = smatch (cp, caches)) {
192 ambigsw (cp, caches);
195 adios (NULL, "%s unknown", cp);
223 if (!(cp = *argp++) || *cp == '-')
224 adios (NULL, "missing argument to %s", argp[-2]);
226 adios (NULL, "too many parts (starting with %s), %d max",
232 if (!(cp = *argp++) || *cp == '-')
233 adios (NULL, "missing argument to %s", argp[-2]);
235 adios (NULL, "too many types (starting with %s), %d max",
241 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
242 adios (NULL, "missing argument to %s", argp[-2]);
243 file = *cp == '-' ? cp : path (cp, TFILE);
257 if (*cp == '+' || *cp == '@') {
259 adios (NULL, "only one folder at a time!");
261 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
264 * Check if we need to allocate more space
265 * for message names/ranges/sequences.
267 if (nummsgs >= maxmsgs) {
269 if (!(msgs = (char **) realloc (msgs,
270 (size_t) (maxmsgs * sizeof(*msgs)))))
271 adios (NULL, "unable to reallocate msgs storage");
273 msgs[nummsgs++] = cp;
277 /* null terminate the list of acceptable parts/types */
283 /* Check for public cache location */
284 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
287 /* Check for private cache location */
288 if (!(cache_private = context_find (nmhprivcache)))
289 cache_private = ".cache";
290 cache_private = getcpy (m_maildir (cache_private));
293 * Check for storage directory. If specified,
294 * then store temporary files there. Else we
295 * store them in standard nmh directory.
297 if ((cp = context_find (nmhstorage)) && *cp)
298 tmp = concat (cp, "/", invo_name, NULL);
300 tmp = add (m_maildir (invo_name), NULL);
302 if (!context_find ("path"))
303 free (path ("./", TFOLDER));
306 adios (NULL, "cannot specify msg and file at same time!");
309 * check if message is coming from file
312 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
313 adios (NULL, "out of memory");
316 if ((ct = parse_mime (file)));
320 * message(s) are coming from a folder
323 msgs[nummsgs++] = "cur";
325 folder = getfolder (1);
326 maildir = m_maildir (folder);
328 if (chdir (maildir) == NOTOK)
329 adios (maildir, "unable to change directory to");
331 /* read folder and create message structure */
332 if (!(mp = folder_read (folder)))
333 adios (NULL, "unable to read folder %s", folder);
335 /* check for empty folder */
337 adios (NULL, "no messages in %s", folder);
339 /* parse all the message ranges/sequences and set SELECTED */
340 for (msgnum = 0; msgnum < nummsgs; msgnum++)
341 if (!m_convert (mp, msgs[msgnum]))
343 seq_setprev (mp); /* set the previous-sequence */
345 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
346 adios (NULL, "out of memory");
349 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
350 if (is_selected(mp, msgnum)) {
353 msgnam = m_name (msgnum);
354 if ((ct = parse_mime (msgnam)))
364 SIGNAL (SIGQUIT, quitser);
365 SIGNAL (SIGPIPE, pipeser);
368 * Get the associated umask for the relevant contents.
370 for (ctp = cts; *ctp; ctp++) {
374 if (type_ok (ct, 1) && !ct->c_umask) {
375 if (stat (ct->c_file, &st) != NOTOK)
376 ct->c_umask = ~(st.st_mode & 0777);
378 ct->c_umask = ~m_gmprot();
383 * List the message content
385 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
387 /* Now free all the structures for the content */
388 for (ctp = cts; *ctp; ctp++)
394 /* If reading from a folder, do some updating */
396 context_replace (pfolder, folder);/* update current folder */
397 seq_setcur (mp, mp->hghsel); /* update current message */
398 seq_save (mp); /* synchronize sequences */
399 context_save (); /* save the context file */
412 fprintf (stderr, "\n");
431 return 1; /* dead code to satisfy the compiler */