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