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