]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
1 /* mhshow.c -- display the contents of MIME messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/signals.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
20 #define MHSHOW_SWITCHES \
21 X("check", 0, CHECKSW) \
22 X("nocheck", 0, NCHECKSW) \
23 X("verbose", 0, VERBSW) \
24 X("noverbose", 0, NVERBSW) \
25 X("concat", 0, CONCATSW) \
26 X("noconcat", 0, NCONCATSW) \
27 X("textonly", 0, TEXTONLYSW) \
28 X("notextonly", 0, NTEXTONLYSW) \
29 X("inlineonly", 0, INLINESW) \
30 X("noinlineonly", 0, NINLINESW) \
31 X("file file", 0, FILESW) \
32 X("form formfile", 0, FORMSW) \
33 X("header", 0, HEADSW) \
34 X("noheader", 0, NHEADSW) \
35 X("headerform formfile", 0, HEADFORMSW) \
36 X("markform formfile", 0, MARKFORMSW) \
37 X("part number", 0, PARTSW) \
38 X("type content", 0, TYPESW) \
39 X("prefer content", 0, PREFERSW) \
40 X("rcache policy", 0, RCACHESW) \
41 X("wcache policy", 0, WCACHESW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 * switches for moreproc/mhlproc \
47 X("moreproc program", -4, PROGSW) \
48 X("nomoreproc", -3, NPROGSW) \
49 X("length lines", -4, LENSW) \
50 X("width columns", -4, WIDTHSW) \
52 * switches for debugging \
54 X("debug", -5, DEBUGSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(MHSHOW
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
68 extern char *cache_public
;
69 extern char *cache_private
;
73 extern int nomore
; /* flags for moreproc/header display */
76 extern char *headerform
;
77 extern char *markerform
;
83 extern char *parts
[NPARTS
+ 1];
84 extern char *types
[NTYPES
+ 1];
88 extern char *preferred_types
[];
89 extern char *preferred_subtypes
[];
90 extern int npreferred
;
95 #define quitser pipeser
98 CT
parse_mime (char *);
102 int type_ok (CT
, int);
103 void flush_errors (void);
108 static void pipeser (int);
112 main (int argc
, char **argv
)
114 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
115 char *cp
, *file
= NULL
;
116 char *maildir
, buf
[100], **argp
;
118 struct msgs_array msgs
= { 0, 0, NULL
};
119 struct msgs
*mp
= NULL
;
123 if (nmh_init(argv
[0], 1)) { return 1; }
127 arguments
= getarguments (invo_name
, argc
, argv
, 1);
133 while ((cp
= *argp
++)) {
135 switch (smatch (++cp
, switches
)) {
137 ambigsw (cp
, switches
);
140 adios (NULL
, "-%s unknown", cp
);
143 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
145 print_help (buf
, switches
, 1);
148 print_version(invo_name
);
152 icachesw
= &rcachesw
;
155 icachesw
= &wcachesw
;
157 if (!(cp
= *argp
++) || *cp
== '-')
158 adios (NULL
, "missing argument to %s", argp
[-2]);
159 switch (*icachesw
= smatch (cp
, caches
)) {
161 ambigsw (cp
, caches
);
164 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
== '-')
216 adios (NULL
, "missing argument to %s", argp
[-2]);
217 if (npreferred
>= NPREFS
)
218 adios (NULL
, "too many preferred types (starting with %s), %d max",
220 preferred_types
[npreferred
] = cp
;
221 cp
= strchr(cp
, '/');
222 if (cp
) *cp
++ = '\0';
223 preferred_subtypes
[npreferred
++] = cp
;
227 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
228 adios (NULL
, "missing argument to %s", argp
[-2]);
229 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 adios (NULL
, "missing argument to %s", argp
[-2]);
236 formsw
= getcpy (etcpath (cp
));
247 if (!(headerform
= *argp
++) || *headerform
== '-')
248 adios (NULL
, "missing argument to %s", argp
[-2]);
252 if (!(markerform
= *argp
++) || *markerform
== '-')
253 adios (NULL
, "missing argument to %s", argp
[-2]);
257 * Switches for moreproc/mhlproc
260 if (!(progsw
= *argp
++) || *progsw
== '-')
261 adios (NULL
, "missing argument to %s", argp
[-2]);
269 if (!(cp
= *argp
++) || *cp
== '-')
270 adios (NULL
, "missing argument to %s", argp
[-2]);
284 if (*cp
== '+' || *cp
== '@') {
286 adios (NULL
, "only one folder at a time!");
288 folder
= pluspath (cp
);
290 app_msgarg(&msgs
, cp
);
293 /* null terminate the list of acceptable parts/types */
298 * If we had any specific parts or types specified, turn off text only
302 if (npart
> 0 || ntype
> 0) {
305 if (inlineonly
== -1)
310 * Check if we've specified an additional profile
312 if ((cp
= getenv ("MHSHOW"))) {
313 if ((fp
= fopen (cp
, "r"))) {
314 readconfig ((struct node
**) 0, fp
, cp
, 0);
317 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
322 * Read the standard profile setup
324 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
325 readconfig ((struct node
**) 0, fp
, cp
, 0);
329 /* Check for public cache location */
330 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
333 /* Check for private cache location */
334 if (!(cache_private
= context_find (nmhprivcache
)))
335 cache_private
= ".cache";
336 cache_private
= getcpy (m_maildir (cache_private
));
338 if (!context_find ("path"))
339 free (path ("./", TFOLDER
));
341 if (file
&& msgs
.size
)
342 adios (NULL
, "cannot specify msg and file at same time!");
345 * check if message is coming from file
348 cts
= mh_xcalloc(2, sizeof *cts
);
351 if ((ct
= parse_mime (file
)))
357 * message(s) are coming from a folder
360 app_msgarg(&msgs
, "cur");
362 folder
= getfolder (1);
363 maildir
= m_maildir (folder
);
365 if (chdir (maildir
) == NOTOK
)
366 adios (maildir
, "unable to change directory to");
368 /* read folder and create message structure */
369 if (!(mp
= folder_read (folder
, 1)))
370 adios (NULL
, "unable to read folder %s", folder
);
372 /* check for empty folder */
374 adios (NULL
, "no messages in %s", folder
);
376 /* parse all the message ranges/sequences and set SELECTED */
377 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
378 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
382 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
383 * since we will use that as a tag to know which messages
384 * to remove from the "unseen" sequence.
386 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
387 if (is_selected(mp
, msgnum
))
388 set_unseen (mp
, msgnum
);
390 seq_setprev (mp
); /* set the Previous-Sequence */
391 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
393 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
397 * Parse all the SELECTED messages.
399 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
400 if (is_selected(mp
, msgnum
)) {
403 msgnam
= m_name (msgnum
);
404 if ((ct
= parse_mime (msgnam
)))
414 SIGNAL (SIGQUIT
, quitser
);
415 SIGNAL (SIGPIPE
, pipeser
);
418 * Get the associated umask for the relevant contents.
420 for (ctp
= cts
; *ctp
; ctp
++) {
424 if (type_ok (ct
, 1) && !ct
->c_umask
) {
425 if (stat (ct
->c_file
, &st
) != NOTOK
)
426 ct
->c_umask
= ~(st
.st_mode
& 0777);
428 ct
->c_umask
= ~m_gmprot();
432 /* If reading from a folder, do some updating */
434 context_replace (pfolder
, folder
);/* update current folder */
435 seq_setcur (mp
, mp
->hghsel
); /* update current message */
436 seq_save (mp
); /* synchronize sequences */
437 context_save (); /* save the context file */
441 m_popen(moreproc
, 0);
444 * Show the message content
446 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
448 /* Now free all the structures for the content */
449 for (ctp
= cts
; *ctp
; ctp
++)
468 fprintf (stderr
, "\n");