* patch #3966: Create a mh_xmalloc function to prevent mistakes when
[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 #include <h/utils.h>
24
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
27 #endif
28
29 /*
30  * We allocate space for message names (msgs array)
31  * this number of elements at a time.
32  */
33 #define MAXMSGS  256
34
35
36 static struct swit switches[] = {
37 #define CHECKSW                 0
38     { "check", 0 },
39 #define NCHECKSW                1
40     { "nocheck", 0 },
41 #define PAUSESW                 2
42     { "pause", 0 },
43 #define NPAUSESW                3
44     { "nopause", 0 },
45 #define SERIALSW                4
46     { "serialonly", 0 },
47 #define NSERIALSW               5
48     { "noserialonly", 0 },
49 #define VERBSW                  6
50     { "verbose", 0 },
51 #define NVERBSW                 7
52     { "noverbose", 0 },
53 #define FILESW                  8       /* interface from show */
54     { "file file", 0 },
55 #define FORMSW                  9
56     { "form formfile", 0 },
57 #define PARTSW                 10
58     { "part number", 0 },
59 #define TYPESW                 11
60     { "type content", 0 },
61 #define RCACHESW               12
62     { "rcache policy", 0 },
63 #define WCACHESW               13
64     { "wcache policy", 0 },
65 #define VERSIONSW              14
66     { "version", 0 },
67 #define HELPSW                 15
68     { "help", 0 },
69
70 /*
71  * switches for moreproc/mhlproc
72  */
73 #define PROGSW                 16
74     { "moreproc program", -4 },
75 #define NPROGSW                17
76     { "nomoreproc", -3 },
77 #define LENSW                  18
78     { "length lines", -4 },
79 #define WIDTHSW                19
80     { "width columns", -4 },
81
82 /*
83  * switches for debugging
84  */
85 #define DEBUGSW                20
86     { "debug", -5 },
87     { NULL, 0 }
88 };
89
90
91 /* mhparse.c */
92 extern int checksw;
93 extern char *tmp;       /* directory to place temp files */
94
95 /* mhcachesbr.c */
96 extern int rcachesw;
97 extern int wcachesw;
98 extern char *cache_public;
99 extern char *cache_private;
100
101 /* mhshowsbr.c */
102 extern int pausesw;
103 extern int serialsw;
104 extern char *progsw;
105 extern int nolist;
106 extern int nomore;      /* flags for moreproc/header display */
107 extern char *formsw;
108
109 /* mhmisc.c */
110 extern int npart;
111 extern int ntype;
112 extern char *parts[NPARTS + 1];
113 extern char *types[NTYPES + 1];
114 extern int userrs;
115
116 int debugsw = 0;
117 int verbosw = 0;
118
119 /* The list of top-level contents to display */
120 CT *cts = NULL;
121
122 #define quitser pipeser
123
124 /* mhparse.c */
125 CT parse_mime (char *);
126
127 /* mhmisc.c */
128 int part_ok (CT, int);
129 int type_ok (CT, int);
130 void set_endian (void);
131 void flush_errors (void);
132
133 /* mhshowsbr.c */
134 void show_all_messages (CT *);
135
136 /* mhfree.c */
137 void free_content (CT);
138
139 /*
140  * static prototypes
141  */
142 static RETSIGTYPE pipeser (int);
143
144
145 int
146 main (int argc, char **argv)
147 {
148     int nummsgs, maxmsgs, msgnum, *icachesw;
149     char *cp, *file = NULL, *folder = NULL;
150     char *maildir, buf[100], **argp;
151     char **arguments, **msgs;
152     struct msgs *mp = NULL;
153     CT ct, *ctp;
154     FILE *fp;
155
156 #ifdef LOCALE
157     setlocale(LC_ALL, "");
158 #endif
159     invo_name = r1bindex (argv[0], '/');
160
161     /* read user profile/context */
162     context_read();
163
164     arguments = getarguments (invo_name, argc, argv, 1);
165     argp = arguments;
166
167     /*
168      * Allocate the initial space to record message
169      * names, ranges, and sequences.
170      */
171     nummsgs = 0;
172     maxmsgs = MAXMSGS;
173     msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
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 }