2 ** mhshow.c -- display 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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
25 static struct swit switches[] = {
33 { "noserialonly", 0 },
38 #define FILESW 6 /* interface from show */
41 { "form formfile", 0 },
45 { "type content", 0 },
47 { "rcache policy", 0 },
49 { "wcache policy", 0 },
55 ** switches for moreproc/mhlproc
58 { "moreproc program", -4 },
60 { "length lines", -4 },
62 { "width columns", -4 },
64 ** switches for debugging
73 extern char *tmp; /* directory to place temp files */
78 extern char *cache_public;
79 extern char *cache_private;
90 extern char *parts[NPARTS + 1];
91 extern char *types[NTYPES + 1];
97 #define quitser pipeser
100 CT parse_mime(char *);
103 int part_ok(CT, int);
104 int type_ok(CT, int);
105 void set_endian(void);
106 void flush_errors(void);
109 void show_all_messages(CT *);
112 void free_content(CT);
114 void freects_done(int) NORETURN;
119 static RETSIGTYPE pipeser(int);
123 main(int argc, char **argv)
125 int msgnum, *icachesw;
126 char *cp, *file = NULL, *folder = NULL;
127 char *maildir, buf[100], **argp;
129 struct msgs_array msgs = { 0, 0, NULL };
130 struct msgs *mp = NULL;
137 setlocale(LC_ALL, "");
139 invo_name = mhbasename(argv[0]);
141 /* read user profile/context */
144 arguments = getarguments(invo_name, argc, argv, 1);
150 while ((cp = *argp++)) {
152 switch (smatch(++cp, switches)) {
154 ambigsw(cp, switches);
157 adios(NULL, "-%s unknown", cp);
160 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
161 print_help(buf, switches, 1);
164 print_version(invo_name);
168 icachesw = &rcachesw;
171 icachesw = &wcachesw;
173 if (!(cp = *argp++) || *cp == '-')
174 adios(NULL, "missing argument to %s",
176 switch (*icachesw = smatch(cp, caches)) {
181 adios(NULL, "%s unknown", cp);
202 if (!(cp = *argp++) || *cp == '-')
203 adios(NULL, "missing argument to %s",
206 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
211 if (!(cp = *argp++) || *cp == '-')
212 adios(NULL, "missing argument to %s",
215 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
220 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
221 adios(NULL, "missing argument to %s",
223 file = *cp == '-' ? cp : getcpy(expanddir(cp));
227 if (!(cp = *argp++) || *cp == '-')
228 adios(NULL, "missing argument to %s",
232 formsw = getcpy(etcpath(cp));
236 ** Switches for moreproc/mhlproc
239 if (!(progsw = *argp++) || *progsw == '-')
240 adios(NULL, "missing argument to %s",
246 if (!(cp = *argp++) || *cp == '-')
247 adios(NULL, "missing argument to %s",
262 if (*cp == '+' || *cp == '@') {
264 adios(NULL, "only one folder at a time!");
266 folder = getcpy(expandfol(cp));
268 app_msgarg(&msgs, cp);
271 /* null terminate the list of acceptable parts/types */
277 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
282 ** Check if we've specified an additional profile
284 if ((cp = getenv("MHSHOW"))) {
285 if ((fp = fopen(cp, "r"))) {
286 readconfig((struct node **) 0, fp, cp, 0);
289 admonish("", "unable to read $MHSHOW profile (%s)",
295 ** Read the standard profile setup
297 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
298 readconfig((struct node **) 0, fp, cp, 0);
302 /* Check for public cache location */
303 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
306 /* Check for private cache location */
307 if (!(cache_private = context_find(nmhprivcache)))
308 cache_private = ".cache";
309 cache_private = getcpy(toabsdir(cache_private));
312 ** Check for storage directory. If specified,
313 ** then store temporary files there. Else we
314 ** store them in standard nmh directory.
316 if ((cp = context_find(nmhstorage)) && *cp)
317 tmp = concat(cp, "/", invo_name, NULL);
319 tmp = getcpy(toabsdir(invo_name));
321 if (file && msgs.size)
322 adios(NULL, "cannot specify msg and file at same time!");
325 ** check if message is coming from file
328 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
329 adios(NULL, "out of memory");
332 if ((ct = parse_mime(file)));
336 ** message(s) are coming from a folder
339 app_msgarg(&msgs, seq_cur);
341 folder = getcurfol();
342 maildir = toabsdir(folder);
344 if (chdir(maildir) == NOTOK)
345 adios(maildir, "unable to change directory to");
347 /* read folder and create message structure */
348 if (!(mp = folder_read(folder)))
349 adios(NULL, "unable to read folder %s", folder);
351 /* check for empty folder */
353 adios(NULL, "no messages in %s", folder);
355 /* parse all the message ranges/sequences and set SELECTED */
356 for (msgnum = 0; msgnum < msgs.size; msgnum++)
357 if (!m_convert(mp, msgs.msgs[msgnum]))
361 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
362 ** since we will use that as a tag to know which messages
363 ** to remove from the "unseen" sequence.
365 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
366 if (is_selected(mp, msgnum))
367 set_unseen(mp, msgnum);
369 seq_setprev(mp); /* set the Previous-Sequence */
370 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
372 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
374 adios(NULL, "out of memory");
378 ** Parse all the SELECTED messages.
380 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
381 if (is_selected(mp, msgnum)) {
384 msgnam = m_name(msgnum);
385 if ((ct = parse_mime(msgnam)))
395 SIGNAL(SIGQUIT, quitser);
396 SIGNAL(SIGPIPE, pipeser);
399 ** Get the associated umask for the relevant contents.
401 for (ctp = cts; *ctp; ctp++) {
405 if (type_ok(ct, 1) && !ct->c_umask) {
406 if (stat(ct->c_file, &st) != NOTOK)
407 ct->c_umask = ~(st.st_mode & 0777);
409 ct->c_umask = ~m_gmprot();
414 ** Show the message content
416 show_all_messages(cts);
418 /* Now free all the structures for the content */
419 for (ctp = cts; *ctp; ctp++)
425 /* If reading from a folder, do some updating */
427 context_replace(curfolder, folder); /* update current folder */
428 seq_setcur(mp, mp->hghsel); /* update current message */
429 seq_save(mp); /* synchronize sequences */
430 context_save(); /* save the context file */
444 fprintf(stderr, "\n");