When building on gcc, use noreturn attribute on adios and the various done
[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 /*
161  * variables for mhbuild (mhn -build)
162  */
163 static int buildsw  = 0;
164 static int ebcdicsw = 0;
165 static int rfc934sw = 0;
166
167 /*
168  * what action to take?
169  */
170 static int cachesw = 0;
171 static int listsw  = 0;
172 static int showsw  = 0;
173 static int storesw = 0;
174
175 #define quitser pipeser
176
177 /* mhparse.c */
178 CT parse_mime (char *);
179
180 /* mhmisc.c */
181 int part_ok (CT, int);
182 int type_ok (CT, int);
183 void set_endian (void);
184 void flush_errors (void);
185
186 /* mhshowsbr.c */
187 void show_all_messages (CT *);
188
189 /* mhlistsbr.c */
190 void list_all_messages (CT *, int, int, int, int);
191
192 /* mhstoresbr.c */
193 void store_all_messages (CT *);
194
195 /* mhcachesbr.c */
196 void cache_all_messages (CT *);
197
198 /* mhfree.c */
199 void free_content (CT);
200 extern CT *cts;
201 void freects_done (int) NORETURN;
202
203 /*
204  * static prototypes
205  */
206 static RETSIGTYPE pipeser (int);
207
208
209 int
210 main (int argc, char **argv)
211 {
212     int sizesw = 1, headsw = 1;
213     int msgnum, *icachesw;
214     char *cp, *file = NULL, *folder = NULL;
215     char *maildir, buf[100], **argp;
216     char **arguments;
217     struct msgs_array msgs = { 0, 0, NULL };
218     struct msgs *mp = NULL;
219     CT ct, *ctp;
220     FILE *fp;
221
222     done=freects_done;
223
224 #ifdef LOCALE
225     setlocale(LC_ALL, "");
226 #endif
227     invo_name = r1bindex (argv[0], '/');
228
229     /* read user profile/context */
230     context_read();
231
232     arguments = getarguments (invo_name, argc, argv, 1);
233     argp = arguments;
234
235     /*
236      * Parse arguments
237      */
238     while ((cp = *argp++)) {
239         if (*cp == '-') {
240             switch (smatch (++cp, switches)) {
241             case AMBIGSW: 
242                 ambigsw (cp, switches);
243                 done (1);
244             case UNKWNSW: 
245                 adios (NULL, "-%s unknown", cp);
246
247             case HELPSW: 
248                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
249                         invo_name);
250                 print_help (buf, switches, 1);
251                 done (1);
252             case VERSIONSW:
253                 print_version(invo_name);
254                 done (1);
255
256             case AUTOSW:
257                 autosw++;
258                 continue;
259             case NAUTOSW:
260                 autosw = 0;
261                 continue;
262
263             case CACHESW:
264                 cachesw++;
265                 continue;
266             case NCACHESW:
267                 cachesw = 0;
268                 continue;
269
270             case RCACHESW:
271                 icachesw = &rcachesw;
272                 goto do_cache;
273             case WCACHESW:
274                 icachesw = &wcachesw;
275 do_cache:
276                 if (!(cp = *argp++) || *cp == '-')
277                     adios (NULL, "missing argument to %s", argp[-2]);
278                 switch (*icachesw = smatch (cp, caches)) {
279                 case AMBIGSW:
280                     ambigsw (cp, caches);
281                     done (1);
282                 case UNKWNSW:
283                     adios (NULL, "%s unknown", cp);
284                 default:
285                     break;
286                 }
287                 continue;
288
289             case CHECKSW:
290                 checksw++;
291                 continue;
292             case NCHECKSW:
293                 checksw = 0;
294                 continue;
295
296             case HEADSW:
297                 headsw = 1;
298                 continue;
299             case NHEADSW:
300                 headsw = 0;
301                 continue;
302
303             case LISTSW:
304                 listsw = 1;
305                 continue;
306             case NLISTSW:
307                 listsw = 0;
308                 continue;
309
310             case PAUSESW:
311                 pausesw = 1;
312                 continue;
313             case NPAUSESW:
314                 pausesw = 0;
315                 continue;
316
317             case SERIALSW:
318                 serialsw = 1;
319                 continue;
320             case NSERIALSW:
321                 serialsw = 0;
322                 continue;
323
324             case SHOWSW:
325                 showsw = 1;
326                 continue;
327             case NSHOWSW:
328                 showsw = 0;
329                 continue;
330
331             case SIZESW:
332                 sizesw = 1;
333                 continue;
334             case NSIZESW:
335                 sizesw = 0;
336                 continue;
337
338             case STORESW:
339                 storesw = 1;
340                 continue;
341             case NSTORESW:
342                 storesw = 0;
343                 continue;
344
345             case PARTSW:
346                 if (!(cp = *argp++) || *cp == '-')
347                     adios (NULL, "missing argument to %s", argp[-2]);
348                 if (npart >= NPARTS)
349                     adios (NULL, "too many parts (starting with %s), %d max",
350                            cp, NPARTS);
351                 parts[npart++] = cp;
352                 continue;
353
354             case TYPESW:
355                 if (!(cp = *argp++) || *cp == '-')
356                     adios (NULL, "missing argument to %s", argp[-2]);
357                 if (ntype >= NTYPES)
358                     adios (NULL, "too many types (starting with %s), %d max",
359                            cp, NTYPES);
360                 types[ntype++] = cp;
361                 continue;
362
363             case FILESW:
364                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
365                     adios (NULL, "missing argument to %s", argp[-2]);
366                 file = *cp == '-' ? cp : path (cp, TFILE);
367                 continue;
368
369             case FORMSW:
370                 if (!(cp = *argp++) || *cp == '-')
371                     adios (NULL, "missing argument to %s", argp[-2]);
372                 if (formsw)
373                     free (formsw);
374                 formsw = getcpy (etcpath (cp));
375                 continue;
376
377             /*
378              * Switches for moreproc/mhlproc
379              */
380             case PROGSW:
381                 if (!(progsw = *argp++) || *progsw == '-')
382                     adios (NULL, "missing argument to %s", argp[-2]);
383                 continue;
384             case NPROGSW:
385                 nomore++;
386                 continue;
387
388             case LENSW:
389             case WIDTHSW:
390                 if (!(cp = *argp++) || *cp == '-')
391                     adios (NULL, "missing argument to %s", argp[-2]);
392                 continue;
393
394             /*
395              * Switches for mhbuild
396              */
397             case BUILDSW:
398                 buildsw = 1;
399                 continue;
400             case NBUILDSW:
401                 buildsw = 0;
402                 continue;
403             case RFC934SW:
404                 rfc934sw = 1;
405                 continue;
406             case NRFC934SW:
407                 rfc934sw = -1;
408                 continue;
409             case EBCDICSW:
410                 ebcdicsw = 1;
411                 continue;
412             case NEBCDICSW:
413                 ebcdicsw = -1;
414                 continue;
415
416             case VERBSW: 
417                 verbosw = 1;
418                 continue;
419             case NVERBSW: 
420                 verbosw = 0;
421                 continue;
422             case DEBUGSW:
423                 debugsw = 1;
424                 continue;
425             }
426         }
427         if (*cp == '+' || *cp == '@') {
428             if (folder)
429                 adios (NULL, "only one folder at a time!");
430             else
431                 folder = pluspath (cp);
432         } else
433                 app_msgarg(&msgs, cp);
434     }
435
436     /* null terminate the list of acceptable parts/types */
437     parts[npart] = NULL;
438     types[ntype] = NULL;
439
440     set_endian ();
441
442     if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
443         nolist  = 1;
444         listsw  = 0;
445         pausesw = 0;
446     }
447
448     /*
449      * Check if we've specified an additional profile
450      */
451     if ((cp = getenv ("MHN"))) {
452         if ((fp = fopen (cp, "r"))) {
453             readconfig ((struct node **) 0, fp, cp, 0);
454             fclose (fp);
455         } else {
456             admonish ("", "unable to read $MHN profile (%s)", cp);
457         }
458     }
459
460     /*
461      * Read the standard profile setup
462      */
463     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
464         readconfig ((struct node **) 0, fp, cp, 0);
465         fclose (fp);
466     }
467
468     /* Check for public cache location */
469     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
470         cache_public = NULL;
471
472     /* Check for private cache location */
473     if (!(cache_private = context_find (nmhprivcache)))
474         cache_private = ".cache";
475     cache_private = getcpy (m_maildir (cache_private));
476
477     /*
478      * Cache the current directory before we do any chdirs()'s.
479      */
480     cwd = getcpy (pwd());
481
482     /*
483      * Check for storage directory.  If specified,
484      * then store temporary files there.  Else we
485      * store them in standard nmh directory.
486      */
487     if ((cp = context_find (nmhstorage)) && *cp)
488         tmp = concat (cp, "/", invo_name, NULL);
489     else
490         tmp = add (m_maildir (invo_name), NULL);
491
492     if (!context_find ("path"))
493         free (path ("./", TFOLDER));
494
495     /*
496      * Process a mhn composition file (mhn -build)
497      */
498     if (buildsw) {
499         char *vec[MAXARGS];
500         int vecp;
501
502         if (showsw || storesw || cachesw)
503             adios (NULL, "cannot use -build with -show, -store, -cache");
504         if (msgs.size < 1)
505             adios (NULL, "need to specify a %s composition file", invo_name);
506         if (msgs.size > 1)
507             adios (NULL, "only one %s composition file at a time", invo_name);
508
509         vecp = 0;
510         vec[vecp++] = "mhbuild";
511
512         if (ebcdicsw == 1)
513             vec[vecp++] = "-ebcdicsafe";
514         else if (ebcdicsw == -1)
515             vec[vecp++] = "-noebcdicsafe";
516
517         if (rfc934sw == 1)
518             vec[vecp++] = "-rfc934mode";
519         else if (rfc934sw == -1)
520             vec[vecp++] = "-norfc934mode";
521
522         vec[vecp++] = msgs.msgs[0];
523         vec[vecp] = NULL;
524
525         execvp ("mhbuild", vec);
526         fprintf (stderr, "unable to exec ");
527         _exit (-1);
528     }
529
530     /*
531      * Process a mhn composition file (old MH style)
532      */
533     if (msgs.size == 1 && !folder && !npart && !cachesw
534         && !showsw && !storesw && !ntype && !file
535         && (cp = getenv ("mhdraft"))
536         && strcmp (cp, msgs.msgs[0]) == 0) {
537
538         char *vec[MAXARGS];
539         int vecp;
540
541         vecp = 0;
542         vec[vecp++] = "mhbuild";
543
544         if (ebcdicsw == 1)
545             vec[vecp++] = "-ebcdicsafe";
546         else if (ebcdicsw == -1)
547             vec[vecp++] = "-noebcdicsafe";
548
549         if (rfc934sw == 1)
550             vec[vecp++] = "-rfc934mode";
551         else if (rfc934sw == -1)
552             vec[vecp++] = "-norfc934mode";
553
554         vec[vecp++] = cp;
555         vec[vecp] = NULL;
556
557         execvp ("mhbuild", vec);
558         fprintf (stderr, "unable to exec ");
559         _exit (-1);
560     }
561
562     if (file && msgs.size)
563         adios (NULL, "cannot specify msg and file at same time!");
564
565     /*
566      * check if message is coming from file
567      */
568     if (file) {
569         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
570             adios (NULL, "out of memory");
571         ctp = cts;
572
573         if ((ct = parse_mime (file)));
574             *ctp++ = ct;
575     } else {
576         /*
577          * message(s) are coming from a folder
578          */
579         if (!msgs.size)
580             app_msgarg(&msgs, "cur");
581         if (!folder)
582             folder = getfolder (1);
583         maildir = m_maildir (folder);
584
585         if (chdir (maildir) == NOTOK)
586             adios (maildir, "unable to change directory to");
587
588         /* read folder and create message structure */
589         if (!(mp = folder_read (folder)))
590             adios (NULL, "unable to read folder %s", folder);
591
592         /* check for empty folder */
593         if (mp->nummsg == 0)
594             adios (NULL, "no messages in %s", folder);
595
596         /* parse all the message ranges/sequences and set SELECTED */
597         for (msgnum = 0; msgnum < msgs.size; msgnum++)
598             if (!m_convert (mp, msgs.msgs[msgnum]))
599                 done (1);
600         seq_setprev (mp);       /* set the previous-sequence */
601
602         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
603             adios (NULL, "out of memory");
604         ctp = cts;
605
606         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
607             if (is_selected(mp, msgnum)) {
608                 char *msgnam;
609
610                 msgnam = m_name (msgnum);
611                 if ((ct = parse_mime (msgnam)))
612                     *ctp++ = ct;
613             }
614         }
615     }
616
617     if (!*cts)
618         done (1);
619
620     /*
621      * You can't give more than one of these flags
622      * at a time.
623      */
624     if (showsw + listsw + storesw + cachesw > 1)
625         adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
626
627     /* If no action is specified, assume -show */
628     if (!listsw && !showsw && !storesw && !cachesw)
629         showsw = 1;
630
631     userrs = 1;
632     SIGNAL (SIGQUIT, quitser);
633     SIGNAL (SIGPIPE, pipeser);
634
635     /*
636      * Get the associated umask for the relevant contents.
637      */
638     for (ctp = cts; *ctp; ctp++) {
639         struct stat st;
640
641         ct = *ctp;
642         if (type_ok (ct, 1) && !ct->c_umask) {
643             if (stat (ct->c_file, &st) != NOTOK)
644                 ct->c_umask = ~(st.st_mode & 0777);
645             else
646                 ct->c_umask = ~m_gmprot();
647         }
648     }
649
650     /*
651      * List the message content
652      */
653     if (listsw)
654         list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
655
656     /*
657      * Store the message content
658      */
659     if (storesw)
660         store_all_messages (cts);
661
662     /*
663      * Cache the message content
664      */
665     if (cachesw)
666         cache_all_messages (cts);
667
668     /*
669      * Show the message content
670      */
671     if (showsw)
672         show_all_messages (cts);
673
674     /* Now free all the structures for the content */
675     for (ctp = cts; *ctp; ctp++)
676         free_content (*ctp);
677
678     free ((char *) cts);
679     cts = NULL;
680
681     /* If reading from a folder, do some updating */
682     if (mp) {
683         context_replace (pfolder, folder);/* update current folder  */
684         seq_setcur (mp, mp->hghsel);      /* update current message */
685         seq_save (mp);                    /* synchronize sequences  */
686         context_save ();                  /* save the context file  */
687     }
688
689     done (0);
690     return 1;
691 }
692
693
694 static RETSIGTYPE
695 pipeser (int i)
696 {
697     if (i == SIGQUIT) {
698         unlink ("core");
699         fflush (stdout);
700         fprintf (stderr, "\n");
701         fflush (stderr);
702     }
703
704     done (1);
705     /* NOTREACHED */
706 }