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>
22 static struct swit switches[] = {
27 #define FILESW 2 /* interface from show */
32 { "type content", 0 },
44 extern char *tmp; /* directory to place temp files */
49 extern char *parts[NPARTS + 1];
50 extern char *types[NTYPES + 1];
54 ** This is currently needed to keep mhparse happy.
55 ** This needs to be changed.
62 #define quitser pipeser
65 CT parse_mime(char *);
70 void set_endian(void);
71 void flush_errors(void);
74 void list_all_messages(CT *, int, int);
77 void free_content(CT);
84 static void pipeser(int);
88 main(int argc, char **argv)
91 char *cp, *file = NULL, *folder = NULL;
92 char *maildir, buf[100], **argp;
94 struct msgs_array msgs = { 0, 0, NULL };
95 struct msgs *mp = NULL;
98 if (atexit(freects_done) != 0) {
99 adios(NULL, "atexit failed");
102 setlocale(LC_ALL, "");
103 invo_name = mhbasename(argv[0]);
105 /* read user profile/context */
108 arguments = getarguments(invo_name, argc, argv, 1);
114 while ((cp = *argp++)) {
116 switch (smatch(++cp, switches)) {
118 ambigsw(cp, switches);
121 adios(NULL, "-%s unknown", cp);
124 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
125 print_help(buf, switches, 1);
128 print_version(invo_name);
132 if (!(cp = *argp++) || *cp == '-')
133 adios(NULL, "missing argument to %s",
136 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
141 if (!(cp = *argp++) || *cp == '-')
142 adios(NULL, "missing argument to %s",
145 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
150 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
151 adios(NULL, "missing argument to %s",
153 file = *cp == '-' ? cp : getcpy(expanddir(cp));
167 if (*cp == '+' || *cp == '@') {
169 adios(NULL, "only one folder at a time!");
171 folder = getcpy(expandfol(cp));
173 app_msgarg(&msgs, cp);
176 /* null terminate the list of acceptable parts/types */
183 ** Check for storage directory. If specified,
184 ** then store temporary files there. Else we
185 ** store them in standard nmh directory.
187 if ((cp = context_find(nmhstorage)) && *cp)
188 tmp = concat(cp, "/", invo_name, NULL);
190 tmp = getcpy(toabsdir(invo_name));
192 if (file && msgs.size)
193 adios(NULL, "cannot specify msg and file at same time!");
196 ** check if message is coming from file
199 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
200 adios(NULL, "out of memory");
203 if ((ct = parse_mime(file)))
207 ** message(s) are coming from a folder
210 app_msgarg(&msgs, seq_cur);
212 folder = getcurfol();
213 maildir = toabsdir(folder);
215 if (chdir(maildir) == NOTOK)
216 adios(maildir, "unable to change directory to");
218 /* read folder and create message structure */
219 if (!(mp = folder_read(folder)))
220 adios(NULL, "unable to read folder %s", folder);
222 /* check for empty folder */
224 adios(NULL, "no messages in %s", folder);
226 /* parse all the message ranges/sequences and set SELECTED */
227 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
228 if (!m_convert(mp, msgs.msgs[msgnum])) {
229 /* sysexits.h EX_USAGE */
233 seq_setprev(mp); /* set the previous-sequence */
235 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
237 adios(NULL, "out of memory");
240 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
241 if (is_selected(mp, msgnum)) {
244 msgnam = m_name(msgnum);
245 if ((ct = parse_mime(msgnam)))
255 SIGNAL(SIGQUIT, quitser);
256 SIGNAL(SIGPIPE, pipeser);
259 ** Get the associated umask for the relevant contents.
261 for (ctp = cts; *ctp; ctp++) {
265 if (type_ok(ct, 1) && !ct->c_umask) {
266 if (stat(ct->c_file, &st) != NOTOK)
267 ct->c_umask = ~(st.st_mode & 0777);
269 ct->c_umask = ~m_gmprot();
274 ** List the message content
276 list_all_messages(cts, verbosw, debugsw);
278 /* Now free all the structures for the content */
279 for (ctp = cts; *ctp; ctp++)
285 /* If reading from a folder, do some updating */
287 context_replace(curfolder, folder); /* update current folder */
288 seq_setcur(mp, mp->hghsel); /* update current message */
289 seq_save(mp); /* synchronize sequences */
290 context_save(); /* save the context file */
303 fprintf(stderr, "\n");