]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
d0ff57611181eac56f9254cac2d0abefec5c79cf
2 * mhshow.c -- display the contents of MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
16 #include <h/mhparse.h>
17 #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);
107 void freects_done (int) NORETURN
;
112 static void pipeser (int);
116 main (int argc
, char **argv
)
118 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
119 char *cp
, *file
= NULL
;
120 char *maildir
, buf
[100], **argp
;
122 struct msgs_array msgs
= { 0, 0, NULL
};
123 struct msgs
*mp
= NULL
;
127 if (nmh_init(argv
[0], 1)) { return 1; }
131 arguments
= getarguments (invo_name
, argc
, argv
, 1);
137 while ((cp
= *argp
++)) {
139 switch (smatch (++cp
, switches
)) {
141 ambigsw (cp
, switches
);
144 adios (NULL
, "-%s unknown", cp
);
147 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
149 print_help (buf
, switches
, 1);
152 print_version(invo_name
);
156 icachesw
= &rcachesw
;
159 icachesw
= &wcachesw
;
161 if (!(cp
= *argp
++) || *cp
== '-')
162 adios (NULL
, "missing argument to %s", argp
[-2]);
163 switch (*icachesw
= smatch (cp
, caches
)) {
165 ambigsw (cp
, caches
);
168 adios (NULL
, "%s unknown", cp
);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 adios (NULL
, "missing argument to %s", argp
[-2]);
204 adios (NULL
, "too many parts (starting with %s), %d max",
210 if (!(cp
= *argp
++) || *cp
== '-')
211 adios (NULL
, "missing argument to %s", argp
[-2]);
213 adios (NULL
, "too many types (starting with %s), %d max",
219 if (!(cp
= *argp
++) || *cp
== '-')
220 adios (NULL
, "missing argument to %s", argp
[-2]);
221 if (npreferred
>= NPREFS
)
222 adios (NULL
, "too many preferred types (starting with %s), %d max",
224 preferred_types
[npreferred
] = cp
;
225 cp
= strchr(cp
, '/');
226 if (cp
) *cp
++ = '\0';
227 preferred_subtypes
[npreferred
++] = cp
;
231 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
232 adios (NULL
, "missing argument to %s", argp
[-2]);
233 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
237 if (!(cp
= *argp
++) || *cp
== '-')
238 adios (NULL
, "missing argument to %s", argp
[-2]);
240 formsw
= getcpy (etcpath (cp
));
251 if (!(headerform
= *argp
++) || *headerform
== '-')
252 adios (NULL
, "missing argument to %s", argp
[-2]);
256 if (!(markerform
= *argp
++) || *markerform
== '-')
257 adios (NULL
, "missing argument to %s", argp
[-2]);
261 * Switches for moreproc/mhlproc
264 if (!(progsw
= *argp
++) || *progsw
== '-')
265 adios (NULL
, "missing argument to %s", argp
[-2]);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 adios (NULL
, "missing argument to %s", argp
[-2]);
288 if (*cp
== '+' || *cp
== '@') {
290 adios (NULL
, "only one folder at a time!");
292 folder
= pluspath (cp
);
294 app_msgarg(&msgs
, cp
);
297 /* null terminate the list of acceptable parts/types */
302 * If we had any specific parts or types specified, turn off text only
306 if (npart
> 0 || ntype
> 0) {
309 if (inlineonly
== -1)
314 * Check if we've specified an additional profile
316 if ((cp
= getenv ("MHSHOW"))) {
317 if ((fp
= fopen (cp
, "r"))) {
318 readconfig ((struct node
**) 0, fp
, cp
, 0);
321 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
326 * Read the standard profile setup
328 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
329 readconfig ((struct node
**) 0, fp
, cp
, 0);
333 /* Check for public cache location */
334 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
337 /* Check for private cache location */
338 if (!(cache_private
= context_find (nmhprivcache
)))
339 cache_private
= ".cache";
340 cache_private
= getcpy (m_maildir (cache_private
));
342 if (!context_find ("path"))
343 free (path ("./", TFOLDER
));
345 if (file
&& msgs
.size
)
346 adios (NULL
, "cannot specify msg and file at same time!");
349 * check if message is coming from file
352 cts
= mh_xcalloc(2, sizeof *cts
);
355 if ((ct
= parse_mime (file
)))
361 * message(s) are coming from a folder
364 app_msgarg(&msgs
, "cur");
366 folder
= getfolder (1);
367 maildir
= m_maildir (folder
);
369 if (chdir (maildir
) == NOTOK
)
370 adios (maildir
, "unable to change directory to");
372 /* read folder and create message structure */
373 if (!(mp
= folder_read (folder
, 1)))
374 adios (NULL
, "unable to read folder %s", folder
);
376 /* check for empty folder */
378 adios (NULL
, "no messages in %s", folder
);
380 /* parse all the message ranges/sequences and set SELECTED */
381 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
382 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
386 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
387 * since we will use that as a tag to know which messages
388 * to remove from the "unseen" sequence.
390 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
391 if (is_selected(mp
, msgnum
))
392 set_unseen (mp
, msgnum
);
394 seq_setprev (mp
); /* set the Previous-Sequence */
395 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
397 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
401 * Parse all the SELECTED messages.
403 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
404 if (is_selected(mp
, msgnum
)) {
407 msgnam
= m_name (msgnum
);
408 if ((ct
= parse_mime (msgnam
)))
418 SIGNAL (SIGQUIT
, quitser
);
419 SIGNAL (SIGPIPE
, pipeser
);
422 * Get the associated umask for the relevant contents.
424 for (ctp
= cts
; *ctp
; ctp
++) {
428 if (type_ok (ct
, 1) && !ct
->c_umask
) {
429 if (stat (ct
->c_file
, &st
) != NOTOK
)
430 ct
->c_umask
= ~(st
.st_mode
& 0777);
432 ct
->c_umask
= ~m_gmprot();
436 /* If reading from a folder, do some updating */
438 context_replace (pfolder
, folder
);/* update current folder */
439 seq_setcur (mp
, mp
->hghsel
); /* update current message */
440 seq_save (mp
); /* synchronize sequences */
441 context_save (); /* save the context file */
445 m_popen(moreproc
, 0);
448 * Show the message content
450 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
452 /* Now free all the structures for the content */
453 for (ctp
= cts
; *ctp
; ctp
++)
472 fprintf (stderr
, "\n");