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);
120 static int freectp_done (int);
124 main (int argc, char **argv)
126 int sizesw = 1, headsw = 1;
127 int msgnum, *icachesw;
128 char *cp, *file = NULL, *folder = NULL;
129 char *maildir, buf[100], **argp;
131 struct msgs_array msgs = { 0, 0, NULL };
132 struct msgs *mp = NULL;
138 setlocale(LC_ALL, "");
140 invo_name = r1bindex (argv[0], '/');
142 /* read user profile/context */
145 arguments = getarguments (invo_name, argc, argv, 1);
151 while ((cp = *argp++)) {
153 switch (smatch (++cp, switches)) {
155 ambigsw (cp, switches);
158 adios (NULL, "-%s unknown", cp);
161 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
163 print_help (buf, switches, 1);
166 print_version(invo_name);
170 icachesw = &rcachesw;
173 icachesw = &wcachesw;
175 if (!(cp = *argp++) || *cp == '-')
176 adios (NULL, "missing argument to %s", argp[-2]);
177 switch (*icachesw = smatch (cp, caches)) {
179 ambigsw (cp, caches);
182 adios (NULL, "%s unknown", cp);
210 if (!(cp = *argp++) || *cp == '-')
211 adios (NULL, "missing argument to %s", argp[-2]);
213 adios (NULL, "too many parts (starting with %s), %d max",
219 if (!(cp = *argp++) || *cp == '-')
220 adios (NULL, "missing argument to %s", argp[-2]);
222 adios (NULL, "too many types (starting with %s), %d max",
228 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
229 adios (NULL, "missing argument to %s", argp[-2]);
230 file = *cp == '-' ? cp : path (cp, TFILE);
244 if (*cp == '+' || *cp == '@') {
246 adios (NULL, "only one folder at a time!");
248 folder = pluspath (cp);
250 app_msgarg(&msgs, cp);
253 /* null terminate the list of acceptable parts/types */
259 /* Check for public cache location */
260 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
263 /* Check for private cache location */
264 if (!(cache_private = context_find (nmhprivcache)))
265 cache_private = ".cache";
266 cache_private = getcpy (m_maildir (cache_private));
269 * Check for storage directory. If specified,
270 * then store temporary files there. Else we
271 * store them in standard nmh directory.
273 if ((cp = context_find (nmhstorage)) && *cp)
274 tmp = concat (cp, "/", invo_name, NULL);
276 tmp = add (m_maildir (invo_name), NULL);
278 if (!context_find ("path"))
279 free (path ("./", TFOLDER));
281 if (file && msgs.size)
282 adios (NULL, "cannot specify msg and file at same time!");
285 * check if message is coming from file
288 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
289 adios (NULL, "out of memory");
292 if ((ct = parse_mime (file)));
296 * message(s) are coming from a folder
299 app_msgarg(&msgs, "cur");
301 folder = getfolder (1);
302 maildir = m_maildir (folder);
304 if (chdir (maildir) == NOTOK)
305 adios (maildir, "unable to change directory to");
307 /* read folder and create message structure */
308 if (!(mp = folder_read (folder)))
309 adios (NULL, "unable to read folder %s", folder);
311 /* check for empty folder */
313 adios (NULL, "no messages in %s", folder);
315 /* parse all the message ranges/sequences and set SELECTED */
316 for (msgnum = 0; msgnum < msgs.size; msgnum++)
317 if (!m_convert (mp, msgs.msgs[msgnum]))
319 seq_setprev (mp); /* set the previous-sequence */
321 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
322 adios (NULL, "out of memory");
325 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
326 if (is_selected(mp, msgnum)) {
329 msgnam = m_name (msgnum);
330 if ((ct = parse_mime (msgnam)))
340 SIGNAL (SIGQUIT, quitser);
341 SIGNAL (SIGPIPE, pipeser);
344 * Get the associated umask for the relevant contents.
346 for (ctp = cts; *ctp; ctp++) {
350 if (type_ok (ct, 1) && !ct->c_umask) {
351 if (stat (ct->c_file, &st) != NOTOK)
352 ct->c_umask = ~(st.st_mode & 0777);
354 ct->c_umask = ~m_gmprot();
359 * List the message content
361 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
363 /* Now free all the structures for the content */
364 for (ctp = cts; *ctp; ctp++)
370 /* If reading from a folder, do some updating */
372 context_replace (pfolder, folder);/* update current folder */
373 seq_setcur (mp, mp->hghsel); /* update current message */
374 seq_save (mp); /* synchronize sequences */
375 context_save (); /* save the context file */
388 fprintf (stderr, "\n");
398 freectp_done (int status)
407 return 1; /* dead code to satisfy the compiler */