]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
fdad6e13bb95994752955d64dc140392e0ccf8ae
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("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("file file", 0, FILESW) \
29 X("form formfile", 0, FORMSW) \
30 X("part number", 0, PARTSW) \
31 X("type content", 0, TYPESW) \
32 X("rcache policy", 0, RCACHESW) \
33 X("wcache policy", 0, WCACHESW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
37 * switches for moreproc/mhlproc \
39 X("moreproc program", -4, PROGSW) \
40 X("nomoreproc", -3, NPROGSW) \
41 X("length lines", -4, LENSW) \
42 X("width columns", -4, WIDTHSW) \
44 * switches for debugging \
46 X("debug", -5, DEBUGSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(MHSHOW
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
60 extern char *cache_public
;
61 extern char *cache_private
;
66 extern int nomore
; /* flags for moreproc/header display */
72 extern char *parts
[NPARTS
+ 1];
73 extern char *types
[NTYPES
+ 1];
79 #define quitser pipeser
82 CT
parse_mime (char *);
85 int part_ok (CT
, int);
86 int type_ok (CT
, int);
87 void flush_errors (void);
90 void show_all_messages (CT
*);
94 void freects_done (int) NORETURN
;
99 static void pipeser (int);
103 main (int argc
, char **argv
)
105 int msgnum
, *icachesw
;
106 char *cp
, *file
= NULL
, *folder
= NULL
;
107 char *maildir
, buf
[100], **argp
;
109 struct msgs_array msgs
= { 0, 0, NULL
};
110 struct msgs
*mp
= NULL
;
114 if (nmh_init(argv
[0], 1)) { return 1; }
118 arguments
= getarguments (invo_name
, argc
, argv
, 1);
124 while ((cp
= *argp
++)) {
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 adios (NULL
, "-%s unknown", cp
);
134 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
143 icachesw
= &rcachesw
;
146 icachesw
= &wcachesw
;
148 if (!(cp
= *argp
++) || *cp
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
150 switch (*icachesw
= smatch (cp
, caches
)) {
152 ambigsw (cp
, caches
);
155 adios (NULL
, "%s unknown", cp
);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
179 adios (NULL
, "too many parts (starting with %s), %d max",
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
188 adios (NULL
, "too many types (starting with %s), %d max",
194 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
195 adios (NULL
, "missing argument to %s", argp
[-2]);
196 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 adios (NULL
, "missing argument to %s", argp
[-2]);
204 formsw
= getcpy (etcpath (cp
));
208 * Switches for moreproc/mhlproc
211 if (!(progsw
= *argp
++) || *progsw
== '-')
212 adios (NULL
, "missing argument to %s", argp
[-2]);
220 if (!(cp
= *argp
++) || *cp
== '-')
221 adios (NULL
, "missing argument to %s", argp
[-2]);
235 if (*cp
== '+' || *cp
== '@') {
237 adios (NULL
, "only one folder at a time!");
239 folder
= pluspath (cp
);
241 app_msgarg(&msgs
, cp
);
244 /* null terminate the list of acceptable parts/types */
249 * Check if we've specified an additional profile
251 if ((cp
= getenv ("MHSHOW"))) {
252 if ((fp
= fopen (cp
, "r"))) {
253 readconfig ((struct node
**) 0, fp
, cp
, 0);
256 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
261 * Read the standard profile setup
263 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
264 readconfig ((struct node
**) 0, fp
, cp
, 0);
268 /* Check for public cache location */
269 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
272 /* Check for private cache location */
273 if (!(cache_private
= context_find (nmhprivcache
)))
274 cache_private
= ".cache";
275 cache_private
= getcpy (m_maildir (cache_private
));
277 if (!context_find ("path"))
278 free (path ("./", TFOLDER
));
280 if (file
&& msgs
.size
)
281 adios (NULL
, "cannot specify msg and file at same time!");
284 * check if message is coming from file
287 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
288 adios (NULL
, "out of memory");
291 if ((ct
= parse_mime (file
)))
295 * message(s) are coming from a folder
298 app_msgarg(&msgs
, "cur");
300 folder
= getfolder (1);
301 maildir
= m_maildir (folder
);
303 if (chdir (maildir
) == NOTOK
)
304 adios (maildir
, "unable to change directory to");
306 /* read folder and create message structure */
307 if (!(mp
= folder_read (folder
, 1)))
308 adios (NULL
, "unable to read folder %s", folder
);
310 /* check for empty folder */
312 adios (NULL
, "no messages in %s", folder
);
314 /* parse all the message ranges/sequences and set SELECTED */
315 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
316 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
320 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
321 * since we will use that as a tag to know which messages
322 * to remove from the "unseen" sequence.
324 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
325 if (is_selected(mp
, msgnum
))
326 set_unseen (mp
, msgnum
);
328 seq_setprev (mp
); /* set the Previous-Sequence */
329 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
331 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
332 adios (NULL
, "out of memory");
336 * Parse all the SELECTED messages.
338 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
339 if (is_selected(mp
, msgnum
)) {
342 msgnam
= m_name (msgnum
);
343 if ((ct
= parse_mime (msgnam
)))
353 SIGNAL (SIGQUIT
, quitser
);
354 SIGNAL (SIGPIPE
, pipeser
);
357 * Get the associated umask for the relevant contents.
359 for (ctp
= cts
; *ctp
; ctp
++) {
363 if (type_ok (ct
, 1) && !ct
->c_umask
) {
364 if (stat (ct
->c_file
, &st
) != NOTOK
)
365 ct
->c_umask
= ~(st
.st_mode
& 0777);
367 ct
->c_umask
= ~m_gmprot();
371 /* If reading from a folder, do some updating */
373 context_replace (pfolder
, folder
);/* update current folder */
374 seq_setcur (mp
, mp
->hghsel
); /* update current message */
375 seq_save (mp
); /* synchronize sequences */
376 context_save (); /* save the context file */
380 * Show the message content
382 show_all_messages (cts
);
384 /* Now free all the structures for the content */
385 for (ctp
= cts
; *ctp
; ctp
++)
401 fprintf (stderr
, "\n");