]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
d4d767c380d42be2c261411500033329dc75fc11
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>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #define MHSHOW_SWITCHES \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("pause", 0, PAUSESW) \
26 X("nopause", 0, NPAUSESW) \
27 X("serialonly", 0, SERIALSW) \
28 X("noserialonly", 0, NSERIALSW) \
29 X("verbose", 0, VERBSW) \
30 X("noverbose", 0, NVERBSW) \
31 X("file file", 0, FILESW) \
32 X("form formfile", 0, FORMSW) \
33 X("part number", 0, PARTSW) \
34 X("type content", 0, TYPESW) \
35 X("rcache policy", 0, RCACHESW) \
36 X("wcache policy", 0, WCACHESW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 * switches for moreproc/mhlproc \
42 X("moreproc program", -4, PROGSW) \
43 X("nomoreproc", -3, NPROGSW) \
44 X("length lines", -4, LENSW) \
45 X("width columns", -4, WIDTHSW) \
47 * switches for debugging \
49 X("debug", -5, DEBUGSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MHSHOW
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
61 extern char *tmp
; /* directory to place temp files */
66 extern char *cache_public
;
67 extern char *cache_private
;
74 extern int nomore
; /* flags for moreproc/header display */
80 extern char *parts
[NPARTS
+ 1];
81 extern char *types
[NTYPES
+ 1];
87 #define quitser pipeser
90 CT
parse_mime (char *);
93 int part_ok (CT
, int);
94 int type_ok (CT
, int);
95 void flush_errors (void);
98 void show_all_messages (CT
*);
102 void freects_done (int) NORETURN
;
107 static void pipeser (int);
111 main (int argc
, char **argv
)
113 int msgnum
, *icachesw
;
114 char *cp
, *file
= NULL
, *folder
= NULL
;
115 char *maildir
, buf
[100], **argp
;
117 struct msgs_array msgs
= { 0, 0, NULL
};
118 struct msgs
*mp
= NULL
;
125 setlocale(LC_ALL
, "");
127 invo_name
= r1bindex (argv
[0], '/');
129 /* read user profile/context */
132 arguments
= getarguments (invo_name
, argc
, argv
, 1);
138 while ((cp
= *argp
++)) {
140 switch (smatch (++cp
, switches
)) {
142 ambigsw (cp
, switches
);
145 adios (NULL
, "-%s unknown", cp
);
148 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
150 print_help (buf
, switches
, 1);
153 print_version(invo_name
);
157 icachesw
= &rcachesw
;
160 icachesw
= &wcachesw
;
162 if (!(cp
= *argp
++) || *cp
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
164 switch (*icachesw
= smatch (cp
, caches
)) {
166 ambigsw (cp
, caches
);
169 adios (NULL
, "%s unknown", cp
);
197 if (!(cp
= *argp
++) || *cp
== '-')
198 adios (NULL
, "missing argument to %s", argp
[-2]);
200 adios (NULL
, "too many parts (starting with %s), %d max",
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
209 adios (NULL
, "too many types (starting with %s), %d max",
215 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
216 adios (NULL
, "missing argument to %s", argp
[-2]);
217 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
221 if (!(cp
= *argp
++) || *cp
== '-')
222 adios (NULL
, "missing argument to %s", argp
[-2]);
225 formsw
= getcpy (etcpath (cp
));
229 * Switches for moreproc/mhlproc
232 if (!(progsw
= *argp
++) || *progsw
== '-')
233 adios (NULL
, "missing argument to %s", argp
[-2]);
241 if (!(cp
= *argp
++) || *cp
== '-')
242 adios (NULL
, "missing argument to %s", argp
[-2]);
256 if (*cp
== '+' || *cp
== '@') {
258 adios (NULL
, "only one folder at a time!");
260 folder
= pluspath (cp
);
262 app_msgarg(&msgs
, cp
);
265 /* null terminate the list of acceptable parts/types */
270 * Check if we've specified an additional profile
272 if ((cp
= getenv ("MHSHOW"))) {
273 if ((fp
= fopen (cp
, "r"))) {
274 readconfig ((struct node
**) 0, fp
, cp
, 0);
277 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
282 * Read the standard profile setup
284 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
285 readconfig ((struct node
**) 0, fp
, cp
, 0);
289 /* Check for public cache location */
290 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
293 /* Check for private cache location */
294 if (!(cache_private
= context_find (nmhprivcache
)))
295 cache_private
= ".cache";
296 cache_private
= getcpy (m_maildir (cache_private
));
299 * Check for storage directory. If specified,
300 * then store temporary files there. Else we
301 * store them in standard nmh directory.
303 if ((cp
= context_find (nmhstorage
)) && *cp
)
304 tmp
= concat (cp
, "/", invo_name
, NULL
);
306 tmp
= add (m_maildir (invo_name
), NULL
);
308 if (!context_find ("path"))
309 free (path ("./", TFOLDER
));
311 if (file
&& msgs
.size
)
312 adios (NULL
, "cannot specify msg and file at same time!");
315 * check if message is coming from file
318 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
319 adios (NULL
, "out of memory");
322 if ((ct
= parse_mime (file
)))
326 * message(s) are coming from a folder
329 app_msgarg(&msgs
, "cur");
331 folder
= getfolder (1);
332 maildir
= m_maildir (folder
);
334 if (chdir (maildir
) == NOTOK
)
335 adios (maildir
, "unable to change directory to");
337 /* read folder and create message structure */
338 if (!(mp
= folder_read (folder
, 1)))
339 adios (NULL
, "unable to read folder %s", folder
);
341 /* check for empty folder */
343 adios (NULL
, "no messages in %s", folder
);
345 /* parse all the message ranges/sequences and set SELECTED */
346 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
347 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
351 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
352 * since we will use that as a tag to know which messages
353 * to remove from the "unseen" sequence.
355 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
356 if (is_selected(mp
, msgnum
))
357 set_unseen (mp
, msgnum
);
359 seq_setprev (mp
); /* set the Previous-Sequence */
360 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
362 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
363 adios (NULL
, "out of memory");
367 * Parse all the SELECTED messages.
369 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
370 if (is_selected(mp
, msgnum
)) {
373 msgnam
= m_name (msgnum
);
374 if ((ct
= parse_mime (msgnam
)))
384 SIGNAL (SIGQUIT
, quitser
);
385 SIGNAL (SIGPIPE
, pipeser
);
388 * Get the associated umask for the relevant contents.
390 for (ctp
= cts
; *ctp
; ctp
++) {
394 if (type_ok (ct
, 1) && !ct
->c_umask
) {
395 if (stat (ct
->c_file
, &st
) != NOTOK
)
396 ct
->c_umask
= ~(st
.st_mode
& 0777);
398 ct
->c_umask
= ~m_gmprot();
402 /* If reading from a folder, do some updating */
404 context_replace (pfolder
, folder
);/* update current folder */
405 seq_setcur (mp
, mp
->hghsel
); /* update current message */
406 seq_save (mp
); /* synchronize sequences */
407 context_save (); /* save the context file */
411 * Show the message content
413 show_all_messages (cts
);
415 /* Now free all the structures for the content */
416 for (ctp
= cts
; *ctp
; ctp
++)
432 fprintf (stderr
, "\n");