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 : mh_xstrdup(expanddir(cp));
168 if (*cp == '+' || *cp == '@') {
170 adios(EX_USAGE, NULL, "only one folder at a time!");
172 folder = mh_xstrdup(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 = mh_xstrdup(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 cts = mh_xcalloc(2, sizeof(*cts));
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(EX_OSERR, maildir, "unable to change directory to");
218 /* read folder and create message structure */
219 if (!(mp = folder_read(folder)))
220 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
222 /* check for empty folder */
224 adios(EX_DATAERR, 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])) {
232 seq_setprev(mp); /* set the previous-sequence */
234 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
237 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
238 if (is_selected(mp, msgnum)) {
241 msgnam = m_name(msgnum);
242 if ((ct = parse_mime(msgnam)))
252 SIGNAL(SIGQUIT, quitser);
253 SIGNAL(SIGPIPE, pipeser);
256 ** Get the associated umask for the relevant contents.
258 for (ctp = cts; *ctp; ctp++) {
262 if (type_ok(ct, 1) && !ct->c_umask) {
263 if (stat(ct->c_file, &st) != NOTOK)
264 ct->c_umask = ~(st.st_mode & 0777);
266 ct->c_umask = ~m_gmprot();
271 ** List the message content
273 list_all_messages(cts, verbosw, debugsw);
275 /* Now free all the structures for the content */
276 for (ctp = cts; *ctp; ctp++)
281 /* If reading from a folder, do some updating */
283 context_replace(curfolder, folder); /* update current folder */
284 seq_setcur(mp, mp->hghsel); /* update current message */
285 seq_save(mp); /* synchronize sequences */
286 context_save(); /* save the context file */
299 fprintf(stderr, "\n");