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", argp[-2]);
223 file = *cp == '-' ? cp : path(cp, TFILE);
237 if (*cp == '+' || *cp == '@') {
239 adios(NULL, "only one folder at a time!");
241 folder = pluspath(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(m_maildir(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 = add(m_maildir(invo_name), NULL);
271 if (!context_find("path"))
272 free(path("./", TFOLDER));
274 if (file && msgs.size)
275 adios(NULL, "cannot specify msg and file at same time!");
278 ** check if message is coming from file
281 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
282 adios(NULL, "out of memory");
285 if ((ct = parse_mime(file)));
289 ** message(s) are coming from a folder
292 app_msgarg(&msgs, "cur");
294 folder = getfolder(1);
295 maildir = m_maildir(folder);
297 if (chdir(maildir) == NOTOK)
298 adios(maildir, "unable to change directory to");
300 /* read folder and create message structure */
301 if (!(mp = folder_read(folder)))
302 adios(NULL, "unable to read folder %s", folder);
304 /* check for empty folder */
306 adios(NULL, "no messages in %s", folder);
308 /* parse all the message ranges/sequences and set SELECTED */
309 for (msgnum = 0; msgnum < msgs.size; msgnum++)
310 if (!m_convert(mp, msgs.msgs[msgnum]))
312 seq_setprev(mp); /* set the previous-sequence */
314 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
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");