]> diplodocus.org Git - nmh/blob - uip/mhlist.c
Quiet lock warning.
[nmh] / uip / mhlist.c
1
2 /*
3 * mhlist.c -- list the contents of MIME messages
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/signals.h>
15 #include <h/md5.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <h/mts.h>
19 #include <h/tws.h>
20 #include <h/mime.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
23
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
26 #endif
27
28 /*
29 * We allocate space for message names (msgs array)
30 * this number of elements at a time.
31 */
32 #define MAXMSGS 256
33
34
35 static struct swit switches[] = {
36 #define CHECKSW 0
37 { "check", 0 },
38 #define NCHECKSW 1
39 { "nocheck", 0 },
40 #define HEADSW 2
41 { "headers", 0 },
42 #define NHEADSW 3
43 { "noheaders", 0 },
44 #define SIZESW 4
45 { "realsize", 0 },
46 #define NSIZESW 5
47 { "norealsize", 0 },
48 #define VERBSW 6
49 { "verbose", 0 },
50 #define NVERBSW 7
51 { "noverbose", 0 },
52 #define FILESW 8 /* interface from show */
53 { "file file", 0 },
54 #define PARTSW 9
55 { "part number", 0 },
56 #define TYPESW 10
57 { "type content", 0 },
58 #define RCACHESW 11
59 { "rcache policy", 0 },
60 #define WCACHESW 12
61 { "wcache policy", 0 },
62 #define VERSIONSW 13
63 { "version", 0 },
64 #define HELPSW 14
65 { "help", 0 },
66
67 /*
68 * switches for debugging
69 */
70 #define DEBUGSW 15
71 { "debug", -5 },
72 { NULL, 0 }
73 };
74
75
76 extern int errno;
77
78 /* mhparse.c */
79 extern int checksw;
80 extern char *tmp; /* directory to place temp files */
81
82 /* mhcachesbr.c */
83 extern int rcachesw;
84 extern int wcachesw;
85 extern char *cache_public;
86 extern char *cache_private;
87
88 /* mhmisc.c */
89 extern int npart;
90 extern int ntype;
91 extern char *parts[NPARTS + 1];
92 extern char *types[NTYPES + 1];
93 extern int userrs;
94
95 /*
96 * This is currently needed to keep mhparse happy.
97 * This needs to be changed.
98 */
99 pid_t xpid = 0;
100
101 int debugsw = 0;
102 int verbosw = 0;
103
104 /* The list of top-level contents to display */
105 CT *cts = NULL;
106
107 #define quitser pipeser
108
109 /* mhparse.c */
110 CT parse_mime (char *);
111
112 /* mhmisc.c */
113 int part_ok (CT, int);
114 int type_ok (CT, int);
115 void set_endian (void);
116 void flush_errors (void);
117
118 /* mhlistsbr.c */
119 void list_all_messages (CT *, int, int, int, int);
120
121 /* mhfree.c */
122 void free_content (CT);
123
124 /*
125 * static prototypes
126 */
127 static RETSIGTYPE pipeser (int);
128
129
130 int
131 main (int argc, char **argv)
132 {
133 int sizesw = 1, headsw = 1;
134 int nummsgs, maxmsgs, msgnum, *icachesw;
135 char *cp, *file = NULL, *folder = NULL;
136 char *maildir, buf[100], **argp;
137 char **arguments, **msgs;
138 struct msgs *mp = NULL;
139 CT ct, *ctp;
140
141 #ifdef LOCALE
142 setlocale(LC_ALL, "");
143 #endif
144 invo_name = r1bindex (argv[0], '/');
145
146 /* read user profile/context */
147 context_read();
148
149 arguments = getarguments (invo_name, argc, argv, 1);
150 argp = arguments;
151
152 /*
153 * Allocate the initial space to record message
154 * names, ranges, and sequences.
155 */
156 nummsgs = 0;
157 maxmsgs = MAXMSGS;
158 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
159 adios (NULL, "unable to allocate storage");
160
161 /*
162 * Parse arguments
163 */
164 while ((cp = *argp++)) {
165 if (*cp == '-') {
166 switch (smatch (++cp, switches)) {
167 case AMBIGSW:
168 ambigsw (cp, switches);
169 done (1);
170 case UNKWNSW:
171 adios (NULL, "-%s unknown", cp);
172
173 case HELPSW:
174 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
175 invo_name);
176 print_help (buf, switches, 1);
177 done (1);
178 case VERSIONSW:
179 print_version(invo_name);
180 done (1);
181
182 case RCACHESW:
183 icachesw = &rcachesw;
184 goto do_cache;
185 case WCACHESW:
186 icachesw = &wcachesw;
187 do_cache:
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 switch (*icachesw = smatch (cp, caches)) {
191 case AMBIGSW:
192 ambigsw (cp, caches);
193 done (1);
194 case UNKWNSW:
195 adios (NULL, "%s unknown", cp);
196 default:
197 break;
198 }
199 continue;
200
201 case CHECKSW:
202 checksw++;
203 continue;
204 case NCHECKSW:
205 checksw = 0;
206 continue;
207
208 case HEADSW:
209 headsw = 1;
210 continue;
211 case NHEADSW:
212 headsw = 0;
213 continue;
214
215 case SIZESW:
216 sizesw = 1;
217 continue;
218 case NSIZESW:
219 sizesw = 0;
220 continue;
221
222 case PARTSW:
223 if (!(cp = *argp++) || *cp == '-')
224 adios (NULL, "missing argument to %s", argp[-2]);
225 if (npart >= NPARTS)
226 adios (NULL, "too many parts (starting with %s), %d max",
227 cp, NPARTS);
228 parts[npart++] = cp;
229 continue;
230
231 case TYPESW:
232 if (!(cp = *argp++) || *cp == '-')
233 adios (NULL, "missing argument to %s", argp[-2]);
234 if (ntype >= NTYPES)
235 adios (NULL, "too many types (starting with %s), %d max",
236 cp, NTYPES);
237 types[ntype++] = cp;
238 continue;
239
240 case FILESW:
241 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
242 adios (NULL, "missing argument to %s", argp[-2]);
243 file = *cp == '-' ? cp : path (cp, TFILE);
244 continue;
245
246 case VERBSW:
247 verbosw = 1;
248 continue;
249 case NVERBSW:
250 verbosw = 0;
251 continue;
252 case DEBUGSW:
253 debugsw = 1;
254 continue;
255 }
256 }
257 if (*cp == '+' || *cp == '@') {
258 if (folder)
259 adios (NULL, "only one folder at a time!");
260 else
261 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
262 } else {
263 /*
264 * Check if we need to allocate more space
265 * for message names/ranges/sequences.
266 */
267 if (nummsgs >= maxmsgs) {
268 maxmsgs += MAXMSGS;
269 if (!(msgs = (char **) realloc (msgs,
270 (size_t) (maxmsgs * sizeof(*msgs)))))
271 adios (NULL, "unable to reallocate msgs storage");
272 }
273 msgs[nummsgs++] = cp;
274 }
275 }
276
277 /* null terminate the list of acceptable parts/types */
278 parts[npart] = NULL;
279 types[ntype] = NULL;
280
281 set_endian ();
282
283 /* Check for public cache location */
284 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
285 cache_public = NULL;
286
287 /* Check for private cache location */
288 if (!(cache_private = context_find (nmhprivcache)))
289 cache_private = ".cache";
290 cache_private = getcpy (m_maildir (cache_private));
291
292 /*
293 * Check for storage directory. If specified,
294 * then store temporary files there. Else we
295 * store them in standard nmh directory.
296 */
297 if ((cp = context_find (nmhstorage)) && *cp)
298 tmp = concat (cp, "/", invo_name, NULL);
299 else
300 tmp = add (m_maildir (invo_name), NULL);
301
302 if (!context_find ("path"))
303 free (path ("./", TFOLDER));
304
305 if (file && nummsgs)
306 adios (NULL, "cannot specify msg and file at same time!");
307
308 /*
309 * check if message is coming from file
310 */
311 if (file) {
312 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
313 adios (NULL, "out of memory");
314 ctp = cts;
315
316 if ((ct = parse_mime (file)));
317 *ctp++ = ct;
318 } else {
319 /*
320 * message(s) are coming from a folder
321 */
322 if (!nummsgs)
323 msgs[nummsgs++] = "cur";
324 if (!folder)
325 folder = getfolder (1);
326 maildir = m_maildir (folder);
327
328 if (chdir (maildir) == NOTOK)
329 adios (maildir, "unable to change directory to");
330
331 /* read folder and create message structure */
332 if (!(mp = folder_read (folder)))
333 adios (NULL, "unable to read folder %s", folder);
334
335 /* check for empty folder */
336 if (mp->nummsg == 0)
337 adios (NULL, "no messages in %s", folder);
338
339 /* parse all the message ranges/sequences and set SELECTED */
340 for (msgnum = 0; msgnum < nummsgs; msgnum++)
341 if (!m_convert (mp, msgs[msgnum]))
342 done (1);
343 seq_setprev (mp); /* set the previous-sequence */
344
345 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
346 adios (NULL, "out of memory");
347 ctp = cts;
348
349 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
350 if (is_selected(mp, msgnum)) {
351 char *msgnam;
352
353 msgnam = m_name (msgnum);
354 if ((ct = parse_mime (msgnam)))
355 *ctp++ = ct;
356 }
357 }
358 }
359
360 if (!*cts)
361 done (1);
362
363 userrs = 1;
364 SIGNAL (SIGQUIT, quitser);
365 SIGNAL (SIGPIPE, pipeser);
366
367 /*
368 * Get the associated umask for the relevant contents.
369 */
370 for (ctp = cts; *ctp; ctp++) {
371 struct stat st;
372
373 ct = *ctp;
374 if (type_ok (ct, 1) && !ct->c_umask) {
375 if (stat (ct->c_file, &st) != NOTOK)
376 ct->c_umask = ~(st.st_mode & 0777);
377 else
378 ct->c_umask = ~m_gmprot();
379 }
380 }
381
382 /*
383 * List the message content
384 */
385 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
386
387 /* Now free all the structures for the content */
388 for (ctp = cts; *ctp; ctp++)
389 free_content (*ctp);
390
391 free ((char *) cts);
392 cts = NULL;
393
394 /* If reading from a folder, do some updating */
395 if (mp) {
396 context_replace (pfolder, folder);/* update current folder */
397 seq_setcur (mp, mp->hghsel); /* update current message */
398 seq_save (mp); /* synchronize sequences */
399 context_save (); /* save the context file */
400 }
401
402 return done (0);
403 }
404
405
406 static RETSIGTYPE
407 pipeser (int i)
408 {
409 if (i == SIGQUIT) {
410 unlink ("core");
411 fflush (stdout);
412 fprintf (stderr, "\n");
413 fflush (stderr);
414 }
415
416 done (1);
417 /* NOTREACHED */
418 }
419
420
421 int
422 done (int status)
423 {
424 CT *ctp;
425
426 if ((ctp = cts))
427 for (; *ctp; ctp++)
428 free_content (*ctp);
429
430 exit (status);
431 return 1; /* dead code to satisfy the compiler */
432 }