]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
eb6ea16b89e93ff1b617edb0ef316309c8e681a2
3 * mhshow.c -- display the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHSHOW_SWITCHES \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("pause", 0, PAUSESW) \
25 X("nopause", 0, NPAUSESW) \
26 X("serialonly", 0, SERIALSW) \
27 X("noserialonly", 0, NSERIALSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("file file", 0, FILESW) \
31 X("form formfile", 0, FORMSW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("textcharset", 0, TEXTCHARSETSW) \
35 X("notextcharset", 0, NTEXTCHARSETSW) \
36 X("rcache policy", 0, RCACHESW) \
37 X("wcache policy", 0, WCACHESW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 * switches for moreproc/mhlproc \
43 X("moreproc program", -4, PROGSW) \
44 X("nomoreproc", -3, NPROGSW) \
45 X("length lines", -4, LENSW) \
46 X("width columns", -4, WIDTHSW) \
48 * switches for debugging \
50 X("debug", -5, DEBUGSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(MHSHOW
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
64 extern char *cache_public
;
65 extern char *cache_private
;
71 extern char *display_charset
;
72 extern int nomore
; /* flags for moreproc/header display */
78 extern char *parts
[NPARTS
+ 1];
79 extern char *types
[NTYPES
+ 1];
85 #define quitser pipeser
88 CT
parse_mime (char *);
91 int part_ok (CT
, int);
92 int type_ok (CT
, int);
93 void flush_errors (void);
96 void show_all_messages (CT
*);
100 void freects_done (int) NORETURN
;
105 static void pipeser (int);
109 main (int argc
, char **argv
)
111 int msgnum
, *icachesw
;
112 char *cp
, *file
= NULL
, *folder
= NULL
;
113 char *maildir
, buf
[100], **argp
;
115 struct msgs_array msgs
= { 0, 0, NULL
};
116 struct msgs
*mp
= NULL
;
120 if (nmh_init(argv
[0], 1)) { return 1; }
124 arguments
= getarguments (invo_name
, argc
, argv
, 1);
130 while ((cp
= *argp
++)) {
132 switch (smatch (++cp
, switches
)) {
134 ambigsw (cp
, switches
);
137 adios (NULL
, "-%s unknown", cp
);
140 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
142 print_help (buf
, switches
, 1);
145 print_version(invo_name
);
149 icachesw
= &rcachesw
;
152 icachesw
= &wcachesw
;
154 if (!(cp
= *argp
++) || *cp
== '-')
155 adios (NULL
, "missing argument to %s", argp
[-2]);
156 switch (*icachesw
= smatch (cp
, caches
)) {
158 ambigsw (cp
, caches
);
161 adios (NULL
, "%s unknown", cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 adios (NULL
, "missing argument to %s", argp
[-2]);
192 adios (NULL
, "too many parts (starting with %s), %d max",
198 if (!(cp
= *argp
++) || *cp
== '-')
199 adios (NULL
, "missing argument to %s", argp
[-2]);
201 adios (NULL
, "too many types (starting with %s), %d max",
207 if (!(cp
= *argp
++) || *cp
== '-')
208 adios (NULL
, "missing argument to %s", argp
[-2]);
209 display_charset
= cp
;
213 display_charset
= NULL
;
217 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
218 adios (NULL
, "missing argument to %s", argp
[-2]);
219 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
223 if (!(cp
= *argp
++) || *cp
== '-')
224 adios (NULL
, "missing argument to %s", argp
[-2]);
227 formsw
= getcpy (etcpath (cp
));
231 * Switches for moreproc/mhlproc
234 if (!(progsw
= *argp
++) || *progsw
== '-')
235 adios (NULL
, "missing argument to %s", argp
[-2]);
243 if (!(cp
= *argp
++) || *cp
== '-')
244 adios (NULL
, "missing argument to %s", argp
[-2]);
258 if (*cp
== '+' || *cp
== '@') {
260 adios (NULL
, "only one folder at a time!");
262 folder
= pluspath (cp
);
264 app_msgarg(&msgs
, cp
);
267 /* null terminate the list of acceptable parts/types */
272 * Check if we've specified an additional profile
274 if ((cp
= getenv ("MHSHOW"))) {
275 if ((fp
= fopen (cp
, "r"))) {
276 readconfig ((struct node
**) 0, fp
, cp
, 0);
279 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
284 * Read the standard profile setup
286 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
287 readconfig ((struct node
**) 0, fp
, cp
, 0);
291 /* Check for public cache location */
292 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
295 /* Check for private cache location */
296 if (!(cache_private
= context_find (nmhprivcache
)))
297 cache_private
= ".cache";
298 cache_private
= getcpy (m_maildir (cache_private
));
300 if (!context_find ("path"))
301 free (path ("./", TFOLDER
));
303 if (file
&& msgs
.size
)
304 adios (NULL
, "cannot specify msg and file at same time!");
307 * check if message is coming from file
310 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
311 adios (NULL
, "out of memory");
314 if ((ct
= parse_mime (file
)))
318 * message(s) are coming from a folder
321 app_msgarg(&msgs
, "cur");
323 folder
= getfolder (1);
324 maildir
= m_maildir (folder
);
326 if (chdir (maildir
) == NOTOK
)
327 adios (maildir
, "unable to change directory to");
329 /* read folder and create message structure */
330 if (!(mp
= folder_read (folder
, 1)))
331 adios (NULL
, "unable to read folder %s", folder
);
333 /* check for empty folder */
335 adios (NULL
, "no messages in %s", folder
);
337 /* parse all the message ranges/sequences and set SELECTED */
338 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
339 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
343 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
344 * since we will use that as a tag to know which messages
345 * to remove from the "unseen" sequence.
347 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
348 if (is_selected(mp
, msgnum
))
349 set_unseen (mp
, msgnum
);
351 seq_setprev (mp
); /* set the Previous-Sequence */
352 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
354 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
355 adios (NULL
, "out of memory");
359 * Parse all the SELECTED messages.
361 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
362 if (is_selected(mp
, msgnum
)) {
365 msgnam
= m_name (msgnum
);
366 if ((ct
= parse_mime (msgnam
)))
376 SIGNAL (SIGQUIT
, quitser
);
377 SIGNAL (SIGPIPE
, pipeser
);
380 * Get the associated umask for the relevant contents.
382 for (ctp
= cts
; *ctp
; ctp
++) {
386 if (type_ok (ct
, 1) && !ct
->c_umask
) {
387 if (stat (ct
->c_file
, &st
) != NOTOK
)
388 ct
->c_umask
= ~(st
.st_mode
& 0777);
390 ct
->c_umask
= ~m_gmprot();
394 /* If reading from a folder, do some updating */
396 context_replace (pfolder
, folder
);/* update current folder */
397 seq_setcur (mp
, mp
->hghsel
); /* update current message */
398 seq_save (mp
); /* synchronize sequences */
399 context_save (); /* save the context file */
403 * Show the message content
405 show_all_messages (cts
);
407 /* Now free all the structures for the content */
408 for (ctp
= cts
; *ctp
; ctp
++)
424 fprintf (stderr
, "\n");