]>
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>
18 #include "../sbr/m_maildir.h"
21 #define MHLIST_SWITCHES \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("headers", 0, HEADSW) \
25 X("noheaders", 0, NHEADSW) \
26 X("realsize", 0, SIZESW) \
27 X("norealsize", 0, NSIZESW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("disposition", 0, DISPOSW) \
31 X("nodisposition", 0, NDISPOSW) \
32 X("file file", 0, FILESW) \
33 X("part number", 0, PARTSW) \
34 X("type content", 0, TYPESW) \
35 X("prefer content", 0, PREFERSW) \
36 X("rcache policy", 0, RCACHESW) \
37 X("wcache policy", 0, WCACHESW) \
38 X("changecur", 0, CHGSW) \
39 X("nochangecur", 0, NCHGSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
42 X("debug", -5, DEBUGSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(MHLIST
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
56 extern char *parts
[NPARTS
+ 1];
57 extern char *types
[NTYPES
+ 1];
61 extern char *preferred_types
[];
62 extern char *preferred_subtypes
[];
63 extern int npreferred
;
66 * This is currently needed to keep mhparse happy.
67 * This needs to be changed.
71 #define quitser pipeser
74 CT
parse_mime (char *);
78 int type_ok (CT
, int);
79 void flush_errors (void);
84 static void pipeser (int);
88 main (int argc
, char **argv
)
90 int sizesw
= 1, headsw
= 1, chgflag
= 1, verbosw
= 0, dispo
= 0;
91 int msgnum
, *icachesw
;
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], 1)) { return 1; }
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
109 while ((cp
= *argp
++)) {
111 switch (smatch (++cp
, switches
)) {
113 ambigsw (cp
, switches
);
116 adios (NULL
, "-%s unknown", cp
);
119 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
121 print_help (buf
, switches
, 1);
124 print_version(invo_name
);
128 icachesw
= &rcachesw
;
131 icachesw
= &wcachesw
;
133 if (!(cp
= *argp
++) || *cp
== '-')
134 adios (NULL
, "missing argument to %s", argp
[-2]);
135 switch (*icachesw
= smatch (cp
, cache_policy
)) {
137 ambigsw (cp
, cache_policy
);
140 adios (NULL
, "%s unknown", cp
);
168 if (!(cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
171 adios (NULL
, "too many parts (starting with %s), %d max",
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
180 adios (NULL
, "too many types (starting with %s), %d max",
186 if (!(cp
= *argp
++) || *cp
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
188 if (npreferred
>= NPREFS
)
189 adios (NULL
, "too many preferred types (starting with %s), %d max",
191 preferred_types
[npreferred
] = cp
;
192 cp
= strchr(cp
, '/');
193 if (cp
) *cp
++ = '\0';
194 preferred_subtypes
[npreferred
++] = cp
;
198 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
199 adios (NULL
, "missing argument to %s", argp
[-2]);
200 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
227 if (*cp
== '+' || *cp
== '@') {
229 adios (NULL
, "only one folder at a time!");
230 folder
= pluspath (cp
);
232 app_msgarg(&msgs
, cp
);
235 /* null terminate the list of acceptable parts/types */
239 /* Check for public cache location */
240 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
243 /* Check for private cache location */
244 if (!(cache_private
= context_find (nmhprivcache
)))
245 cache_private
= ".cache";
246 cache_private
= getcpy (m_maildir (cache_private
));
248 if (!context_find ("path"))
249 free (path ("./", TFOLDER
));
251 if (file
&& msgs
.size
)
252 adios (NULL
, "cannot specify msg and file at same time!");
255 * check if message is coming from file
258 cts
= mh_xcalloc(2, sizeof *cts
);
261 if ((ct
= parse_mime (file
)))
265 * message(s) are coming from a folder
268 app_msgarg(&msgs
, "cur");
270 folder
= getfolder (1);
271 maildir
= m_maildir (folder
);
273 if (chdir (maildir
) == NOTOK
)
274 adios (maildir
, "unable to change directory to");
276 /* read folder and create message structure */
277 if (!(mp
= folder_read (folder
, 0)))
278 adios (NULL
, "unable to read folder %s", folder
);
280 /* check for empty folder */
282 adios (NULL
, "no messages in %s", folder
);
284 /* parse all the message ranges/sequences and set SELECTED */
285 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
286 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
288 seq_setprev (mp
); /* set the previous-sequence */
290 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
293 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
294 if (is_selected(mp
, msgnum
)) {
297 msgnam
= m_name (msgnum
);
298 if ((ct
= parse_mime (msgnam
)))
308 SIGNAL (SIGQUIT
, quitser
);
309 SIGNAL (SIGPIPE
, pipeser
);
312 * Get the associated umask for the relevant contents.
314 for (ctp
= cts
; *ctp
; ctp
++) {
318 if (type_ok (ct
, 1) && !ct
->c_umask
) {
319 if (stat (ct
->c_file
, &st
) != NOTOK
)
320 ct
->c_umask
= ~(st
.st_mode
& 0777);
322 ct
->c_umask
= ~m_gmprot();
327 * List the message content
329 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
331 /* Now free all the structures for the content */
332 for (ctp
= cts
; *ctp
; ctp
++)
338 /* If reading from a folder, do some updating */
340 context_replace (pfolder
, folder
);/* update current folder */
342 seq_setcur (mp
, mp
->hghsel
); /* update current message */
343 seq_save (mp
); /* synchronize sequences */
344 context_save (); /* save the context file */
357 fprintf (stderr
, "\n");