]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
1 /* mhlist.c -- list 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.
9 #include "sbr/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
28 #include "h/signals.h"
33 #include "h/mhparse.h"
34 #include "h/mhcachesbr.h"
38 #include "sbr/m_maildir.h"
41 #define MHLIST_SWITCHES \
42 X("check", 0, CHECKSW) \
43 X("nocheck", 0, NCHECKSW) \
44 X("headers", 0, HEADSW) \
45 X("noheaders", 0, NHEADSW) \
46 X("realsize", 0, SIZESW) \
47 X("norealsize", 0, NSIZESW) \
48 X("verbose", 0, VERBSW) \
49 X("noverbose", 0, NVERBSW) \
50 X("disposition", 0, DISPOSW) \
51 X("nodisposition", 0, NDISPOSW) \
52 X("file file", 0, FILESW) \
53 X("part number", 0, PARTSW) \
54 X("type content", 0, TYPESW) \
55 X("prefer content", 0, PREFERSW) \
56 X("noprefer", 0, NPREFERSW) \
57 X("rcache policy", 0, RCACHESW) \
58 X("wcache policy", 0, WCACHESW) \
59 X("changecur", 0, CHGSW) \
60 X("nochangecur", 0, NCHGSW) \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63 X("debug", -5, DEBUGSW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(MHLIST
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
74 * This is currently needed to keep mhparse happy.
75 * This needs to be changed.
79 #define quitser pipeser
84 static void pipeser (int);
88 main (int argc
, char **argv
)
95 int msgnum
, *icachesw
;
96 char *cp
, *file
= NULL
, *folder
= NULL
;
97 char *maildir
, buf
[100], **argp
;
99 struct msgs_array msgs
= { 0, 0, NULL
};
100 struct msgs
*mp
= NULL
;
103 if (nmh_init(argv
[0], true, true)) { return 1; }
105 set_done(freects_done
);
107 arguments
= getarguments (invo_name
, argc
, argv
, 1);
113 while ((cp
= *argp
++)) {
115 switch (smatch (++cp
, switches
)) {
117 ambigsw (cp
, switches
);
120 die("-%s unknown", cp
);
123 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
125 print_help (buf
, switches
, 1);
128 print_version(invo_name
);
132 icachesw
= &rcachesw
;
135 icachesw
= &wcachesw
;
137 if (!(cp
= *argp
++) || *cp
== '-')
138 die("missing argument to %s", argp
[-2]);
139 switch (*icachesw
= smatch (cp
, cache_policy
)) {
141 ambigsw (cp
, cache_policy
);
144 die("%s unknown", cp
);
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
175 die("too many parts (starting with %s), %d max",
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
184 die("too many types (starting with %s), %d max",
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 if (npreferred
>= NPREFS
)
193 die("too many preferred types (starting with %s), %d max",
195 mime_preference
[npreferred
].type
= cp
;
196 cp
= strchr(cp
, '/');
197 if (cp
) *cp
++ = '\0';
198 mime_preference
[npreferred
++].subtype
= cp
;
206 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
207 die("missing argument to %s", argp
[-2]);
208 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
235 if (*cp
== '+' || *cp
== '@') {
237 die("only one folder at a time!");
238 folder
= pluspath (cp
);
240 app_msgarg(&msgs
, cp
);
243 /* null terminate the list of acceptable parts/types */
247 /* Check for public cache location */
248 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
251 /* Check for private cache location */
252 if (!(cache_private
= context_find (nmhprivcache
)))
253 cache_private
= ".cache";
254 cache_private
= mh_xstrdup(m_maildir(cache_private
));
256 if (!context_find ("path"))
257 free (path ("./", TFOLDER
));
259 if (file
&& msgs
.size
)
260 die("cannot specify msg and file at same time!");
263 * check if message is coming from file
266 cts
= mh_xcalloc(2, sizeof *cts
);
269 if ((ct
= parse_mime (file
)))
273 * message(s) are coming from a folder
276 app_msgarg(&msgs
, "cur");
278 folder
= getfolder (1);
279 maildir
= m_maildir (folder
);
281 if (chdir (maildir
) == NOTOK
)
282 adios (maildir
, "unable to change directory to");
284 /* read folder and create message structure */
285 if (!(mp
= folder_read (folder
, 0)))
286 die("unable to read folder %s", folder
);
288 /* check for empty folder */
290 die("no messages in %s", folder
);
292 /* parse all the message ranges/sequences and set SELECTED */
293 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
294 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
296 seq_setprev (mp
); /* set the previous-sequence */
298 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
301 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
302 if (is_selected(mp
, msgnum
)) {
305 msgnam
= m_name (msgnum
);
306 if ((ct
= parse_mime (msgnam
)))
316 SIGNAL (SIGQUIT
, quitser
);
317 SIGNAL (SIGPIPE
, pipeser
);
320 * Get the associated umask for the relevant contents.
322 for (ctp
= cts
; *ctp
; ctp
++) {
326 if (type_ok (ct
, 1) && !ct
->c_umask
) {
327 if (stat (ct
->c_file
, &st
) != NOTOK
)
328 ct
->c_umask
= ~(st
.st_mode
& 0777);
330 ct
->c_umask
= ~m_gmprot();
335 * List the message content
337 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
339 /* Now free all the structures for the content */
340 for (ctp
= cts
; *ctp
; ctp
++)
346 /* If reading from a folder, do some updating */
348 context_replace (pfolder
, folder
);/* update current folder */
350 seq_setcur (mp
, mp
->hghsel
); /* update current message */
351 seq_save (mp
); /* synchronize sequences */
352 context_save (); /* save the context file */
365 fprintf (stderr
, "\n");