]>
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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #define MHLIST_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("headers", 0, HEADSW) \
27 X("noheaders", 0, NHEADSW) \
28 X("realsize", 0, SIZESW) \
29 X("norealsize", 0, NSIZESW) \
30 X("verbose", 0, VERBSW) \
31 X("noverbose", 0, NVERBSW) \
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("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;
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
;
113 setlocale(LC_ALL
, "");
115 invo_name
= r1bindex (argv
[0], '/');
117 /* read user profile/context */
120 arguments
= getarguments (invo_name
, argc
, argv
, 1);
126 while ((cp
= *argp
++)) {
128 switch (smatch (++cp
, switches
)) {
130 ambigsw (cp
, switches
);
133 adios (NULL
, "-%s unknown", cp
);
136 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
138 print_help (buf
, switches
, 1);
141 print_version(invo_name
);
145 icachesw
= &rcachesw
;
148 icachesw
= &wcachesw
;
150 if (!(cp
= *argp
++) || *cp
== '-')
151 adios (NULL
, "missing argument to %s", argp
[-2]);
152 switch (*icachesw
= smatch (cp
, caches
)) {
154 ambigsw (cp
, caches
);
157 adios (NULL
, "%s unknown", cp
);
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
188 adios (NULL
, "too many parts (starting with %s), %d max",
194 if (!(cp
= *argp
++) || *cp
== '-')
195 adios (NULL
, "missing argument to %s", argp
[-2]);
197 adios (NULL
, "too many types (starting with %s), %d max",
203 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
204 adios (NULL
, "missing argument to %s", argp
[-2]);
205 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
219 if (*cp
== '+' || *cp
== '@') {
221 adios (NULL
, "only one folder at a time!");
223 folder
= pluspath (cp
);
225 app_msgarg(&msgs
, cp
);
228 /* null terminate the list of acceptable parts/types */
232 /* Check for public cache location */
233 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
236 /* Check for private cache location */
237 if (!(cache_private
= context_find (nmhprivcache
)))
238 cache_private
= ".cache";
239 cache_private
= getcpy (m_maildir (cache_private
));
242 * Check for storage directory. If specified,
243 * then store temporary files there. Else we
244 * store them in standard nmh directory.
246 if ((cp
= context_find (nmhstorage
)) && *cp
)
247 tmp
= concat (cp
, "/", invo_name
, NULL
);
249 tmp
= add (m_maildir (invo_name
), NULL
);
251 if (!context_find ("path"))
252 free (path ("./", TFOLDER
));
254 if (file
&& msgs
.size
)
255 adios (NULL
, "cannot specify msg and file at same time!");
258 * check if message is coming from file
261 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
262 adios (NULL
, "out of memory");
265 if ((ct
= parse_mime (file
)))
269 * message(s) are coming from a folder
272 app_msgarg(&msgs
, "cur");
274 folder
= getfolder (1);
275 maildir
= m_maildir (folder
);
277 if (chdir (maildir
) == NOTOK
)
278 adios (maildir
, "unable to change directory to");
280 /* read folder and create message structure */
281 if (!(mp
= folder_read (folder
)))
282 adios (NULL
, "unable to read folder %s", folder
);
284 /* check for empty folder */
286 adios (NULL
, "no messages in %s", folder
);
288 /* parse all the message ranges/sequences and set SELECTED */
289 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
290 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
292 seq_setprev (mp
); /* set the previous-sequence */
294 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
295 adios (NULL
, "out of memory");
298 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
299 if (is_selected(mp
, msgnum
)) {
302 msgnam
= m_name (msgnum
);
303 if ((ct
= parse_mime (msgnam
)))
313 SIGNAL (SIGQUIT
, quitser
);
314 SIGNAL (SIGPIPE
, pipeser
);
317 * Get the associated umask for the relevant contents.
319 for (ctp
= cts
; *ctp
; ctp
++) {
323 if (type_ok (ct
, 1) && !ct
->c_umask
) {
324 if (stat (ct
->c_file
, &st
) != NOTOK
)
325 ct
->c_umask
= ~(st
.st_mode
& 0777);
327 ct
->c_umask
= ~m_gmprot();
332 * List the message content
334 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
336 /* Now free all the structures for the content */
337 for (ctp
= cts
; *ctp
; ctp
++)
343 /* If reading from a folder, do some updating */
345 context_replace (pfolder
, folder
);/* update current folder */
346 seq_setcur (mp
, mp
->hghsel
); /* update current message */
347 seq_save (mp
); /* synchronize sequences */
348 context_save (); /* save the context file */
361 fprintf (stderr
, "\n");