]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
a9be19e1ca65add659a8b9e3a66949d85e693f49
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 <h/signals.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
19 #include "../sbr/m_maildir.h"
22 #define MHLIST_SWITCHES \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("headers", 0, HEADSW) \
26 X("noheaders", 0, NHEADSW) \
27 X("realsize", 0, SIZESW) \
28 X("norealsize", 0, NSIZESW) \
29 X("verbose", 0, VERBSW) \
30 X("noverbose", 0, NVERBSW) \
31 X("disposition", 0, DISPOSW) \
32 X("nodisposition", 0, NDISPOSW) \
33 X("file file", 0, FILESW) \
34 X("part number", 0, PARTSW) \
35 X("type content", 0, TYPESW) \
36 X("prefer content", 0, PREFERSW) \
37 X("rcache policy", 0, RCACHESW) \
38 X("wcache policy", 0, WCACHESW) \
39 X("changecur", 0, CHGSW) \
40 X("nochangecur", 0, NCHGSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("debug", -5, DEBUGSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(MHLIST
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
54 * This is currently needed to keep mhparse happy.
55 * This needs to be changed.
59 #define quitser pipeser
64 static void pipeser (int);
68 main (int argc
, char **argv
)
70 int sizesw
= 1, headsw
= 1, chgflag
= 1, verbosw
= 0, dispo
= 0;
71 int msgnum
, *icachesw
;
72 char *cp
, *file
= NULL
, *folder
= NULL
;
73 char *maildir
, buf
[100], **argp
;
75 struct msgs_array msgs
= { 0, 0, NULL
};
76 struct msgs
*mp
= NULL
;
79 if (nmh_init(argv
[0], 1)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 adios (NULL
, "-%s unknown", cp
);
99 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
101 print_help (buf
, switches
, 1);
104 print_version(invo_name
);
108 icachesw
= &rcachesw
;
111 icachesw
= &wcachesw
;
113 if (!(cp
= *argp
++) || *cp
== '-')
114 adios (NULL
, "missing argument to %s", argp
[-2]);
115 switch (*icachesw
= smatch (cp
, cache_policy
)) {
117 ambigsw (cp
, cache_policy
);
120 adios (NULL
, "%s unknown", cp
);
148 if (!(cp
= *argp
++) || *cp
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
151 adios (NULL
, "too many parts (starting with %s), %d max",
157 if (!(cp
= *argp
++) || *cp
== '-')
158 adios (NULL
, "missing argument to %s", argp
[-2]);
160 adios (NULL
, "too many types (starting with %s), %d max",
166 if (!(cp
= *argp
++) || *cp
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
168 if (npreferred
>= NPREFS
)
169 adios (NULL
, "too many preferred types (starting with %s), %d max",
171 preferred_types
[npreferred
] = cp
;
172 cp
= strchr(cp
, '/');
173 if (cp
) *cp
++ = '\0';
174 preferred_subtypes
[npreferred
++] = cp
;
178 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
179 adios (NULL
, "missing argument to %s", argp
[-2]);
180 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
207 if (*cp
== '+' || *cp
== '@') {
209 adios (NULL
, "only one folder at a time!");
210 folder
= pluspath (cp
);
212 app_msgarg(&msgs
, cp
);
215 /* null terminate the list of acceptable parts/types */
219 /* Check for public cache location */
220 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
223 /* Check for private cache location */
224 if (!(cache_private
= context_find (nmhprivcache
)))
225 cache_private
= ".cache";
226 cache_private
= getcpy (m_maildir (cache_private
));
228 if (!context_find ("path"))
229 free (path ("./", TFOLDER
));
231 if (file
&& msgs
.size
)
232 adios (NULL
, "cannot specify msg and file at same time!");
235 * check if message is coming from file
238 cts
= mh_xcalloc(2, sizeof *cts
);
241 if ((ct
= parse_mime (file
)))
245 * message(s) are coming from a folder
248 app_msgarg(&msgs
, "cur");
250 folder
= getfolder (1);
251 maildir
= m_maildir (folder
);
253 if (chdir (maildir
) == NOTOK
)
254 adios (maildir
, "unable to change directory to");
256 /* read folder and create message structure */
257 if (!(mp
= folder_read (folder
, 0)))
258 adios (NULL
, "unable to read folder %s", folder
);
260 /* check for empty folder */
262 adios (NULL
, "no messages in %s", folder
);
264 /* parse all the message ranges/sequences and set SELECTED */
265 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
266 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
268 seq_setprev (mp
); /* set the previous-sequence */
270 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
273 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
274 if (is_selected(mp
, msgnum
)) {
277 msgnam
= m_name (msgnum
);
278 if ((ct
= parse_mime (msgnam
)))
288 SIGNAL (SIGQUIT
, quitser
);
289 SIGNAL (SIGPIPE
, pipeser
);
292 * Get the associated umask for the relevant contents.
294 for (ctp
= cts
; *ctp
; ctp
++) {
298 if (type_ok (ct
, 1) && !ct
->c_umask
) {
299 if (stat (ct
->c_file
, &st
) != NOTOK
)
300 ct
->c_umask
= ~(st
.st_mode
& 0777);
302 ct
->c_umask
= ~m_gmprot();
307 * List the message content
309 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
311 /* Now free all the structures for the content */
312 for (ctp
= cts
; *ctp
; ctp
++)
318 /* If reading from a folder, do some updating */
320 context_replace (pfolder
, folder
);/* update current folder */
322 seq_setcur (mp
, mp
->hghsel
); /* update current message */
323 seq_save (mp
); /* synchronize sequences */
324 context_save (); /* save the context file */
337 fprintf (stderr
, "\n");