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 */
247 /* Check for public cache location */
248 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
251 /* Check for private cache location */
252 if (!(cache_private = context_find (nmhprivcache)))
253 cache_private = ".cache";
254 cache_private = getcpy (m_maildir (cache_private));
257 * Check for storage directory. If specified,
258 * then store temporary files there. Else we
259 * store them in standard nmh directory.
261 if ((cp = context_find (nmhstorage)) && *cp)
262 tmp = concat (cp, "/", invo_name, NULL);
264 tmp = add (m_maildir (invo_name), NULL);
266 if (!context_find ("path"))
267 free (path ("./", TFOLDER));
269 if (file && msgs.size)
270 adios (NULL, "cannot specify msg and file at same time!");
273 * check if message is coming from file
276 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
277 adios (NULL, "out of memory");
280 if ((ct = parse_mime (file)))
284 * message(s) are coming from a folder
287 app_msgarg(&msgs, "cur");
289 folder = getfolder (1);
290 maildir = m_maildir (folder);
292 if (chdir (maildir) == NOTOK)
293 adios (maildir, "unable to change directory to");
295 /* read folder and create message structure */
296 if (!(mp = folder_read (folder)))
297 adios (NULL, "unable to read folder %s", folder);
299 /* check for empty folder */
301 adios (NULL, "no messages in %s", folder);
303 /* parse all the message ranges/sequences and set SELECTED */
304 for (msgnum = 0; msgnum < msgs.size; msgnum++)
305 if (!m_convert (mp, msgs.msgs[msgnum]))
307 seq_setprev (mp); /* set the previous-sequence */
309 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
310 adios (NULL, "out of memory");
313 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
314 if (is_selected(mp, msgnum)) {
317 msgnam = m_name (msgnum);
318 if ((ct = parse_mime (msgnam)))
328 SIGNAL (SIGQUIT, quitser);
329 SIGNAL (SIGPIPE, pipeser);
332 * Get the associated umask for the relevant contents.
334 for (ctp = cts; *ctp; ctp++) {
338 if (type_ok (ct, 1) && !ct->c_umask) {
339 if (stat (ct->c_file, &st) != NOTOK)
340 ct->c_umask = ~(st.st_mode & 0777);
342 ct->c_umask = ~m_gmprot();
347 * List the message content
349 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
351 /* Now free all the structures for the content */
352 for (ctp = cts; *ctp; ctp++)
358 /* If reading from a folder, do some updating */
360 context_replace (pfolder, folder);/* update current folder */
361 seq_setcur (mp, mp->hghsel); /* update current message */
362 seq_save (mp); /* synchronize sequences */
363 context_save (); /* save the context file */
377 fprintf (stderr, "\n");