Remove tests/inc/test-eom-align
[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 char *version=VERSION;
46
47 /* mhparse.c */
48 extern char *tmp;  /* directory to place temp files */
49
50 /* mhshowsbr.c */
51 extern int nolist;
52 extern char *formsw;
53
54 /* mhmisc.c */
55 extern int npart;
56 extern int ntype;
57 extern char *parts[NPARTS + 1];
58 extern char *types[NTYPES + 1];
59 extern int userrs;
60
61 static enum { SHOW, NEXT, PREV } mode = SHOW;
62
63 int debugsw = 0;
64 int verbosw = 0;
65
66 #define quitser pipeser
67
68 /* mhparse.c */
69 CT parse_mime(char *);
70
71 /* mhmisc.c */
72 int part_ok(CT, int);
73 int type_ok(CT, int);
74 void set_endian(void);
75 void flush_errors(void);
76
77 /* mhshowsbr.c */
78 void show_all_messages(CT *);
79
80 /* mhfree.c */
81 void free_content(CT);
82 extern CT *cts;
83 void freects_done();
84
85 /*
86 ** static prototypes
87 */
88 static void pipeser(int);
89 static void m_popen(char *);
90 static void m_pclose(void);
91
92
93 int
94 main(int argc, char **argv)
95 {
96         int msgnum;
97         char *cp, *file = NULL, *folder = NULL;
98         char *maildir, buf[100], **argp;
99         char **arguments;
100         struct msgs_array msgs = { 0, 0, NULL };
101         struct msgs *mp = NULL;
102         CT ct, *ctp;
103         FILE *fp;
104         int ontty = 0;
105
106         if (atexit(freects_done) != 0) {
107                 adios(EX_OSERR, NULL, "atexit failed");
108         }
109
110         setlocale(LC_ALL, "");
111         invo_name = mhbasename(argv[0]);
112         if (mh_strcasecmp(invo_name, "next")==0) {
113                 mode = NEXT;
114         } else if (mh_strcasecmp(invo_name, "prev")==0) {
115                 mode = PREV;
116         }
117
118         /* read user profile/context */
119         context_read();
120
121         arguments = getarguments(invo_name, argc, argv, 1);
122         argp = arguments;
123
124         /*
125         ** Parse arguments
126         */
127         while ((cp = *argp++)) {
128                 if (*cp == '-') {
129                         switch (smatch(++cp, switches)) {
130                         case AMBIGSW:
131                                 ambigsw(cp, switches);
132                                 exit(EX_USAGE);
133                         case UNKWNSW:
134                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
135
136                         case HELPSW:
137                                 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches]", invo_name, mode==SHOW ? "[msgs] " : "");
138                                 print_help(buf, switches, 1);
139                                 exit(argc == 2 ? EX_OK : EX_USAGE);
140                         case VERSIONSW:
141                                 print_version(invo_name);
142                                 exit(argc == 2 ? EX_OK : EX_USAGE);
143
144                         case PARTSW:
145                                 if (!(cp = *argp++) || *cp == '-')
146                                         adios(EX_USAGE, NULL, "missing argument to %s",
147                                                         argp[-2]);
148                                 if (npart >= NPARTS)
149                                         adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
150                                 parts[npart++] = cp;
151                                 continue;
152
153                         case TYPESW:
154                                 if (!(cp = *argp++) || *cp == '-')
155                                         adios(EX_USAGE, NULL, "missing argument to %s",
156                                                         argp[-2]);
157                                 if (ntype >= NTYPES)
158                                         adios(EX_USAGE, NULL, "too many types (starting with %s), %d max", cp, NTYPES);
159                                 types[ntype++] = cp;
160                                 continue;
161
162                         case FILESW:
163                                 if (mode != SHOW) {
164                                         adios(EX_USAGE, NULL, "Either call show as `%s' or use -file", invo_name);
165                                 }
166
167                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
168                                         adios(EX_USAGE, NULL, "missing argument to %s",
169                                                         argp[-2]);
170                                 file = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
171                                 continue;
172
173                         case FORMSW:
174                                 if (!(cp = *argp++) || *cp == '-')
175                                         adios(EX_USAGE, NULL, "missing argument to %s",
176                                                         argp[-2]);
177                                 if (formsw)
178                                         mh_free0(&formsw);
179                                 formsw = mh_xstrdup(etcpath(cp));
180                                 continue;
181
182                         case VERBSW:
183                                 verbosw = 1;
184                                 continue;
185                         case NVERBSW:
186                                 verbosw = 0;
187                                 continue;
188                         case DEBUGSW:
189                                 debugsw = 1;
190                                 continue;
191                         }
192                 }
193                 if (*cp == '+' || *cp == '@') {
194                         if (folder)
195                                 adios(EX_USAGE, NULL, "only one folder at a time!");
196                         else
197                                 folder = mh_xstrdup(expandfol(cp));
198                 } else if (mode != SHOW) {
199                         adios(EX_USAGE, NULL, "Either call show as `%s' or give message arguments", invo_name);
200                 } else {
201                         app_msgarg(&msgs, cp);
202                 }
203         }
204
205         /* null terminate the list of acceptable parts/types */
206         parts[npart] = NULL;
207         types[ntype] = NULL;
208
209         set_endian();
210
211         if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0) {
212                 nolist  = 1;
213         }
214
215         /*
216         ** Check if we've specified an additional profile
217         */
218         if ((cp = getenv("MHSHOW"))) {
219                 if ((fp = fopen(cp, "r"))) {
220                         readconfig((struct node **) 0, fp, cp, 0);
221                         fclose(fp);
222                 } else {
223                         admonish("", "unable to read $MHSHOW profile (%s)",
224                                         cp);
225                 }
226         }
227
228         /*
229         ** Read the standard profile setup
230         */
231         if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
232                 readconfig((struct node **) 0, fp, cp, 0);
233                 fclose(fp);
234         }
235
236         /*
237         ** Check for storage directory.  If specified,
238         ** then store temporary files there.  Else we
239         ** store them in standard nmh directory.
240         */
241         if ((cp = context_find(nmhstorage)) && *cp)
242                 tmp = concat(cp, "/", invo_name, NULL);
243         else
244                 tmp = mh_xstrdup(toabsdir(invo_name));
245
246         if (file && msgs.size)
247                 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
248
249         /*
250         ** check if message is coming from file
251         */
252         if (file) {
253                 cts = mh_xcalloc(2, sizeof(*cts));
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                 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
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(EX_SOFTWARE);
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         mh_free0(&cts);
364
365         /* If reading from a folder, do some updating */
366         if (mp) {
367                 context_replace(curfolder, folder); /* update current folder */
368                 seq_setcur(mp, mp->hghsel);        /* update current message */
369                 seq_save(mp);                      /* synchronize sequences */
370                 context_save();                    /* save the context file */
371         }
372
373         return 0;
374 }
375
376
377 static void
378 pipeser(int i)
379 {
380         if (i == SIGQUIT) {
381                 unlink("core");
382                 fflush(stdout);
383                 fprintf(stderr, "\n");
384                 fflush(stderr);
385         }
386
387         _exit(EX_IOERR);
388         /* NOTREACHED */
389 }
390
391
392 static int m_pid = NOTOK;
393 static int sd = NOTOK;
394
395
396 static void
397 m_popen(char *name)
398 {
399         int pd[2];
400
401         if ((sd = dup(fileno(stdout))) == NOTOK)
402                 adios(EX_OSERR, "standard output", "unable to dup()");
403
404         if (pipe(pd) == NOTOK)
405                 adios(EX_OSERR, "pipe", "unable to");
406
407         switch (m_pid = fork()) {
408         case NOTOK:
409                 adios(EX_OSERR, "fork", "unable to");
410
411         case OK:
412                 SIGNAL(SIGINT, SIG_DFL);
413                 SIGNAL(SIGQUIT, SIG_DFL);
414
415                 close(pd[1]);
416                 if (pd[0] != fileno(stdin)) {
417                         dup2(pd[0], fileno(stdin));
418                         close(pd[0]);
419                 }
420                 execlp(name, mhbasename(name), NULL);
421                 fprintf(stderr, "unable to exec ");
422                 perror(name);
423                 _exit(EX_OSERR);
424
425         default:
426                 close(pd[0]);
427                 if (pd[1] != fileno(stdout)) {
428                         dup2(pd[1], fileno(stdout));
429                         close(pd[1]);
430                 }
431         }
432 }
433
434
435 void
436 m_pclose(void)
437 {
438         if (m_pid == NOTOK)
439                 return;
440
441         if (sd != NOTOK) {
442                 fflush(stdout);
443                 if (dup2(sd, fileno(stdout)) == NOTOK)
444                         adios(EX_OSERR, "standard output", "unable to dup2()");
445
446                 clearerr(stdout);
447                 close(sd);
448                 sd = NOTOK;
449         } else
450                 fclose(stdout);
451
452         pidwait(m_pid, OK);
453         m_pid = NOTOK;
454 }