]>
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 "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "h/signals.h"
19 #include "h/mhparse.h"
20 #include "h/mhcachesbr.h"
24 #include "sbr/m_maildir.h"
27 #define MHLIST_SWITCHES \
28 X("check", 0, CHECKSW) \
29 X("nocheck", 0, NCHECKSW) \
30 X("headers", 0, HEADSW) \
31 X("noheaders", 0, NHEADSW) \
32 X("realsize", 0, SIZESW) \
33 X("norealsize", 0, NSIZESW) \
34 X("verbose", 0, VERBSW) \
35 X("noverbose", 0, NVERBSW) \
36 X("disposition", 0, DISPOSW) \
37 X("nodisposition", 0, NDISPOSW) \
38 X("file file", 0, FILESW) \
39 X("part number", 0, PARTSW) \
40 X("type content", 0, TYPESW) \
41 X("prefer content", 0, PREFERSW) \
42 X("noprefer", 0, NPREFERSW) \
43 X("rcache policy", 0, RCACHESW) \
44 X("wcache policy", 0, WCACHESW) \
45 X("changecur", 0, CHGSW) \
46 X("nochangecur", 0, NCHGSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
49 X("debug", -5, DEBUGSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MHLIST
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
60 * This is currently needed to keep mhparse happy.
61 * This needs to be changed.
65 #define quitser pipeser
70 static void pipeser (int);
74 main (int argc
, char **argv
)
81 int msgnum
, *icachesw
;
82 char *cp
, *file
= NULL
, *folder
= NULL
;
83 char *maildir
, buf
[100], **argp
;
85 struct msgs_array msgs
= { 0, 0, NULL
};
86 struct msgs
*mp
= NULL
;
89 if (nmh_init(argv
[0], true, true)) { return 1; }
91 set_done(freects_done
);
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
99 while ((cp
= *argp
++)) {
101 switch (smatch (++cp
, switches
)) {
103 ambigsw (cp
, switches
);
106 die("-%s unknown", cp
);
109 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
118 icachesw
= &rcachesw
;
121 icachesw
= &wcachesw
;
123 if (!(cp
= *argp
++) || *cp
== '-')
124 die("missing argument to %s", argp
[-2]);
125 switch (*icachesw
= smatch (cp
, cache_policy
)) {
127 ambigsw (cp
, cache_policy
);
130 die("%s unknown", cp
);
158 if (!(cp
= *argp
++) || *cp
== '-')
159 die("missing argument to %s", argp
[-2]);
161 die("too many parts (starting with %s), %d max",
167 if (!(cp
= *argp
++) || *cp
== '-')
168 die("missing argument to %s", argp
[-2]);
170 die("too many types (starting with %s), %d max",
176 if (!(cp
= *argp
++) || *cp
== '-')
177 die("missing argument to %s", argp
[-2]);
178 if (npreferred
>= NPREFS
)
179 die("too many preferred types (starting with %s), %d max",
181 mime_preference
[npreferred
].type
= cp
;
182 cp
= strchr(cp
, '/');
183 if (cp
) *cp
++ = '\0';
184 mime_preference
[npreferred
++].subtype
= cp
;
192 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
193 die("missing argument to %s", argp
[-2]);
194 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
221 if (*cp
== '+' || *cp
== '@') {
223 die("only one folder at a time!");
224 folder
= pluspath (cp
);
226 app_msgarg(&msgs
, cp
);
229 /* null terminate the list of acceptable parts/types */
233 /* Check for public cache location */
234 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
237 /* Check for private cache location */
238 if (!(cache_private
= context_find (nmhprivcache
)))
239 cache_private
= ".cache";
240 cache_private
= mh_xstrdup(m_maildir(cache_private
));
242 if (!context_find ("path"))
243 free (path ("./", TFOLDER
));
245 if (file
&& msgs
.size
)
246 die("cannot specify msg and file at same time!");
249 * check if message is coming from file
252 cts
= mh_xcalloc(2, sizeof *cts
);
255 if ((ct
= parse_mime (file
)))
259 * message(s) are coming from a folder
262 app_msgarg(&msgs
, "cur");
264 folder
= getfolder (1);
265 maildir
= m_maildir (folder
);
267 if (chdir (maildir
) == NOTOK
)
268 adios (maildir
, "unable to change directory to");
270 /* read folder and create message structure */
271 if (!(mp
= folder_read (folder
, 0)))
272 die("unable to read folder %s", folder
);
274 /* check for empty folder */
276 die("no messages in %s", folder
);
278 /* parse all the message ranges/sequences and set SELECTED */
279 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
280 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
282 seq_setprev (mp
); /* set the previous-sequence */
284 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
287 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
288 if (is_selected(mp
, msgnum
)) {
291 msgnam
= m_name (msgnum
);
292 if ((ct
= parse_mime (msgnam
)))
302 SIGNAL (SIGQUIT
, quitser
);
303 SIGNAL (SIGPIPE
, pipeser
);
306 * Get the associated umask for the relevant contents.
308 for (ctp
= cts
; *ctp
; ctp
++) {
312 if (type_ok (ct
, 1) && !ct
->c_umask
) {
313 if (stat (ct
->c_file
, &st
) != NOTOK
)
314 ct
->c_umask
= ~(st
.st_mode
& 0777);
316 ct
->c_umask
= ~m_gmprot();
321 * List the message content
323 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
325 /* Now free all the structures for the content */
326 for (ctp
= cts
; *ctp
; ctp
++)
332 /* If reading from a folder, do some updating */
334 context_replace (pfolder
, folder
);/* update current folder */
336 seq_setcur (mp
, mp
->hghsel
); /* update current message */
337 seq_save (mp
); /* synchronize sequences */
338 context_save (); /* save the context file */
351 fprintf (stderr
, "\n");