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 flush_errors (void);
100 void list_all_messages (CT *, int, int, int, int);
103 void free_content (CT);
105 void freects_done (int) NORETURN;
110 static void pipeser (int);
114 main (int argc, char **argv)
116 int sizesw = 1, headsw = 1;
117 int msgnum, *icachesw;
118 char *cp, *file = NULL, *folder = NULL;
119 char *maildir, buf[100], **argp;
121 struct msgs_array msgs = { 0, 0, NULL };
122 struct msgs *mp = NULL;
128 setlocale(LC_ALL, "");
130 invo_name = r1bindex (argv[0], '/');
132 /* read user profile/context */
135 arguments = getarguments (invo_name, argc, argv, 1);
141 while ((cp = *argp++)) {
143 switch (smatch (++cp, switches)) {
145 ambigsw (cp, switches);
148 adios (NULL, "-%s unknown", cp);
151 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
153 print_help (buf, switches, 1);
156 print_version(invo_name);
160 icachesw = &rcachesw;
163 icachesw = &wcachesw;
165 if (!(cp = *argp++) || *cp == '-')
166 adios (NULL, "missing argument to %s", argp[-2]);
167 switch (*icachesw = smatch (cp, caches)) {
169 ambigsw (cp, caches);
172 adios (NULL, "%s unknown", cp);
200 if (!(cp = *argp++) || *cp == '-')
201 adios (NULL, "missing argument to %s", argp[-2]);
203 adios (NULL, "too many parts (starting with %s), %d max",
209 if (!(cp = *argp++) || *cp == '-')
210 adios (NULL, "missing argument to %s", argp[-2]);
212 adios (NULL, "too many types (starting with %s), %d max",
218 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
219 adios (NULL, "missing argument to %s", argp[-2]);
220 file = *cp == '-' ? cp : path (cp, TFILE);
234 if (*cp == '+' || *cp == '@') {
236 adios (NULL, "only one folder at a time!");
238 folder = pluspath (cp);
240 app_msgarg(&msgs, cp);
243 /* null terminate the list of acceptable parts/types */
249 /* Check for public cache location */
250 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
253 /* Check for private cache location */
254 if (!(cache_private = context_find (nmhprivcache)))
255 cache_private = ".cache";
256 cache_private = getcpy (m_maildir (cache_private));
259 * Check for storage directory. If specified,
260 * then store temporary files there. Else we
261 * store them in standard nmh directory.
263 if ((cp = context_find (nmhstorage)) && *cp)
264 tmp = concat (cp, "/", invo_name, NULL);
266 tmp = add (m_maildir (invo_name), NULL);
268 if (!context_find ("path"))
269 free (path ("./", TFOLDER));
271 if (file && msgs.size)
272 adios (NULL, "cannot specify msg and file at same time!");
275 * check if message is coming from file
278 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
279 adios (NULL, "out of memory");
282 if ((ct = parse_mime (file)))
286 * message(s) are coming from a folder
289 app_msgarg(&msgs, "cur");
291 folder = getfolder (1);
292 maildir = m_maildir (folder);
294 if (chdir (maildir) == NOTOK)
295 adios (maildir, "unable to change directory to");
297 /* read folder and create message structure */
298 if (!(mp = folder_read (folder)))
299 adios (NULL, "unable to read folder %s", folder);
301 /* check for empty folder */
303 adios (NULL, "no messages in %s", folder);
305 /* parse all the message ranges/sequences and set SELECTED */
306 for (msgnum = 0; msgnum < msgs.size; msgnum++)
307 if (!m_convert (mp, msgs.msgs[msgnum]))
309 seq_setprev (mp); /* set the previous-sequence */
311 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
312 adios (NULL, "out of memory");
315 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
316 if (is_selected(mp, msgnum)) {
319 msgnam = m_name (msgnum);
320 if ((ct = parse_mime (msgnam)))
330 SIGNAL (SIGQUIT, quitser);
331 SIGNAL (SIGPIPE, pipeser);
334 * Get the associated umask for the relevant contents.
336 for (ctp = cts; *ctp; ctp++) {
340 if (type_ok (ct, 1) && !ct->c_umask) {
341 if (stat (ct->c_file, &st) != NOTOK)
342 ct->c_umask = ~(st.st_mode & 0777);
344 ct->c_umask = ~m_gmprot();
349 * List the message content
351 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
353 /* Now free all the structures for the content */
354 for (ctp = cts; *ctp; ctp++)
360 /* If reading from a folder, do some updating */
362 context_replace (pfolder, folder);/* update current folder */
363 seq_setcur (mp, mp->hghsel); /* update current message */
364 seq_save (mp); /* synchronize sequences */
365 context_save (); /* save the context file */
379 fprintf (stderr, "\n");