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 *);
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 = mhbasename(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]", invo_name);
156 print_help(buf, switches, 1);
159 print_version(invo_name);
163 icachesw = &rcachesw;
166 icachesw = &wcachesw;
168 if (!(cp = *argp++) || *cp == '-')
169 adios(NULL, "missing argument to %s", argp[-2]);
170 switch (*icachesw = smatch(cp, caches)) {
175 adios(NULL, "%s unknown", cp);
203 if (!(cp = *argp++) || *cp == '-')
204 adios(NULL, "missing argument to %s",
207 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
212 if (!(cp = *argp++) || *cp == '-')
213 adios(NULL, "missing argument to %s",
216 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
221 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
222 adios(NULL, "missing argument to %s",
224 file = *cp == '-' ? cp : getcpy(expanddir(cp));
238 if (*cp == '+' || *cp == '@') {
240 adios(NULL, "only one folder at a time!");
242 folder = getcpy(expandfol(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(toabsdir(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 = getcpy(toabsdir(invo_name));
272 if (file && msgs.size)
273 adios(NULL, "cannot specify msg and file at same time!");
276 ** check if message is coming from file
279 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
280 adios(NULL, "out of memory");
283 if ((ct = parse_mime(file)));
287 ** message(s) are coming from a folder
290 app_msgarg(&msgs, "cur");
292 folder = getcurfol();
293 maildir = toabsdir(folder);
295 if (chdir(maildir) == NOTOK)
296 adios(maildir, "unable to change directory to");
298 /* read folder and create message structure */
299 if (!(mp = folder_read(folder)))
300 adios(NULL, "unable to read folder %s", folder);
302 /* check for empty folder */
304 adios(NULL, "no messages in %s", folder);
306 /* parse all the message ranges/sequences and set SELECTED */
307 for (msgnum = 0; msgnum < msgs.size; msgnum++)
308 if (!m_convert(mp, msgs.msgs[msgnum]))
310 seq_setprev(mp); /* set the previous-sequence */
312 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
314 adios(NULL, "out of memory");
317 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
318 if (is_selected(mp, msgnum)) {
321 msgnam = m_name(msgnum);
322 if ((ct = parse_mime(msgnam)))
332 SIGNAL(SIGQUIT, quitser);
333 SIGNAL(SIGPIPE, pipeser);
336 ** Get the associated umask for the relevant contents.
338 for (ctp = cts; *ctp; ctp++) {
342 if (type_ok(ct, 1) && !ct->c_umask) {
343 if (stat(ct->c_file, &st) != NOTOK)
344 ct->c_umask = ~(st.st_mode & 0777);
346 ct->c_umask = ~m_gmprot();
351 ** List the message content
353 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
355 /* Now free all the structures for the content */
356 for (ctp = cts; *ctp; ctp++)
362 /* If reading from a folder, do some updating */
364 context_replace(curfolder, folder); /* update current folder */
365 seq_setcur(mp, mp->hghsel); /* update current message */
366 seq_save(mp); /* synchronize sequences */
367 context_save(); /* save the context file */
381 fprintf(stderr, "\n");