]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
cd3114af273eaab0e861fd1be985b4c0c0091726
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.
11 #include <h/signals.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
21 #include "sbr/m_maildir.h"
24 #define MHLIST_SWITCHES \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("headers", 0, HEADSW) \
28 X("noheaders", 0, NHEADSW) \
29 X("realsize", 0, SIZESW) \
30 X("norealsize", 0, NSIZESW) \
31 X("verbose", 0, VERBSW) \
32 X("noverbose", 0, NVERBSW) \
33 X("disposition", 0, DISPOSW) \
34 X("nodisposition", 0, NDISPOSW) \
35 X("file file", 0, FILESW) \
36 X("part number", 0, PARTSW) \
37 X("type content", 0, TYPESW) \
38 X("prefer content", 0, PREFERSW) \
39 X("noprefer", 0, NPREFERSW) \
40 X("rcache policy", 0, RCACHESW) \
41 X("wcache policy", 0, WCACHESW) \
42 X("changecur", 0, CHGSW) \
43 X("nochangecur", 0, NCHGSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("debug", -5, DEBUGSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(MHLIST
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
57 * This is currently needed to keep mhparse happy.
58 * This needs to be changed.
62 #define quitser pipeser
67 static void pipeser (int);
71 main (int argc
, char **argv
)
78 int msgnum
, *icachesw
;
79 char *cp
, *file
= NULL
, *folder
= NULL
;
80 char *maildir
, buf
[100], **argp
;
82 struct msgs_array msgs
= { 0, 0, NULL
};
83 struct msgs
*mp
= NULL
;
86 if (nmh_init(argv
[0], true, true)) { return 1; }
88 set_done(freects_done
);
90 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 die("-%s unknown", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
115 icachesw
= &rcachesw
;
118 icachesw
= &wcachesw
;
120 if (!(cp
= *argp
++) || *cp
== '-')
121 die("missing argument to %s", argp
[-2]);
122 switch (*icachesw
= smatch (cp
, cache_policy
)) {
124 ambigsw (cp
, cache_policy
);
127 die("%s unknown", cp
);
155 if (!(cp
= *argp
++) || *cp
== '-')
156 die("missing argument to %s", argp
[-2]);
158 die("too many parts (starting with %s), %d max",
164 if (!(cp
= *argp
++) || *cp
== '-')
165 die("missing argument to %s", argp
[-2]);
167 die("too many types (starting with %s), %d max",
173 if (!(cp
= *argp
++) || *cp
== '-')
174 die("missing argument to %s", argp
[-2]);
175 if (npreferred
>= NPREFS
)
176 die("too many preferred types (starting with %s), %d max",
178 mime_preference
[npreferred
].type
= cp
;
179 cp
= strchr(cp
, '/');
180 if (cp
) *cp
++ = '\0';
181 mime_preference
[npreferred
++].subtype
= cp
;
189 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
190 die("missing argument to %s", argp
[-2]);
191 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
218 if (*cp
== '+' || *cp
== '@') {
220 die("only one folder at a time!");
221 folder
= pluspath (cp
);
223 app_msgarg(&msgs
, cp
);
226 /* null terminate the list of acceptable parts/types */
230 /* Check for public cache location */
231 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
234 /* Check for private cache location */
235 if (!(cache_private
= context_find (nmhprivcache
)))
236 cache_private
= ".cache";
237 cache_private
= mh_xstrdup(m_maildir(cache_private
));
239 if (!context_find ("path"))
240 free (path ("./", TFOLDER
));
242 if (file
&& msgs
.size
)
243 die("cannot specify msg and file at same time!");
246 * check if message is coming from file
249 cts
= mh_xcalloc(2, sizeof *cts
);
252 if ((ct
= parse_mime (file
)))
256 * message(s) are coming from a folder
259 app_msgarg(&msgs
, "cur");
261 folder
= getfolder (1);
262 maildir
= m_maildir (folder
);
264 if (chdir (maildir
) == NOTOK
)
265 adios (maildir
, "unable to change directory to");
267 /* read folder and create message structure */
268 if (!(mp
= folder_read (folder
, 0)))
269 die("unable to read folder %s", folder
);
271 /* check for empty folder */
273 die("no messages in %s", folder
);
275 /* parse all the message ranges/sequences and set SELECTED */
276 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
277 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
279 seq_setprev (mp
); /* set the previous-sequence */
281 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
284 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
285 if (is_selected(mp
, msgnum
)) {
288 msgnam
= m_name (msgnum
);
289 if ((ct
= parse_mime (msgnam
)))
299 SIGNAL (SIGQUIT
, quitser
);
300 SIGNAL (SIGPIPE
, pipeser
);
303 * Get the associated umask for the relevant contents.
305 for (ctp
= cts
; *ctp
; ctp
++) {
309 if (type_ok (ct
, 1) && !ct
->c_umask
) {
310 if (stat (ct
->c_file
, &st
) != NOTOK
)
311 ct
->c_umask
= ~(st
.st_mode
& 0777);
313 ct
->c_umask
= ~m_gmprot();
318 * List the message content
320 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
322 /* Now free all the structures for the content */
323 for (ctp
= cts
; *ctp
; ctp
++)
329 /* If reading from a folder, do some updating */
331 context_replace (pfolder
, folder
);/* update current folder */
333 seq_setcur (mp
, mp
->hghsel
); /* update current message */
334 seq_save (mp
); /* synchronize sequences */
335 context_save (); /* save the context file */
348 fprintf (stderr
, "\n");