3 * mhlist.c -- list 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 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
27 static struct swit switches[] = {
44 #define FILESW 8 /* interface from show */
49 { "type content", 0 },
51 { "rcache policy", 0 },
53 { "wcache policy", 0 },
60 * switches for debugging
69 extern char *tmp; /* directory to place temp files */
74 extern char *cache_public;
75 extern char *cache_private;
80 extern char *parts[NPARTS + 1];
81 extern char *types[NTYPES + 1];
85 * This is currently needed to keep mhparse happy.
86 * This needs to be changed.
93 #define quitser pipeser
96 CT parse_mime (char *);
99 int part_ok (CT, int);
100 int type_ok (CT, int);
101 void set_endian (void);
102 void flush_errors (void);
105 void list_all_messages (CT *, int, int, int, int);
108 void free_content (CT);
110 void freects_done (int) NORETURN;
115 static RETSIGTYPE pipeser (int);
119 main (int argc, char **argv)
121 int sizesw = 1, headsw = 1;
122 int msgnum, *icachesw;
123 char *cp, *file = NULL, *folder = NULL;
124 char *maildir, buf[100], **argp;
126 struct msgs_array msgs = { 0, 0, NULL };
127 struct msgs *mp = NULL;
133 setlocale(LC_ALL, "");
135 invo_name = r1bindex (argv[0], '/');
137 /* read user profile/context */
140 arguments = getarguments (invo_name, argc, argv, 1);
146 while ((cp = *argp++)) {
148 switch (smatch (++cp, switches)) {
150 ambigsw (cp, switches);
153 adios (NULL, "-%s unknown", cp);
156 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
158 print_help (buf, switches, 1);
161 print_version(invo_name);
165 icachesw = &rcachesw;
168 icachesw = &wcachesw;
170 if (!(cp = *argp++) || *cp == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
172 switch (*icachesw = smatch (cp, caches)) {
174 ambigsw (cp, caches);
177 adios (NULL, "%s unknown", cp);
205 if (!(cp = *argp++) || *cp == '-')
206 adios (NULL, "missing argument to %s", argp[-2]);
208 adios (NULL, "too many parts (starting with %s), %d max",
214 if (!(cp = *argp++) || *cp == '-')
215 adios (NULL, "missing argument to %s", argp[-2]);
217 adios (NULL, "too many types (starting with %s), %d max",
223 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
224 adios (NULL, "missing argument to %s", argp[-2]);
225 file = *cp == '-' ? cp : path (cp, TFILE);
239 if (*cp == '+' || *cp == '@') {
241 adios (NULL, "only one folder at a time!");
243 folder = pluspath (cp);
245 app_msgarg(&msgs, cp);
248 /* null terminate the list of acceptable parts/types */
254 /* Check for public cache location */
255 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
258 /* Check for private cache location */
259 if (!(cache_private = context_find (nmhprivcache)))
260 cache_private = ".cache";
261 cache_private = getcpy (m_maildir (cache_private));
264 * Check for storage directory. If specified,
265 * then store temporary files there. Else we
266 * store them in standard nmh directory.
268 if ((cp = context_find (nmhstorage)) && *cp)
269 tmp = concat (cp, "/", invo_name, NULL);
271 tmp = add (m_maildir (invo_name), NULL);
273 if (!context_find ("path"))
274 free (path ("./", TFOLDER));
276 if (file && msgs.size)
277 adios (NULL, "cannot specify msg and file at same time!");
280 * check if message is coming from file
283 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
284 adios (NULL, "out of memory");
287 if ((ct = parse_mime (file)));
291 * message(s) are coming from a folder
294 app_msgarg(&msgs, "cur");
296 folder = getfolder (1);
297 maildir = m_maildir (folder);
299 if (chdir (maildir) == NOTOK)
300 adios (maildir, "unable to change directory to");
302 /* read folder and create message structure */
303 if (!(mp = folder_read (folder)))
304 adios (NULL, "unable to read folder %s", folder);
306 /* check for empty folder */
308 adios (NULL, "no messages in %s", folder);
310 /* parse all the message ranges/sequences and set SELECTED */
311 for (msgnum = 0; msgnum < msgs.size; msgnum++)
312 if (!m_convert (mp, msgs.msgs[msgnum]))
314 seq_setprev (mp); /* set the previous-sequence */
316 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
317 adios (NULL, "out of memory");
320 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
321 if (is_selected(mp, msgnum)) {
324 msgnam = m_name (msgnum);
325 if ((ct = parse_mime (msgnam)))
335 SIGNAL (SIGQUIT, quitser);
336 SIGNAL (SIGPIPE, pipeser);
339 * Get the associated umask for the relevant contents.
341 for (ctp = cts; *ctp; ctp++) {
345 if (type_ok (ct, 1) && !ct->c_umask) {
346 if (stat (ct->c_file, &st) != NOTOK)
347 ct->c_umask = ~(st.st_mode & 0777);
349 ct->c_umask = ~m_gmprot();
354 * List the message content
356 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
358 /* Now free all the structures for the content */
359 for (ctp = cts; *ctp; ctp++)
365 /* If reading from a folder, do some updating */
367 context_replace (pfolder, folder);/* update current folder */
368 seq_setcur (mp, mp->hghsel); /* update current message */
369 seq_save (mp); /* synchronize sequences */
370 context_save (); /* save the context file */
384 fprintf (stderr, "\n");