]>
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("file file", 0, FILESW) \
31 X("part number", 0, PARTSW) \
32 X("type content", 0, TYPESW) \
33 X("rcache policy", 0, RCACHESW) \
34 X("wcache policy", 0, WCACHESW) \
35 X("changecur", 0, CHGSW) \
36 X("nochangecur", 0, NCHGSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("debug", -5, DEBUGSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(MHLIST
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
53 extern char *cache_public
;
54 extern char *cache_private
;
59 extern char *parts
[NPARTS
+ 1];
60 extern char *types
[NTYPES
+ 1];
64 * This is currently needed to keep mhparse happy.
65 * This needs to be changed.
72 #define quitser pipeser
75 CT
parse_mime (char *);
78 int part_ok (CT
, int);
79 int type_ok (CT
, int);
80 void flush_errors (void);
83 void list_all_messages (CT
*, int, int, int, int);
87 void freects_done (int) NORETURN
;
92 static void pipeser (int);
96 main (int argc
, char **argv
)
98 int sizesw
= 1, headsw
= 1, chgflag
= 1;
99 int msgnum
, *icachesw
;
100 char *cp
, *file
= NULL
, *folder
= NULL
;
101 char *maildir
, buf
[100], **argp
;
103 struct msgs_array msgs
= { 0, 0, NULL
};
104 struct msgs
*mp
= NULL
;
107 if (nmh_init(argv
[0], 1)) { return 1; }
111 arguments
= getarguments (invo_name
, argc
, argv
, 1);
117 while ((cp
= *argp
++)) {
119 switch (smatch (++cp
, switches
)) {
121 ambigsw (cp
, switches
);
124 adios (NULL
, "-%s unknown", cp
);
127 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
129 print_help (buf
, switches
, 1);
132 print_version(invo_name
);
136 icachesw
= &rcachesw
;
139 icachesw
= &wcachesw
;
141 if (!(cp
= *argp
++) || *cp
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
143 switch (*icachesw
= smatch (cp
, caches
)) {
145 ambigsw (cp
, caches
);
148 adios (NULL
, "%s unknown", cp
);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
179 adios (NULL
, "too many parts (starting with %s), %d max",
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
188 adios (NULL
, "too many types (starting with %s), %d max",
194 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
195 adios (NULL
, "missing argument to %s", argp
[-2]);
196 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
217 if (*cp
== '+' || *cp
== '@') {
219 adios (NULL
, "only one folder at a time!");
221 folder
= pluspath (cp
);
223 app_msgarg(&msgs
, cp
);
226 /* null terminate the list of acceptable parts/types */
230 /* Check for public cache location */
231 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
234 /* Check for private cache location */
235 if (!(cache_private
= context_find (nmhprivcache
)))
236 cache_private
= ".cache";
237 cache_private
= getcpy (m_maildir (cache_private
));
239 if (!context_find ("path"))
240 free (path ("./", TFOLDER
));
242 if (file
&& msgs
.size
)
243 adios (NULL
, "cannot specify msg and file at same time!");
246 * check if message is coming from file
249 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
250 adios (NULL
, "out of memory");
253 if ((ct
= parse_mime (file
)))
257 * message(s) are coming from a folder
260 app_msgarg(&msgs
, "cur");
262 folder
= getfolder (1);
263 maildir
= m_maildir (folder
);
265 if (chdir (maildir
) == NOTOK
)
266 adios (maildir
, "unable to change directory to");
268 /* read folder and create message structure */
269 if (!(mp
= folder_read (folder
, 0)))
270 adios (NULL
, "unable to read folder %s", folder
);
272 /* check for empty folder */
274 adios (NULL
, "no messages in %s", folder
);
276 /* parse all the message ranges/sequences and set SELECTED */
277 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
278 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
280 seq_setprev (mp
); /* set the previous-sequence */
282 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
283 adios (NULL
, "out of memory");
286 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
287 if (is_selected(mp
, msgnum
)) {
290 msgnam
= m_name (msgnum
);
291 if ((ct
= parse_mime (msgnam
)))
301 SIGNAL (SIGQUIT
, quitser
);
302 SIGNAL (SIGPIPE
, pipeser
);
305 * Get the associated umask for the relevant contents.
307 for (ctp
= cts
; *ctp
; ctp
++) {
311 if (type_ok (ct
, 1) && !ct
->c_umask
) {
312 if (stat (ct
->c_file
, &st
) != NOTOK
)
313 ct
->c_umask
= ~(st
.st_mode
& 0777);
315 ct
->c_umask
= ~m_gmprot();
320 * List the message content
322 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
324 /* Now free all the structures for the content */
325 for (ctp
= cts
; *ctp
; ctp
++)
331 /* If reading from a folder, do some updating */
333 context_replace (pfolder
, folder
);/* update current folder */
335 seq_setcur (mp
, mp
->hghsel
); /* update current message */
336 seq_save (mp
); /* synchronize sequences */
337 context_save (); /* save the context file */
350 fprintf (stderr
, "\n");