]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
aa9c3aeef2a81fb83afde506cce07c83329538eb
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_find.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
16 #include "h/signals.h"
21 #include "h/mhparse.h"
22 #include "h/mhcachesbr.h"
26 #include "sbr/m_maildir.h"
29 #define MHLIST_SWITCHES \
30 X("check", 0, CHECKSW) \
31 X("nocheck", 0, NCHECKSW) \
32 X("headers", 0, HEADSW) \
33 X("noheaders", 0, NHEADSW) \
34 X("realsize", 0, SIZESW) \
35 X("norealsize", 0, NSIZESW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("disposition", 0, DISPOSW) \
39 X("nodisposition", 0, NDISPOSW) \
40 X("file file", 0, FILESW) \
41 X("part number", 0, PARTSW) \
42 X("type content", 0, TYPESW) \
43 X("prefer content", 0, PREFERSW) \
44 X("noprefer", 0, NPREFERSW) \
45 X("rcache policy", 0, RCACHESW) \
46 X("wcache policy", 0, WCACHESW) \
47 X("changecur", 0, CHGSW) \
48 X("nochangecur", 0, NCHGSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
51 X("debug", -5, DEBUGSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(MHLIST
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
62 * This is currently needed to keep mhparse happy.
63 * This needs to be changed.
67 #define quitser pipeser
72 static void pipeser (int);
76 main (int argc
, char **argv
)
83 int msgnum
, *icachesw
;
84 char *cp
, *file
= NULL
, *folder
= NULL
;
85 char *maildir
, buf
[100], **argp
;
87 struct msgs_array msgs
= { 0, 0, NULL
};
88 struct msgs
*mp
= NULL
;
91 if (nmh_init(argv
[0], true, true)) { return 1; }
93 set_done(freects_done
);
95 arguments
= getarguments (invo_name
, argc
, argv
, 1);
101 while ((cp
= *argp
++)) {
103 switch (smatch (++cp
, switches
)) {
105 ambigsw (cp
, switches
);
108 die("-%s unknown", cp
);
111 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
113 print_help (buf
, switches
, 1);
116 print_version(invo_name
);
120 icachesw
= &rcachesw
;
123 icachesw
= &wcachesw
;
125 if (!(cp
= *argp
++) || *cp
== '-')
126 die("missing argument to %s", argp
[-2]);
127 switch (*icachesw
= smatch (cp
, cache_policy
)) {
129 ambigsw (cp
, cache_policy
);
132 die("%s unknown", cp
);
160 if (!(cp
= *argp
++) || *cp
== '-')
161 die("missing argument to %s", argp
[-2]);
163 die("too many parts (starting with %s), %d max",
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
172 die("too many types (starting with %s), %d max",
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
180 if (npreferred
>= NPREFS
)
181 die("too many preferred types (starting with %s), %d max",
183 mime_preference
[npreferred
].type
= cp
;
184 cp
= strchr(cp
, '/');
185 if (cp
) *cp
++ = '\0';
186 mime_preference
[npreferred
++].subtype
= cp
;
194 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
195 die("missing argument to %s", argp
[-2]);
196 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
223 if (*cp
== '+' || *cp
== '@') {
225 die("only one folder at a time!");
226 folder
= pluspath (cp
);
228 app_msgarg(&msgs
, cp
);
231 /* null terminate the list of acceptable parts/types */
235 /* Check for public cache location */
236 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
239 /* Check for private cache location */
240 if (!(cache_private
= context_find (nmhprivcache
)))
241 cache_private
= ".cache";
242 cache_private
= mh_xstrdup(m_maildir(cache_private
));
244 if (!context_find ("path"))
245 free (path ("./", TFOLDER
));
247 if (file
&& msgs
.size
)
248 die("cannot specify msg and file at same time!");
251 * check if message is coming from file
254 cts
= mh_xcalloc(2, sizeof *cts
);
257 if ((ct
= parse_mime (file
)))
261 * message(s) are coming from a folder
264 app_msgarg(&msgs
, "cur");
266 folder
= getfolder (1);
267 maildir
= m_maildir (folder
);
269 if (chdir (maildir
) == NOTOK
)
270 adios (maildir
, "unable to change directory to");
272 /* read folder and create message structure */
273 if (!(mp
= folder_read (folder
, 0)))
274 die("unable to read folder %s", folder
);
276 /* check for empty folder */
278 die("no messages in %s", folder
);
280 /* parse all the message ranges/sequences and set SELECTED */
281 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
282 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
284 seq_setprev (mp
); /* set the previous-sequence */
286 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
289 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
290 if (is_selected(mp
, msgnum
)) {
293 msgnam
= m_name (msgnum
);
294 if ((ct
= parse_mime (msgnam
)))
304 SIGNAL (SIGQUIT
, quitser
);
305 SIGNAL (SIGPIPE
, pipeser
);
308 * Get the associated umask for the relevant contents.
310 for (ctp
= cts
; *ctp
; ctp
++) {
314 if (type_ok (ct
, 1) && !ct
->c_umask
) {
315 if (stat (ct
->c_file
, &st
) != NOTOK
)
316 ct
->c_umask
= ~(st
.st_mode
& 0777);
318 ct
->c_umask
= ~m_gmprot();
323 * List the message content
325 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
327 /* Now free all the structures for the content */
328 for (ctp
= cts
; *ctp
; ctp
++)
334 /* If reading from a folder, do some updating */
336 context_replace (pfolder
, folder
);/* update current folder */
338 seq_setcur (mp
, mp
->hghsel
); /* update current message */
339 seq_save (mp
); /* synchronize sequences */
340 context_save (); /* save the context file */
353 fprintf (stderr
, "\n");