]>
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.
73 #define quitser pipeser
76 CT
parse_mime (char *);
79 int part_ok (CT
, int);
80 int type_ok (CT
, int);
81 void flush_errors (void);
85 void freects_done (int) NORETURN
;
90 static void pipeser (int);
94 main (int argc
, char **argv
)
96 int sizesw
= 1, headsw
= 1, chgflag
= 1, verbosw
= 0, dispo
= 0;
97 int msgnum
, *icachesw
;
98 char *cp
, *file
= NULL
, *folder
= NULL
;
99 char *maildir
, buf
[100], **argp
;
101 struct msgs_array msgs
= { 0, 0, NULL
};
102 struct msgs
*mp
= NULL
;
105 if (nmh_init(argv
[0], 1)) { return 1; }
109 arguments
= getarguments (invo_name
, argc
, argv
, 1);
115 while ((cp
= *argp
++)) {
117 switch (smatch (++cp
, switches
)) {
119 ambigsw (cp
, switches
);
122 adios (NULL
, "-%s unknown", cp
);
125 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
127 print_help (buf
, switches
, 1);
130 print_version(invo_name
);
134 icachesw
= &rcachesw
;
137 icachesw
= &wcachesw
;
139 if (!(cp
= *argp
++) || *cp
== '-')
140 adios (NULL
, "missing argument to %s", argp
[-2]);
141 switch (*icachesw
= smatch (cp
, caches
)) {
143 ambigsw (cp
, caches
);
146 adios (NULL
, "%s unknown", cp
);
174 if (!(cp
= *argp
++) || *cp
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
177 adios (NULL
, "too many parts (starting with %s), %d max",
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
186 adios (NULL
, "too many types (starting with %s), %d max",
192 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
193 adios (NULL
, "missing argument to %s", argp
[-2]);
194 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
221 if (*cp
== '+' || *cp
== '@') {
223 adios (NULL
, "only one folder at a time!");
225 folder
= pluspath (cp
);
227 app_msgarg(&msgs
, cp
);
230 /* null terminate the list of acceptable parts/types */
234 /* Check for public cache location */
235 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
238 /* Check for private cache location */
239 if (!(cache_private
= context_find (nmhprivcache
)))
240 cache_private
= ".cache";
241 cache_private
= getcpy (m_maildir (cache_private
));
243 if (!context_find ("path"))
244 free (path ("./", TFOLDER
));
246 if (file
&& msgs
.size
)
247 adios (NULL
, "cannot specify msg and file at same time!");
250 * check if message is coming from file
253 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
254 adios (NULL
, "out of memory");
257 if ((ct
= parse_mime (file
)))
261 * message(s) are coming from a folder
264 app_msgarg(&msgs
, "cur");
266 folder
= getfolder (1);
267 maildir
= m_maildir (folder
);
269 if (chdir (maildir
) == NOTOK
)
270 adios (maildir
, "unable to change directory to");
272 /* read folder and create message structure */
273 if (!(mp
= folder_read (folder
, 0)))
274 adios (NULL
, "unable to read folder %s", folder
);
276 /* check for empty folder */
278 adios (NULL
, "no messages in %s", folder
);
280 /* parse all the message ranges/sequences and set SELECTED */
281 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
282 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
284 seq_setprev (mp
); /* set the previous-sequence */
286 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
287 adios (NULL
, "out of memory");
290 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
291 if (is_selected(mp
, msgnum
)) {
294 msgnam
= m_name (msgnum
);
295 if ((ct
= parse_mime (msgnam
)))
305 SIGNAL (SIGQUIT
, quitser
);
306 SIGNAL (SIGPIPE
, pipeser
);
309 * Get the associated umask for the relevant contents.
311 for (ctp
= cts
; *ctp
; ctp
++) {
315 if (type_ok (ct
, 1) && !ct
->c_umask
) {
316 if (stat (ct
->c_file
, &st
) != NOTOK
)
317 ct
->c_umask
= ~(st
.st_mode
& 0777);
319 ct
->c_umask
= ~m_gmprot();
324 * List the message content
326 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
328 /* Now free all the structures for the content */
329 for (ctp
= cts
; *ctp
; ctp
++)
335 /* If reading from a folder, do some updating */
337 context_replace (pfolder
, folder
);/* update current folder */
339 seq_setcur (mp
, mp
->hghsel
); /* update current message */
340 seq_save (mp
); /* synchronize sequences */
341 context_save (); /* save the context file */
354 fprintf (stderr
, "\n");