]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
fb0554d470ed81d110b8759823dcf7cf750d5900
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/context_save.h"
10 #include "sbr/context_replace.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
18 #include "h/signals.h"
23 #include "h/mhparse.h"
24 #include "h/mhcachesbr.h"
28 #include "sbr/m_maildir.h"
31 #define MHLIST_SWITCHES \
32 X("check", 0, CHECKSW) \
33 X("nocheck", 0, NCHECKSW) \
34 X("headers", 0, HEADSW) \
35 X("noheaders", 0, NHEADSW) \
36 X("realsize", 0, SIZESW) \
37 X("norealsize", 0, NSIZESW) \
38 X("verbose", 0, VERBSW) \
39 X("noverbose", 0, NVERBSW) \
40 X("disposition", 0, DISPOSW) \
41 X("nodisposition", 0, NDISPOSW) \
42 X("file file", 0, FILESW) \
43 X("part number", 0, PARTSW) \
44 X("type content", 0, TYPESW) \
45 X("prefer content", 0, PREFERSW) \
46 X("noprefer", 0, NPREFERSW) \
47 X("rcache policy", 0, RCACHESW) \
48 X("wcache policy", 0, WCACHESW) \
49 X("changecur", 0, CHGSW) \
50 X("nochangecur", 0, NCHGSW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
53 X("debug", -5, DEBUGSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(MHLIST
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
64 * This is currently needed to keep mhparse happy.
65 * This needs to be changed.
69 #define quitser pipeser
74 static void pipeser (int);
78 main (int argc
, char **argv
)
85 int msgnum
, *icachesw
;
86 char *cp
, *file
= NULL
, *folder
= NULL
;
87 char *maildir
, buf
[100], **argp
;
89 struct msgs_array msgs
= { 0, 0, NULL
};
90 struct msgs
*mp
= NULL
;
93 if (nmh_init(argv
[0], true, true)) { return 1; }
95 set_done(freects_done
);
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
103 while ((cp
= *argp
++)) {
105 switch (smatch (++cp
, switches
)) {
107 ambigsw (cp
, switches
);
110 die("-%s unknown", cp
);
113 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
115 print_help (buf
, switches
, 1);
118 print_version(invo_name
);
122 icachesw
= &rcachesw
;
125 icachesw
= &wcachesw
;
127 if (!(cp
= *argp
++) || *cp
== '-')
128 die("missing argument to %s", argp
[-2]);
129 switch (*icachesw
= smatch (cp
, cache_policy
)) {
131 ambigsw (cp
, cache_policy
);
134 die("%s unknown", cp
);
162 if (!(cp
= *argp
++) || *cp
== '-')
163 die("missing argument to %s", argp
[-2]);
165 die("too many parts (starting with %s), %d max",
171 if (!(cp
= *argp
++) || *cp
== '-')
172 die("missing argument to %s", argp
[-2]);
174 die("too many types (starting with %s), %d max",
180 if (!(cp
= *argp
++) || *cp
== '-')
181 die("missing argument to %s", argp
[-2]);
182 if (npreferred
>= NPREFS
)
183 die("too many preferred types (starting with %s), %d max",
185 mime_preference
[npreferred
].type
= cp
;
186 cp
= strchr(cp
, '/');
187 if (cp
) *cp
++ = '\0';
188 mime_preference
[npreferred
++].subtype
= cp
;
196 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
197 die("missing argument to %s", argp
[-2]);
198 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
225 if (*cp
== '+' || *cp
== '@') {
227 die("only one folder at a time!");
228 folder
= pluspath (cp
);
230 app_msgarg(&msgs
, cp
);
233 /* null terminate the list of acceptable parts/types */
237 /* Check for public cache location */
238 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
241 /* Check for private cache location */
242 if (!(cache_private
= context_find (nmhprivcache
)))
243 cache_private
= ".cache";
244 cache_private
= mh_xstrdup(m_maildir(cache_private
));
246 if (!context_find ("path"))
247 free (path ("./", TFOLDER
));
249 if (file
&& msgs
.size
)
250 die("cannot specify msg and file at same time!");
253 * check if message is coming from file
256 cts
= mh_xcalloc(2, sizeof *cts
);
259 if ((ct
= parse_mime (file
)))
263 * message(s) are coming from a folder
266 app_msgarg(&msgs
, "cur");
268 folder
= getfolder (1);
269 maildir
= m_maildir (folder
);
271 if (chdir (maildir
) == NOTOK
)
272 adios (maildir
, "unable to change directory to");
274 /* read folder and create message structure */
275 if (!(mp
= folder_read (folder
, 0)))
276 die("unable to read folder %s", folder
);
278 /* check for empty folder */
280 die("no messages in %s", folder
);
282 /* parse all the message ranges/sequences and set SELECTED */
283 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
284 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
286 seq_setprev (mp
); /* set the previous-sequence */
288 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
291 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
292 if (is_selected(mp
, msgnum
)) {
295 msgnam
= m_name (msgnum
);
296 if ((ct
= parse_mime (msgnam
)))
306 SIGNAL (SIGQUIT
, quitser
);
307 SIGNAL (SIGPIPE
, pipeser
);
310 * Get the associated umask for the relevant contents.
312 for (ctp
= cts
; *ctp
; ctp
++) {
316 if (type_ok (ct
, 1) && !ct
->c_umask
) {
317 if (stat (ct
->c_file
, &st
) != NOTOK
)
318 ct
->c_umask
= ~(st
.st_mode
& 0777);
320 ct
->c_umask
= ~m_gmprot();
325 * List the message content
327 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
329 /* Now free all the structures for the content */
330 for (ctp
= cts
; *ctp
; ctp
++)
336 /* If reading from a folder, do some updating */
338 context_replace (pfolder
, folder
);/* update current folder */
340 seq_setcur (mp
, mp
->hghsel
); /* update current message */
341 seq_save (mp
); /* synchronize sequences */
342 context_save (); /* save the context file */
355 fprintf (stderr
, "\n");