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[] = {
24 #define FILESW 2 /* interface from show */
29 { "type content", 0 },
41 extern char *tmp; /* directory to place temp files */
46 extern char *parts[NPARTS + 1];
47 extern char *types[NTYPES + 1];
51 ** This is currently needed to keep mhparse happy.
52 ** This needs to be changed.
59 #define quitser pipeser
62 CT parse_mime(char *);
67 void set_endian(void);
68 void flush_errors(void);
71 void list_all_messages(CT *, int, int);
74 void free_content(CT);
76 void freects_done(int) NORETURN;
81 static void pipeser(int);
85 main(int argc, char **argv)
88 char *cp, *file = NULL, *folder = NULL;
89 char *maildir, buf[100], **argp;
91 struct msgs_array msgs = { 0, 0, NULL };
92 struct msgs *mp = NULL;
98 setlocale(LC_ALL, "");
100 invo_name = mhbasename(argv[0]);
102 /* read user profile/context */
105 arguments = getarguments(invo_name, argc, argv, 1);
111 while ((cp = *argp++)) {
113 switch (smatch(++cp, switches)) {
115 ambigsw(cp, switches);
118 adios(NULL, "-%s unknown", cp);
121 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
122 print_help(buf, switches, 1);
125 print_version(invo_name);
129 if (!(cp = *argp++) || *cp == '-')
130 adios(NULL, "missing argument to %s",
133 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
138 if (!(cp = *argp++) || *cp == '-')
139 adios(NULL, "missing argument to %s",
142 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
147 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
148 adios(NULL, "missing argument to %s",
150 file = *cp == '-' ? cp : getcpy(expanddir(cp));
164 if (*cp == '+' || *cp == '@') {
166 adios(NULL, "only one folder at a time!");
168 folder = getcpy(expandfol(cp));
170 app_msgarg(&msgs, cp);
173 /* null terminate the list of acceptable parts/types */
180 ** Check for storage directory. If specified,
181 ** then store temporary files there. Else we
182 ** store them in standard nmh directory.
184 if ((cp = context_find(nmhstorage)) && *cp)
185 tmp = concat(cp, "/", invo_name, NULL);
187 tmp = getcpy(toabsdir(invo_name));
189 if (file && msgs.size)
190 adios(NULL, "cannot specify msg and file at same time!");
193 ** check if message is coming from file
196 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
197 adios(NULL, "out of memory");
200 if ((ct = parse_mime(file)))
204 ** message(s) are coming from a folder
207 app_msgarg(&msgs, seq_cur);
209 folder = getcurfol();
210 maildir = toabsdir(folder);
212 if (chdir(maildir) == NOTOK)
213 adios(maildir, "unable to change directory to");
215 /* read folder and create message structure */
216 if (!(mp = folder_read(folder)))
217 adios(NULL, "unable to read folder %s", folder);
219 /* check for empty folder */
221 adios(NULL, "no messages in %s", folder);
223 /* parse all the message ranges/sequences and set SELECTED */
224 for (msgnum = 0; msgnum < msgs.size; msgnum++)
225 if (!m_convert(mp, msgs.msgs[msgnum]))
227 seq_setprev(mp); /* set the previous-sequence */
229 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
231 adios(NULL, "out of memory");
234 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
235 if (is_selected(mp, msgnum)) {
238 msgnam = m_name(msgnum);
239 if ((ct = parse_mime(msgnam)))
249 SIGNAL(SIGQUIT, quitser);
250 SIGNAL(SIGPIPE, pipeser);
253 ** Get the associated umask for the relevant contents.
255 for (ctp = cts; *ctp; ctp++) {
259 if (type_ok(ct, 1) && !ct->c_umask) {
260 if (stat(ct->c_file, &st) != NOTOK)
261 ct->c_umask = ~(st.st_mode & 0777);
263 ct->c_umask = ~m_gmprot();
268 ** List the message content
270 list_all_messages(cts, verbosw, debugsw);
272 /* Now free all the structures for the content */
273 for (ctp = cts; *ctp; ctp++)
279 /* If reading from a folder, do some updating */
281 context_replace(curfolder, folder); /* update current folder */
282 seq_setcur(mp, mp->hghsel); /* update current message */
283 seq_save(mp); /* synchronize sequences */
284 context_save(); /* save the context file */
298 fprintf(stderr, "\n");