]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
1 /* mhshow.c -- display the contents of MIME messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/signals.h>
14 #include <h/fmt_scan.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
19 #include "../sbr/m_maildir.h"
20 #include "../sbr/m_popen.h"
22 #include "mhshowsbr.h"
24 #define MHSHOW_SWITCHES \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("concat", 0, CONCATSW) \
30 X("noconcat", 0, NCONCATSW) \
31 X("textonly", 0, TEXTONLYSW) \
32 X("notextonly", 0, NTEXTONLYSW) \
33 X("inlineonly", 0, INLINESW) \
34 X("noinlineonly", 0, NINLINESW) \
35 X("file file", 0, FILESW) \
36 X("form formfile", 0, FORMSW) \
37 X("header", 0, HEADSW) \
38 X("noheader", 0, NHEADSW) \
39 X("headerform formfile", 0, HEADFORMSW) \
40 X("markform formfile", 0, MARKFORMSW) \
41 X("part number", 0, PARTSW) \
42 X("type content", 0, TYPESW) \
43 X("prefer content", 0, PREFERSW) \
44 X("rcache policy", 0, RCACHESW) \
45 X("wcache policy", 0, WCACHESW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
49 * switches for moreproc/mhlproc \
51 X("moreproc program", -4, PROGSW) \
52 X("nomoreproc", -3, NPROGSW) \
53 X("length lines", -4, LENSW) \
54 X("width columns", -4, WIDTHSW) \
56 * switches for debugging \
58 X("debug", -5, DEBUGSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(MHSHOW
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
72 extern char *parts
[NPARTS
+ 1];
73 extern char *types
[NTYPES
+ 1];
77 extern char *preferred_types
[];
78 extern char *preferred_subtypes
[];
79 extern int npreferred
;
84 #define quitser pipeser
87 CT
parse_mime (char *);
91 int type_ok (CT
, int);
92 void flush_errors (void);
97 static void pipeser (int);
101 main (int argc
, char **argv
)
103 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
104 char *cp
, *file
= NULL
;
105 char *maildir
, buf
[100], **argp
;
107 struct msgs_array msgs
= { 0, 0, NULL
};
108 struct msgs
*mp
= NULL
;
112 if (nmh_init(argv
[0], 1)) { return 1; }
116 arguments
= getarguments (invo_name
, argc
, argv
, 1);
122 while ((cp
= *argp
++)) {
124 switch (smatch (++cp
, switches
)) {
126 ambigsw (cp
, switches
);
129 adios (NULL
, "-%s unknown", cp
);
132 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
134 print_help (buf
, switches
, 1);
137 print_version(invo_name
);
141 icachesw
= &rcachesw
;
144 icachesw
= &wcachesw
;
146 if (!(cp
= *argp
++) || *cp
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
148 switch (*icachesw
= smatch (cp
, cache_policy
)) {
150 ambigsw (cp
, cache_policy
);
153 adios (NULL
, "%s unknown", cp
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
189 adios (NULL
, "too many parts (starting with %s), %d max",
195 if (!(cp
= *argp
++) || *cp
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
198 adios (NULL
, "too many types (starting with %s), %d max",
204 if (!(cp
= *argp
++) || *cp
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
206 if (npreferred
>= NPREFS
)
207 adios (NULL
, "too many preferred types (starting with %s), %d max",
209 preferred_types
[npreferred
] = cp
;
210 cp
= strchr(cp
, '/');
211 if (cp
) *cp
++ = '\0';
212 preferred_subtypes
[npreferred
++] = cp
;
216 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
217 adios (NULL
, "missing argument to %s", argp
[-2]);
218 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
222 if (!(cp
= *argp
++) || *cp
== '-')
223 adios (NULL
, "missing argument to %s", argp
[-2]);
225 formsw
= getcpy (etcpath (cp
));
236 if (!(headerform
= *argp
++) || *headerform
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
241 if (!(markerform
= *argp
++) || *markerform
== '-')
242 adios (NULL
, "missing argument to %s", argp
[-2]);
246 * Switches for moreproc/mhlproc
249 if (!(progsw
= *argp
++) || *progsw
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
258 if (!(cp
= *argp
++) || *cp
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
273 if (*cp
== '+' || *cp
== '@') {
275 adios (NULL
, "only one folder at a time!");
277 folder
= pluspath (cp
);
279 app_msgarg(&msgs
, cp
);
282 /* null terminate the list of acceptable parts/types */
287 * If we had any specific parts or types specified, turn off text only
291 if (npart
> 0 || ntype
> 0) {
294 if (inlineonly
== -1)
299 * Check if we've specified an additional profile
301 if ((cp
= getenv ("MHSHOW"))) {
302 if ((fp
= fopen (cp
, "r"))) {
303 readconfig ((struct node
**) 0, fp
, cp
, 0);
306 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
311 * Read the standard profile setup
313 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
314 readconfig ((struct node
**) 0, fp
, cp
, 0);
318 /* Check for public cache location */
319 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
322 /* Check for private cache location */
323 if (!(cache_private
= context_find (nmhprivcache
)))
324 cache_private
= ".cache";
325 cache_private
= getcpy (m_maildir (cache_private
));
327 if (!context_find ("path"))
328 free (path ("./", TFOLDER
));
330 if (file
&& msgs
.size
)
331 adios (NULL
, "cannot specify msg and file at same time!");
334 * check if message is coming from file
337 cts
= mh_xcalloc(2, sizeof *cts
);
340 if ((ct
= parse_mime (file
)))
346 * message(s) are coming from a folder
349 app_msgarg(&msgs
, "cur");
351 folder
= getfolder (1);
352 maildir
= m_maildir (folder
);
354 if (chdir (maildir
) == NOTOK
)
355 adios (maildir
, "unable to change directory to");
357 /* read folder and create message structure */
358 if (!(mp
= folder_read (folder
, 1)))
359 adios (NULL
, "unable to read folder %s", folder
);
361 /* check for empty folder */
363 adios (NULL
, "no messages in %s", folder
);
365 /* parse all the message ranges/sequences and set SELECTED */
366 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
367 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
371 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
372 * since we will use that as a tag to know which messages
373 * to remove from the "unseen" sequence.
375 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
376 if (is_selected(mp
, msgnum
))
377 set_unseen (mp
, msgnum
);
379 seq_setprev (mp
); /* set the Previous-Sequence */
380 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
382 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
386 * Parse all the SELECTED messages.
388 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
389 if (is_selected(mp
, msgnum
)) {
392 msgnam
= m_name (msgnum
);
393 if ((ct
= parse_mime (msgnam
)))
403 SIGNAL (SIGQUIT
, quitser
);
404 SIGNAL (SIGPIPE
, pipeser
);
407 * Get the associated umask for the relevant contents.
409 for (ctp
= cts
; *ctp
; ctp
++) {
413 if (type_ok (ct
, 1) && !ct
->c_umask
) {
414 if (stat (ct
->c_file
, &st
) != NOTOK
)
415 ct
->c_umask
= ~(st
.st_mode
& 0777);
417 ct
->c_umask
= ~m_gmprot();
421 /* If reading from a folder, do some updating */
423 context_replace (pfolder
, folder
);/* update current folder */
424 seq_setcur (mp
, mp
->hghsel
); /* update current message */
425 seq_save (mp
); /* synchronize sequences */
426 context_save (); /* save the context file */
430 m_popen(moreproc
, 0);
433 * Show the message content
435 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
437 /* Now free all the structures for the content */
438 for (ctp
= cts
; *ctp
; ctp
++)
457 fprintf (stderr
, "\n");