]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
f4aa532c8a35d9979c83afbc352305c820c1555b
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/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
25 #include "h/signals.h"
30 #include "h/mhparse.h"
31 #include "h/mhcachesbr.h"
35 #include "sbr/m_maildir.h"
38 #define MHLIST_SWITCHES \
39 X("check", 0, CHECKSW) \
40 X("nocheck", 0, NCHECKSW) \
41 X("headers", 0, HEADSW) \
42 X("noheaders", 0, NHEADSW) \
43 X("realsize", 0, SIZESW) \
44 X("norealsize", 0, NSIZESW) \
45 X("verbose", 0, VERBSW) \
46 X("noverbose", 0, NVERBSW) \
47 X("disposition", 0, DISPOSW) \
48 X("nodisposition", 0, NDISPOSW) \
49 X("file file", 0, FILESW) \
50 X("part number", 0, PARTSW) \
51 X("type content", 0, TYPESW) \
52 X("prefer content", 0, PREFERSW) \
53 X("noprefer", 0, NPREFERSW) \
54 X("rcache policy", 0, RCACHESW) \
55 X("wcache policy", 0, WCACHESW) \
56 X("changecur", 0, CHGSW) \
57 X("nochangecur", 0, NCHGSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("debug", -5, DEBUGSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHLIST
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
71 * This is currently needed to keep mhparse happy.
72 * This needs to be changed.
76 #define quitser pipeser
81 static void pipeser (int);
85 main (int argc
, char **argv
)
92 int msgnum
, *icachesw
;
93 char *cp
, *file
= NULL
, *folder
= NULL
;
94 char *maildir
, buf
[100], **argp
;
96 struct msgs_array msgs
= { 0, 0, NULL
};
97 struct msgs
*mp
= NULL
;
100 if (nmh_init(argv
[0], true, true)) { return 1; }
102 set_done(freects_done
);
104 arguments
= getarguments (invo_name
, argc
, argv
, 1);
110 while ((cp
= *argp
++)) {
112 switch (smatch (++cp
, switches
)) {
114 ambigsw (cp
, switches
);
117 die("-%s unknown", cp
);
120 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
122 print_help (buf
, switches
, 1);
125 print_version(invo_name
);
129 icachesw
= &rcachesw
;
132 icachesw
= &wcachesw
;
134 if (!(cp
= *argp
++) || *cp
== '-')
135 die("missing argument to %s", argp
[-2]);
136 switch (*icachesw
= smatch (cp
, cache_policy
)) {
138 ambigsw (cp
, cache_policy
);
141 die("%s unknown", cp
);
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
172 die("too many parts (starting with %s), %d max",
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
181 die("too many types (starting with %s), %d max",
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 if (npreferred
>= NPREFS
)
190 die("too many preferred types (starting with %s), %d max",
192 mime_preference
[npreferred
].type
= cp
;
193 cp
= strchr(cp
, '/');
194 if (cp
) *cp
++ = '\0';
195 mime_preference
[npreferred
++].subtype
= cp
;
203 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
204 die("missing argument to %s", argp
[-2]);
205 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
232 if (*cp
== '+' || *cp
== '@') {
234 die("only one folder at a time!");
235 folder
= pluspath (cp
);
237 app_msgarg(&msgs
, cp
);
240 /* null terminate the list of acceptable parts/types */
244 /* Check for public cache location */
245 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
248 /* Check for private cache location */
249 if (!(cache_private
= context_find (nmhprivcache
)))
250 cache_private
= ".cache";
251 cache_private
= mh_xstrdup(m_maildir(cache_private
));
253 if (!context_find ("path"))
254 free (path ("./", TFOLDER
));
256 if (file
&& msgs
.size
)
257 die("cannot specify msg and file at same time!");
260 * check if message is coming from file
263 cts
= mh_xcalloc(2, sizeof *cts
);
266 if ((ct
= parse_mime (file
)))
270 * message(s) are coming from a folder
273 app_msgarg(&msgs
, "cur");
275 folder
= getfolder (1);
276 maildir
= m_maildir (folder
);
278 if (chdir (maildir
) == NOTOK
)
279 adios (maildir
, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp
= folder_read (folder
, 0)))
283 die("unable to read folder %s", folder
);
285 /* check for empty folder */
287 die("no messages in %s", folder
);
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
291 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
293 seq_setprev (mp
); /* set the previous-sequence */
295 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
298 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
299 if (is_selected(mp
, msgnum
)) {
302 msgnam
= m_name (msgnum
);
303 if ((ct
= parse_mime (msgnam
)))
313 SIGNAL (SIGQUIT
, quitser
);
314 SIGNAL (SIGPIPE
, pipeser
);
317 * Get the associated umask for the relevant contents.
319 for (ctp
= cts
; *ctp
; ctp
++) {
323 if (type_ok (ct
, 1) && !ct
->c_umask
) {
324 if (stat (ct
->c_file
, &st
) != NOTOK
)
325 ct
->c_umask
= ~(st
.st_mode
& 0777);
327 ct
->c_umask
= ~m_gmprot();
332 * List the message content
334 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
336 /* Now free all the structures for the content */
337 for (ctp
= cts
; *ctp
; ctp
++)
343 /* If reading from a folder, do some updating */
345 context_replace (pfolder
, folder
);/* update current folder */
347 seq_setcur (mp
, mp
->hghsel
); /* update current message */
348 seq_save (mp
); /* synchronize sequences */
349 context_save (); /* save the context file */
362 fprintf (stderr
, "\n");