]>
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
*);
102 void free_content (CT
);
104 void freects_done (int) NORETURN
;
109 static void pipeser (int);
113 main (int argc
, char **argv
)
115 int msgnum
, *icachesw
;
116 char *cp
, *file
= NULL
, *folder
= NULL
;
117 char *maildir
, buf
[100], **argp
;
119 struct msgs_array msgs
= { 0, 0, NULL
};
120 struct msgs
*mp
= NULL
;
127 setlocale(LC_ALL
, "");
129 invo_name
= r1bindex (argv
[0], '/');
131 /* read user profile/context */
134 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 adios (NULL
, "-%s unknown", cp
);
150 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
152 print_help (buf
, switches
, 1);
155 print_version(invo_name
);
159 icachesw
= &rcachesw
;
162 icachesw
= &wcachesw
;
164 if (!(cp
= *argp
++) || *cp
== '-')
165 adios (NULL
, "missing argument to %s", argp
[-2]);
166 switch (*icachesw
= smatch (cp
, caches
)) {
168 ambigsw (cp
, caches
);
171 adios (NULL
, "%s unknown", cp
);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 adios (NULL
, "missing argument to %s", argp
[-2]);
202 adios (NULL
, "too many parts (starting with %s), %d max",
208 if (!(cp
= *argp
++) || *cp
== '-')
209 adios (NULL
, "missing argument to %s", argp
[-2]);
211 adios (NULL
, "too many types (starting with %s), %d max",
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
));
301 * Check for storage directory. If specified,
302 * then store temporary files there. Else we
303 * store them in standard nmh directory.
305 if ((cp
= context_find (nmhstorage
)) && *cp
)
306 tmp
= concat (cp
, "/", invo_name
, NULL
);
308 tmp
= add (m_maildir (invo_name
), NULL
);
310 if (!context_find ("path"))
311 free (path ("./", TFOLDER
));
313 if (file
&& msgs
.size
)
314 adios (NULL
, "cannot specify msg and file at same time!");
317 * check if message is coming from file
320 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
321 adios (NULL
, "out of memory");
324 if ((ct
= parse_mime (file
)))
328 * message(s) are coming from a folder
331 app_msgarg(&msgs
, "cur");
333 folder
= getfolder (1);
334 maildir
= m_maildir (folder
);
336 if (chdir (maildir
) == NOTOK
)
337 adios (maildir
, "unable to change directory to");
339 /* read folder and create message structure */
340 if (!(mp
= folder_read (folder
)))
341 adios (NULL
, "unable to read folder %s", folder
);
343 /* check for empty folder */
345 adios (NULL
, "no messages in %s", folder
);
347 /* parse all the message ranges/sequences and set SELECTED */
348 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
349 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
353 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
354 * since we will use that as a tag to know which messages
355 * to remove from the "unseen" sequence.
357 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
358 if (is_selected(mp
, msgnum
))
359 set_unseen (mp
, msgnum
);
361 seq_setprev (mp
); /* set the Previous-Sequence */
362 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
364 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
365 adios (NULL
, "out of memory");
369 * Parse all the SELECTED messages.
371 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
372 if (is_selected(mp
, msgnum
)) {
375 msgnam
= m_name (msgnum
);
376 if ((ct
= parse_mime (msgnam
)))
386 SIGNAL (SIGQUIT
, quitser
);
387 SIGNAL (SIGPIPE
, pipeser
);
390 * Get the associated umask for the relevant contents.
392 for (ctp
= cts
; *ctp
; ctp
++) {
396 if (type_ok (ct
, 1) && !ct
->c_umask
) {
397 if (stat (ct
->c_file
, &st
) != NOTOK
)
398 ct
->c_umask
= ~(st
.st_mode
& 0777);
400 ct
->c_umask
= ~m_gmprot();
404 /* If reading from a folder, do some updating */
406 context_replace (pfolder
, folder
);/* update current folder */
407 seq_setcur (mp
, mp
->hghsel
); /* update current message */
408 seq_save (mp
); /* synchronize sequences */
409 context_save (); /* save the context file */
413 * Show the message content
415 show_all_messages (cts
);
417 /* Now free all the structures for the content */
418 for (ctp
= cts
; *ctp
; ctp
++)
435 fprintf (stderr
, "\n");