]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
5d08908270313122f7539004a46327ccd2df7562
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/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
15 #include "h/signals.h"
20 #include "h/mhparse.h"
21 #include "h/mhcachesbr.h"
25 #include "sbr/m_maildir.h"
28 #define MHLIST_SWITCHES \
29 X("check", 0, CHECKSW) \
30 X("nocheck", 0, NCHECKSW) \
31 X("headers", 0, HEADSW) \
32 X("noheaders", 0, NHEADSW) \
33 X("realsize", 0, SIZESW) \
34 X("norealsize", 0, NSIZESW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("disposition", 0, DISPOSW) \
38 X("nodisposition", 0, NDISPOSW) \
39 X("file file", 0, FILESW) \
40 X("part number", 0, PARTSW) \
41 X("type content", 0, TYPESW) \
42 X("prefer content", 0, PREFERSW) \
43 X("noprefer", 0, NPREFERSW) \
44 X("rcache policy", 0, RCACHESW) \
45 X("wcache policy", 0, WCACHESW) \
46 X("changecur", 0, CHGSW) \
47 X("nochangecur", 0, NCHGSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50 X("debug", -5, DEBUGSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(MHLIST
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
61 * This is currently needed to keep mhparse happy.
62 * This needs to be changed.
66 #define quitser pipeser
71 static void pipeser (int);
75 main (int argc
, char **argv
)
82 int msgnum
, *icachesw
;
83 char *cp
, *file
= NULL
, *folder
= NULL
;
84 char *maildir
, buf
[100], **argp
;
86 struct msgs_array msgs
= { 0, 0, NULL
};
87 struct msgs
*mp
= NULL
;
90 if (nmh_init(argv
[0], true, true)) { return 1; }
92 set_done(freects_done
);
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 die("-%s unknown", cp
);
110 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
112 print_help (buf
, switches
, 1);
115 print_version(invo_name
);
119 icachesw
= &rcachesw
;
122 icachesw
= &wcachesw
;
124 if (!(cp
= *argp
++) || *cp
== '-')
125 die("missing argument to %s", argp
[-2]);
126 switch (*icachesw
= smatch (cp
, cache_policy
)) {
128 ambigsw (cp
, cache_policy
);
131 die("%s unknown", cp
);
159 if (!(cp
= *argp
++) || *cp
== '-')
160 die("missing argument to %s", argp
[-2]);
162 die("too many parts (starting with %s), %d max",
168 if (!(cp
= *argp
++) || *cp
== '-')
169 die("missing argument to %s", argp
[-2]);
171 die("too many types (starting with %s), %d max",
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
179 if (npreferred
>= NPREFS
)
180 die("too many preferred types (starting with %s), %d max",
182 mime_preference
[npreferred
].type
= cp
;
183 cp
= strchr(cp
, '/');
184 if (cp
) *cp
++ = '\0';
185 mime_preference
[npreferred
++].subtype
= cp
;
193 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
194 die("missing argument to %s", argp
[-2]);
195 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
222 if (*cp
== '+' || *cp
== '@') {
224 die("only one folder at a time!");
225 folder
= pluspath (cp
);
227 app_msgarg(&msgs
, cp
);
230 /* null terminate the list of acceptable parts/types */
234 /* Check for public cache location */
235 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
238 /* Check for private cache location */
239 if (!(cache_private
= context_find (nmhprivcache
)))
240 cache_private
= ".cache";
241 cache_private
= mh_xstrdup(m_maildir(cache_private
));
243 if (!context_find ("path"))
244 free (path ("./", TFOLDER
));
246 if (file
&& msgs
.size
)
247 die("cannot specify msg and file at same time!");
250 * check if message is coming from file
253 cts
= mh_xcalloc(2, sizeof *cts
);
256 if ((ct
= parse_mime (file
)))
260 * message(s) are coming from a folder
263 app_msgarg(&msgs
, "cur");
265 folder
= getfolder (1);
266 maildir
= m_maildir (folder
);
268 if (chdir (maildir
) == NOTOK
)
269 adios (maildir
, "unable to change directory to");
271 /* read folder and create message structure */
272 if (!(mp
= folder_read (folder
, 0)))
273 die("unable to read folder %s", folder
);
275 /* check for empty folder */
277 die("no messages in %s", folder
);
279 /* parse all the message ranges/sequences and set SELECTED */
280 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
281 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
283 seq_setprev (mp
); /* set the previous-sequence */
285 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
288 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
289 if (is_selected(mp
, msgnum
)) {
292 msgnam
= m_name (msgnum
);
293 if ((ct
= parse_mime (msgnam
)))
303 SIGNAL (SIGQUIT
, quitser
);
304 SIGNAL (SIGPIPE
, pipeser
);
307 * Get the associated umask for the relevant contents.
309 for (ctp
= cts
; *ctp
; ctp
++) {
313 if (type_ok (ct
, 1) && !ct
->c_umask
) {
314 if (stat (ct
->c_file
, &st
) != NOTOK
)
315 ct
->c_umask
= ~(st
.st_mode
& 0777);
317 ct
->c_umask
= ~m_gmprot();
322 * List the message content
324 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
326 /* Now free all the structures for the content */
327 for (ctp
= cts
; *ctp
; ctp
++)
333 /* If reading from a folder, do some updating */
335 context_replace (pfolder
, folder
);/* update current folder */
337 seq_setcur (mp
, mp
->hghsel
); /* update current message */
338 seq_save (mp
); /* synchronize sequences */
339 context_save (); /* save the context file */
352 fprintf (stderr
, "\n");