]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
6faef9b696b601416e432d1d1422ca6b74b9506e
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/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
28 #include "h/signals.h"
32 #include "h/mhparse.h"
36 #include "sbr/m_maildir.h"
39 #define MHLIST_SWITCHES \
40 X("check", -5, CHECKSW) \
41 X("nocheck", -7, NCHECKSW) \
42 X("headers", 0, HEADSW) \
43 X("noheaders", 0, NHEADSW) \
44 X("realsize", 0, SIZESW) \
45 X("norealsize", 0, NSIZESW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("disposition", 0, DISPOSW) \
49 X("nodisposition", 0, NDISPOSW) \
50 X("file file", 0, FILESW) \
51 X("part number", 0, PARTSW) \
52 X("type content", 0, TYPESW) \
53 X("prefer content", 0, PREFERSW) \
54 X("noprefer", 0, NPREFERSW) \
55 X("changecur", 0, CHGSW) \
56 X("nochangecur", 0, NCHGSW) \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
59 X("debug", -5, DEBUGSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MHLIST
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
70 * This is currently needed to keep mhparse happy.
71 * This needs to be changed.
75 #define quitser pipeser
80 static void pipeser (int);
84 main (int argc
, char **argv
)
92 char *cp
, *file
= NULL
, *folder
= NULL
;
93 char *maildir
, buf
[100], **argp
;
95 struct msgs_array msgs
= { 0, 0, NULL
};
96 struct msgs
*mp
= NULL
;
99 if (nmh_init(argv
[0], true, true)) { return 1; }
101 set_done(freects_done
);
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
109 while ((cp
= *argp
++)) {
111 switch (smatch (++cp
, switches
)) {
113 ambigsw (cp
, switches
);
116 die("-%s unknown", cp
);
119 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
121 print_help (buf
, switches
, 1);
124 print_version(invo_name
);
129 /* Currently a NOP */
147 if (!(cp
= *argp
++) || *cp
== '-')
148 die("missing argument to %s", argp
[-2]);
150 die("too many parts (starting with %s), %d max",
156 if (!(cp
= *argp
++) || *cp
== '-')
157 die("missing argument to %s", argp
[-2]);
159 die("too many types (starting with %s), %d max",
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
167 if (npreferred
>= NPREFS
)
168 die("too many preferred types (starting with %s), %d max",
170 mime_preference
[npreferred
].type
= cp
;
171 cp
= strchr(cp
, '/');
172 if (cp
) *cp
++ = '\0';
173 mime_preference
[npreferred
++].subtype
= cp
;
181 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
182 die("missing argument to %s", argp
[-2]);
183 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
210 if (*cp
== '+' || *cp
== '@') {
212 die("only one folder at a time!");
213 folder
= pluspath (cp
);
215 app_msgarg(&msgs
, cp
);
218 /* null terminate the list of acceptable parts/types */
222 if (!context_find ("path"))
223 free (path ("./", TFOLDER
));
225 if (file
&& msgs
.size
)
226 die("cannot specify msg and file at same time!");
229 * check if message is coming from file
232 cts
= mh_xcalloc(2, sizeof *cts
);
235 if ((ct
= parse_mime (file
)))
239 * message(s) are coming from a folder
242 app_msgarg(&msgs
, "cur");
244 folder
= getfolder (1);
245 maildir
= m_maildir (folder
);
247 if (chdir (maildir
) == NOTOK
)
248 adios (maildir
, "unable to change directory to");
250 /* read folder and create message structure */
251 if (!(mp
= folder_read (folder
, 0)))
252 die("unable to read folder %s", folder
);
254 /* check for empty folder */
256 die("no messages in %s", folder
);
258 /* parse all the message ranges/sequences and set SELECTED */
259 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
260 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
262 seq_setprev (mp
); /* set the previous-sequence */
264 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
267 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
268 if (is_selected(mp
, msgnum
)) {
271 msgnam
= m_name (msgnum
);
272 if ((ct
= parse_mime (msgnam
)))
282 SIGNAL (SIGQUIT
, quitser
);
283 SIGNAL (SIGPIPE
, pipeser
);
286 * Get the associated umask for the relevant contents.
288 for (ctp
= cts
; *ctp
; ctp
++) {
292 if (type_ok (ct
, 1) && !ct
->c_umask
) {
293 if (stat (ct
->c_file
, &st
) != NOTOK
)
294 ct
->c_umask
= ~(st
.st_mode
& 0777);
296 ct
->c_umask
= ~m_gmprot();
301 * List the message content
303 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
305 /* Now free all the structures for the content */
306 for (ctp
= cts
; *ctp
; ctp
++)
312 /* If reading from a folder, do some updating */
314 context_replace (pfolder
, folder
);/* update current folder */
316 seq_setcur (mp
, mp
->hghsel
); /* update current message */
317 seq_save (mp
); /* synchronize sequences */
318 context_save (); /* save the context file */
331 fprintf (stderr
, "\n");