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 static struct swit switches[] = {
38 #define FILESW 8 /* interface from show */
43 { "type content", 0 },
45 { "rcache policy", 0 },
47 { "wcache policy", 0 },
59 extern char *tmp; /* directory to place temp files */
64 extern char *cache_public;
65 extern char *cache_private;
70 extern char *parts[NPARTS + 1];
71 extern char *types[NTYPES + 1];
75 ** This is currently needed to keep mhparse happy.
76 ** This needs to be changed.
83 #define quitser pipeser
86 CT parse_mime(char *);
91 void set_endian(void);
92 void flush_errors(void);
95 void list_all_messages(CT *, int, int, int, int);
98 void free_content(CT);
100 void freects_done(int) NORETURN;
105 static void pipeser(int);
109 main(int argc, char **argv)
111 int sizesw = 1, headsw = 1;
112 int msgnum, *icachesw;
113 char *cp, *file = NULL, *folder = NULL;
114 char *maildir, buf[100], **argp;
116 struct msgs_array msgs = { 0, 0, NULL };
117 struct msgs *mp = NULL;
123 setlocale(LC_ALL, "");
125 invo_name = mhbasename(argv[0]);
127 /* read user profile/context */
130 arguments = getarguments(invo_name, argc, argv, 1);
136 while ((cp = *argp++)) {
138 switch (smatch(++cp, switches)) {
140 ambigsw(cp, switches);
143 adios(NULL, "-%s unknown", cp);
146 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
147 print_help(buf, switches, 1);
150 print_version(invo_name);
154 icachesw = &rcachesw;
157 icachesw = &wcachesw;
159 if (!(cp = *argp++) || *cp == '-')
160 adios(NULL, "missing argument to %s", argp[-2]);
161 switch (*icachesw = smatch(cp, caches)) {
166 adios(NULL, "%s unknown", cp);
194 if (!(cp = *argp++) || *cp == '-')
195 adios(NULL, "missing argument to %s",
198 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
203 if (!(cp = *argp++) || *cp == '-')
204 adios(NULL, "missing argument to %s",
207 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
212 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
213 adios(NULL, "missing argument to %s",
215 file = *cp == '-' ? cp : getcpy(expanddir(cp));
229 if (*cp == '+' || *cp == '@') {
231 adios(NULL, "only one folder at a time!");
233 folder = getcpy(expandfol(cp));
235 app_msgarg(&msgs, cp);
238 /* null terminate the list of acceptable parts/types */
244 /* Check for public cache location */
245 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
248 /* Check for private cache location */
249 if (!(cache_private = context_find(nmhprivcache)))
250 cache_private = ".cache";
251 cache_private = getcpy(toabsdir(cache_private));
254 ** Check for storage directory. If specified,
255 ** then store temporary files there. Else we
256 ** store them in standard nmh directory.
258 if ((cp = context_find(nmhstorage)) && *cp)
259 tmp = concat(cp, "/", invo_name, NULL);
261 tmp = getcpy(toabsdir(invo_name));
263 if (file && msgs.size)
264 adios(NULL, "cannot specify msg and file at same time!");
267 ** check if message is coming from file
270 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
271 adios(NULL, "out of memory");
274 if ((ct = parse_mime(file)))
278 ** message(s) are coming from a folder
281 app_msgarg(&msgs, seq_cur);
283 folder = getcurfol();
284 maildir = toabsdir(folder);
286 if (chdir(maildir) == NOTOK)
287 adios(maildir, "unable to change directory to");
289 /* read folder and create message structure */
290 if (!(mp = folder_read(folder)))
291 adios(NULL, "unable to read folder %s", folder);
293 /* check for empty folder */
295 adios(NULL, "no messages in %s", folder);
297 /* parse all the message ranges/sequences and set SELECTED */
298 for (msgnum = 0; msgnum < msgs.size; msgnum++)
299 if (!m_convert(mp, msgs.msgs[msgnum]))
301 seq_setprev(mp); /* set the previous-sequence */
303 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
305 adios(NULL, "out of memory");
308 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
309 if (is_selected(mp, msgnum)) {
312 msgnam = m_name(msgnum);
313 if ((ct = parse_mime(msgnam)))
323 SIGNAL(SIGQUIT, quitser);
324 SIGNAL(SIGPIPE, pipeser);
327 ** Get the associated umask for the relevant contents.
329 for (ctp = cts; *ctp; ctp++) {
333 if (type_ok(ct, 1) && !ct->c_umask) {
334 if (stat(ct->c_file, &st) != NOTOK)
335 ct->c_umask = ~(st.st_mode & 0777);
337 ct->c_umask = ~m_gmprot();
342 ** List the message content
344 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
346 /* Now free all the structures for the content */
347 for (ctp = cts; *ctp; ctp++)
353 /* If reading from a folder, do some updating */
355 context_replace(curfolder, folder); /* update current folder */
356 seq_setcur(mp, mp->hghsel); /* update current message */
357 seq_save(mp); /* synchronize sequences */
358 context_save(); /* save the context file */
372 fprintf(stderr, "\n");