]>
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>
19 #define MHLIST_SWITCHES \
20 X("check", 0, CHECKSW) \
21 X("nocheck", 0, NCHECKSW) \
22 X("headers", 0, HEADSW) \
23 X("noheaders", 0, NHEADSW) \
24 X("realsize", 0, SIZESW) \
25 X("norealsize", 0, NSIZESW) \
26 X("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("disposition", 0, DISPOSW) \
29 X("nodisposition", 0, NDISPOSW) \
30 X("file file", 0, FILESW) \
31 X("part number", 0, PARTSW) \
32 X("type content", 0, TYPESW) \
33 X("prefer content", 0, PREFERSW) \
34 X("rcache policy", 0, RCACHESW) \
35 X("wcache policy", 0, WCACHESW) \
36 X("changecur", 0, CHGSW) \
37 X("nochangecur", 0, NCHGSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
40 X("debug", -5, DEBUGSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(MHLIST
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(MHLIST
, switches
);
54 extern char *cache_public
;
55 extern char *cache_private
;
60 extern char *parts
[NPARTS
+ 1];
61 extern char *types
[NTYPES
+ 1];
65 extern char *preferred_types
[];
66 extern char *preferred_subtypes
[];
67 extern int npreferred
;
70 * This is currently needed to keep mhparse happy.
71 * This needs to be changed.
75 #define quitser pipeser
78 CT
parse_mime (char *);
82 int type_ok (CT
, int);
83 void flush_errors (void);
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, verbosw
= 0, dispo
= 0;
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
== '-')
195 adios (NULL
, "missing argument to %s", argp
[-2]);
196 if (npreferred
>= NPREFS
)
197 adios (NULL
, "too many preferred types (starting with %s), %d max",
199 preferred_types
[npreferred
] = cp
;
200 cp
= strchr(cp
, '/');
201 if (cp
) *cp
++ = '\0';
202 preferred_subtypes
[npreferred
++] = cp
;
206 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
207 adios (NULL
, "missing argument to %s", argp
[-2]);
208 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
235 if (*cp
== '+' || *cp
== '@') {
237 adios (NULL
, "only one folder at a time!");
239 folder
= pluspath (cp
);
241 app_msgarg(&msgs
, cp
);
244 /* null terminate the list of acceptable parts/types */
248 /* Check for public cache location */
249 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
252 /* Check for private cache location */
253 if (!(cache_private
= context_find (nmhprivcache
)))
254 cache_private
= ".cache";
255 cache_private
= getcpy (m_maildir (cache_private
));
257 if (!context_find ("path"))
258 free (path ("./", TFOLDER
));
260 if (file
&& msgs
.size
)
261 adios (NULL
, "cannot specify msg and file at same time!");
264 * check if message is coming from file
267 cts
= mh_xcalloc(2, sizeof *cts
);
270 if ((ct
= parse_mime (file
)))
274 * message(s) are coming from a folder
277 app_msgarg(&msgs
, "cur");
279 folder
= getfolder (1);
280 maildir
= m_maildir (folder
);
282 if (chdir (maildir
) == NOTOK
)
283 adios (maildir
, "unable to change directory to");
285 /* read folder and create message structure */
286 if (!(mp
= folder_read (folder
, 0)))
287 adios (NULL
, "unable to read folder %s", folder
);
289 /* check for empty folder */
291 adios (NULL
, "no messages in %s", folder
);
293 /* parse all the message ranges/sequences and set SELECTED */
294 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
295 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
297 seq_setprev (mp
); /* set the previous-sequence */
299 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
302 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
303 if (is_selected(mp
, msgnum
)) {
306 msgnam
= m_name (msgnum
);
307 if ((ct
= parse_mime (msgnam
)))
317 SIGNAL (SIGQUIT
, quitser
);
318 SIGNAL (SIGPIPE
, pipeser
);
321 * Get the associated umask for the relevant contents.
323 for (ctp
= cts
; *ctp
; ctp
++) {
327 if (type_ok (ct
, 1) && !ct
->c_umask
) {
328 if (stat (ct
->c_file
, &st
) != NOTOK
)
329 ct
->c_umask
= ~(st
.st_mode
& 0777);
331 ct
->c_umask
= ~m_gmprot();
336 * List the message content
338 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, dispo
);
340 /* Now free all the structures for the content */
341 for (ctp
= cts
; *ctp
; ctp
++)
347 /* If reading from a folder, do some updating */
349 context_replace (pfolder
, folder
);/* update current folder */
351 seq_setcur (mp
, mp
->hghsel
); /* update current message */
352 seq_save (mp
); /* synchronize sequences */
353 context_save (); /* save the context file */
366 fprintf (stderr
, "\n");