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