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>
19 static struct swit switches[] = {
32 #define FILESW 6 /* interface from show */
37 { "type content", 0 },
49 extern char *tmp; /* directory to place temp files */
54 extern char *parts[NPARTS + 1];
55 extern char *types[NTYPES + 1];
59 ** This is currently needed to keep mhparse happy.
60 ** This needs to be changed.
67 #define quitser pipeser
70 CT parse_mime(char *);
75 void set_endian(void);
76 void flush_errors(void);
79 void list_all_messages(CT *, int, int, int, int);
82 void free_content(CT);
84 void freects_done(int) NORETURN;
89 static void pipeser(int);
93 main(int argc, char **argv)
95 int sizesw = 1, headsw = 1;
97 char *cp, *file = NULL, *folder = NULL;
98 char *maildir, buf[100], **argp;
100 struct msgs_array msgs = { 0, 0, NULL };
101 struct msgs *mp = NULL;
107 setlocale(LC_ALL, "");
109 invo_name = mhbasename(argv[0]);
111 /* read user profile/context */
114 arguments = getarguments(invo_name, argc, argv, 1);
120 while ((cp = *argp++)) {
122 switch (smatch(++cp, switches)) {
124 ambigsw(cp, switches);
127 adios(NULL, "-%s unknown", cp);
130 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
131 print_help(buf, switches, 1);
134 print_version(invo_name);
152 if (!(cp = *argp++) || *cp == '-')
153 adios(NULL, "missing argument to %s",
156 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
161 if (!(cp = *argp++) || *cp == '-')
162 adios(NULL, "missing argument to %s",
165 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
170 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
171 adios(NULL, "missing argument to %s",
173 file = *cp == '-' ? cp : getcpy(expanddir(cp));
187 if (*cp == '+' || *cp == '@') {
189 adios(NULL, "only one folder at a time!");
191 folder = getcpy(expandfol(cp));
193 app_msgarg(&msgs, cp);
196 /* null terminate the list of acceptable parts/types */
203 ** Check for storage directory. If specified,
204 ** then store temporary files there. Else we
205 ** store them in standard nmh directory.
207 if ((cp = context_find(nmhstorage)) && *cp)
208 tmp = concat(cp, "/", invo_name, NULL);
210 tmp = getcpy(toabsdir(invo_name));
212 if (file && msgs.size)
213 adios(NULL, "cannot specify msg and file at same time!");
216 ** check if message is coming from file
219 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
220 adios(NULL, "out of memory");
223 if ((ct = parse_mime(file)))
227 ** message(s) are coming from a folder
230 app_msgarg(&msgs, seq_cur);
232 folder = getcurfol();
233 maildir = toabsdir(folder);
235 if (chdir(maildir) == NOTOK)
236 adios(maildir, "unable to change directory to");
238 /* read folder and create message structure */
239 if (!(mp = folder_read(folder)))
240 adios(NULL, "unable to read folder %s", folder);
242 /* check for empty folder */
244 adios(NULL, "no messages in %s", folder);
246 /* parse all the message ranges/sequences and set SELECTED */
247 for (msgnum = 0; msgnum < msgs.size; msgnum++)
248 if (!m_convert(mp, msgs.msgs[msgnum]))
250 seq_setprev(mp); /* set the previous-sequence */
252 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
254 adios(NULL, "out of memory");
257 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
258 if (is_selected(mp, msgnum)) {
261 msgnam = m_name(msgnum);
262 if ((ct = parse_mime(msgnam)))
272 SIGNAL(SIGQUIT, quitser);
273 SIGNAL(SIGPIPE, pipeser);
276 ** Get the associated umask for the relevant contents.
278 for (ctp = cts; *ctp; ctp++) {
282 if (type_ok(ct, 1) && !ct->c_umask) {
283 if (stat(ct->c_file, &st) != NOTOK)
284 ct->c_umask = ~(st.st_mode & 0777);
286 ct->c_umask = ~m_gmprot();
291 ** List the message content
293 list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
295 /* Now free all the structures for the content */
296 for (ctp = cts; *ctp; ctp++)
302 /* If reading from a folder, do some updating */
304 context_replace(curfolder, folder); /* update current folder */
305 seq_setcur(mp, mp->hghsel); /* update current message */
306 seq_save(mp); /* synchronize sequences */
307 context_save(); /* save the context file */
321 fprintf(stderr, "\n");