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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
25 static struct swit switches[] = {
42 #define FILESW 8 /* interface from show */
47 { "type content", 0 },
49 { "rcache policy", 0 },
51 { "wcache policy", 0 },
58 ** switches for debugging
67 extern char *tmp; /* directory to place temp files */
72 extern char *cache_public;
73 extern char *cache_private;
78 extern char *parts[NPARTS + 1];
79 extern char *types[NTYPES + 1];
83 ** This is currently needed to keep mhparse happy.
84 ** This needs to be changed.
91 #define quitser pipeser
94 CT parse_mime(char *);
99 void set_endian(void);
100 void flush_errors(void);
103 void list_all_messages(CT *, int, int, int, int);
106 void free_content(CT);
108 void freects_done(int) NORETURN;
113 static RETSIGTYPE pipeser(int);
117 main(int argc, char **argv)
119 int sizesw = 1, headsw = 1;
120 int msgnum, *icachesw;
121 char *cp, *file = NULL, *folder = NULL;
122 char *maildir, buf[100], **argp;
124 struct msgs_array msgs = { 0, 0, NULL };
125 struct msgs *mp = NULL;
131 setlocale(LC_ALL, "");
133 invo_name = mhbasename(argv[0]);
135 /* read user profile/context */
138 arguments = getarguments(invo_name, argc, argv, 1);
144 while ((cp = *argp++)) {
146 switch (smatch(++cp, switches)) {
148 ambigsw(cp, switches);
151 adios(NULL, "-%s unknown", cp);
154 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
155 print_help(buf, switches, 1);
158 print_version(invo_name);
162 icachesw = &rcachesw;
165 icachesw = &wcachesw;
167 if (!(cp = *argp++) || *cp == '-')
168 adios(NULL, "missing argument to %s", argp[-2]);
169 switch (*icachesw = smatch(cp, caches)) {
174 adios(NULL, "%s unknown", cp);
202 if (!(cp = *argp++) || *cp == '-')
203 adios(NULL, "missing argument to %s",
206 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
211 if (!(cp = *argp++) || *cp == '-')
212 adios(NULL, "missing argument to %s",
215 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
220 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
221 adios(NULL, "missing argument to %s",
223 file = *cp == '-' ? cp : getcpy(expanddir(cp));
237 if (*cp == '+' || *cp == '@') {
239 adios(NULL, "only one folder at a time!");
241 folder = getcpy(expandfol(cp));
243 app_msgarg(&msgs, cp);
246 /* null terminate the list of acceptable parts/types */
252 /* Check for public cache location */
253 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
256 /* Check for private cache location */
257 if (!(cache_private = context_find(nmhprivcache)))
258 cache_private = ".cache";
259 cache_private = getcpy(toabsdir(cache_private));
262 ** Check for storage directory. If specified,
263 ** then store temporary files there. Else we
264 ** store them in standard nmh directory.
266 if ((cp = context_find(nmhstorage)) && *cp)
267 tmp = concat(cp, "/", invo_name, NULL);
269 tmp = getcpy(toabsdir(invo_name));
271 if (file && msgs.size)
272 adios(NULL, "cannot specify msg and file at same time!");
275 ** check if message is coming from file
278 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
279 adios(NULL, "out of memory");
282 if ((ct = parse_mime(file)));
286 ** message(s) are coming from a folder
289 app_msgarg(&msgs, seq_cur);
291 folder = getcurfol();
292 maildir = toabsdir(folder);
294 if (chdir(maildir) == NOTOK)
295 adios(maildir, "unable to change directory to");
297 /* read folder and create message structure */
298 if (!(mp = folder_read(folder)))
299 adios(NULL, "unable to read folder %s", folder);
301 /* check for empty folder */
303 adios(NULL, "no messages in %s", folder);
305 /* parse all the message ranges/sequences and set SELECTED */
306 for (msgnum = 0; msgnum < msgs.size; msgnum++)
307 if (!m_convert(mp, msgs.msgs[msgnum]))
309 seq_setprev(mp); /* set the previous-sequence */
311 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
313 adios(NULL, "out of memory");
316 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
317 if (is_selected(mp, msgnum)) {
320 msgnam = m_name(msgnum);
321 if ((ct = parse_mime(msgnam)))
331 SIGNAL(SIGQUIT, quitser);
332 SIGNAL(SIGPIPE, pipeser);
335 ** Get the associated umask for the relevant contents.
337 for (ctp = cts; *ctp; ctp++) {
341 if (type_ok(ct, 1) && !ct->c_umask) {
342 if (stat(ct->c_file, &st) != NOTOK)
343 ct->c_umask = ~(st.st_mode & 0777);
345 ct->c_umask = ~m_gmprot();
350 ** List the message content
352 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
354 /* Now free all the structures for the content */
355 for (ctp = cts; *ctp; ctp++)
361 /* If reading from a folder, do some updating */
363 context_replace(curfolder, folder); /* update current folder */
364 seq_setcur(mp, mp->hghsel); /* update current message */
365 seq_save(mp); /* synchronize sequences */
366 context_save(); /* save the context file */
380 fprintf(stderr, "\n");