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