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