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>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
29 static struct swit switches[] = {
46 #define FILESW 8 /* interface from show */
51 { "type content", 0 },
53 { "rcache policy", 0 },
55 { "wcache policy", 0 },
62 * switches for debugging
72 extern char *tmp; /* directory to place temp files */
77 extern char *cache_public;
78 extern char *cache_private;
83 extern char *parts[NPARTS + 1];
84 extern char *types[NTYPES + 1];
88 * This is currently needed to keep mhparse happy.
89 * This needs to be changed.
96 /* The list of top-level contents to display */
99 #define quitser pipeser
102 CT parse_mime (char *);
105 int part_ok (CT, int);
106 int type_ok (CT, int);
107 void set_endian (void);
108 void flush_errors (void);
111 void list_all_messages (CT *, int, int, int, int);
114 void free_content (CT);
119 static RETSIGTYPE pipeser (int);
123 main (int argc, char **argv)
125 int sizesw = 1, headsw = 1;
126 int msgnum, *icachesw;
127 char *cp, *file = NULL, *folder = NULL;
128 char *maildir, buf[100], **argp;
130 struct msgs_array msgs = { 0, 0, NULL };
131 struct msgs *mp = NULL;
135 setlocale(LC_ALL, "");
137 invo_name = r1bindex (argv[0], '/');
139 /* read user profile/context */
142 arguments = getarguments (invo_name, argc, argv, 1);
148 while ((cp = *argp++)) {
150 switch (smatch (++cp, switches)) {
152 ambigsw (cp, switches);
155 adios (NULL, "-%s unknown", cp);
158 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
160 print_help (buf, switches, 1);
163 print_version(invo_name);
167 icachesw = &rcachesw;
170 icachesw = &wcachesw;
172 if (!(cp = *argp++) || *cp == '-')
173 adios (NULL, "missing argument to %s", argp[-2]);
174 switch (*icachesw = smatch (cp, caches)) {
176 ambigsw (cp, caches);
179 adios (NULL, "%s unknown", cp);
207 if (!(cp = *argp++) || *cp == '-')
208 adios (NULL, "missing argument to %s", argp[-2]);
210 adios (NULL, "too many parts (starting with %s), %d max",
216 if (!(cp = *argp++) || *cp == '-')
217 adios (NULL, "missing argument to %s", argp[-2]);
219 adios (NULL, "too many types (starting with %s), %d max",
225 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
226 adios (NULL, "missing argument to %s", argp[-2]);
227 file = *cp == '-' ? cp : path (cp, TFILE);
241 if (*cp == '+' || *cp == '@') {
243 adios (NULL, "only one folder at a time!");
245 folder = pluspath (cp);
247 app_msgarg(&msgs, cp);
250 /* null terminate the list of acceptable parts/types */
256 /* Check for public cache location */
257 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
260 /* Check for private cache location */
261 if (!(cache_private = context_find (nmhprivcache)))
262 cache_private = ".cache";
263 cache_private = getcpy (m_maildir (cache_private));
266 * Check for storage directory. If specified,
267 * then store temporary files there. Else we
268 * store them in standard nmh directory.
270 if ((cp = context_find (nmhstorage)) && *cp)
271 tmp = concat (cp, "/", invo_name, NULL);
273 tmp = add (m_maildir (invo_name), NULL);
275 if (!context_find ("path"))
276 free (path ("./", TFOLDER));
278 if (file && msgs.size)
279 adios (NULL, "cannot specify msg and file at same time!");
282 * check if message is coming from file
285 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
286 adios (NULL, "out of memory");
289 if ((ct = parse_mime (file)));
293 * message(s) are coming from a folder
296 app_msgarg(&msgs, "cur");
298 folder = getfolder (1);
299 maildir = m_maildir (folder);
301 if (chdir (maildir) == NOTOK)
302 adios (maildir, "unable to change directory to");
304 /* read folder and create message structure */
305 if (!(mp = folder_read (folder)))
306 adios (NULL, "unable to read folder %s", folder);
308 /* check for empty folder */
310 adios (NULL, "no messages in %s", folder);
312 /* parse all the message ranges/sequences and set SELECTED */
313 for (msgnum = 0; msgnum < msgs.size; msgnum++)
314 if (!m_convert (mp, msgs.msgs[msgnum]))
316 seq_setprev (mp); /* set the previous-sequence */
318 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
319 adios (NULL, "out of memory");
322 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
323 if (is_selected(mp, msgnum)) {
326 msgnam = m_name (msgnum);
327 if ((ct = parse_mime (msgnam)))
337 SIGNAL (SIGQUIT, quitser);
338 SIGNAL (SIGPIPE, pipeser);
341 * Get the associated umask for the relevant contents.
343 for (ctp = cts; *ctp; ctp++) {
347 if (type_ok (ct, 1) && !ct->c_umask) {
348 if (stat (ct->c_file, &st) != NOTOK)
349 ct->c_umask = ~(st.st_mode & 0777);
351 ct->c_umask = ~m_gmprot();
356 * List the message content
358 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
360 /* Now free all the structures for the content */
361 for (ctp = cts; *ctp; ctp++)
367 /* If reading from a folder, do some updating */
369 context_replace (pfolder, folder);/* update current folder */
370 seq_setcur (mp, mp->hghsel); /* update current message */
371 seq_save (mp); /* synchronize sequences */
372 context_save (); /* save the context file */
385 fprintf (stderr, "\n");
404 return 1; /* dead code to satisfy the compiler */