]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
b9636393eb94ac88dc1985f6195bd8cbc1484097
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_replace.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
17 #include "h/signals.h"
22 #include "h/mhparse.h"
23 #include "h/mhcachesbr.h"
27 #include "sbr/m_maildir.h"
30 #define MHLIST_SWITCHES \
31 X("check", 0, CHECKSW) \
32 X("nocheck", 0, NCHECKSW) \
33 X("headers", 0, HEADSW) \
34 X("noheaders", 0, NHEADSW) \
35 X("realsize", 0, SIZESW) \
36 X("norealsize", 0, NSIZESW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("disposition", 0, DISPOSW) \
40 X("nodisposition", 0, NDISPOSW) \
41 X("file file", 0, FILESW) \
42 X("part number", 0, PARTSW) \
43 X("type content", 0, TYPESW) \
44 X("prefer content", 0, PREFERSW) \
45 X("noprefer", 0, NPREFERSW) \
46 X("rcache policy", 0, RCACHESW) \
47 X("wcache policy", 0, WCACHESW) \
48 X("changecur", 0, CHGSW) \
49 X("nochangecur", 0, NCHGSW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
52 X("debug", -5, DEBUGSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(MHLIST
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
63 * This is currently needed to keep mhparse happy.
64 * This needs to be changed.
68 #define quitser pipeser
73 static void pipeser (int);
77 main (int argc
, char **argv
)
84 int msgnum
, *icachesw
;
85 char *cp
, *file
= NULL
, *folder
= NULL
;
86 char *maildir
, buf
[100], **argp
;
88 struct msgs_array msgs
= { 0, 0, NULL
};
89 struct msgs
*mp
= NULL
;
92 if (nmh_init(argv
[0], true, true)) { return 1; }
94 set_done(freects_done
);
96 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
104 switch (smatch (++cp
, switches
)) {
106 ambigsw (cp
, switches
);
109 die("-%s unknown", cp
);
112 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
114 print_help (buf
, switches
, 1);
117 print_version(invo_name
);
121 icachesw
= &rcachesw
;
124 icachesw
= &wcachesw
;
126 if (!(cp
= *argp
++) || *cp
== '-')
127 die("missing argument to %s", argp
[-2]);
128 switch (*icachesw
= smatch (cp
, cache_policy
)) {
130 ambigsw (cp
, cache_policy
);
133 die("%s unknown", cp
);
161 if (!(cp
= *argp
++) || *cp
== '-')
162 die("missing argument to %s", argp
[-2]);
164 die("too many parts (starting with %s), %d max",
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
173 die("too many types (starting with %s), %d max",
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
181 if (npreferred
>= NPREFS
)
182 die("too many preferred types (starting with %s), %d max",
184 mime_preference
[npreferred
].type
= cp
;
185 cp
= strchr(cp
, '/');
186 if (cp
) *cp
++ = '\0';
187 mime_preference
[npreferred
++].subtype
= cp
;
195 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
196 die("missing argument to %s", argp
[-2]);
197 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
224 if (*cp
== '+' || *cp
== '@') {
226 die("only one folder at a time!");
227 folder
= pluspath (cp
);
229 app_msgarg(&msgs
, cp
);
232 /* null terminate the list of acceptable parts/types */
236 /* Check for public cache location */
237 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
240 /* Check for private cache location */
241 if (!(cache_private
= context_find (nmhprivcache
)))
242 cache_private
= ".cache";
243 cache_private
= mh_xstrdup(m_maildir(cache_private
));
245 if (!context_find ("path"))
246 free (path ("./", TFOLDER
));
248 if (file
&& msgs
.size
)
249 die("cannot specify msg and file at same time!");
252 * check if message is coming from file
255 cts
= mh_xcalloc(2, sizeof *cts
);
258 if ((ct
= parse_mime (file
)))
262 * message(s) are coming from a folder
265 app_msgarg(&msgs
, "cur");
267 folder
= getfolder (1);
268 maildir
= m_maildir (folder
);
270 if (chdir (maildir
) == NOTOK
)
271 adios (maildir
, "unable to change directory to");
273 /* read folder and create message structure */
274 if (!(mp
= folder_read (folder
, 0)))
275 die("unable to read folder %s", folder
);
277 /* check for empty folder */
279 die("no messages in %s", folder
);
281 /* parse all the message ranges/sequences and set SELECTED */
282 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
283 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
285 seq_setprev (mp
); /* set the previous-sequence */
287 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
290 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
291 if (is_selected(mp
, msgnum
)) {
294 msgnam
= m_name (msgnum
);
295 if ((ct
= parse_mime (msgnam
)))
305 SIGNAL (SIGQUIT
, quitser
);
306 SIGNAL (SIGPIPE
, pipeser
);
309 * Get the associated umask for the relevant contents.
311 for (ctp
= cts
; *ctp
; ctp
++) {
315 if (type_ok (ct
, 1) && !ct
->c_umask
) {
316 if (stat (ct
->c_file
, &st
) != NOTOK
)
317 ct
->c_umask
= ~(st
.st_mode
& 0777);
319 ct
->c_umask
= ~m_gmprot();
324 * List the message content
326 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
328 /* Now free all the structures for the content */
329 for (ctp
= cts
; *ctp
; ctp
++)
335 /* If reading from a folder, do some updating */
337 context_replace (pfolder
, folder
);/* update current folder */
339 seq_setcur (mp
, mp
->hghsel
); /* update current message */
340 seq_save (mp
); /* synchronize sequences */
341 context_save (); /* save the context file */
354 fprintf (stderr
, "\n");