]>
diplodocus.org Git - nmh/blob - uip/mhshow.c
3d1f3e6b86bca92e3abbc001c3b90db1481d0abe
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("pause", 0, PAUSESW) \
25 X("nopause", 0, NPAUSESW) \
26 X("serialonly", 0, SERIALSW) \
27 X("noserialonly", 0, NSERIALSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("file file", 0, FILESW) \
31 X("form formfile", 0, FORMSW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("rcache policy", 0, RCACHESW) \
35 X("wcache policy", 0, WCACHESW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 * switches for moreproc/mhlproc \
41 X("moreproc program", -4, PROGSW) \
42 X("nomoreproc", -3, NPROGSW) \
43 X("length lines", -4, LENSW) \
44 X("width columns", -4, WIDTHSW) \
46 * switches for debugging \
48 X("debug", -5, DEBUGSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MHSHOW
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MHSHOW
, switches
);
62 extern char *cache_public
;
63 extern char *cache_private
;
69 extern int nomore
; /* flags for moreproc/header display */
75 extern char *parts
[NPARTS
+ 1];
76 extern char *types
[NTYPES
+ 1];
82 #define quitser pipeser
85 CT
parse_mime (char *);
88 int part_ok (CT
, int);
89 int type_ok (CT
, int);
90 void flush_errors (void);
93 void show_all_messages (CT
*);
97 void freects_done (int) NORETURN
;
102 static void pipeser (int);
106 main (int argc
, char **argv
)
108 int msgnum
, *icachesw
;
109 char *cp
, *file
= NULL
, *folder
= NULL
;
110 char *maildir
, buf
[100], **argp
;
112 struct msgs_array msgs
= { 0, 0, NULL
};
113 struct msgs
*mp
= NULL
;
120 setlocale(LC_ALL
, "");
122 invo_name
= r1bindex (argv
[0], '/');
124 /* read user profile/context */
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
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 adios (NULL
, "missing argument to %s", argp
[-2]);
195 adios (NULL
, "too many parts (starting with %s), %d max",
201 if (!(cp
= *argp
++) || *cp
== '-')
202 adios (NULL
, "missing argument to %s", argp
[-2]);
204 adios (NULL
, "too many types (starting with %s), %d max",
210 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
211 adios (NULL
, "missing argument to %s", argp
[-2]);
212 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
216 if (!(cp
= *argp
++) || *cp
== '-')
217 adios (NULL
, "missing argument to %s", argp
[-2]);
220 formsw
= getcpy (etcpath (cp
));
224 * Switches for moreproc/mhlproc
227 if (!(progsw
= *argp
++) || *progsw
== '-')
228 adios (NULL
, "missing argument to %s", argp
[-2]);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 adios (NULL
, "missing argument to %s", argp
[-2]);
251 if (*cp
== '+' || *cp
== '@') {
253 adios (NULL
, "only one folder at a time!");
255 folder
= pluspath (cp
);
257 app_msgarg(&msgs
, cp
);
260 /* null terminate the list of acceptable parts/types */
265 * Check if we've specified an additional profile
267 if ((cp
= getenv ("MHSHOW"))) {
268 if ((fp
= fopen (cp
, "r"))) {
269 readconfig ((struct node
**) 0, fp
, cp
, 0);
272 admonish ("", "unable to read $MHSHOW profile (%s)", cp
);
277 * Read the standard profile setup
279 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
280 readconfig ((struct node
**) 0, fp
, cp
, 0);
284 /* Check for public cache location */
285 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
288 /* Check for private cache location */
289 if (!(cache_private
= context_find (nmhprivcache
)))
290 cache_private
= ".cache";
291 cache_private
= getcpy (m_maildir (cache_private
));
293 if (!context_find ("path"))
294 free (path ("./", TFOLDER
));
296 if (file
&& msgs
.size
)
297 adios (NULL
, "cannot specify msg and file at same time!");
300 * check if message is coming from file
303 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
304 adios (NULL
, "out of memory");
307 if ((ct
= parse_mime (file
)))
311 * message(s) are coming from a folder
314 app_msgarg(&msgs
, "cur");
316 folder
= getfolder (1);
317 maildir
= m_maildir (folder
);
319 if (chdir (maildir
) == NOTOK
)
320 adios (maildir
, "unable to change directory to");
322 /* read folder and create message structure */
323 if (!(mp
= folder_read (folder
, 1)))
324 adios (NULL
, "unable to read folder %s", folder
);
326 /* check for empty folder */
328 adios (NULL
, "no messages in %s", folder
);
330 /* parse all the message ranges/sequences and set SELECTED */
331 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
332 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
336 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
337 * since we will use that as a tag to know which messages
338 * to remove from the "unseen" sequence.
340 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
341 if (is_selected(mp
, msgnum
))
342 set_unseen (mp
, msgnum
);
344 seq_setprev (mp
); /* set the Previous-Sequence */
345 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
347 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
348 adios (NULL
, "out of memory");
352 * Parse all the SELECTED messages.
354 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
355 if (is_selected(mp
, msgnum
)) {
358 msgnam
= m_name (msgnum
);
359 if ((ct
= parse_mime (msgnam
)))
369 SIGNAL (SIGQUIT
, quitser
);
370 SIGNAL (SIGPIPE
, pipeser
);
373 * Get the associated umask for the relevant contents.
375 for (ctp
= cts
; *ctp
; ctp
++) {
379 if (type_ok (ct
, 1) && !ct
->c_umask
) {
380 if (stat (ct
->c_file
, &st
) != NOTOK
)
381 ct
->c_umask
= ~(st
.st_mode
& 0777);
383 ct
->c_umask
= ~m_gmprot();
387 /* If reading from a folder, do some updating */
389 context_replace (pfolder
, folder
);/* update current folder */
390 seq_setcur (mp
, mp
->hghsel
); /* update current message */
391 seq_save (mp
); /* synchronize sequences */
392 context_save (); /* save the context file */
396 * Show the message content
398 show_all_messages (cts
);
400 /* Now free all the structures for the content */
401 for (ctp
= cts
; *ctp
; ctp
++)
417 fprintf (stderr
, "\n");