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 #define quitser pipeser
99 CT parse_mime (char *);
102 int part_ok (CT, int);
103 int type_ok (CT, int);
104 void set_endian (void);
105 void flush_errors (void);
108 void list_all_messages (CT *, int, int, int, int);
111 void free_content (CT);
113 void freects_done (int);
118 static RETSIGTYPE pipeser (int);
122 main (int argc, char **argv)
124 int sizesw = 1, headsw = 1;
125 int msgnum, *icachesw;
126 char *cp, *file = NULL, *folder = NULL;
127 char *maildir, buf[100], **argp;
129 struct msgs_array msgs = { 0, 0, NULL };
130 struct msgs *mp = NULL;
136 setlocale(LC_ALL, "");
138 invo_name = r1bindex (argv[0], '/');
140 /* read user profile/context */
143 arguments = getarguments (invo_name, argc, argv, 1);
149 while ((cp = *argp++)) {
151 switch (smatch (++cp, switches)) {
153 ambigsw (cp, switches);
156 adios (NULL, "-%s unknown", cp);
159 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
161 print_help (buf, switches, 1);
164 print_version(invo_name);
168 icachesw = &rcachesw;
171 icachesw = &wcachesw;
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 switch (*icachesw = smatch (cp, caches)) {
177 ambigsw (cp, caches);
180 adios (NULL, "%s unknown", cp);
208 if (!(cp = *argp++) || *cp == '-')
209 adios (NULL, "missing argument to %s", argp[-2]);
211 adios (NULL, "too many parts (starting with %s), %d max",
217 if (!(cp = *argp++) || *cp == '-')
218 adios (NULL, "missing argument to %s", argp[-2]);
220 adios (NULL, "too many types (starting with %s), %d max",
226 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
227 adios (NULL, "missing argument to %s", argp[-2]);
228 file = *cp == '-' ? cp : path (cp, TFILE);
242 if (*cp == '+' || *cp == '@') {
244 adios (NULL, "only one folder at a time!");
246 folder = pluspath (cp);
248 app_msgarg(&msgs, cp);
251 /* null terminate the list of acceptable parts/types */
257 /* Check for public cache location */
258 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
261 /* Check for private cache location */
262 if (!(cache_private = context_find (nmhprivcache)))
263 cache_private = ".cache";
264 cache_private = getcpy (m_maildir (cache_private));
267 * Check for storage directory. If specified,
268 * then store temporary files there. Else we
269 * store them in standard nmh directory.
271 if ((cp = context_find (nmhstorage)) && *cp)
272 tmp = concat (cp, "/", invo_name, NULL);
274 tmp = add (m_maildir (invo_name), NULL);
276 if (!context_find ("path"))
277 free (path ("./", TFOLDER));
279 if (file && msgs.size)
280 adios (NULL, "cannot specify msg and file at same time!");
283 * check if message is coming from file
286 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
287 adios (NULL, "out of memory");
290 if ((ct = parse_mime (file)));
294 * message(s) are coming from a folder
297 app_msgarg(&msgs, "cur");
299 folder = getfolder (1);
300 maildir = m_maildir (folder);
302 if (chdir (maildir) == NOTOK)
303 adios (maildir, "unable to change directory to");
305 /* read folder and create message structure */
306 if (!(mp = folder_read (folder)))
307 adios (NULL, "unable to read folder %s", folder);
309 /* check for empty folder */
311 adios (NULL, "no messages in %s", folder);
313 /* parse all the message ranges/sequences and set SELECTED */
314 for (msgnum = 0; msgnum < msgs.size; msgnum++)
315 if (!m_convert (mp, msgs.msgs[msgnum]))
317 seq_setprev (mp); /* set the previous-sequence */
319 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
320 adios (NULL, "out of memory");
323 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
324 if (is_selected(mp, msgnum)) {
327 msgnam = m_name (msgnum);
328 if ((ct = parse_mime (msgnam)))
338 SIGNAL (SIGQUIT, quitser);
339 SIGNAL (SIGPIPE, pipeser);
342 * Get the associated umask for the relevant contents.
344 for (ctp = cts; *ctp; ctp++) {
348 if (type_ok (ct, 1) && !ct->c_umask) {
349 if (stat (ct->c_file, &st) != NOTOK)
350 ct->c_umask = ~(st.st_mode & 0777);
352 ct->c_umask = ~m_gmprot();
357 * List the message content
359 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
361 /* Now free all the structures for the content */
362 for (ctp = cts; *ctp; ctp++)
368 /* If reading from a folder, do some updating */
370 context_replace (pfolder, folder);/* update current folder */
371 seq_setcur (mp, mp->hghsel); /* update current message */
372 seq_save (mp); /* synchronize sequences */
373 context_save (); /* save the context file */
387 fprintf (stderr, "\n");