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 debugging
64 extern char *tmp; /* directory to place temp files */
69 extern char *cache_public;
70 extern char *cache_private;
80 extern char *parts[NPARTS + 1];
81 extern char *types[NTYPES + 1];
87 #define quitser pipeser
90 CT parse_mime(char *);
95 void set_endian(void);
96 void flush_errors(void);
99 void show_all_messages(CT *);
102 void free_content(CT);
104 void freects_done(int) NORETURN;
109 static RETSIGTYPE pipeser(int);
113 main(int argc, char **argv)
115 int msgnum, *icachesw;
116 char *cp, *file = NULL, *folder = NULL;
117 char *maildir, buf[100], **argp;
119 struct msgs_array msgs = { 0, 0, NULL };
120 struct msgs *mp = NULL;
127 setlocale(LC_ALL, "");
129 invo_name = mhbasename(argv[0]);
131 /* read user profile/context */
134 arguments = getarguments(invo_name, argc, argv, 1);
140 while ((cp = *argp++)) {
142 switch (smatch(++cp, switches)) {
144 ambigsw(cp, switches);
147 adios(NULL, "-%s unknown", cp);
150 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
151 print_help(buf, switches, 1);
154 print_version(invo_name);
158 icachesw = &rcachesw;
161 icachesw = &wcachesw;
163 if (!(cp = *argp++) || *cp == '-')
164 adios(NULL, "missing argument to %s",
166 switch (*icachesw = smatch(cp, caches)) {
171 adios(NULL, "%s unknown", cp);
192 if (!(cp = *argp++) || *cp == '-')
193 adios(NULL, "missing argument to %s",
196 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
201 if (!(cp = *argp++) || *cp == '-')
202 adios(NULL, "missing argument to %s",
205 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
210 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
211 adios(NULL, "missing argument to %s",
213 file = *cp == '-' ? cp : getcpy(expanddir(cp));
217 if (!(cp = *argp++) || *cp == '-')
218 adios(NULL, "missing argument to %s",
222 formsw = getcpy(etcpath(cp));
236 if (*cp == '+' || *cp == '@') {
238 adios(NULL, "only one folder at a time!");
240 folder = getcpy(expandfol(cp));
242 app_msgarg(&msgs, cp);
245 /* null terminate the list of acceptable parts/types */
251 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
256 ** Check if we've specified an additional profile
258 if ((cp = getenv("MHSHOW"))) {
259 if ((fp = fopen(cp, "r"))) {
260 readconfig((struct node **) 0, fp, cp, 0);
263 admonish("", "unable to read $MHSHOW profile (%s)",
269 ** Read the standard profile setup
271 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
272 readconfig((struct node **) 0, fp, cp, 0);
276 /* Check for public cache location */
277 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
280 /* Check for private cache location */
281 if (!(cache_private = context_find(nmhprivcache)))
282 cache_private = ".cache";
283 cache_private = getcpy(toabsdir(cache_private));
286 ** Check for storage directory. If specified,
287 ** then store temporary files there. Else we
288 ** store them in standard nmh directory.
290 if ((cp = context_find(nmhstorage)) && *cp)
291 tmp = concat(cp, "/", invo_name, NULL);
293 tmp = getcpy(toabsdir(invo_name));
295 if (file && msgs.size)
296 adios(NULL, "cannot specify msg and file at same time!");
299 ** check if message is coming from file
302 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
303 adios(NULL, "out of memory");
306 if ((ct = parse_mime(file)));
310 ** message(s) are coming from a folder
313 app_msgarg(&msgs, seq_cur);
315 folder = getcurfol();
316 maildir = toabsdir(folder);
318 if (chdir(maildir) == NOTOK)
319 adios(maildir, "unable to change directory to");
321 /* read folder and create message structure */
322 if (!(mp = folder_read(folder)))
323 adios(NULL, "unable to read folder %s", folder);
325 /* check for empty folder */
327 adios(NULL, "no messages in %s", folder);
329 /* parse all the message ranges/sequences and set SELECTED */
330 for (msgnum = 0; msgnum < msgs.size; msgnum++)
331 if (!m_convert(mp, msgs.msgs[msgnum]))
335 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
336 ** since we will use that as a tag to know which messages
337 ** to remove from the "unseen" sequence.
339 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
340 if (is_selected(mp, msgnum))
341 set_unseen(mp, msgnum);
343 seq_setprev(mp); /* set the Previous-Sequence */
344 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
346 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
348 adios(NULL, "out of memory");
352 ** Parse all the SELECTED messages.
354 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
355 if (is_selected(mp, msgnum)) {
358 msgnam = m_name(msgnum);
359 if ((ct = parse_mime(msgnam)))
369 SIGNAL(SIGQUIT, quitser);
370 SIGNAL(SIGPIPE, pipeser);
373 ** Get the associated umask for the relevant contents.
375 for (ctp = cts; *ctp; ctp++) {
379 if (type_ok(ct, 1) && !ct->c_umask) {
380 if (stat(ct->c_file, &st) != NOTOK)
381 ct->c_umask = ~(st.st_mode & 0777);
383 ct->c_umask = ~m_gmprot();
388 ** Show the message content
390 show_all_messages(cts);
392 /* Now free all the structures for the content */
393 for (ctp = cts; *ctp; ctp++)
399 /* If reading from a folder, do some updating */
401 context_replace(curfolder, folder); /* update current folder */
402 seq_setcur(mp, mp->hghsel); /* update current message */
403 seq_save(mp); /* synchronize sequences */
404 context_save(); /* save the context file */
418 fprintf(stderr, "\n");