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>
23 static struct swit switches[] = {
28 #define FILESW 2 /* 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);
78 void free_content(CT);
85 static void pipeser(int);
89 main(int argc, char **argv)
92 char *cp, *file = NULL, *folder = NULL;
93 char *maildir, buf[100], **argp;
95 struct msgs_array msgs = { 0, 0, NULL };
96 struct msgs *mp = NULL;
99 if (atexit(freects_done) != 0) {
100 adios(EX_OSERR, NULL, "atexit failed");
103 setlocale(LC_ALL, "");
104 invo_name = mhbasename(argv[0]);
106 /* read user profile/context */
109 arguments = getarguments(invo_name, argc, argv, 1);
115 while ((cp = *argp++)) {
117 switch (smatch(++cp, switches)) {
119 ambigsw(cp, switches);
122 adios(EX_USAGE, NULL, "-%s unknown", cp);
125 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
126 print_help(buf, switches, 1);
127 exit(argc == 2 ? EX_OK : EX_USAGE);
129 print_version(invo_name);
130 exit(argc == 2 ? EX_OK : EX_USAGE);
133 if (!(cp = *argp++) || *cp == '-')
134 adios(EX_USAGE, NULL, "missing argument to %s",
137 adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
142 if (!(cp = *argp++) || *cp == '-')
143 adios(EX_USAGE, NULL, "missing argument to %s",
146 adios(EX_USAGE, NULL, "too many types (starting with %s), %d max", cp, NTYPES);
151 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
152 adios(EX_USAGE, NULL, "missing argument to %s",
154 file = *cp == '-' ? cp : getcpy(expanddir(cp));
168 if (*cp == '+' || *cp == '@') {
170 adios(EX_USAGE, NULL, "only one folder at a time!");
172 folder = getcpy(expandfol(cp));
174 app_msgarg(&msgs, cp);
177 /* null terminate the list of acceptable parts/types */
184 ** Check for storage directory. If specified,
185 ** then store temporary files there. Else we
186 ** store them in standard nmh directory.
188 if ((cp = context_find(nmhstorage)) && *cp)
189 tmp = concat(cp, "/", invo_name, NULL);
191 tmp = getcpy(toabsdir(invo_name));
193 if (file && msgs.size)
194 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
197 ** check if message is coming from file
200 if (!(cts = (CT *) mh_xcalloc((size_t) 2, sizeof(*cts))))
201 adios(EX_OSERR, NULL, "out of memory");
204 if ((ct = parse_mime(file)))
208 ** message(s) are coming from a folder
211 app_msgarg(&msgs, seq_cur);
213 folder = getcurfol();
214 maildir = toabsdir(folder);
216 if (chdir(maildir) == NOTOK)
217 adios(EX_OSERR, maildir, "unable to change directory to");
219 /* read folder and create message structure */
220 if (!(mp = folder_read(folder)))
221 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
223 /* check for empty folder */
225 adios(EX_DATAERR, NULL, "no messages in %s", folder);
227 /* parse all the message ranges/sequences and set SELECTED */
228 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
229 if (!m_convert(mp, msgs.msgs[msgnum])) {
233 seq_setprev(mp); /* set the previous-sequence */
235 if (!(cts = (CT *) mh_xcalloc((size_t) (mp->numsel + 1),
237 adios(EX_OSERR, 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");