]>
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/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
19 #include "h/signals.h"
24 #include "h/mhparse.h"
25 #include "h/mhcachesbr.h"
29 #include "sbr/m_maildir.h"
32 #define MHLIST_SWITCHES \
33 X("check", 0, CHECKSW) \
34 X("nocheck", 0, NCHECKSW) \
35 X("headers", 0, HEADSW) \
36 X("noheaders", 0, NHEADSW) \
37 X("realsize", 0, SIZESW) \
38 X("norealsize", 0, NSIZESW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("disposition", 0, DISPOSW) \
42 X("nodisposition", 0, NDISPOSW) \
43 X("file file", 0, FILESW) \
44 X("part number", 0, PARTSW) \
45 X("type content", 0, TYPESW) \
46 X("prefer content", 0, PREFERSW) \
47 X("noprefer", 0, NPREFERSW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
50 X("changecur", 0, CHGSW) \
51 X("nochangecur", 0, NCHGSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
54 X("debug", -5, DEBUGSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(MHLIST
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
65 * This is currently needed to keep mhparse happy.
66 * This needs to be changed.
70 #define quitser pipeser
75 static void pipeser (int);
79 main (int argc
, char **argv
)
86 int msgnum
, *icachesw
;
87 char *cp
, *file
= NULL
, *folder
= NULL
;
88 char *maildir
, buf
[100], **argp
;
90 struct msgs_array msgs
= { 0, 0, NULL
};
91 struct msgs
*mp
= NULL
;
94 if (nmh_init(argv
[0], true, true)) { return 1; }
96 set_done(freects_done
);
98 arguments
= getarguments (invo_name
, argc
, argv
, 1);
104 while ((cp
= *argp
++)) {
106 switch (smatch (++cp
, switches
)) {
108 ambigsw (cp
, switches
);
111 die("-%s unknown", cp
);
114 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
123 icachesw
= &rcachesw
;
126 icachesw
= &wcachesw
;
128 if (!(cp
= *argp
++) || *cp
== '-')
129 die("missing argument to %s", argp
[-2]);
130 switch (*icachesw
= smatch (cp
, cache_policy
)) {
132 ambigsw (cp
, cache_policy
);
135 die("%s unknown", cp
);
163 if (!(cp
= *argp
++) || *cp
== '-')
164 die("missing argument to %s", argp
[-2]);
166 die("too many parts (starting with %s), %d max",
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
175 die("too many types (starting with %s), %d max",
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 if (npreferred
>= NPREFS
)
184 die("too many preferred types (starting with %s), %d max",
186 mime_preference
[npreferred
].type
= cp
;
187 cp
= strchr(cp
, '/');
188 if (cp
) *cp
++ = '\0';
189 mime_preference
[npreferred
++].subtype
= cp
;
197 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
198 die("missing argument to %s", argp
[-2]);
199 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
226 if (*cp
== '+' || *cp
== '@') {
228 die("only one folder at a time!");
229 folder
= pluspath (cp
);
231 app_msgarg(&msgs
, cp
);
234 /* null terminate the list of acceptable parts/types */
238 /* Check for public cache location */
239 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
242 /* Check for private cache location */
243 if (!(cache_private
= context_find (nmhprivcache
)))
244 cache_private
= ".cache";
245 cache_private
= mh_xstrdup(m_maildir(cache_private
));
247 if (!context_find ("path"))
248 free (path ("./", TFOLDER
));
250 if (file
&& msgs
.size
)
251 die("cannot specify msg and file at same time!");
254 * check if message is coming from file
257 cts
= mh_xcalloc(2, sizeof *cts
);
260 if ((ct
= parse_mime (file
)))
264 * message(s) are coming from a folder
267 app_msgarg(&msgs
, "cur");
269 folder
= getfolder (1);
270 maildir
= m_maildir (folder
);
272 if (chdir (maildir
) == NOTOK
)
273 adios (maildir
, "unable to change directory to");
275 /* read folder and create message structure */
276 if (!(mp
= folder_read (folder
, 0)))
277 die("unable to read folder %s", folder
);
279 /* check for empty folder */
281 die("no messages in %s", folder
);
283 /* parse all the message ranges/sequences and set SELECTED */
284 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
285 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
287 seq_setprev (mp
); /* set the previous-sequence */
289 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
292 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
293 if (is_selected(mp
, msgnum
)) {
296 msgnam
= m_name (msgnum
);
297 if ((ct
= parse_mime (msgnam
)))
307 SIGNAL (SIGQUIT
, quitser
);
308 SIGNAL (SIGPIPE
, pipeser
);
311 * Get the associated umask for the relevant contents.
313 for (ctp
= cts
; *ctp
; ctp
++) {
317 if (type_ok (ct
, 1) && !ct
->c_umask
) {
318 if (stat (ct
->c_file
, &st
) != NOTOK
)
319 ct
->c_umask
= ~(st
.st_mode
& 0777);
321 ct
->c_umask
= ~m_gmprot();
326 * List the message content
328 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
330 /* Now free all the structures for the content */
331 for (ctp
= cts
; *ctp
; ctp
++)
337 /* If reading from a folder, do some updating */
339 context_replace (pfolder
, folder
);/* update current folder */
341 seq_setcur (mp
, mp
->hghsel
); /* update current message */
342 seq_save (mp
); /* synchronize sequences */
343 context_save (); /* save the context file */
356 fprintf (stderr
, "\n");