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