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