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 },
43 char *version=VERSION;
46 extern char *tmp; /* directory to place temp files */
51 extern char *parts[NPARTS + 1];
52 extern char *types[NTYPES + 1];
56 ** This is currently needed to keep mhparse happy.
57 ** This needs to be changed.
64 #define quitser pipeser
67 CT parse_mime(char *);
72 void set_endian(void);
73 void flush_errors(void);
76 void list_all_messages(CT *, int, int);
79 void free_content(CT);
86 static void pipeser(int);
90 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;
100 if (atexit(freects_done) != 0) {
101 adios(EX_OSERR, NULL, "atexit failed");
104 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(EX_USAGE, NULL, "-%s unknown", cp);
126 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
127 print_help(buf, switches, 1);
128 exit(argc == 2 ? EX_OK : EX_USAGE);
130 print_version(invo_name);
131 exit(argc == 2 ? EX_OK : EX_USAGE);
134 if (!(cp = *argp++) || *cp == '-')
135 adios(EX_USAGE, NULL, "missing argument to %s",
138 adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
143 if (!(cp = *argp++) || *cp == '-')
144 adios(EX_USAGE, NULL, "missing argument to %s",
147 adios(EX_USAGE, NULL, "too many types (starting with %s), %d max", cp, NTYPES);
152 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
153 adios(EX_USAGE, NULL, "missing argument to %s",
155 file = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
169 if (*cp == '+' || *cp == '@') {
171 adios(EX_USAGE, NULL, "only one folder at a time!");
173 folder = mh_xstrdup(expandfol(cp));
175 app_msgarg(&msgs, cp);
178 /* null terminate the list of acceptable parts/types */
185 ** Check for storage directory. If specified,
186 ** then store temporary files there. Else we
187 ** store them in standard nmh directory.
189 if ((cp = context_find(nmhstorage)) && *cp)
190 tmp = concat(cp, "/", invo_name, NULL);
192 tmp = mh_xstrdup(toabsdir(invo_name));
194 if (file && msgs.size)
195 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
198 ** check if message is coming from file
201 cts = mh_xcalloc(2, sizeof(*cts));
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 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
238 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
239 if (is_selected(mp, msgnum)) {
242 msgnam = m_name(msgnum);
243 if ((ct = parse_mime(msgnam)))
253 SIGNAL(SIGQUIT, quitser);
254 SIGNAL(SIGPIPE, pipeser);
257 ** Get the associated umask for the relevant contents.
259 for (ctp = cts; *ctp; ctp++) {
263 if (type_ok(ct, 1) && !ct->c_umask) {
264 if (stat(ct->c_file, &st) != NOTOK)
265 ct->c_umask = ~(st.st_mode & 0777);
267 ct->c_umask = ~m_gmprot();
272 ** List the message content
274 list_all_messages(cts, verbosw, debugsw);
276 /* Now free all the structures for the content */
277 for (ctp = cts; *ctp; ctp++)
282 /* If reading from a folder, do some updating */
284 context_replace(curfolder, folder); /* update current folder */
285 seq_setcur(mp, mp->hghsel); /* update current message */
286 seq_save(mp); /* synchronize sequences */
287 context_save(); /* save the context file */
300 fprintf(stderr, "\n");