Change link-time overriding of done() to run-time reassigning of equivalent
[mmh] / uip / mhshow.c
1
2 /*
3  * mhshow.c -- display 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 PAUSESW                 2
35     { "pause", 0 },
36 #define NPAUSESW                3
37     { "nopause", 0 },
38 #define SERIALSW                4
39     { "serialonly", 0 },
40 #define NSERIALSW               5
41     { "noserialonly", 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 FORMSW                  9
49     { "form formfile", 0 },
50 #define PARTSW                 10
51     { "part number", 0 },
52 #define TYPESW                 11
53     { "type content", 0 },
54 #define RCACHESW               12
55     { "rcache policy", 0 },
56 #define WCACHESW               13
57     { "wcache policy", 0 },
58 #define VERSIONSW              14
59     { "version", 0 },
60 #define HELPSW                 15
61     { "help", 0 },
62
63 /*
64  * switches for moreproc/mhlproc
65  */
66 #define PROGSW                 16
67     { "moreproc program", -4 },
68 #define NPROGSW                17
69     { "nomoreproc", -3 },
70 #define LENSW                  18
71     { "length lines", -4 },
72 #define WIDTHSW                19
73     { "width columns", -4 },
74
75 /*
76  * switches for debugging
77  */
78 #define DEBUGSW                20
79     { "debug", -5 },
80     { NULL, 0 }
81 };
82
83
84 /* mhparse.c */
85 extern int checksw;
86 extern char *tmp;       /* directory to place temp files */
87
88 /* mhcachesbr.c */
89 extern int rcachesw;
90 extern int wcachesw;
91 extern char *cache_public;
92 extern char *cache_private;
93
94 /* mhshowsbr.c */
95 extern int pausesw;
96 extern int serialsw;
97 extern char *progsw;
98 extern int nolist;
99 extern int nomore;      /* flags for moreproc/header display */
100 extern char *formsw;
101
102 /* mhmisc.c */
103 extern int npart;
104 extern int ntype;
105 extern char *parts[NPARTS + 1];
106 extern char *types[NTYPES + 1];
107 extern int userrs;
108
109 int debugsw = 0;
110 int verbosw = 0;
111
112 /* The list of top-level contents to display */
113 CT *cts = NULL;
114
115 #define quitser pipeser
116
117 /* mhparse.c */
118 CT parse_mime (char *);
119
120 /* mhmisc.c */
121 int part_ok (CT, int);
122 int type_ok (CT, int);
123 void set_endian (void);
124 void flush_errors (void);
125
126 /* mhshowsbr.c */
127 void show_all_messages (CT *);
128
129 /* mhfree.c */
130 void free_content (CT);
131
132 /*
133  * static prototypes
134  */
135 static RETSIGTYPE pipeser (int);
136 static int freectp_done (int);
137
138
139 int
140 main (int argc, char **argv)
141 {
142     int msgnum, *icachesw;
143     char *cp, *file = NULL, *folder = NULL;
144     char *maildir, buf[100], **argp;
145     char **arguments;
146     struct msgs_array msgs = { 0, 0, NULL };
147     struct msgs *mp = NULL;
148     CT ct, *ctp;
149     FILE *fp;
150
151     done=freectp_done;
152
153 #ifdef LOCALE
154     setlocale(LC_ALL, "");
155 #endif
156     invo_name = r1bindex (argv[0], '/');
157
158     /* read user profile/context */
159     context_read();
160
161     arguments = getarguments (invo_name, argc, argv, 1);
162     argp = arguments;
163
164     /*
165      * Parse arguments
166      */
167     while ((cp = *argp++)) {
168         if (*cp == '-') {
169             switch (smatch (++cp, switches)) {
170             case AMBIGSW: 
171                 ambigsw (cp, switches);
172                 done (1);
173             case UNKWNSW: 
174                 adios (NULL, "-%s unknown", cp);
175
176             case HELPSW: 
177                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
178                         invo_name);
179                 print_help (buf, switches, 1);
180                 done (1);
181             case VERSIONSW:
182                 print_version(invo_name);
183                 done (1);
184
185             case RCACHESW:
186                 icachesw = &rcachesw;
187                 goto do_cache;
188             case WCACHESW:
189                 icachesw = &wcachesw;
190 do_cache:
191                 if (!(cp = *argp++) || *cp == '-')
192                     adios (NULL, "missing argument to %s", argp[-2]);
193                 switch (*icachesw = smatch (cp, caches)) {
194                 case AMBIGSW:
195                     ambigsw (cp, caches);
196                     done (1);
197                 case UNKWNSW:
198                     adios (NULL, "%s unknown", cp);
199                 default:
200                     break;
201                 }
202                 continue;
203
204             case CHECKSW:
205                 checksw++;
206                 continue;
207             case NCHECKSW:
208                 checksw = 0;
209                 continue;
210
211             case PAUSESW:
212                 pausesw = 1;
213                 continue;
214             case NPAUSESW:
215                 pausesw = 0;
216                 continue;
217
218             case SERIALSW:
219                 serialsw = 1;
220                 continue;
221             case NSERIALSW:
222                 serialsw = 0;
223                 continue;
224
225             case PARTSW:
226                 if (!(cp = *argp++) || *cp == '-')
227                     adios (NULL, "missing argument to %s", argp[-2]);
228                 if (npart >= NPARTS)
229                     adios (NULL, "too many parts (starting with %s), %d max",
230                            cp, NPARTS);
231                 parts[npart++] = cp;
232                 continue;
233
234             case TYPESW:
235                 if (!(cp = *argp++) || *cp == '-')
236                     adios (NULL, "missing argument to %s", argp[-2]);
237                 if (ntype >= NTYPES)
238                     adios (NULL, "too many types (starting with %s), %d max",
239                            cp, NTYPES);
240                 types[ntype++] = cp;
241                 continue;
242
243             case FILESW:
244                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
245                     adios (NULL, "missing argument to %s", argp[-2]);
246                 file = *cp == '-' ? cp : path (cp, TFILE);
247                 continue;
248
249             case FORMSW:
250                 if (!(cp = *argp++) || *cp == '-')
251                     adios (NULL, "missing argument to %s", argp[-2]);
252                 if (formsw)
253                     free (formsw);
254                 formsw = getcpy (etcpath (cp));
255                 continue;
256
257             /*
258              * Switches for moreproc/mhlproc
259              */
260             case PROGSW:
261                 if (!(progsw = *argp++) || *progsw == '-')
262                     adios (NULL, "missing argument to %s", argp[-2]);
263                 continue;
264             case NPROGSW:
265                 nomore++;
266                 continue;
267
268             case LENSW:
269             case WIDTHSW:
270                 if (!(cp = *argp++) || *cp == '-')
271                     adios (NULL, "missing argument to %s", argp[-2]);
272                 continue;
273
274             case VERBSW: 
275                 verbosw = 1;
276                 continue;
277             case NVERBSW: 
278                 verbosw = 0;
279                 continue;
280             case DEBUGSW:
281                 debugsw = 1;
282                 continue;
283             }
284         }
285         if (*cp == '+' || *cp == '@') {
286             if (folder)
287                 adios (NULL, "only one folder at a time!");
288             else
289                 folder = pluspath (cp);
290         } else
291                 app_msgarg(&msgs, cp);
292     }
293
294     /* null terminate the list of acceptable parts/types */
295     parts[npart] = NULL;
296     types[ntype] = NULL;
297
298     set_endian ();
299
300     if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
301         nolist  = 1;
302         pausesw = 0;
303     }
304
305     /*
306      * Check if we've specified an additional profile
307      */
308     if ((cp = getenv ("MHSHOW"))) {
309         if ((fp = fopen (cp, "r"))) {
310             readconfig ((struct node **) 0, fp, cp, 0);
311             fclose (fp);
312         } else {
313             admonish ("", "unable to read $MHSHOW profile (%s)", cp);
314         }
315     }
316
317     /*
318      * Read the standard profile setup
319      */
320     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
321         readconfig ((struct node **) 0, fp, cp, 0);
322         fclose (fp);
323     }
324
325     /* Check for public cache location */
326     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
327         cache_public = NULL;
328
329     /* Check for private cache location */
330     if (!(cache_private = context_find (nmhprivcache)))
331         cache_private = ".cache";
332     cache_private = getcpy (m_maildir (cache_private));
333
334     /*
335      * Check for storage directory.  If specified,
336      * then store temporary files there.  Else we
337      * store them in standard nmh directory.
338      */
339     if ((cp = context_find (nmhstorage)) && *cp)
340         tmp = concat (cp, "/", invo_name, NULL);
341     else
342         tmp = add (m_maildir (invo_name), NULL);
343
344     if (!context_find ("path"))
345         free (path ("./", TFOLDER));
346
347     if (file && msgs.size)
348         adios (NULL, "cannot specify msg and file at same time!");
349
350     /*
351      * check if message is coming from file
352      */
353     if (file) {
354         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
355             adios (NULL, "out of memory");
356         ctp = cts;
357
358         if ((ct = parse_mime (file)));
359             *ctp++ = ct;
360     } else {
361         /*
362          * message(s) are coming from a folder
363          */
364         if (!msgs.size)
365             app_msgarg(&msgs, "cur");
366         if (!folder)
367             folder = getfolder (1);
368         maildir = m_maildir (folder);
369
370         if (chdir (maildir) == NOTOK)
371             adios (maildir, "unable to change directory to");
372
373         /* read folder and create message structure */
374         if (!(mp = folder_read (folder)))
375             adios (NULL, "unable to read folder %s", folder);
376
377         /* check for empty folder */
378         if (mp->nummsg == 0)
379             adios (NULL, "no messages in %s", folder);
380
381         /* parse all the message ranges/sequences and set SELECTED */
382         for (msgnum = 0; msgnum < msgs.size; msgnum++)
383             if (!m_convert (mp, msgs.msgs[msgnum]))
384                 done (1);
385
386         /*
387          * Set the SELECT_UNSEEN bit for all the SELECTED messages,
388          * since we will use that as a tag to know which messages
389          * to remove from the "unseen" sequence.
390          */
391         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
392             if (is_selected(mp, msgnum))
393                 set_unseen (mp, msgnum);
394
395         seq_setprev (mp);       /* set the Previous-Sequence */
396         seq_setunseen (mp, 1);  /* unset the Unseen-Sequence */
397
398         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
399             adios (NULL, "out of memory");
400         ctp = cts;
401
402         /*
403          * Parse all the SELECTED messages.
404          */
405         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
406             if (is_selected(mp, msgnum)) {
407                 char *msgnam;
408
409                 msgnam = m_name (msgnum);
410                 if ((ct = parse_mime (msgnam)))
411                     *ctp++ = ct;
412             }
413         }
414     }
415
416     if (!*cts)
417         done (1);
418
419     userrs = 1;
420     SIGNAL (SIGQUIT, quitser);
421     SIGNAL (SIGPIPE, pipeser);
422
423     /*
424      * Get the associated umask for the relevant contents.
425      */
426     for (ctp = cts; *ctp; ctp++) {
427         struct stat st;
428
429         ct = *ctp;
430         if (type_ok (ct, 1) && !ct->c_umask) {
431             if (stat (ct->c_file, &st) != NOTOK)
432                 ct->c_umask = ~(st.st_mode & 0777);
433             else
434                 ct->c_umask = ~m_gmprot();
435         }
436     }
437
438     /*
439      * Show the message content
440      */
441     show_all_messages (cts);
442
443     /* Now free all the structures for the content */
444     for (ctp = cts; *ctp; ctp++)
445         free_content (*ctp);
446
447     free ((char *) cts);
448     cts = NULL;
449
450     /* If reading from a folder, do some updating */
451     if (mp) {
452         context_replace (pfolder, folder);/* update current folder  */
453         seq_setcur (mp, mp->hghsel);      /* update current message */
454         seq_save (mp);                    /* synchronize sequences  */
455         context_save ();                  /* save the context file  */
456     }
457
458     return done (0);
459 }
460
461
462 static RETSIGTYPE
463 pipeser (int i)
464 {
465     if (i == SIGQUIT) {
466         unlink ("core");
467         fflush (stdout);
468         fprintf (stderr, "\n");
469         fflush (stderr);
470     }
471
472     done (1);
473     /* NOTREACHED */
474 }
475
476
477 static int
478 freectp_done (int status)
479 {
480     CT *ctp;
481
482     if ((ctp = cts))
483         for (; *ctp; ctp++)
484             free_content (*ctp);
485
486     exit (status);
487     return 1;  /* dead code to satisfy the compiler */
488 }