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