]>
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("prefer content", 0, PREFERSW) \
38 X("rcache policy", 0, RCACHESW) \
39 X("wcache policy", 0, WCACHESW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 * switches for moreproc/mhlproc \
45 X("moreproc program", -4, PROGSW) \
46 X("nomoreproc", -3, NPROGSW) \
47 X("length lines", -4, LENSW) \
48 X("width columns", -4, WIDTHSW) \
50 * switches for debugging \
52 X("debug", -5, DEBUGSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(MHSHOW
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
66 extern char *cache_public
;
67 extern char *cache_private
;
71 extern int nomore
; /* flags for moreproc/header display */
77 extern char *parts
[NPARTS
+ 1];
78 extern char *types
[NTYPES
+ 1];
82 extern char *preferred_types
[];
83 extern char *preferred_subtypes
[];
84 extern int npreferred
;
89 #define quitser pipeser
92 CT
parse_mime (char *);
96 int type_ok (CT
, int);
97 void flush_errors (void);
101 void freects_done (int) NORETURN
;
106 static void pipeser (int);
110 main (int argc
, char **argv
)
112 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
113 char *cp
, *file
= NULL
, *folder
= NULL
, *markform
= NULL
;
114 char *maildir
, buf
[100], **argp
;
116 struct msgs_array msgs
= { 0, 0, NULL
};
117 struct msgs
*mp
= NULL
;
121 if (nmh_init(argv
[0], 1)) { return 1; }
125 arguments
= getarguments (invo_name
, argc
, argv
, 1);
131 while ((cp
= *argp
++)) {
133 switch (smatch (++cp
, switches
)) {
135 ambigsw (cp
, switches
);
138 adios (NULL
, "-%s unknown", cp
);
141 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
143 print_help (buf
, switches
, 1);
146 print_version(invo_name
);
150 icachesw
= &rcachesw
;
153 icachesw
= &wcachesw
;
155 if (!(cp
= *argp
++) || *cp
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
157 switch (*icachesw
= smatch (cp
, caches
)) {
159 ambigsw (cp
, caches
);
162 adios (NULL
, "%s unknown", cp
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
198 adios (NULL
, "too many parts (starting with %s), %d max",
204 if (!(cp
= *argp
++) || *cp
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
207 adios (NULL
, "too many types (starting with %s), %d max",
213 if (!(cp
= *argp
++) || *cp
== '-')
214 adios (NULL
, "missing argument to %s", argp
[-2]);
215 if (npreferred
>= NPREFS
)
216 adios (NULL
, "too many preferred types (starting with %s), %d max",
218 preferred_types
[npreferred
] = cp
;
219 cp
= strchr(cp
, '/');
220 if (cp
) *cp
++ = '\0';
221 preferred_subtypes
[npreferred
++] = cp
;
225 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
226 adios (NULL
, "missing argument to %s", argp
[-2]);
227 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
231 if (!(cp
= *argp
++) || *cp
== '-')
232 adios (NULL
, "missing argument to %s", argp
[-2]);
235 formsw
= getcpy (etcpath (cp
));
239 if (!(markform
= *argp
++) || *markform
== '-')
240 adios (NULL
, "missing argument to %s", argp
[-2]);
244 * Switches for moreproc/mhlproc
247 if (!(progsw
= *argp
++) || *progsw
== '-')
248 adios (NULL
, "missing argument to %s", argp
[-2]);
256 if (!(cp
= *argp
++) || *cp
== '-')
257 adios (NULL
, "missing argument to %s", argp
[-2]);
271 if (*cp
== '+' || *cp
== '@') {
273 adios (NULL
, "only one folder at a time!");
275 folder
= pluspath (cp
);
277 app_msgarg(&msgs
, cp
);
280 /* null terminate the list of acceptable parts/types */
285 * If we had any specific parts or types specified, turn off text only
289 if (npart
> 0 || ntype
> 0) {
292 if (inlineonly
== -1)
297 * Check if we've specified an additional profile
299 if ((cp
= getenv ("MHSHOW"))) {
300 if ((fp
= fopen (cp
, "r"))) {
301 readconfig ((struct node
**) 0, fp
, cp
, 0);
304 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
309 * Read the standard profile setup
311 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
312 readconfig ((struct node
**) 0, fp
, cp
, 0);
316 /* Check for public cache location */
317 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
320 /* Check for private cache location */
321 if (!(cache_private
= context_find (nmhprivcache
)))
322 cache_private
= ".cache";
323 cache_private
= getcpy (m_maildir (cache_private
));
325 if (!context_find ("path"))
326 free (path ("./", TFOLDER
));
328 if (file
&& msgs
.size
)
329 adios (NULL
, "cannot specify msg and file at same time!");
332 * check if message is coming from file
335 if (!(cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof(*cts
))))
336 adios (NULL
, "out of memory");
339 if ((ct
= parse_mime (file
)))
343 * message(s) are coming from a folder
346 app_msgarg(&msgs
, "cur");
348 folder
= getfolder (1);
349 maildir
= m_maildir (folder
);
351 if (chdir (maildir
) == NOTOK
)
352 adios (maildir
, "unable to change directory to");
354 /* read folder and create message structure */
355 if (!(mp
= folder_read (folder
, 1)))
356 adios (NULL
, "unable to read folder %s", folder
);
358 /* check for empty folder */
360 adios (NULL
, "no messages in %s", folder
);
362 /* parse all the message ranges/sequences and set SELECTED */
363 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
364 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
368 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
369 * since we will use that as a tag to know which messages
370 * to remove from the "unseen" sequence.
372 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
373 if (is_selected(mp
, msgnum
))
374 set_unseen (mp
, msgnum
);
376 seq_setprev (mp
); /* set the Previous-Sequence */
377 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
379 if (!(cts
= (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
380 adios (NULL
, "out of memory");
384 * Parse all the SELECTED messages.
386 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
387 if (is_selected(mp
, msgnum
)) {
390 msgnam
= m_name (msgnum
);
391 if ((ct
= parse_mime (msgnam
)))
401 SIGNAL (SIGQUIT
, quitser
);
402 SIGNAL (SIGPIPE
, pipeser
);
405 * Get the associated umask for the relevant contents.
407 for (ctp
= cts
; *ctp
; ctp
++) {
411 if (type_ok (ct
, 1) && !ct
->c_umask
) {
412 if (stat (ct
->c_file
, &st
) != NOTOK
)
413 ct
->c_umask
= ~(st
.st_mode
& 0777);
415 ct
->c_umask
= ~m_gmprot();
419 /* If reading from a folder, do some updating */
421 context_replace (pfolder
, folder
);/* update current folder */
422 seq_setcur (mp
, mp
->hghsel
); /* update current message */
423 seq_save (mp
); /* synchronize sequences */
424 context_save (); /* save the context file */
428 m_popen(moreproc
, 0);
431 * Show the message content
433 show_all_messages (cts
, concatsw
, textonly
, inlineonly
, markform
);
435 /* Now free all the structures for the content */
436 for (ctp
= cts
; *ctp
; ctp
++)
455 fprintf (stderr
, "\n");