]>
diplodocus.org Git - nmh/blob - uip/mhlist.c
7092434895a57f34375cd137495c427dbdbc5440
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
);
51 extern char *tmp
; /* directory to place temp files */
56 extern char *cache_public
;
57 extern char *cache_private
;
62 extern char *parts
[NPARTS
+ 1];
63 extern char *types
[NTYPES
+ 1];
67 * This is currently needed to keep mhparse happy.
68 * This needs to be changed.
75 #define quitser pipeser
78 CT
parse_mime (char *);
81 int part_ok (CT
, int);
82 int type_ok (CT
, int);
83 void flush_errors (void);
86 void list_all_messages (CT
*, int, int, int, int);
90 void freects_done (int) NORETURN
;
95 static void pipeser (int);
99 main (int argc
, char **argv
)
101 int sizesw
= 1, headsw
= 1, chgflag
= 1;
102 int msgnum
, *icachesw
;
103 char *cp
, *file
= NULL
, *folder
= NULL
;
104 char *maildir
, buf
[100], **argp
;
106 struct msgs_array msgs
= { 0, 0, NULL
};
107 struct msgs
*mp
= NULL
;
112 setlocale(LC_ALL
, "");
113 invo_name
= r1bindex (argv
[0], '/');
115 /* read user profile/context */
118 arguments
= getarguments (invo_name
, argc
, argv
, 1);
124 while ((cp
= *argp
++)) {
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 adios (NULL
, "-%s unknown", cp
);
134 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
143 icachesw
= &rcachesw
;
146 icachesw
= &wcachesw
;
148 if (!(cp
= *argp
++) || *cp
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
150 switch (*icachesw
= smatch (cp
, caches
)) {
152 ambigsw (cp
, caches
);
155 adios (NULL
, "%s unknown", cp
);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
186 adios (NULL
, "too many parts (starting with %s), %d max",
192 if (!(cp
= *argp
++) || *cp
== '-')
193 adios (NULL
, "missing argument to %s", argp
[-2]);
195 adios (NULL
, "too many types (starting with %s), %d max",
201 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
202 adios (NULL
, "missing argument to %s", argp
[-2]);
203 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
224 if (*cp
== '+' || *cp
== '@') {
226 adios (NULL
, "only one folder at a time!");
228 folder
= pluspath (cp
);
230 app_msgarg(&msgs
, cp
);
233 /* null terminate the list of acceptable parts/types */
237 /* Check for public cache location */
238 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
241 /* Check for private cache location */
242 if (!(cache_private
= context_find (nmhprivcache
)))
243 cache_private
= ".cache";
244 cache_private
= getcpy (m_maildir (cache_private
));
247 * Check for storage directory. If specified,
248 * then store temporary files there. Else we
249 * store them in standard nmh directory.
251 if ((cp
= context_find (nmhstorage
)) && *cp
)
252 tmp
= concat (cp
, "/", invo_name
, NULL
);
254 tmp
= add (m_maildir (invo_name
), NULL
);
256 if (!context_find ("path"))
257 free (path ("./", TFOLDER
));
259 if (file
&& msgs
.size
)
260 adios (NULL
, "cannot specify msg and file at same time!");
263 * check if message is coming from file
266 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
267 adios (NULL
, "out of memory");
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 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
300 adios (NULL
, "out of memory");
303 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
304 if (is_selected(mp
, msgnum
)) {
307 msgnam
= m_name (msgnum
);
308 if ((ct
= parse_mime (msgnam
)))
318 SIGNAL (SIGQUIT
, quitser
);
319 SIGNAL (SIGPIPE
, pipeser
);
322 * Get the associated umask for the relevant contents.
324 for (ctp
= cts
; *ctp
; ctp
++) {
328 if (type_ok (ct
, 1) && !ct
->c_umask
) {
329 if (stat (ct
->c_file
, &st
) != NOTOK
)
330 ct
->c_umask
= ~(st
.st_mode
& 0777);
332 ct
->c_umask
= ~m_gmprot();
337 * List the message content
339 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
341 /* Now free all the structures for the content */
342 for (ctp
= cts
; *ctp
; ctp
++)
348 /* If reading from a folder, do some updating */
350 context_replace (pfolder
, folder
);/* update current folder */
352 seq_setcur (mp
, mp
->hghsel
); /* update current message */
353 seq_save (mp
); /* synchronize sequences */
354 context_save (); /* save the context file */
367 fprintf (stderr
, "\n");