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 static struct swit switches[] = {
40 #define FILESW 8 /* interface from show */
45 { "type content", 0 },
47 { "rcache policy", 0 },
49 { "wcache policy", 0 },
56 * switches for debugging
65 extern char *tmp; /* directory to place temp files */
70 extern char *cache_public;
71 extern char *cache_private;
76 extern char *parts[NPARTS + 1];
77 extern char *types[NTYPES + 1];
81 * This is currently needed to keep mhparse happy.
82 * This needs to be changed.
89 #define quitser pipeser
92 CT parse_mime (char *);
95 int part_ok (CT, int);
96 int type_ok (CT, int);
97 void set_endian (void);
98 void flush_errors (void);
101 void list_all_messages (CT *, int, int, int, int);
104 void free_content (CT);
106 void freects_done (int) NORETURN;
111 static void pipeser (int);
115 main (int argc, char **argv)
117 int sizesw = 1, headsw = 1;
118 int msgnum, *icachesw;
119 char *cp, *file = NULL, *folder = NULL;
120 char *maildir, buf[100], **argp;
122 struct msgs_array msgs = { 0, 0, NULL };
123 struct msgs *mp = NULL;
129 setlocale(LC_ALL, "");
131 invo_name = r1bindex (argv[0], '/');
133 /* read user profile/context */
136 arguments = getarguments (invo_name, argc, argv, 1);
142 while ((cp = *argp++)) {
144 switch (smatch (++cp, switches)) {
146 ambigsw (cp, switches);
149 adios (NULL, "-%s unknown", cp);
152 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
154 print_help (buf, switches, 1);
157 print_version(invo_name);
161 icachesw = &rcachesw;
164 icachesw = &wcachesw;
166 if (!(cp = *argp++) || *cp == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
168 switch (*icachesw = smatch (cp, caches)) {
170 ambigsw (cp, caches);
173 adios (NULL, "%s unknown", cp);
201 if (!(cp = *argp++) || *cp == '-')
202 adios (NULL, "missing argument to %s", argp[-2]);
204 adios (NULL, "too many parts (starting with %s), %d max",
210 if (!(cp = *argp++) || *cp == '-')
211 adios (NULL, "missing argument to %s", argp[-2]);
213 adios (NULL, "too many types (starting with %s), %d max",
219 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
220 adios (NULL, "missing argument to %s", argp[-2]);
221 file = *cp == '-' ? cp : path (cp, TFILE);
235 if (*cp == '+' || *cp == '@') {
237 adios (NULL, "only one folder at a time!");
239 folder = pluspath (cp);
241 app_msgarg(&msgs, cp);
244 /* null terminate the list of acceptable parts/types */
250 /* Check for public cache location */
251 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
254 /* Check for private cache location */
255 if (!(cache_private = context_find (nmhprivcache)))
256 cache_private = ".cache";
257 cache_private = getcpy (m_maildir (cache_private));
260 * Check for storage directory. If specified,
261 * then store temporary files there. Else we
262 * store them in standard nmh directory.
264 if ((cp = context_find (nmhstorage)) && *cp)
265 tmp = concat (cp, "/", invo_name, NULL);
267 tmp = add (m_maildir (invo_name), NULL);
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER));
272 if (file && msgs.size)
273 adios (NULL, "cannot specify msg and file at same time!");
276 * check if message is coming from file
279 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
280 adios (NULL, "out of memory");
283 if ((ct = parse_mime (file)))
287 * message(s) are coming from a folder
290 app_msgarg(&msgs, "cur");
292 folder = getfolder (1);
293 maildir = m_maildir (folder);
295 if (chdir (maildir) == NOTOK)
296 adios (maildir, "unable to change directory to");
298 /* read folder and create message structure */
299 if (!(mp = folder_read (folder)))
300 adios (NULL, "unable to read folder %s", folder);
302 /* check for empty folder */
304 adios (NULL, "no messages in %s", folder);
306 /* parse all the message ranges/sequences and set SELECTED */
307 for (msgnum = 0; msgnum < msgs.size; msgnum++)
308 if (!m_convert (mp, msgs.msgs[msgnum]))
310 seq_setprev (mp); /* set the previous-sequence */
312 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
313 adios (NULL, "out of memory");
316 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
317 if (is_selected(mp, msgnum)) {
320 msgnam = m_name (msgnum);
321 if ((ct = parse_mime (msgnam)))
331 SIGNAL (SIGQUIT, quitser);
332 SIGNAL (SIGPIPE, pipeser);
335 * Get the associated umask for the relevant contents.
337 for (ctp = cts; *ctp; ctp++) {
341 if (type_ok (ct, 1) && !ct->c_umask) {
342 if (stat (ct->c_file, &st) != NOTOK)
343 ct->c_umask = ~(st.st_mode & 0777);
345 ct->c_umask = ~m_gmprot();
350 * List the message content
352 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
354 /* Now free all the structures for the content */
355 for (ctp = cts; *ctp; ctp++)
361 /* If reading from a folder, do some updating */
363 context_replace (pfolder, folder);/* update current folder */
364 seq_setcur (mp, mp->hghsel); /* update current message */
365 seq_save (mp); /* synchronize sequences */
366 context_save (); /* save the context file */
380 fprintf (stderr, "\n");