]>
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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHSHOW_SWITCHES \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("concat", 0, CONCATSW) \
27 X("noconcat", 0, NCONCATSW) \
28 X("textonly", 0, TEXTONLYSW) \
29 X("notextonly", 0, NTEXTONLYSW) \
30 X("inlineonly", 0, INLINESW) \
31 X("noinlineonly", 0, NINLINESW) \
32 X("file file", 0, FILESW) \
33 X("form formfile", 0, FORMSW) \
34 X("header", 0, HEADSW) \
35 X("noheader", 0, NHEADSW) \
36 X("headerform formfile", 0, HEADFORMSW) \
37 X("markform formfile", 0, MARKFORMSW) \
38 X("part number", 0, PARTSW) \
39 X("type content", 0, TYPESW) \
40 X("prefer content", 0, PREFERSW) \
41 X("rcache policy", 0, RCACHESW) \
42 X("wcache policy", 0, WCACHESW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
46 * switches for moreproc/mhlproc \
48 X("moreproc program", -4, PROGSW) \
49 X("nomoreproc", -3, NPROGSW) \
50 X("length lines", -4, LENSW) \
51 X("width columns", -4, WIDTHSW) \
53 * switches for debugging \
55 X("debug", -5, DEBUGSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(MHSHOW
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
69 extern char *cache_public
;
70 extern char *cache_private
;
74 extern int nomore
; /* flags for moreproc/header display */
77 extern char *headerform
;
78 extern char *markerform
;
84 extern char *parts
[NPARTS
+ 1];
85 extern char *types
[NTYPES
+ 1];
89 extern char *preferred_types
[];
90 extern char *preferred_subtypes
[];
91 extern int npreferred
;
96 #define quitser pipeser
99 CT
parse_mime (char *);
103 int type_ok (CT
, int);
104 void flush_errors (void);
108 void freects_done (int) NORETURN
;
113 static void pipeser (int);
117 main (int argc
, char **argv
)
119 int msgnum
, *icachesw
, concatsw
= -1, textonly
= -1, inlineonly
= -1;
120 char *cp
, *file
= NULL
;
121 char *maildir
, buf
[100], **argp
;
123 struct msgs_array msgs
= { 0, 0, NULL
};
124 struct msgs
*mp
= NULL
;
128 if (nmh_init(argv
[0], 1)) { return 1; }
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
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 adios (NULL
, "missing argument to %s", argp
[-2]);
205 adios (NULL
, "too many parts (starting with %s), %d max",
211 if (!(cp
= *argp
++) || *cp
== '-')
212 adios (NULL
, "missing argument to %s", argp
[-2]);
214 adios (NULL
, "too many types (starting with %s), %d max",
220 if (!(cp
= *argp
++) || *cp
== '-')
221 adios (NULL
, "missing argument to %s", argp
[-2]);
222 if (npreferred
>= NPREFS
)
223 adios (NULL
, "too many preferred types (starting with %s), %d max",
225 preferred_types
[npreferred
] = cp
;
226 cp
= strchr(cp
, '/');
227 if (cp
) *cp
++ = '\0';
228 preferred_subtypes
[npreferred
++] = cp
;
232 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
233 adios (NULL
, "missing argument to %s", argp
[-2]);
234 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
238 if (!(cp
= *argp
++) || *cp
== '-')
239 adios (NULL
, "missing argument to %s", argp
[-2]);
242 formsw
= getcpy (etcpath (cp
));
253 if (!(headerform
= *argp
++) || *headerform
== '-')
254 adios (NULL
, "missing argument to %s", argp
[-2]);
258 if (!(markerform
= *argp
++) || *markerform
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
263 * Switches for moreproc/mhlproc
266 if (!(progsw
= *argp
++) || *progsw
== '-')
267 adios (NULL
, "missing argument to %s", argp
[-2]);
275 if (!(cp
= *argp
++) || *cp
== '-')
276 adios (NULL
, "missing argument to %s", argp
[-2]);
290 if (*cp
== '+' || *cp
== '@') {
292 adios (NULL
, "only one folder at a time!");
294 folder
= pluspath (cp
);
296 app_msgarg(&msgs
, cp
);
299 /* null terminate the list of acceptable parts/types */
304 * If we had any specific parts or types specified, turn off text only
308 if (npart
> 0 || ntype
> 0) {
311 if (inlineonly
== -1)
316 * Check if we've specified an additional profile
318 if ((cp
= getenv ("MHSHOW"))) {
319 if ((fp
= fopen (cp
, "r"))) {
320 readconfig ((struct node
**) 0, fp
, cp
, 0);
323 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
328 * Read the standard profile setup
330 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
331 readconfig ((struct node
**) 0, fp
, cp
, 0);
335 /* Check for public cache location */
336 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
339 /* Check for private cache location */
340 if (!(cache_private
= context_find (nmhprivcache
)))
341 cache_private
= ".cache";
342 cache_private
= getcpy (m_maildir (cache_private
));
344 if (!context_find ("path"))
345 free (path ("./", TFOLDER
));
347 if (file
&& msgs
.size
)
348 adios (NULL
, "cannot specify msg and file at same time!");
351 * check if message is coming from file
354 if (!(cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof(*cts
))))
355 adios (NULL
, "out of memory");
358 if ((ct
= parse_mime (file
)))
364 * message(s) are coming from a folder
367 app_msgarg(&msgs
, "cur");
369 folder
= getfolder (1);
370 maildir
= m_maildir (folder
);
372 if (chdir (maildir
) == NOTOK
)
373 adios (maildir
, "unable to change directory to");
375 /* read folder and create message structure */
376 if (!(mp
= folder_read (folder
, 1)))
377 adios (NULL
, "unable to read folder %s", folder
);
379 /* check for empty folder */
381 adios (NULL
, "no messages in %s", folder
);
383 /* parse all the message ranges/sequences and set SELECTED */
384 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
385 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
389 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
390 * since we will use that as a tag to know which messages
391 * to remove from the "unseen" sequence.
393 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
394 if (is_selected(mp
, msgnum
))
395 set_unseen (mp
, msgnum
);
397 seq_setprev (mp
); /* set the Previous-Sequence */
398 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
400 if (!(cts
= (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
401 adios (NULL
, "out of memory");
405 * Parse all the SELECTED messages.
407 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
408 if (is_selected(mp
, msgnum
)) {
411 msgnam
= m_name (msgnum
);
412 if ((ct
= parse_mime (msgnam
)))
422 SIGNAL (SIGQUIT
, quitser
);
423 SIGNAL (SIGPIPE
, pipeser
);
426 * Get the associated umask for the relevant contents.
428 for (ctp
= cts
; *ctp
; ctp
++) {
432 if (type_ok (ct
, 1) && !ct
->c_umask
) {
433 if (stat (ct
->c_file
, &st
) != NOTOK
)
434 ct
->c_umask
= ~(st
.st_mode
& 0777);
436 ct
->c_umask
= ~m_gmprot();
440 /* If reading from a folder, do some updating */
442 context_replace (pfolder
, folder
);/* update current folder */
443 seq_setcur (mp
, mp
->hghsel
); /* update current message */
444 seq_save (mp
); /* synchronize sequences */
445 context_save (); /* save the context file */
449 m_popen(moreproc
, 0);
452 * Show the message content
454 show_all_messages (cts
, concatsw
, textonly
, inlineonly
);
456 /* Now free all the structures for the content */
457 for (ctp
= cts
; *ctp
; ctp
++)
476 fprintf (stderr
, "\n");