Change link-time overriding of done() to run-time reassigning of equivalent
[mmh] / uip / mhn.c
1
2 /*
3  * mhn.c -- display, list, cache, or store 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 AUTOSW                  0
31     { "auto", 0 },
32 #define NAUTOSW                 1
33     { "noauto", 0 },
34 #define CACHESW                 2
35     { "cache", 0 },
36 #define NCACHESW                3
37     { "nocache", 0 },
38 #define CHECKSW                 4
39     { "check", 0 },
40 #define NCHECKSW                5
41     { "nocheck", 0 },
42 #define HEADSW                  6
43     { "headers", 0 },
44 #define NHEADSW                 7
45     { "noheaders", 0 },
46 #define LISTSW                  8
47     { "list", 0 },
48 #define NLISTSW                 9
49     { "nolist", 0 },
50 #define PAUSESW                10
51     { "pause", 0 },
52 #define NPAUSESW               11
53     { "nopause", 0 },
54 #define SIZESW                 12
55     { "realsize", 0 },
56 #define NSIZESW                13
57     { "norealsize", 0 },
58 #define SERIALSW               14
59     { "serialonly", 0 },
60 #define NSERIALSW              15
61     { "noserialonly", 0 },
62 #define SHOWSW                 16
63     { "show", 0 },
64 #define NSHOWSW                17
65     { "noshow", 0 },
66 #define STORESW                18
67     { "store", 0 },
68 #define NSTORESW               19
69     { "nostore", 0 },
70 #define VERBSW                 20
71     { "verbose", 0 },
72 #define NVERBSW                21
73     { "noverbose", 0 },
74 #define FILESW                 22       /* interface from show */
75     { "file file", 0 },
76 #define FORMSW                 23
77     { "form formfile", 0 },
78 #define PARTSW                 24
79     { "part number", 0 },
80 #define TYPESW                 25
81     { "type content", 0 },
82 #define RCACHESW               26
83     { "rcache policy", 0 },
84 #define WCACHESW               27
85     { "wcache policy", 0 },
86 #define VERSIONSW              28
87     { "version", 0 },
88 #define HELPSW                 29
89     { "help", 0 },
90
91 /*
92  * switches for debugging
93  */
94 #define DEBUGSW                30
95     { "debug", -5 },
96
97 /*
98  * switches for moreproc/mhlproc
99  */
100 #define PROGSW                 31
101     { "moreproc program", -4 },
102 #define NPROGSW                32
103     { "nomoreproc", -3 },
104 #define LENSW                  33
105     { "length lines", -4 },
106 #define WIDTHSW                34
107     { "width columns", -4 },
108
109 /*
110  * switches for mhbuild
111  */
112 #define BUILDSW                35
113     { "build", -5 },
114 #define NBUILDSW               36
115     { "nobuild", -7 },
116 #define EBCDICSW               37
117     { "ebcdicsafe", -10 },
118 #define NEBCDICSW              38
119     { "noebcdicsafe", -12 },
120 #define RFC934SW               39
121     { "rfc934mode", -10 },
122 #define NRFC934SW              40
123     { "norfc934mode", -12 },
124     { NULL, 0 }
125 };
126
127
128 /* mhparse.c */
129 extern int checksw;
130 extern char *tmp;       /* directory to place temp files */
131
132 /* mhcachesbr.c */
133 extern int rcachesw;
134 extern int wcachesw;
135 extern char *cache_public;
136 extern char *cache_private;
137
138 /* mhshowsbr.c */
139 extern int pausesw;
140 extern int serialsw;
141 extern char *progsw;
142 extern int nolist;
143 extern int nomore;      /* flags for moreproc/header display */
144 extern char *formsw;
145
146 /* mhstoresbr.c */
147 extern int autosw;
148 extern char *cwd;       /* cache current working directory */
149
150 /* mhmisc.c */
151 extern int npart;
152 extern int ntype;
153 extern char *parts[NPARTS + 1];
154 extern char *types[NTYPES + 1];
155 extern int userrs;
156
157 int debugsw = 0;
158 int verbosw = 0;
159
160 /* The list of top-level contents to display */
161 CT *cts = NULL;
162
163 /*
164  * variables for mhbuild (mhn -build)
165  */
166 static int buildsw  = 0;
167 static int ebcdicsw = 0;
168 static int rfc934sw = 0;
169
170 /*
171  * what action to take?
172  */
173 static int cachesw = 0;
174 static int listsw  = 0;
175 static int showsw  = 0;
176 static int storesw = 0;
177
178 #define quitser pipeser
179
180 /* mhparse.c */
181 CT parse_mime (char *);
182
183 /* mhmisc.c */
184 int part_ok (CT, int);
185 int type_ok (CT, int);
186 void set_endian (void);
187 void flush_errors (void);
188
189 /* mhshowsbr.c */
190 void show_all_messages (CT *);
191
192 /* mhlistsbr.c */
193 void list_all_messages (CT *, int, int, int, int);
194
195 /* mhstoresbr.c */
196 void store_all_messages (CT *);
197
198 /* mhcachesbr.c */
199 void cache_all_messages (CT *);
200
201 /* mhfree.c */
202 void free_content (CT);
203
204 /*
205  * static prototypes
206  */
207 static RETSIGTYPE pipeser (int);
208 static int freectp_done (int);
209
210
211 int
212 main (int argc, char **argv)
213 {
214     int sizesw = 1, headsw = 1;
215     int msgnum, *icachesw;
216     char *cp, *file = NULL, *folder = NULL;
217     char *maildir, buf[100], **argp;
218     char **arguments;
219     struct msgs_array msgs = { 0, 0, NULL };
220     struct msgs *mp = NULL;
221     CT ct, *ctp;
222     FILE *fp;
223
224     done=freectp_done;
225
226 #ifdef LOCALE
227     setlocale(LC_ALL, "");
228 #endif
229     invo_name = r1bindex (argv[0], '/');
230
231     /* read user profile/context */
232     context_read();
233
234     arguments = getarguments (invo_name, argc, argv, 1);
235     argp = arguments;
236
237     /*
238      * Parse arguments
239      */
240     while ((cp = *argp++)) {
241         if (*cp == '-') {
242             switch (smatch (++cp, switches)) {
243             case AMBIGSW: 
244                 ambigsw (cp, switches);
245                 done (1);
246             case UNKWNSW: 
247                 adios (NULL, "-%s unknown", cp);
248
249             case HELPSW: 
250                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
251                         invo_name);
252                 print_help (buf, switches, 1);
253                 done (1);
254             case VERSIONSW:
255                 print_version(invo_name);
256                 done (1);
257
258             case AUTOSW:
259                 autosw++;
260                 continue;
261             case NAUTOSW:
262                 autosw = 0;
263                 continue;
264
265             case CACHESW:
266                 cachesw++;
267                 continue;
268             case NCACHESW:
269                 cachesw = 0;
270                 continue;
271
272             case RCACHESW:
273                 icachesw = &rcachesw;
274                 goto do_cache;
275             case WCACHESW:
276                 icachesw = &wcachesw;
277 do_cache:
278                 if (!(cp = *argp++) || *cp == '-')
279                     adios (NULL, "missing argument to %s", argp[-2]);
280                 switch (*icachesw = smatch (cp, caches)) {
281                 case AMBIGSW:
282                     ambigsw (cp, caches);
283                     done (1);
284                 case UNKWNSW:
285                     adios (NULL, "%s unknown", cp);
286                 default:
287                     break;
288                 }
289                 continue;
290
291             case CHECKSW:
292                 checksw++;
293                 continue;
294             case NCHECKSW:
295                 checksw = 0;
296                 continue;
297
298             case HEADSW:
299                 headsw = 1;
300                 continue;
301             case NHEADSW:
302                 headsw = 0;
303                 continue;
304
305             case LISTSW:
306                 listsw = 1;
307                 continue;
308             case NLISTSW:
309                 listsw = 0;
310                 continue;
311
312             case PAUSESW:
313                 pausesw = 1;
314                 continue;
315             case NPAUSESW:
316                 pausesw = 0;
317                 continue;
318
319             case SERIALSW:
320                 serialsw = 1;
321                 continue;
322             case NSERIALSW:
323                 serialsw = 0;
324                 continue;
325
326             case SHOWSW:
327                 showsw = 1;
328                 continue;
329             case NSHOWSW:
330                 showsw = 0;
331                 continue;
332
333             case SIZESW:
334                 sizesw = 1;
335                 continue;
336             case NSIZESW:
337                 sizesw = 0;
338                 continue;
339
340             case STORESW:
341                 storesw = 1;
342                 continue;
343             case NSTORESW:
344                 storesw = 0;
345                 continue;
346
347             case PARTSW:
348                 if (!(cp = *argp++) || *cp == '-')
349                     adios (NULL, "missing argument to %s", argp[-2]);
350                 if (npart >= NPARTS)
351                     adios (NULL, "too many parts (starting with %s), %d max",
352                            cp, NPARTS);
353                 parts[npart++] = cp;
354                 continue;
355
356             case TYPESW:
357                 if (!(cp = *argp++) || *cp == '-')
358                     adios (NULL, "missing argument to %s", argp[-2]);
359                 if (ntype >= NTYPES)
360                     adios (NULL, "too many types (starting with %s), %d max",
361                            cp, NTYPES);
362                 types[ntype++] = cp;
363                 continue;
364
365             case FILESW:
366                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
367                     adios (NULL, "missing argument to %s", argp[-2]);
368                 file = *cp == '-' ? cp : path (cp, TFILE);
369                 continue;
370
371             case FORMSW:
372                 if (!(cp = *argp++) || *cp == '-')
373                     adios (NULL, "missing argument to %s", argp[-2]);
374                 if (formsw)
375                     free (formsw);
376                 formsw = getcpy (etcpath (cp));
377                 continue;
378
379             /*
380              * Switches for moreproc/mhlproc
381              */
382             case PROGSW:
383                 if (!(progsw = *argp++) || *progsw == '-')
384                     adios (NULL, "missing argument to %s", argp[-2]);
385                 continue;
386             case NPROGSW:
387                 nomore++;
388                 continue;
389
390             case LENSW:
391             case WIDTHSW:
392                 if (!(cp = *argp++) || *cp == '-')
393                     adios (NULL, "missing argument to %s", argp[-2]);
394                 continue;
395
396             /*
397              * Switches for mhbuild
398              */
399             case BUILDSW:
400                 buildsw = 1;
401                 continue;
402             case NBUILDSW:
403                 buildsw = 0;
404                 continue;
405             case RFC934SW:
406                 rfc934sw = 1;
407                 continue;
408             case NRFC934SW:
409                 rfc934sw = -1;
410                 continue;
411             case EBCDICSW:
412                 ebcdicsw = 1;
413                 continue;
414             case NEBCDICSW:
415                 ebcdicsw = -1;
416                 continue;
417
418             case VERBSW: 
419                 verbosw = 1;
420                 continue;
421             case NVERBSW: 
422                 verbosw = 0;
423                 continue;
424             case DEBUGSW:
425                 debugsw = 1;
426                 continue;
427             }
428         }
429         if (*cp == '+' || *cp == '@') {
430             if (folder)
431                 adios (NULL, "only one folder at a time!");
432             else
433                 folder = pluspath (cp);
434         } else
435                 app_msgarg(&msgs, cp);
436     }
437
438     /* null terminate the list of acceptable parts/types */
439     parts[npart] = NULL;
440     types[ntype] = NULL;
441
442     set_endian ();
443
444     if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
445         nolist  = 1;
446         listsw  = 0;
447         pausesw = 0;
448     }
449
450     /*
451      * Check if we've specified an additional profile
452      */
453     if ((cp = getenv ("MHN"))) {
454         if ((fp = fopen (cp, "r"))) {
455             readconfig ((struct node **) 0, fp, cp, 0);
456             fclose (fp);
457         } else {
458             admonish ("", "unable to read $MHN profile (%s)", cp);
459         }
460     }
461
462     /*
463      * Read the standard profile setup
464      */
465     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
466         readconfig ((struct node **) 0, fp, cp, 0);
467         fclose (fp);
468     }
469
470     /* Check for public cache location */
471     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
472         cache_public = NULL;
473
474     /* Check for private cache location */
475     if (!(cache_private = context_find (nmhprivcache)))
476         cache_private = ".cache";
477     cache_private = getcpy (m_maildir (cache_private));
478
479     /*
480      * Cache the current directory before we do any chdirs()'s.
481      */
482     cwd = getcpy (pwd());
483
484     /*
485      * Check for storage directory.  If specified,
486      * then store temporary files there.  Else we
487      * store them in standard nmh directory.
488      */
489     if ((cp = context_find (nmhstorage)) && *cp)
490         tmp = concat (cp, "/", invo_name, NULL);
491     else
492         tmp = add (m_maildir (invo_name), NULL);
493
494     if (!context_find ("path"))
495         free (path ("./", TFOLDER));
496
497     /*
498      * Process a mhn composition file (mhn -build)
499      */
500     if (buildsw) {
501         char *vec[MAXARGS];
502         int vecp;
503
504         if (showsw || storesw || cachesw)
505             adios (NULL, "cannot use -build with -show, -store, -cache");
506         if (msgs.size < 1)
507             adios (NULL, "need to specify a %s composition file", invo_name);
508         if (msgs.size > 1)
509             adios (NULL, "only one %s composition file at a time", invo_name);
510
511         vecp = 0;
512         vec[vecp++] = "mhbuild";
513
514         if (ebcdicsw == 1)
515             vec[vecp++] = "-ebcdicsafe";
516         else if (ebcdicsw == -1)
517             vec[vecp++] = "-noebcdicsafe";
518
519         if (rfc934sw == 1)
520             vec[vecp++] = "-rfc934mode";
521         else if (rfc934sw == -1)
522             vec[vecp++] = "-norfc934mode";
523
524         vec[vecp++] = msgs.msgs[0];
525         vec[vecp] = NULL;
526
527         execvp ("mhbuild", vec);
528         fprintf (stderr, "unable to exec ");
529         _exit (-1);
530     }
531
532     /*
533      * Process a mhn composition file (old MH style)
534      */
535     if (msgs.size == 1 && !folder && !npart && !cachesw
536         && !showsw && !storesw && !ntype && !file
537         && (cp = getenv ("mhdraft"))
538         && strcmp (cp, msgs.msgs[0]) == 0) {
539
540         char *vec[MAXARGS];
541         int vecp;
542
543         vecp = 0;
544         vec[vecp++] = "mhbuild";
545
546         if (ebcdicsw == 1)
547             vec[vecp++] = "-ebcdicsafe";
548         else if (ebcdicsw == -1)
549             vec[vecp++] = "-noebcdicsafe";
550
551         if (rfc934sw == 1)
552             vec[vecp++] = "-rfc934mode";
553         else if (rfc934sw == -1)
554             vec[vecp++] = "-norfc934mode";
555
556         vec[vecp++] = cp;
557         vec[vecp] = NULL;
558
559         execvp ("mhbuild", vec);
560         fprintf (stderr, "unable to exec ");
561         _exit (-1);
562     }
563
564     if (file && msgs.size)
565         adios (NULL, "cannot specify msg and file at same time!");
566
567     /*
568      * check if message is coming from file
569      */
570     if (file) {
571         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
572             adios (NULL, "out of memory");
573         ctp = cts;
574
575         if ((ct = parse_mime (file)));
576             *ctp++ = ct;
577     } else {
578         /*
579          * message(s) are coming from a folder
580          */
581         if (!msgs.size)
582             app_msgarg(&msgs, "cur");
583         if (!folder)
584             folder = getfolder (1);
585         maildir = m_maildir (folder);
586
587         if (chdir (maildir) == NOTOK)
588             adios (maildir, "unable to change directory to");
589
590         /* read folder and create message structure */
591         if (!(mp = folder_read (folder)))
592             adios (NULL, "unable to read folder %s", folder);
593
594         /* check for empty folder */
595         if (mp->nummsg == 0)
596             adios (NULL, "no messages in %s", folder);
597
598         /* parse all the message ranges/sequences and set SELECTED */
599         for (msgnum = 0; msgnum < msgs.size; msgnum++)
600             if (!m_convert (mp, msgs.msgs[msgnum]))
601                 done (1);
602         seq_setprev (mp);       /* set the previous-sequence */
603
604         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
605             adios (NULL, "out of memory");
606         ctp = cts;
607
608         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
609             if (is_selected(mp, msgnum)) {
610                 char *msgnam;
611
612                 msgnam = m_name (msgnum);
613                 if ((ct = parse_mime (msgnam)))
614                     *ctp++ = ct;
615             }
616         }
617     }
618
619     if (!*cts)
620         done (1);
621
622     /*
623      * You can't give more than one of these flags
624      * at a time.
625      */
626     if (showsw + listsw + storesw + cachesw > 1)
627         adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
628
629     /* If no action is specified, assume -show */
630     if (!listsw && !showsw && !storesw && !cachesw)
631         showsw = 1;
632
633     userrs = 1;
634     SIGNAL (SIGQUIT, quitser);
635     SIGNAL (SIGPIPE, pipeser);
636
637     /*
638      * Get the associated umask for the relevant contents.
639      */
640     for (ctp = cts; *ctp; ctp++) {
641         struct stat st;
642
643         ct = *ctp;
644         if (type_ok (ct, 1) && !ct->c_umask) {
645             if (stat (ct->c_file, &st) != NOTOK)
646                 ct->c_umask = ~(st.st_mode & 0777);
647             else
648                 ct->c_umask = ~m_gmprot();
649         }
650     }
651
652     /*
653      * List the message content
654      */
655     if (listsw)
656         list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
657
658     /*
659      * Store the message content
660      */
661     if (storesw)
662         store_all_messages (cts);
663
664     /*
665      * Cache the message content
666      */
667     if (cachesw)
668         cache_all_messages (cts);
669
670     /*
671      * Show the message content
672      */
673     if (showsw)
674         show_all_messages (cts);
675
676     /* Now free all the structures for the content */
677     for (ctp = cts; *ctp; ctp++)
678         free_content (*ctp);
679
680     free ((char *) cts);
681     cts = NULL;
682
683     /* If reading from a folder, do some updating */
684     if (mp) {
685         context_replace (pfolder, folder);/* update current folder  */
686         seq_setcur (mp, mp->hghsel);      /* update current message */
687         seq_save (mp);                    /* synchronize sequences  */
688         context_save ();                  /* save the context file  */
689     }
690
691     return done (0);
692 }
693
694
695 static RETSIGTYPE
696 pipeser (int i)
697 {
698     if (i == SIGQUIT) {
699         unlink ("core");
700         fflush (stdout);
701         fprintf (stderr, "\n");
702         fflush (stderr);
703     }
704
705     done (1);
706     /* NOTREACHED */
707 }
708
709
710 static int
711 freectp_done (int status)
712 {
713     CT *ctp;
714
715     if ((ctp = cts))
716         for (; *ctp; ctp++)
717             free_content (*ctp);
718
719     exit (status);
720     return 1;  /* dead code to satisfy the compiler */
721 }