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>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
20 static struct swit switches[] = {
33 #define FILESW 6 /* interface from show */
38 { "type content", 0 },
40 { "rcache policy", 0 },
42 { "wcache policy", 0 },
54 extern char *tmp; /* directory to place temp files */
59 extern char *cache_public;
60 extern char *cache_private;
65 extern char *parts[NPARTS + 1];
66 extern char *types[NTYPES + 1];
70 ** This is currently needed to keep mhparse happy.
71 ** This needs to be changed.
78 #define quitser pipeser
81 CT parse_mime(char *);
86 void set_endian(void);
87 void flush_errors(void);
90 void list_all_messages(CT *, int, int, int, int);
93 void free_content(CT);
95 void freects_done(int) NORETURN;
100 static void pipeser(int);
104 main(int argc, char **argv)
106 int sizesw = 1, headsw = 1;
107 int msgnum, *icachesw;
108 char *cp, *file = NULL, *folder = NULL;
109 char *maildir, buf[100], **argp;
111 struct msgs_array msgs = { 0, 0, NULL };
112 struct msgs *mp = NULL;
118 setlocale(LC_ALL, "");
120 invo_name = mhbasename(argv[0]);
122 /* read user profile/context */
125 arguments = getarguments(invo_name, argc, argv, 1);
131 while ((cp = *argp++)) {
133 switch (smatch(++cp, switches)) {
135 ambigsw(cp, switches);
138 adios(NULL, "-%s unknown", cp);
141 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
142 print_help(buf, switches, 1);
145 print_version(invo_name);
149 icachesw = &rcachesw;
152 icachesw = &wcachesw;
154 if (!(cp = *argp++) || *cp == '-')
155 adios(NULL, "missing argument to %s", argp[-2]);
156 switch (*icachesw = smatch(cp, caches)) {
161 adios(NULL, "%s unknown", cp);
182 if (!(cp = *argp++) || *cp == '-')
183 adios(NULL, "missing argument to %s",
186 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
191 if (!(cp = *argp++) || *cp == '-')
192 adios(NULL, "missing argument to %s",
195 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
200 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
201 adios(NULL, "missing argument to %s",
203 file = *cp == '-' ? cp : getcpy(expanddir(cp));
217 if (*cp == '+' || *cp == '@') {
219 adios(NULL, "only one folder at a time!");
221 folder = getcpy(expandfol(cp));
223 app_msgarg(&msgs, cp);
226 /* null terminate the list of acceptable parts/types */
232 /* Check for public cache location */
233 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
236 /* Check for private cache location */
237 if (!(cache_private = context_find(nmhprivcache)))
238 cache_private = ".cache";
239 cache_private = getcpy(toabsdir(cache_private));
242 ** Check for storage directory. If specified,
243 ** then store temporary files there. Else we
244 ** store them in standard nmh directory.
246 if ((cp = context_find(nmhstorage)) && *cp)
247 tmp = concat(cp, "/", invo_name, NULL);
249 tmp = getcpy(toabsdir(invo_name));
251 if (file && msgs.size)
252 adios(NULL, "cannot specify msg and file at same time!");
255 ** check if message is coming from file
258 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
259 adios(NULL, "out of memory");
262 if ((ct = parse_mime(file)))
266 ** message(s) are coming from a folder
269 app_msgarg(&msgs, seq_cur);
271 folder = getcurfol();
272 maildir = toabsdir(folder);
274 if (chdir(maildir) == NOTOK)
275 adios(maildir, "unable to change directory to");
277 /* read folder and create message structure */
278 if (!(mp = folder_read(folder)))
279 adios(NULL, "unable to read folder %s", folder);
281 /* check for empty folder */
283 adios(NULL, "no messages in %s", folder);
285 /* parse all the message ranges/sequences and set SELECTED */
286 for (msgnum = 0; msgnum < msgs.size; msgnum++)
287 if (!m_convert(mp, msgs.msgs[msgnum]))
289 seq_setprev(mp); /* set the previous-sequence */
291 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
293 adios(NULL, "out of memory");
296 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
297 if (is_selected(mp, msgnum)) {
300 msgnam = m_name(msgnum);
301 if ((ct = parse_mime(msgnam)))
311 SIGNAL(SIGQUIT, quitser);
312 SIGNAL(SIGPIPE, pipeser);
315 ** Get the associated umask for the relevant contents.
317 for (ctp = cts; *ctp; ctp++) {
321 if (type_ok(ct, 1) && !ct->c_umask) {
322 if (stat(ct->c_file, &st) != NOTOK)
323 ct->c_umask = ~(st.st_mode & 0777);
325 ct->c_umask = ~m_gmprot();
330 ** List the message content
332 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
334 /* Now free all the structures for the content */
335 for (ctp = cts; *ctp; ctp++)
341 /* If reading from a folder, do some updating */
343 context_replace(curfolder, folder); /* update current folder */
344 seq_setcur(mp, mp->hghsel); /* update current message */
345 seq_save(mp); /* synchronize sequences */
346 context_save(); /* save the context file */
360 fprintf(stderr, "\n");