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