2 * mhlist.c -- list the contents of MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
43 #define FILESW 8 /* interface from show */
48 { "type content", 0 },
50 { "rcache policy", 0 },
52 { "wcache policy", 0 },
59 * switches for debugging
68 extern char *tmp; /* directory to place temp files */
73 extern char *cache_public;
74 extern char *cache_private;
79 extern char *parts[NPARTS + 1];
80 extern char *types[NTYPES + 1];
84 * This is currently needed to keep mhparse happy.
85 * This needs to be changed.
92 #define quitser pipeser
95 CT parse_mime (char *);
98 int part_ok (CT, int);
99 int type_ok (CT, int);
100 void set_endian (void);
101 void flush_errors (void);
104 void list_all_messages (CT *, int, int, int, int);
107 void free_content (CT);
109 void freects_done (int) NORETURN;
114 static RETSIGTYPE pipeser (int);
118 main (int argc, char **argv)
120 int sizesw = 1, headsw = 1;
121 int msgnum, *icachesw;
122 char *cp, *file = NULL, *folder = NULL;
123 char *maildir, buf[100], **argp;
125 struct msgs_array msgs = { 0, 0, NULL };
126 struct msgs *mp = NULL;
132 setlocale(LC_ALL, "");
134 invo_name = r1bindex (argv[0], '/');
136 /* read user profile/context */
139 arguments = getarguments (invo_name, argc, argv, 1);
145 while ((cp = *argp++)) {
147 switch (smatch (++cp, switches)) {
149 ambigsw (cp, switches);
152 adios (NULL, "-%s unknown", cp);
155 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
157 print_help (buf, switches, 1);
160 print_version(invo_name);
164 icachesw = &rcachesw;
167 icachesw = &wcachesw;
169 if (!(cp = *argp++) || *cp == '-')
170 adios (NULL, "missing argument to %s", argp[-2]);
171 switch (*icachesw = smatch (cp, caches)) {
173 ambigsw (cp, caches);
176 adios (NULL, "%s unknown", cp);
204 if (!(cp = *argp++) || *cp == '-')
205 adios (NULL, "missing argument to %s", argp[-2]);
207 adios (NULL, "too many parts (starting with %s), %d max",
213 if (!(cp = *argp++) || *cp == '-')
214 adios (NULL, "missing argument to %s", argp[-2]);
216 adios (NULL, "too many types (starting with %s), %d max",
222 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
223 adios (NULL, "missing argument to %s", argp[-2]);
224 file = *cp == '-' ? cp : path (cp, TFILE);
238 if (*cp == '+' || *cp == '@') {
240 adios (NULL, "only one folder at a time!");
242 folder = pluspath (cp);
244 app_msgarg(&msgs, cp);
247 /* null terminate the list of acceptable parts/types */
253 /* Check for public cache location */
254 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
257 /* Check for private cache location */
258 if (!(cache_private = context_find (nmhprivcache)))
259 cache_private = ".cache";
260 cache_private = getcpy (m_maildir (cache_private));
263 * Check for storage directory. If specified,
264 * then store temporary files there. Else we
265 * store them in standard nmh directory.
267 if ((cp = context_find (nmhstorage)) && *cp)
268 tmp = concat (cp, "/", invo_name, NULL);
270 tmp = add (m_maildir (invo_name), NULL);
272 if (!context_find ("path"))
273 free (path ("./", TFOLDER));
275 if (file && msgs.size)
276 adios (NULL, "cannot specify msg and file at same time!");
279 * check if message is coming from file
282 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
283 adios (NULL, "out of memory");
286 if ((ct = parse_mime (file)));
290 * message(s) are coming from a folder
293 app_msgarg(&msgs, "cur");
295 folder = getfolder (1);
296 maildir = m_maildir (folder);
298 if (chdir (maildir) == NOTOK)
299 adios (maildir, "unable to change directory to");
301 /* read folder and create message structure */
302 if (!(mp = folder_read (folder)))
303 adios (NULL, "unable to read folder %s", folder);
305 /* check for empty folder */
307 adios (NULL, "no messages in %s", folder);
309 /* parse all the message ranges/sequences and set SELECTED */
310 for (msgnum = 0; msgnum < msgs.size; msgnum++)
311 if (!m_convert (mp, msgs.msgs[msgnum]))
313 seq_setprev (mp); /* set the previous-sequence */
315 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
316 adios (NULL, "out of memory");
319 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
320 if (is_selected(mp, msgnum)) {
323 msgnam = m_name (msgnum);
324 if ((ct = parse_mime (msgnam)))
334 SIGNAL (SIGQUIT, quitser);
335 SIGNAL (SIGPIPE, pipeser);
338 * Get the associated umask for the relevant contents.
340 for (ctp = cts; *ctp; ctp++) {
344 if (type_ok (ct, 1) && !ct->c_umask) {
345 if (stat (ct->c_file, &st) != NOTOK)
346 ct->c_umask = ~(st.st_mode & 0777);
348 ct->c_umask = ~m_gmprot();
353 * List the message content
355 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
357 /* Now free all the structures for the content */
358 for (ctp = cts; *ctp; ctp++)
364 /* If reading from a folder, do some updating */
366 context_replace (pfolder, folder); /* update current folder */
367 seq_setcur (mp, mp->hghsel); /* update current message */
368 seq_save (mp); /* synchronize sequences */
369 context_save (); /* save the context file */
383 fprintf (stderr, "\n");