]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
3 * mhlist.c -- list the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.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("rcache policy", 0, RCACHESW) \
36 X("wcache policy", 0, WCACHESW) \
37 X("changecur", 0, CHGSW) \
38 X("nochangecur", 0, NCHGSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
41 X("debug", -5, DEBUGSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(MHLIST
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
55 extern char *cache_public
;
56 extern char *cache_private
;
61 extern char *parts
[NPARTS
+ 1];
62 extern char *types
[NTYPES
+ 1];
66 * This is currently needed to keep mhparse happy.
67 * This needs to be changed.
71 #define quitser pipeser
74 CT
parse_mime (char *);
77 int part_ok (CT
, int);
78 int type_ok (CT
, int);
79 void flush_errors (void);
83 void freects_done (int) NORETURN
;
88 static void pipeser (int);
92 main (int argc
, char **argv
)
94 int sizesw
= 1, headsw
= 1, chgflag
= 1, verbosw
= 0, dispo
= 0;
95 int msgnum
, *icachesw
;
96 char *cp
, *file
= NULL
, *folder
= NULL
;
97 char *maildir
, buf
[100], **argp
;
99 struct msgs_array msgs
= { 0, 0, NULL
};
100 struct msgs
*mp
= NULL
;
103 if (nmh_init(argv
[0], 1)) { return 1; }
107 arguments
= getarguments (invo_name
, argc
, argv
, 1);
113 while ((cp
= *argp
++)) {
115 switch (smatch (++cp
, switches
)) {
117 ambigsw (cp
, switches
);
120 adios (NULL
, "-%s unknown", cp
);
123 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
125 print_help (buf
, switches
, 1);
128 print_version(invo_name
);
132 icachesw
= &rcachesw
;
135 icachesw
= &wcachesw
;
137 if (!(cp
= *argp
++) || *cp
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
139 switch (*icachesw
= smatch (cp
, caches
)) {
141 ambigsw (cp
, caches
);
144 adios (NULL
, "%s unknown", cp
);
172 if (!(cp
= *argp
++) || *cp
== '-')
173 adios (NULL
, "missing argument to %s", argp
[-2]);
175 adios (NULL
, "too many parts (starting with %s), %d max",
181 if (!(cp
= *argp
++) || *cp
== '-')
182 adios (NULL
, "missing argument to %s", argp
[-2]);
184 adios (NULL
, "too many types (starting with %s), %d max",
190 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
191 adios (NULL
, "missing argument to %s", argp
[-2]);
192 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
219 if (*cp
== '+' || *cp
== '@') {
221 adios (NULL
, "only one folder at a time!");
223 folder
= pluspath (cp
);
225 app_msgarg(&msgs
, cp
);
228 /* null terminate the list of acceptable parts/types */
232 /* Check for public cache location */
233 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
236 /* Check for private cache location */
237 if (!(cache_private
= context_find (nmhprivcache
)))
238 cache_private
= ".cache";
239 cache_private
= getcpy (m_maildir (cache_private
));
241 if (!context_find ("path"))
242 free (path ("./", TFOLDER
));
244 if (file
&& msgs
.size
)
245 adios (NULL
, "cannot specify msg and file at same time!");
248 * check if message is coming from file
251 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
252 adios (NULL
, "out of memory");
255 if ((ct
= parse_mime (file
)))
259 * message(s) are coming from a folder
262 app_msgarg(&msgs
, "cur");
264 folder
= getfolder (1);
265 maildir
= m_maildir (folder
);
267 if (chdir (maildir
) == NOTOK
)
268 adios (maildir
, "unable to change directory to");
270 /* read folder and create message structure */
271 if (!(mp
= folder_read (folder
, 0)))
272 adios (NULL
, "unable to read folder %s", folder
);
274 /* check for empty folder */
276 adios (NULL
, "no messages in %s", folder
);
278 /* parse all the message ranges/sequences and set SELECTED */
279 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
280 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
282 seq_setprev (mp
); /* set the previous-sequence */
284 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
285 adios (NULL
, "out of memory");
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");