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;
97 setlocale(LC_ALL, "");
98 invo_name = mhbasename(argv[0]);
100 /* read user profile/context */
103 arguments = getarguments(invo_name, argc, argv, 1);
109 while ((cp = *argp++)) {
111 switch (smatch(++cp, switches)) {
113 ambigsw(cp, switches);
116 adios(NULL, "-%s unknown", cp);
119 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
120 print_help(buf, switches, 1);
123 print_version(invo_name);
127 if (!(cp = *argp++) || *cp == '-')
128 adios(NULL, "missing argument to %s",
131 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
136 if (!(cp = *argp++) || *cp == '-')
137 adios(NULL, "missing argument to %s",
140 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
145 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
146 adios(NULL, "missing argument to %s",
148 file = *cp == '-' ? cp : getcpy(expanddir(cp));
162 if (*cp == '+' || *cp == '@') {
164 adios(NULL, "only one folder at a time!");
166 folder = getcpy(expandfol(cp));
168 app_msgarg(&msgs, cp);
171 /* null terminate the list of acceptable parts/types */
178 ** Check for storage directory. If specified,
179 ** then store temporary files there. Else we
180 ** store them in standard nmh directory.
182 if ((cp = context_find(nmhstorage)) && *cp)
183 tmp = concat(cp, "/", invo_name, NULL);
185 tmp = getcpy(toabsdir(invo_name));
187 if (file && msgs.size)
188 adios(NULL, "cannot specify msg and file at same time!");
191 ** check if message is coming from file
194 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
195 adios(NULL, "out of memory");
198 if ((ct = parse_mime(file)))
202 ** message(s) are coming from a folder
205 app_msgarg(&msgs, seq_cur);
207 folder = getcurfol();
208 maildir = toabsdir(folder);
210 if (chdir(maildir) == NOTOK)
211 adios(maildir, "unable to change directory to");
213 /* read folder and create message structure */
214 if (!(mp = folder_read(folder)))
215 adios(NULL, "unable to read folder %s", folder);
217 /* check for empty folder */
219 adios(NULL, "no messages in %s", folder);
221 /* parse all the message ranges/sequences and set SELECTED */
222 for (msgnum = 0; msgnum < msgs.size; msgnum++)
223 if (!m_convert(mp, msgs.msgs[msgnum]))
225 seq_setprev(mp); /* set the previous-sequence */
227 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
229 adios(NULL, "out of memory");
232 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
233 if (is_selected(mp, msgnum)) {
236 msgnam = m_name(msgnum);
237 if ((ct = parse_mime(msgnam)))
247 SIGNAL(SIGQUIT, quitser);
248 SIGNAL(SIGPIPE, pipeser);
251 ** Get the associated umask for the relevant contents.
253 for (ctp = cts; *ctp; ctp++) {
257 if (type_ok(ct, 1) && !ct->c_umask) {
258 if (stat(ct->c_file, &st) != NOTOK)
259 ct->c_umask = ~(st.st_mode & 0777);
261 ct->c_umask = ~m_gmprot();
266 ** List the message content
268 list_all_messages(cts, verbosw, debugsw);
270 /* Now free all the structures for the content */
271 for (ctp = cts; *ctp; ctp++)
277 /* If reading from a folder, do some updating */
279 context_replace(curfolder, folder); /* update current folder */
280 seq_setcur(mp, mp->hghsel); /* update current message */
281 seq_save(mp); /* synchronize sequences */
282 context_save(); /* save the context file */
296 fprintf(stderr, "\n");