]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
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("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("concat", 0, CONCATSW) \
27 X("noconcat", 0, NCONCATSW) \
28 X("textonly", 0, TEXTONLYSW) \
29 X("notextonly", 0, NTEXTONLYSW) \
30 X("inlineonly", 0, INLINESW) \
31 X("noinlineonly", 0, NINLINESW) \
32 X("file file", 0, FILESW) \
33 X("form formfile", 0, FORMSW) \
34 X("markform formfile", 0, MARKFORMSW) \
35 X("part number", 0, PARTSW) \
36 X("type content", 0, TYPESW) \
37 X("rcache policy", 0, RCACHESW) \
38 X("wcache policy", 0, WCACHESW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 * switches for moreproc/mhlproc \
44 X("moreproc program", -4, PROGSW) \
45 X("nomoreproc", -3, NPROGSW) \
46 X("length lines", -4, LENSW) \
47 X("width columns", -4, WIDTHSW) \
49 * switches for debugging \
51 X("debug", -5, DEBUGSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(MHSHOW
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
65 extern char *cache_public
;
66 extern char *cache_private
;
70 extern int nomore
; /* flags for moreproc/header display */
76 extern char *parts
[NPARTS
+ 1];
77 extern char *types
[NTYPES
+ 1];
83 #define quitser pipeser
86 CT
parse_mime (char *);
89 int part_ok (CT
, int);
90 int type_ok (CT
, int);
91 void flush_errors (void);
95 void freects_done (int) NORETURN
;
100 static void pipeser (int);
104 main (int argc
, char **argv
)
106 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
107 char *cp
, *file
= NULL
, *folder
= NULL
, *markform
= NULL
;
108 char *maildir
, buf
[100], **argp
;
110 struct msgs_array msgs
= { 0, 0, NULL
};
111 struct msgs
*mp
= NULL
;
115 if (nmh_init(argv
[0], 1)) { return 1; }
119 arguments
= getarguments (invo_name
, argc
, argv
, 1);
125 while ((cp
= *argp
++)) {
127 switch (smatch (++cp
, switches
)) {
129 ambigsw (cp
, switches
);
132 adios (NULL
, "-%s unknown", cp
);
135 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
137 print_help (buf
, switches
, 1);
140 print_version(invo_name
);
144 icachesw
= &rcachesw
;
147 icachesw
= &wcachesw
;
149 if (!(cp
= *argp
++) || *cp
== '-')
150 adios (NULL
, "missing argument to %s", argp
[-2]);
151 switch (*icachesw
= smatch (cp
, caches
)) {
153 ambigsw (cp
, caches
);
156 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
== '-' && cp
[1]))
208 adios (NULL
, "missing argument to %s", argp
[-2]);
209 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
213 if (!(cp
= *argp
++) || *cp
== '-')
214 adios (NULL
, "missing argument to %s", argp
[-2]);
217 formsw
= getcpy (etcpath (cp
));
221 if (!(markform
= *argp
++) || *markform
== '-')
222 adios (NULL
, "missing argument to %s", argp
[-2]);
226 * Switches for moreproc/mhlproc
229 if (!(progsw
= *argp
++) || *progsw
== '-')
230 adios (NULL
, "missing argument to %s", argp
[-2]);
238 if (!(cp
= *argp
++) || *cp
== '-')
239 adios (NULL
, "missing argument to %s", argp
[-2]);
253 if (*cp
== '+' || *cp
== '@') {
255 adios (NULL
, "only one folder at a time!");
257 folder
= pluspath (cp
);
259 app_msgarg(&msgs
, cp
);
262 /* null terminate the list of acceptable parts/types */
267 * If we had any specific parts or types specified, turn off text only
271 if (npart
> 0 || ntype
> 0) {
274 if (inlineonly
== -1)
279 * Check if we've specified an additional profile
281 if ((cp
= getenv ("MHSHOW"))) {
282 if ((fp
= fopen (cp
, "r"))) {
283 readconfig ((struct node
**) 0, fp
, cp
, 0);
286 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
291 * Read the standard profile setup
293 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
294 readconfig ((struct node
**) 0, fp
, cp
, 0);
298 /* Check for public cache location */
299 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
302 /* Check for private cache location */
303 if (!(cache_private
= context_find (nmhprivcache
)))
304 cache_private
= ".cache";
305 cache_private
= getcpy (m_maildir (cache_private
));
307 if (!context_find ("path"))
308 free (path ("./", TFOLDER
));
310 if (file
&& msgs
.size
)
311 adios (NULL
, "cannot specify msg and file at same time!");
314 * check if message is coming from file
317 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
318 adios (NULL
, "out of memory");
321 if ((ct
= parse_mime (file
)))
325 * message(s) are coming from a folder
328 app_msgarg(&msgs
, "cur");
330 folder
= getfolder (1);
331 maildir
= m_maildir (folder
);
333 if (chdir (maildir
) == NOTOK
)
334 adios (maildir
, "unable to change directory to");
336 /* read folder and create message structure */
337 if (!(mp
= folder_read (folder
, 1)))
338 adios (NULL
, "unable to read folder %s", folder
);
340 /* check for empty folder */
342 adios (NULL
, "no messages in %s", folder
);
344 /* parse all the message ranges/sequences and set SELECTED */
345 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
346 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
350 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
351 * since we will use that as a tag to know which messages
352 * to remove from the "unseen" sequence.
354 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
355 if (is_selected(mp
, msgnum
))
356 set_unseen (mp
, msgnum
);
358 seq_setprev (mp
); /* set the Previous-Sequence */
359 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
361 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
362 adios (NULL
, "out of memory");
366 * Parse all the SELECTED messages.
368 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
369 if (is_selected(mp
, msgnum
)) {
372 msgnam
= m_name (msgnum
);
373 if ((ct
= parse_mime (msgnam
)))
383 SIGNAL (SIGQUIT
, quitser
);
384 SIGNAL (SIGPIPE
, pipeser
);
387 * Get the associated umask for the relevant contents.
389 for (ctp
= cts
; *ctp
; ctp
++) {
393 if (type_ok (ct
, 1) && !ct
->c_umask
) {
394 if (stat (ct
->c_file
, &st
) != NOTOK
)
395 ct
->c_umask
= ~(st
.st_mode
& 0777);
397 ct
->c_umask
= ~m_gmprot();
401 /* If reading from a folder, do some updating */
403 context_replace (pfolder
, folder
);/* update current folder */
404 seq_setcur (mp
, mp
->hghsel
); /* update current message */
405 seq_save (mp
); /* synchronize sequences */
406 context_save (); /* save the context file */
410 m_popen(moreproc
, 0);
413 * Show the message content
415 show_all_messages (cts
, concatsw
, textonly
, inlineonly
, markform
);
417 /* Now free all the structures for the content */
418 for (ctp
= cts
; *ctp
; ctp
++)
437 fprintf (stderr
, "\n");