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