]>
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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #define MHSHOW_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("pause", 0, PAUSESW) \
27 X("nopause", 0, NPAUSESW) \
28 X("serialonly", 0, SERIALSW) \
29 X("noserialonly", 0, NSERIALSW) \
30 X("verbose", 0, VERBSW) \
31 X("noverbose", 0, NVERBSW) \
32 X("file file", 0, FILESW) \
33 X("form formfile", 0, FORMSW) \
34 X("part number", 0, PARTSW) \
35 X("type content", 0, TYPESW) \
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
);
62 extern char *tmp
; /* directory to place temp files */
67 extern char *cache_public
;
68 extern char *cache_private
;
75 extern int nomore
; /* flags for moreproc/header display */
81 extern char *parts
[NPARTS
+ 1];
82 extern char *types
[NTYPES
+ 1];
88 #define quitser pipeser
91 CT
parse_mime (char *);
94 int part_ok (CT
, int);
95 int type_ok (CT
, int);
96 void flush_errors (void);
99 void show_all_messages (CT
*);
103 void freects_done (int) NORETURN
;
108 static void pipeser (int);
112 main (int argc
, char **argv
)
114 int msgnum
, *icachesw
;
115 char *cp
, *file
= NULL
, *folder
= NULL
;
116 char *maildir
, buf
[100], **argp
;
118 struct msgs_array msgs
= { 0, 0, NULL
};
119 struct msgs
*mp
= NULL
;
126 setlocale(LC_ALL
, "");
128 invo_name
= r1bindex (argv
[0], '/');
130 /* read user profile/context */
133 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 while ((cp
= *argp
++)) {
141 switch (smatch (++cp
, switches
)) {
143 ambigsw (cp
, switches
);
146 adios (NULL
, "-%s unknown", cp
);
149 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
151 print_help (buf
, switches
, 1);
154 print_version(invo_name
);
158 icachesw
= &rcachesw
;
161 icachesw
= &wcachesw
;
163 if (!(cp
= *argp
++) || *cp
== '-')
164 adios (NULL
, "missing argument to %s", argp
[-2]);
165 switch (*icachesw
= smatch (cp
, caches
)) {
167 ambigsw (cp
, caches
);
170 adios (NULL
, "%s unknown", cp
);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 adios (NULL
, "missing argument to %s", argp
[-2]);
201 adios (NULL
, "too many parts (starting with %s), %d max",
207 if (!(cp
= *argp
++) || *cp
== '-')
208 adios (NULL
, "missing argument to %s", argp
[-2]);
210 adios (NULL
, "too many types (starting with %s), %d max",
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]);
226 formsw
= getcpy (etcpath (cp
));
230 * Switches for moreproc/mhlproc
233 if (!(progsw
= *argp
++) || *progsw
== '-')
234 adios (NULL
, "missing argument to %s", argp
[-2]);
242 if (!(cp
= *argp
++) || *cp
== '-')
243 adios (NULL
, "missing argument to %s", argp
[-2]);
257 if (*cp
== '+' || *cp
== '@') {
259 adios (NULL
, "only one folder at a time!");
261 folder
= pluspath (cp
);
263 app_msgarg(&msgs
, cp
);
266 /* null terminate the list of acceptable parts/types */
271 * Check if we've specified an additional profile
273 if ((cp
= getenv ("MHSHOW"))) {
274 if ((fp
= fopen (cp
, "r"))) {
275 readconfig ((struct node
**) 0, fp
, cp
, 0);
278 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
283 * Read the standard profile setup
285 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
286 readconfig ((struct node
**) 0, fp
, cp
, 0);
290 /* Check for public cache location */
291 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
294 /* Check for private cache location */
295 if (!(cache_private
= context_find (nmhprivcache
)))
296 cache_private
= ".cache";
297 cache_private
= getcpy (m_maildir (cache_private
));
300 * Check for storage directory. If specified,
301 * then store temporary files there. Else we
302 * store them in standard nmh directory.
304 if ((cp
= context_find (nmhstorage
)) && *cp
)
305 tmp
= concat (cp
, "/", invo_name
, NULL
);
307 tmp
= add (m_maildir (invo_name
), NULL
);
309 if (!context_find ("path"))
310 free (path ("./", TFOLDER
));
312 if (file
&& msgs
.size
)
313 adios (NULL
, "cannot specify msg and file at same time!");
316 * check if message is coming from file
319 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
320 adios (NULL
, "out of memory");
323 if ((ct
= parse_mime (file
)))
327 * message(s) are coming from a folder
330 app_msgarg(&msgs
, "cur");
332 folder
= getfolder (1);
333 maildir
= m_maildir (folder
);
335 if (chdir (maildir
) == NOTOK
)
336 adios (maildir
, "unable to change directory to");
338 /* read folder and create message structure */
339 if (!(mp
= folder_read (folder
)))
340 adios (NULL
, "unable to read folder %s", folder
);
342 /* check for empty folder */
344 adios (NULL
, "no messages in %s", folder
);
346 /* parse all the message ranges/sequences and set SELECTED */
347 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
348 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
352 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
353 * since we will use that as a tag to know which messages
354 * to remove from the "unseen" sequence.
356 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
357 if (is_selected(mp
, msgnum
))
358 set_unseen (mp
, msgnum
);
360 seq_setprev (mp
); /* set the Previous-Sequence */
361 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
363 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
364 adios (NULL
, "out of memory");
368 * Parse all the SELECTED messages.
370 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
371 if (is_selected(mp
, msgnum
)) {
374 msgnam
= m_name (msgnum
);
375 if ((ct
= parse_mime (msgnam
)))
385 SIGNAL (SIGQUIT
, quitser
);
386 SIGNAL (SIGPIPE
, pipeser
);
389 * Get the associated umask for the relevant contents.
391 for (ctp
= cts
; *ctp
; ctp
++) {
395 if (type_ok (ct
, 1) && !ct
->c_umask
) {
396 if (stat (ct
->c_file
, &st
) != NOTOK
)
397 ct
->c_umask
= ~(st
.st_mode
& 0777);
399 ct
->c_umask
= ~m_gmprot();
403 /* If reading from a folder, do some updating */
405 context_replace (pfolder
, folder
);/* update current folder */
406 seq_setcur (mp
, mp
->hghsel
); /* update current message */
407 seq_save (mp
); /* synchronize sequences */
408 context_save (); /* save the context file */
412 * Show the message content
414 show_all_messages (cts
);
416 /* Now free all the structures for the content */
417 for (ctp
= cts
; *ctp
; ctp
++)
433 fprintf (stderr
, "\n");