]>
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 <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("noprefer", 0, NPREFERSW) \
38 X("rcache policy", 0, RCACHESW) \
39 X("wcache policy", 0, WCACHESW) \
40 X("changecur", 0, CHGSW) \
41 X("nochangecur", 0, NCHGSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("debug", -5, DEBUGSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(MHLIST
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
55 * This is currently needed to keep mhparse happy.
56 * This needs to be changed.
60 #define quitser pipeser
65 static void pipeser (int);
69 main (int argc
, char **argv
)
71 int sizesw
= 1, headsw
= 1, chgflag
= 1, verbosw
= 0, dispo
= 0;
72 int msgnum
, *icachesw
;
73 char *cp
, *file
= NULL
, *folder
= NULL
;
74 char *maildir
, buf
[100], **argp
;
76 struct msgs_array msgs
= { 0, 0, NULL
};
77 struct msgs
*mp
= NULL
;
80 if (nmh_init(argv
[0], 1)) { return 1; }
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 adios (NULL
, "-%s unknown", cp
);
100 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
109 icachesw
= &rcachesw
;
112 icachesw
= &wcachesw
;
114 if (!(cp
= *argp
++) || *cp
== '-')
115 adios (NULL
, "missing argument to %s", argp
[-2]);
116 switch (*icachesw
= smatch (cp
, cache_policy
)) {
118 ambigsw (cp
, cache_policy
);
121 adios (NULL
, "%s unknown", cp
);
149 if (!(cp
= *argp
++) || *cp
== '-')
150 adios (NULL
, "missing argument to %s", argp
[-2]);
152 adios (NULL
, "too many parts (starting with %s), %d max",
158 if (!(cp
= *argp
++) || *cp
== '-')
159 adios (NULL
, "missing argument to %s", argp
[-2]);
161 adios (NULL
, "too many types (starting with %s), %d max",
167 if (!(cp
= *argp
++) || *cp
== '-')
168 adios (NULL
, "missing argument to %s", argp
[-2]);
169 if (npreferred
>= NPREFS
)
170 adios (NULL
, "too many preferred types (starting with %s), %d max",
172 mime_preference
[npreferred
].type
= cp
;
173 cp
= strchr(cp
, '/');
174 if (cp
) *cp
++ = '\0';
175 mime_preference
[npreferred
++].subtype
= cp
;
183 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
184 adios (NULL
, "missing argument to %s", argp
[-2]);
185 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
212 if (*cp
== '+' || *cp
== '@') {
214 adios (NULL
, "only one folder at a time!");
215 folder
= pluspath (cp
);
217 app_msgarg(&msgs
, cp
);
220 /* null terminate the list of acceptable parts/types */
224 /* Check for public cache location */
225 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
228 /* Check for private cache location */
229 if (!(cache_private
= context_find (nmhprivcache
)))
230 cache_private
= ".cache";
231 cache_private
= getcpy (m_maildir (cache_private
));
233 if (!context_find ("path"))
234 free (path ("./", TFOLDER
));
236 if (file
&& msgs
.size
)
237 adios (NULL
, "cannot specify msg and file at same time!");
240 * check if message is coming from file
243 cts
= mh_xcalloc(2, sizeof *cts
);
246 if ((ct
= parse_mime (file
)))
250 * message(s) are coming from a folder
253 app_msgarg(&msgs
, "cur");
255 folder
= getfolder (1);
256 maildir
= m_maildir (folder
);
258 if (chdir (maildir
) == NOTOK
)
259 adios (maildir
, "unable to change directory to");
261 /* read folder and create message structure */
262 if (!(mp
= folder_read (folder
, 0)))
263 adios (NULL
, "unable to read folder %s", folder
);
265 /* check for empty folder */
267 adios (NULL
, "no messages in %s", folder
);
269 /* parse all the message ranges/sequences and set SELECTED */
270 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
271 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
273 seq_setprev (mp
); /* set the previous-sequence */
275 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
278 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
279 if (is_selected(mp
, msgnum
)) {
282 msgnam
= m_name (msgnum
);
283 if ((ct
= parse_mime (msgnam
)))
293 SIGNAL (SIGQUIT
, quitser
);
294 SIGNAL (SIGPIPE
, pipeser
);
297 * Get the associated umask for the relevant contents.
299 for (ctp
= cts
; *ctp
; ctp
++) {
303 if (type_ok (ct
, 1) && !ct
->c_umask
) {
304 if (stat (ct
->c_file
, &st
) != NOTOK
)
305 ct
->c_umask
= ~(st
.st_mode
& 0777);
307 ct
->c_umask
= ~m_gmprot();
312 * List the message content
314 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
316 /* Now free all the structures for the content */
317 for (ctp
= cts
; *ctp
; ctp
++)
323 /* If reading from a folder, do some updating */
325 context_replace (pfolder
, folder
);/* update current folder */
327 seq_setcur (mp
, mp
->hghsel
); /* update current message */
328 seq_save (mp
); /* synchronize sequences */
329 context_save (); /* save the context file */
342 fprintf (stderr
, "\n");