]>
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("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 *cache_public
;
57 extern char *cache_private
;
62 extern char *parts
[NPARTS
+ 1];
63 extern char *types
[NTYPES
+ 1];
67 extern char *preferred_types
[];
68 extern char *preferred_subtypes
[];
69 extern int npreferred
;
72 * This is currently needed to keep mhparse happy.
73 * This needs to be changed.
77 #define quitser pipeser
80 CT
parse_mime (char *);
84 int type_ok (CT
, int);
85 void flush_errors (void);
89 void freects_done (int) NORETURN
;
94 static void pipeser (int);
98 main (int argc
, char **argv
)
100 int sizesw
= 1, headsw
= 1, chgflag
= 1, verbosw
= 0, dispo
= 0;
101 int msgnum
, *icachesw
;
102 char *cp
, *file
= NULL
, *folder
= NULL
;
103 char *maildir
, buf
[100], **argp
;
105 struct msgs_array msgs
= { 0, 0, NULL
};
106 struct msgs
*mp
= NULL
;
109 if (nmh_init(argv
[0], 1)) { return 1; }
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
119 while ((cp
= *argp
++)) {
121 switch (smatch (++cp
, switches
)) {
123 ambigsw (cp
, switches
);
126 adios (NULL
, "-%s unknown", cp
);
129 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
131 print_help (buf
, switches
, 1);
134 print_version(invo_name
);
138 icachesw
= &rcachesw
;
141 icachesw
= &wcachesw
;
143 if (!(cp
= *argp
++) || *cp
== '-')
144 adios (NULL
, "missing argument to %s", argp
[-2]);
145 switch (*icachesw
= smatch (cp
, caches
)) {
147 ambigsw (cp
, caches
);
150 adios (NULL
, "%s unknown", cp
);
178 if (!(cp
= *argp
++) || *cp
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
181 adios (NULL
, "too many parts (starting with %s), %d max",
187 if (!(cp
= *argp
++) || *cp
== '-')
188 adios (NULL
, "missing argument to %s", argp
[-2]);
190 adios (NULL
, "too many types (starting with %s), %d max",
196 if (!(cp
= *argp
++) || *cp
== '-')
197 adios (NULL
, "missing argument to %s", argp
[-2]);
198 if (npreferred
>= NPREFS
)
199 adios (NULL
, "too many preferred types (starting with %s), %d max",
201 preferred_types
[npreferred
] = cp
;
202 cp
= strchr(cp
, '/');
203 if (cp
) *cp
++ = '\0';
204 preferred_subtypes
[npreferred
++] = cp
;
208 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
209 adios (NULL
, "missing argument to %s", argp
[-2]);
210 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
237 if (*cp
== '+' || *cp
== '@') {
239 adios (NULL
, "only one folder at a time!");
241 folder
= pluspath (cp
);
243 app_msgarg(&msgs
, cp
);
246 /* null terminate the list of acceptable parts/types */
250 /* Check for public cache location */
251 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
254 /* Check for private cache location */
255 if (!(cache_private
= context_find (nmhprivcache
)))
256 cache_private
= ".cache";
257 cache_private
= getcpy (m_maildir (cache_private
));
259 if (!context_find ("path"))
260 free (path ("./", TFOLDER
));
262 if (file
&& msgs
.size
)
263 adios (NULL
, "cannot specify msg and file at same time!");
266 * check if message is coming from file
269 cts
= mh_xcalloc(2, sizeof *cts
);
272 if ((ct
= parse_mime (file
)))
276 * message(s) are coming from a folder
279 app_msgarg(&msgs
, "cur");
281 folder
= getfolder (1);
282 maildir
= m_maildir (folder
);
284 if (chdir (maildir
) == NOTOK
)
285 adios (maildir
, "unable to change directory to");
287 /* read folder and create message structure */
288 if (!(mp
= folder_read (folder
, 0)))
289 adios (NULL
, "unable to read folder %s", folder
);
291 /* check for empty folder */
293 adios (NULL
, "no messages in %s", folder
);
295 /* parse all the message ranges/sequences and set SELECTED */
296 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
297 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
299 seq_setprev (mp
); /* set the previous-sequence */
301 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
304 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
305 if (is_selected(mp
, msgnum
)) {
308 msgnam
= m_name (msgnum
);
309 if ((ct
= parse_mime (msgnam
)))
319 SIGNAL (SIGQUIT
, quitser
);
320 SIGNAL (SIGPIPE
, pipeser
);
323 * Get the associated umask for the relevant contents.
325 for (ctp
= cts
; *ctp
; ctp
++) {
329 if (type_ok (ct
, 1) && !ct
->c_umask
) {
330 if (stat (ct
->c_file
, &st
) != NOTOK
)
331 ct
->c_umask
= ~(st
.st_mode
& 0777);
333 ct
->c_umask
= ~m_gmprot();
338 * List the message content
340 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
342 /* Now free all the structures for the content */
343 for (ctp
= cts
; *ctp
; ctp
++)
349 /* If reading from a folder, do some updating */
351 context_replace (pfolder
, folder
);/* update current folder */
353 seq_setcur (mp
, mp
->hghsel
); /* update current message */
354 seq_save (mp
); /* synchronize sequences */
355 context_save (); /* save the context file */
368 fprintf (stderr
, "\n");