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