]>
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);
89 void free_content (CT
);
91 void freects_done (int) NORETURN
;
96 static void pipeser (int);
100 main (int argc
, char **argv
)
102 int sizesw
= 1, headsw
= 1;
103 int msgnum
, *icachesw
;
104 char *cp
, *file
= NULL
, *folder
= NULL
;
105 char *maildir
, buf
[100], **argp
;
107 struct msgs_array msgs
= { 0, 0, NULL
};
108 struct msgs
*mp
= NULL
;
114 setlocale(LC_ALL
, "");
116 invo_name
= r1bindex (argv
[0], '/');
118 /* read user profile/context */
121 arguments
= getarguments (invo_name
, argc
, argv
, 1);
127 while ((cp
= *argp
++)) {
129 switch (smatch (++cp
, switches
)) {
131 ambigsw (cp
, switches
);
134 adios (NULL
, "-%s unknown", cp
);
137 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
139 print_help (buf
, switches
, 1);
142 print_version(invo_name
);
146 icachesw
= &rcachesw
;
149 icachesw
= &wcachesw
;
151 if (!(cp
= *argp
++) || *cp
== '-')
152 adios (NULL
, "missing argument to %s", argp
[-2]);
153 switch (*icachesw
= smatch (cp
, caches
)) {
155 ambigsw (cp
, caches
);
158 adios (NULL
, "%s unknown", cp
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
189 adios (NULL
, "too many parts (starting with %s), %d max",
195 if (!(cp
= *argp
++) || *cp
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
198 adios (NULL
, "too many types (starting with %s), %d max",
204 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
205 adios (NULL
, "missing argument to %s", argp
[-2]);
206 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
220 if (*cp
== '+' || *cp
== '@') {
222 adios (NULL
, "only one folder at a time!");
224 folder
= pluspath (cp
);
226 app_msgarg(&msgs
, cp
);
229 /* null terminate the list of acceptable parts/types */
233 /* Check for public cache location */
234 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
237 /* Check for private cache location */
238 if (!(cache_private
= context_find (nmhprivcache
)))
239 cache_private
= ".cache";
240 cache_private
= getcpy (m_maildir (cache_private
));
243 * Check for storage directory. If specified,
244 * then store temporary files there. Else we
245 * store them in standard nmh directory.
247 if ((cp
= context_find (nmhstorage
)) && *cp
)
248 tmp
= concat (cp
, "/", invo_name
, NULL
);
250 tmp
= add (m_maildir (invo_name
), NULL
);
252 if (!context_find ("path"))
253 free (path ("./", TFOLDER
));
255 if (file
&& msgs
.size
)
256 adios (NULL
, "cannot specify msg and file at same time!");
259 * check if message is coming from file
262 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
263 adios (NULL
, "out of memory");
266 if ((ct
= parse_mime (file
)))
270 * message(s) are coming from a folder
273 app_msgarg(&msgs
, "cur");
275 folder
= getfolder (1);
276 maildir
= m_maildir (folder
);
278 if (chdir (maildir
) == NOTOK
)
279 adios (maildir
, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp
= folder_read (folder
)))
283 adios (NULL
, "unable to read folder %s", folder
);
285 /* check for empty folder */
287 adios (NULL
, "no messages in %s", folder
);
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
291 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
293 seq_setprev (mp
); /* set the previous-sequence */
295 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
296 adios (NULL
, "out of memory");
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
);
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 */
347 seq_setcur (mp
, mp
->hghsel
); /* update current message */
348 seq_save (mp
); /* synchronize sequences */
349 context_save (); /* save the context file */
363 fprintf (stderr
, "\n");