]>
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>
20 #define MHLIST_SWITCHES \
21 X("check", 0, CHECKSW) \
22 X("nocheck", 0, NCHECKSW) \
23 X("headers", 0, HEADSW) \
24 X("noheaders", 0, NHEADSW) \
25 X("realsize", 0, SIZESW) \
26 X("norealsize", 0, NSIZESW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("disposition", 0, DISPOSW) \
30 X("nodisposition", 0, NDISPOSW) \
31 X("file file", 0, FILESW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("prefer content", 0, PREFERSW) \
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 extern char *preferred_types
[];
67 extern char *preferred_subtypes
[];
68 extern int npreferred
;
71 * This is currently needed to keep mhparse happy.
72 * This needs to be changed.
76 #define quitser pipeser
79 CT
parse_mime (char *);
83 int type_ok (CT
, int);
84 void flush_errors (void);
89 static void pipeser (int);
93 main (int argc
, char **argv
)
95 int sizesw
= 1, headsw
= 1, chgflag
= 1, verbosw
= 0, dispo
= 0;
96 int msgnum
, *icachesw
;
97 char *cp
, *file
= NULL
, *folder
= NULL
;
98 char *maildir
, buf
[100], **argp
;
100 struct msgs_array msgs
= { 0, 0, NULL
};
101 struct msgs
*mp
= NULL
;
104 if (nmh_init(argv
[0], 1)) { return 1; }
108 arguments
= getarguments (invo_name
, argc
, argv
, 1);
114 while ((cp
= *argp
++)) {
116 switch (smatch (++cp
, switches
)) {
118 ambigsw (cp
, switches
);
121 adios (NULL
, "-%s unknown", cp
);
124 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
126 print_help (buf
, switches
, 1);
129 print_version(invo_name
);
133 icachesw
= &rcachesw
;
136 icachesw
= &wcachesw
;
138 if (!(cp
= *argp
++) || *cp
== '-')
139 adios (NULL
, "missing argument to %s", argp
[-2]);
140 switch (*icachesw
= smatch (cp
, caches
)) {
142 ambigsw (cp
, caches
);
145 adios (NULL
, "%s unknown", cp
);
173 if (!(cp
= *argp
++) || *cp
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
176 adios (NULL
, "too many parts (starting with %s), %d max",
182 if (!(cp
= *argp
++) || *cp
== '-')
183 adios (NULL
, "missing argument to %s", argp
[-2]);
185 adios (NULL
, "too many types (starting with %s), %d max",
191 if (!(cp
= *argp
++) || *cp
== '-')
192 adios (NULL
, "missing argument to %s", argp
[-2]);
193 if (npreferred
>= NPREFS
)
194 adios (NULL
, "too many preferred types (starting with %s), %d max",
196 preferred_types
[npreferred
] = cp
;
197 cp
= strchr(cp
, '/');
198 if (cp
) *cp
++ = '\0';
199 preferred_subtypes
[npreferred
++] = cp
;
203 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
204 adios (NULL
, "missing argument to %s", argp
[-2]);
205 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
232 if (*cp
== '+' || *cp
== '@') {
234 adios (NULL
, "only one folder at a time!");
236 folder
= pluspath (cp
);
238 app_msgarg(&msgs
, cp
);
241 /* null terminate the list of acceptable parts/types */
245 /* Check for public cache location */
246 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
249 /* Check for private cache location */
250 if (!(cache_private
= context_find (nmhprivcache
)))
251 cache_private
= ".cache";
252 cache_private
= getcpy (m_maildir (cache_private
));
254 if (!context_find ("path"))
255 free (path ("./", TFOLDER
));
257 if (file
&& msgs
.size
)
258 adios (NULL
, "cannot specify msg and file at same time!");
261 * check if message is coming from file
264 cts
= mh_xcalloc(2, sizeof *cts
);
267 if ((ct
= parse_mime (file
)))
271 * message(s) are coming from a folder
274 app_msgarg(&msgs
, "cur");
276 folder
= getfolder (1);
277 maildir
= m_maildir (folder
);
279 if (chdir (maildir
) == NOTOK
)
280 adios (maildir
, "unable to change directory to");
282 /* read folder and create message structure */
283 if (!(mp
= folder_read (folder
, 0)))
284 adios (NULL
, "unable to read folder %s", folder
);
286 /* check for empty folder */
288 adios (NULL
, "no messages in %s", folder
);
290 /* parse all the message ranges/sequences and set SELECTED */
291 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
292 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
294 seq_setprev (mp
); /* set the previous-sequence */
296 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
299 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
300 if (is_selected(mp
, msgnum
)) {
303 msgnam
= m_name (msgnum
);
304 if ((ct
= parse_mime (msgnam
)))
314 SIGNAL (SIGQUIT
, quitser
);
315 SIGNAL (SIGPIPE
, pipeser
);
318 * Get the associated umask for the relevant contents.
320 for (ctp
= cts
; *ctp
; ctp
++) {
324 if (type_ok (ct
, 1) && !ct
->c_umask
) {
325 if (stat (ct
->c_file
, &st
) != NOTOK
)
326 ct
->c_umask
= ~(st
.st_mode
& 0777);
328 ct
->c_umask
= ~m_gmprot();
333 * List the message content
335 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
337 /* Now free all the structures for the content */
338 for (ctp
= cts
; *ctp
; ctp
++)
344 /* If reading from a folder, do some updating */
346 context_replace (pfolder
, folder
);/* update current folder */
348 seq_setcur (mp
, mp
->hghsel
); /* update current message */
349 seq_save (mp
); /* synchronize sequences */
350 context_save (); /* save the context file */
363 fprintf (stderr
, "\n");