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[] = {
28 #define FILESW 4 /* interface from show */
33 { "type content", 0 },
45 extern char *tmp; /* directory to place temp files */
50 extern char *parts[NPARTS + 1];
51 extern char *types[NTYPES + 1];
55 ** This is currently needed to keep mhparse happy.
56 ** This needs to be changed.
63 #define quitser pipeser
66 CT parse_mime(char *);
71 void set_endian(void);
72 void flush_errors(void);
75 void list_all_messages(CT *, int, int, int);
78 void free_content(CT);
80 void freects_done(int) NORETURN;
85 static void pipeser(int);
89 main(int argc, char **argv)
93 char *cp, *file = NULL, *folder = NULL;
94 char *maildir, buf[100], **argp;
96 struct msgs_array msgs = { 0, 0, NULL };
97 struct msgs *mp = NULL;
103 setlocale(LC_ALL, "");
105 invo_name = mhbasename(argv[0]);
107 /* read user profile/context */
110 arguments = getarguments(invo_name, argc, argv, 1);
116 while ((cp = *argp++)) {
118 switch (smatch(++cp, switches)) {
120 ambigsw(cp, switches);
123 adios(NULL, "-%s unknown", cp);
126 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
127 print_help(buf, switches, 1);
130 print_version(invo_name);
141 if (!(cp = *argp++) || *cp == '-')
142 adios(NULL, "missing argument to %s",
145 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
150 if (!(cp = *argp++) || *cp == '-')
151 adios(NULL, "missing argument to %s",
154 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
159 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
160 adios(NULL, "missing argument to %s",
162 file = *cp == '-' ? cp : getcpy(expanddir(cp));
176 if (*cp == '+' || *cp == '@') {
178 adios(NULL, "only one folder at a time!");
180 folder = getcpy(expandfol(cp));
182 app_msgarg(&msgs, cp);
185 /* null terminate the list of acceptable parts/types */
192 ** Check for storage directory. If specified,
193 ** then store temporary files there. Else we
194 ** store them in standard nmh directory.
196 if ((cp = context_find(nmhstorage)) && *cp)
197 tmp = concat(cp, "/", invo_name, NULL);
199 tmp = getcpy(toabsdir(invo_name));
201 if (file && msgs.size)
202 adios(NULL, "cannot specify msg and file at same time!");
205 ** check if message is coming from file
208 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
209 adios(NULL, "out of memory");
212 if ((ct = parse_mime(file)))
216 ** message(s) are coming from a folder
219 app_msgarg(&msgs, seq_cur);
221 folder = getcurfol();
222 maildir = toabsdir(folder);
224 if (chdir(maildir) == NOTOK)
225 adios(maildir, "unable to change directory to");
227 /* read folder and create message structure */
228 if (!(mp = folder_read(folder)))
229 adios(NULL, "unable to read folder %s", folder);
231 /* check for empty folder */
233 adios(NULL, "no messages in %s", folder);
235 /* parse all the message ranges/sequences and set SELECTED */
236 for (msgnum = 0; msgnum < msgs.size; msgnum++)
237 if (!m_convert(mp, msgs.msgs[msgnum]))
239 seq_setprev(mp); /* set the previous-sequence */
241 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
243 adios(NULL, "out of memory");
246 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
247 if (is_selected(mp, msgnum)) {
250 msgnam = m_name(msgnum);
251 if ((ct = parse_mime(msgnam)))
261 SIGNAL(SIGQUIT, quitser);
262 SIGNAL(SIGPIPE, pipeser);
265 ** Get the associated umask for the relevant contents.
267 for (ctp = cts; *ctp; ctp++) {
271 if (type_ok(ct, 1) && !ct->c_umask) {
272 if (stat(ct->c_file, &st) != NOTOK)
273 ct->c_umask = ~(st.st_mode & 0777);
275 ct->c_umask = ~m_gmprot();
280 ** List the message content
282 list_all_messages(cts, headsw, verbosw, debugsw);
284 /* Now free all the structures for the content */
285 for (ctp = cts; *ctp; ctp++)
291 /* If reading from a folder, do some updating */
293 context_replace(curfolder, folder); /* update current folder */
294 seq_setcur(mp, mp->hghsel); /* update current message */
295 seq_save(mp); /* synchronize sequences */
296 context_save(); /* save the context file */
310 fprintf(stderr, "\n");