3 * mhlist.c -- list the contents of MIME messages
10 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
25 * We allocate space for message names (msgs array)
26 * this number of elements at a time.
31 static struct swit switches[] = {
48 #define FILESW 8 /* interface from show */
53 { "type content", 0 },
55 { "rcache policy", 0 },
57 { "wcache policy", 0 },
64 * switches for debugging
76 extern char *tmp; /* directory to place temp files */
81 extern char *cache_public;
82 extern char *cache_private;
87 extern char *parts[NPARTS + 1];
88 extern char *types[NTYPES + 1];
92 * This is currently needed to keep mhparse happy.
93 * This needs to be changed.
100 /* The list of top-level contents to display */
103 #define quitser pipeser
106 CT parse_mime (char *);
109 int part_ok (CT, int);
110 int type_ok (CT, int);
111 void set_endian (void);
112 void flush_errors (void);
115 void list_all_messages (CT *, int, int, int, int);
118 void free_content (CT);
123 static RETSIGTYPE pipeser (int);
127 main (int argc, char **argv)
129 int sizesw = 1, headsw = 1;
130 int nummsgs, maxmsgs, msgnum, *icachesw;
131 char *cp, *file = NULL, *folder = NULL;
132 char *maildir, buf[100], **argp;
133 char **arguments, **msgs;
134 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);
149 * Allocate the initial space to record message
150 * names, ranges, and sequences.
154 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
155 adios (NULL, "unable to allocate storage");
160 while ((cp = *argp++)) {
162 switch (smatch (++cp, switches)) {
164 ambigsw (cp, switches);
167 adios (NULL, "-%s unknown", cp);
170 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
172 print_help (buf, switches, 1);
175 print_version(invo_name);
179 icachesw = &rcachesw;
182 icachesw = &wcachesw;
184 if (!(cp = *argp++) || *cp == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 switch (*icachesw = smatch (cp, caches)) {
188 ambigsw (cp, caches);
191 adios (NULL, "%s unknown", cp);
219 if (!(cp = *argp++) || *cp == '-')
220 adios (NULL, "missing argument to %s", argp[-2]);
222 adios (NULL, "too many parts (starting with %s), %d max",
228 if (!(cp = *argp++) || *cp == '-')
229 adios (NULL, "missing argument to %s", argp[-2]);
231 adios (NULL, "too many types (starting with %s), %d max",
237 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
238 adios (NULL, "missing argument to %s", argp[-2]);
239 file = *cp == '-' ? cp : path (cp, TFILE);
253 if (*cp == '+' || *cp == '@') {
255 adios (NULL, "only one folder at a time!");
257 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
260 * Check if we need to allocate more space
261 * for message names/ranges/sequences.
263 if (nummsgs >= maxmsgs) {
265 if (!(msgs = (char **) realloc (msgs,
266 (size_t) (maxmsgs * sizeof(*msgs)))))
267 adios (NULL, "unable to reallocate msgs storage");
269 msgs[nummsgs++] = cp;
273 /* null terminate the list of acceptable parts/types */
279 /* Check for public cache location */
280 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
283 /* Check for private cache location */
284 if (!(cache_private = context_find (nmhprivcache)))
285 cache_private = ".cache";
286 cache_private = getcpy (m_maildir (cache_private));
289 * Check for storage directory. If specified,
290 * then store temporary files there. Else we
291 * store them in standard nmh directory.
293 if ((cp = context_find (nmhstorage)) && *cp)
294 tmp = concat (cp, "/", invo_name, NULL);
296 tmp = add (m_maildir (invo_name), NULL);
298 if (!context_find ("path"))
299 free (path ("./", TFOLDER));
302 adios (NULL, "cannot specify msg and file at same time!");
305 * check if message is coming from file
308 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
309 adios (NULL, "out of memory");
312 if ((ct = parse_mime (file)));
316 * message(s) are coming from a folder
319 msgs[nummsgs++] = "cur";
321 folder = getfolder (1);
322 maildir = m_maildir (folder);
324 if (chdir (maildir) == NOTOK)
325 adios (maildir, "unable to change directory to");
327 /* read folder and create message structure */
328 if (!(mp = folder_read (folder)))
329 adios (NULL, "unable to read folder %s", folder);
331 /* check for empty folder */
333 adios (NULL, "no messages in %s", folder);
335 /* parse all the message ranges/sequences and set SELECTED */
336 for (msgnum = 0; msgnum < nummsgs; msgnum++)
337 if (!m_convert (mp, msgs[msgnum]))
339 seq_setprev (mp); /* set the previous-sequence */
341 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
342 adios (NULL, "out of memory");
345 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
346 if (is_selected(mp, msgnum)) {
349 msgnam = m_name (msgnum);
350 if ((ct = parse_mime (msgnam)))
360 SIGNAL (SIGQUIT, quitser);
361 SIGNAL (SIGPIPE, pipeser);
364 * Get the associated umask for the relevant contents.
366 for (ctp = cts; *ctp; ctp++) {
370 if (type_ok (ct, 1) && !ct->c_umask) {
371 if (stat (ct->c_file, &st) != NOTOK)
372 ct->c_umask = ~(st.st_mode & 0777);
374 ct->c_umask = ~m_gmprot();
379 * List the message content
381 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
383 /* Now free all the structures for the content */
384 for (ctp = cts; *ctp; ctp++)
390 /* If reading from a folder, do some updating */
392 context_replace (pfolder, folder);/* update current folder */
393 seq_setcur (mp, mp->hghsel); /* update current message */
394 seq_save (mp); /* synchronize sequences */
395 context_save (); /* save the context file */
408 fprintf (stderr, "\n");
427 return 1; /* dead code to satisfy the compiler */