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