]>
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.
10 #include <h/signals.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
20 #include "sbr/m_maildir.h"
23 #define MHLIST_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("headers", 0, HEADSW) \
27 X("noheaders", 0, NHEADSW) \
28 X("realsize", 0, SIZESW) \
29 X("norealsize", 0, NSIZESW) \
30 X("verbose", 0, VERBSW) \
31 X("noverbose", 0, NVERBSW) \
32 X("disposition", 0, DISPOSW) \
33 X("nodisposition", 0, NDISPOSW) \
34 X("file file", 0, FILESW) \
35 X("part number", 0, PARTSW) \
36 X("type content", 0, TYPESW) \
37 X("prefer content", 0, PREFERSW) \
38 X("noprefer", 0, NPREFERSW) \
39 X("rcache policy", 0, RCACHESW) \
40 X("wcache policy", 0, WCACHESW) \
41 X("changecur", 0, CHGSW) \
42 X("nochangecur", 0, NCHGSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45 X("debug", -5, DEBUGSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(MHLIST
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
56 * This is currently needed to keep mhparse happy.
57 * This needs to be changed.
61 #define quitser pipeser
66 static void pipeser (int);
70 main (int argc
, char **argv
)
77 int msgnum
, *icachesw
;
78 char *cp
, *file
= NULL
, *folder
= NULL
;
79 char *maildir
, buf
[100], **argp
;
81 struct msgs_array msgs
= { 0, 0, NULL
};
82 struct msgs
*mp
= NULL
;
85 if (nmh_init(argv
[0], true, true)) { return 1; }
87 set_done(freects_done
);
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
102 die("-%s unknown", cp
);
105 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
114 icachesw
= &rcachesw
;
117 icachesw
= &wcachesw
;
119 if (!(cp
= *argp
++) || *cp
== '-')
120 die("missing argument to %s", argp
[-2]);
121 switch (*icachesw
= smatch (cp
, cache_policy
)) {
123 ambigsw (cp
, cache_policy
);
126 die("%s unknown", cp
);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 die("missing argument to %s", argp
[-2]);
157 die("too many parts (starting with %s), %d max",
163 if (!(cp
= *argp
++) || *cp
== '-')
164 die("missing argument to %s", argp
[-2]);
166 die("too many types (starting with %s), %d max",
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
174 if (npreferred
>= NPREFS
)
175 die("too many preferred types (starting with %s), %d max",
177 mime_preference
[npreferred
].type
= cp
;
178 cp
= strchr(cp
, '/');
179 if (cp
) *cp
++ = '\0';
180 mime_preference
[npreferred
++].subtype
= cp
;
188 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
189 die("missing argument to %s", argp
[-2]);
190 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
217 if (*cp
== '+' || *cp
== '@') {
219 die("only one folder at a time!");
220 folder
= pluspath (cp
);
222 app_msgarg(&msgs
, cp
);
225 /* null terminate the list of acceptable parts/types */
229 /* Check for public cache location */
230 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
233 /* Check for private cache location */
234 if (!(cache_private
= context_find (nmhprivcache
)))
235 cache_private
= ".cache";
236 cache_private
= mh_xstrdup(m_maildir(cache_private
));
238 if (!context_find ("path"))
239 free (path ("./", TFOLDER
));
241 if (file
&& msgs
.size
)
242 die("cannot specify msg and file at same time!");
245 * check if message is coming from file
248 cts
= mh_xcalloc(2, sizeof *cts
);
251 if ((ct
= parse_mime (file
)))
255 * message(s) are coming from a folder
258 app_msgarg(&msgs
, "cur");
260 folder
= getfolder (1);
261 maildir
= m_maildir (folder
);
263 if (chdir (maildir
) == NOTOK
)
264 adios (maildir
, "unable to change directory to");
266 /* read folder and create message structure */
267 if (!(mp
= folder_read (folder
, 0)))
268 die("unable to read folder %s", folder
);
270 /* check for empty folder */
272 die("no messages in %s", folder
);
274 /* parse all the message ranges/sequences and set SELECTED */
275 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
276 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
278 seq_setprev (mp
); /* set the previous-sequence */
280 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
283 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
284 if (is_selected(mp
, msgnum
)) {
287 msgnam
= m_name (msgnum
);
288 if ((ct
= parse_mime (msgnam
)))
298 SIGNAL (SIGQUIT
, quitser
);
299 SIGNAL (SIGPIPE
, pipeser
);
302 * Get the associated umask for the relevant contents.
304 for (ctp
= cts
; *ctp
; ctp
++) {
308 if (type_ok (ct
, 1) && !ct
->c_umask
) {
309 if (stat (ct
->c_file
, &st
) != NOTOK
)
310 ct
->c_umask
= ~(st
.st_mode
& 0777);
312 ct
->c_umask
= ~m_gmprot();
317 * List the message content
319 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
321 /* Now free all the structures for the content */
322 for (ctp
= cts
; *ctp
; ctp
++)
328 /* If reading from a folder, do some updating */
330 context_replace (pfolder
, folder
);/* update current folder */
332 seq_setcur (mp
, mp
->hghsel
); /* update current message */
333 seq_save (mp
); /* synchronize sequences */
334 context_save (); /* save the context file */
347 fprintf (stderr
, "\n");