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 },
54 ** switches for debugging
63 extern char *tmp; /* directory to place temp files */
68 extern char *cache_public;
69 extern char *cache_private;
74 extern char *parts[NPARTS + 1];
75 extern char *types[NTYPES + 1];
79 ** This is currently needed to keep mhparse happy.
80 ** This needs to be changed.
87 #define quitser pipeser
90 CT parse_mime(char *);
95 void set_endian(void);
96 void flush_errors(void);
99 void list_all_messages(CT *, int, int, int, int);
102 void free_content(CT);
104 void freects_done(int) NORETURN;
109 static RETSIGTYPE pipeser(int);
113 main(int argc, char **argv)
115 int sizesw = 1, headsw = 1;
116 int msgnum, *icachesw;
117 char *cp, *file = NULL, *folder = NULL;
118 char *maildir, buf[100], **argp;
120 struct msgs_array msgs = { 0, 0, NULL };
121 struct msgs *mp = NULL;
127 setlocale(LC_ALL, "");
129 invo_name = mhbasename(argv[0]);
131 /* read user profile/context */
134 arguments = getarguments(invo_name, argc, argv, 1);
140 while ((cp = *argp++)) {
142 switch (smatch(++cp, switches)) {
144 ambigsw(cp, switches);
147 adios(NULL, "-%s unknown", cp);
150 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
151 print_help(buf, switches, 1);
154 print_version(invo_name);
158 icachesw = &rcachesw;
161 icachesw = &wcachesw;
163 if (!(cp = *argp++) || *cp == '-')
164 adios(NULL, "missing argument to %s", argp[-2]);
165 switch (*icachesw = smatch(cp, caches)) {
170 adios(NULL, "%s unknown", cp);
198 if (!(cp = *argp++) || *cp == '-')
199 adios(NULL, "missing argument to %s",
202 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
207 if (!(cp = *argp++) || *cp == '-')
208 adios(NULL, "missing argument to %s",
211 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
216 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
217 adios(NULL, "missing argument to %s",
219 file = *cp == '-' ? cp : getcpy(expanddir(cp));
233 if (*cp == '+' || *cp == '@') {
235 adios(NULL, "only one folder at a time!");
237 folder = getcpy(expandfol(cp));
239 app_msgarg(&msgs, cp);
242 /* null terminate the list of acceptable parts/types */
248 /* Check for public cache location */
249 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
252 /* Check for private cache location */
253 if (!(cache_private = context_find(nmhprivcache)))
254 cache_private = ".cache";
255 cache_private = getcpy(toabsdir(cache_private));
258 ** Check for storage directory. If specified,
259 ** then store temporary files there. Else we
260 ** store them in standard nmh directory.
262 if ((cp = context_find(nmhstorage)) && *cp)
263 tmp = concat(cp, "/", invo_name, NULL);
265 tmp = getcpy(toabsdir(invo_name));
267 if (file && msgs.size)
268 adios(NULL, "cannot specify msg and file at same time!");
271 ** check if message is coming from file
274 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
275 adios(NULL, "out of memory");
278 if ((ct = parse_mime(file)))
282 ** message(s) are coming from a folder
285 app_msgarg(&msgs, seq_cur);
287 folder = getcurfol();
288 maildir = toabsdir(folder);
290 if (chdir(maildir) == NOTOK)
291 adios(maildir, "unable to change directory to");
293 /* read folder and create message structure */
294 if (!(mp = folder_read(folder)))
295 adios(NULL, "unable to read folder %s", folder);
297 /* check for empty folder */
299 adios(NULL, "no messages in %s", folder);
301 /* parse all the message ranges/sequences and set SELECTED */
302 for (msgnum = 0; msgnum < msgs.size; msgnum++)
303 if (!m_convert(mp, msgs.msgs[msgnum]))
305 seq_setprev(mp); /* set the previous-sequence */
307 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
309 adios(NULL, "out of memory");
312 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
313 if (is_selected(mp, msgnum)) {
316 msgnam = m_name(msgnum);
317 if ((ct = parse_mime(msgnam)))
327 SIGNAL(SIGQUIT, quitser);
328 SIGNAL(SIGPIPE, pipeser);
331 ** Get the associated umask for the relevant contents.
333 for (ctp = cts; *ctp; ctp++) {
337 if (type_ok(ct, 1) && !ct->c_umask) {
338 if (stat(ct->c_file, &st) != NOTOK)
339 ct->c_umask = ~(st.st_mode & 0777);
341 ct->c_umask = ~m_gmprot();
346 ** List the message content
348 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
350 /* Now free all the structures for the content */
351 for (ctp = cts; *ctp; ctp++)
357 /* If reading from a folder, do some updating */
359 context_replace(curfolder, folder); /* update current folder */
360 seq_setcur(mp, mp->hghsel); /* update current message */
361 seq_save(mp); /* synchronize sequences */
362 context_save(); /* save the context file */
376 fprintf(stderr, "\n");