Factor trim format function out
[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 : mh_xstrdup(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                                         mh_free0(&formsw);
178                                 formsw = mh_xstrdup(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 = mh_xstrdup(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 = mh_xstrdup(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                 cts = mh_xcalloc(2, sizeof(*cts));
253                 ctp = cts;
254
255                 if ((ct = parse_mime(file)))
256                         *ctp++ = ct;
257         } else {
258                 /*
259                 ** message(s) are coming from a folder
260                 */
261                 if (!msgs.size) {
262                         switch (mode) {
263                         case NEXT:
264                                 app_msgarg(&msgs, seq_next);
265                                 break;
266                         case PREV:
267                                 app_msgarg(&msgs, seq_prev);
268                                 break;
269                         default:
270                                 app_msgarg(&msgs, seq_cur);
271                                 break;
272                         }
273                 }
274                 if (!folder)
275                         folder = getcurfol();
276                 maildir = toabsdir(folder);
277
278                 if (chdir(maildir) == NOTOK)
279                         adios(EX_OSERR, maildir, "unable to change directory to");
280
281                 /* read folder and create message structure */
282                 if (!(mp = folder_read(folder)))
283                         adios(EX_IOERR, NULL, "unable to read folder %s", folder);
284
285                 /* check for empty folder */
286                 if (mp->nummsg == 0)
287                         adios(EX_DATAERR, NULL, "no messages in %s", folder);
288
289                 /* parse all the message ranges/sequences and set SELECTED */
290                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
291                         if (!m_convert(mp, msgs.msgs[msgnum]))
292                                 exit(EX_USAGE);
293
294                 /*
295                 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
296                 ** since we will use that as a tag to know which messages
297                 ** to remove from the "unseen" sequence.
298                 */
299                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
300                         if (is_selected(mp, msgnum))
301                                 set_unseen(mp, msgnum);
302
303                 seq_setprev(mp);  /* set the Previous-Sequence */
304                 seq_setunseen(mp, 0);  /* unset unseen seqs for shown msgs */
305
306                 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
307                 ctp = cts;
308
309                 /*
310                 ** Parse all the SELECTED messages.
311                 */
312                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
313                         if (is_selected(mp, msgnum)) {
314                                 char *msgnam;
315
316                                 msgnam = m_name(msgnum);
317                                 if ((ct = parse_mime(msgnam)))
318                                         *ctp++ = ct;
319                         }
320                 }
321         }
322
323         if (!*cts)
324                 exit(EX_SOFTWARE);
325
326         userrs = 1;
327         SIGNAL(SIGQUIT, quitser);
328         SIGNAL(SIGPIPE, pipeser);
329
330         /*
331         ** Get the associated umask for the relevant contents.
332         */
333         for (ctp = cts; *ctp; ctp++) {
334                 struct stat st;
335
336                 ct = *ctp;
337                 if (type_ok(ct, 1) && !ct->c_umask) {
338                         if (stat(ct->c_file, &st) != NOTOK)
339                                 ct->c_umask = ~(st.st_mode & 0777);
340                         else
341                                 ct->c_umask = ~m_gmprot();
342                 }
343         }
344
345         if ((ontty = isatty(fileno(stdout)))) {
346                 m_popen(defaultpager);
347         }
348
349         /*
350         ** Show the message content
351         */
352         show_all_messages(cts);
353
354         if (ontty) {
355                 m_pclose();
356         }
357
358         /* Now free all the structures for the content */
359         for (ctp = cts; *ctp; ctp++)
360                 free_content(*ctp);
361
362         mh_free0(&cts);
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(EX_IOERR);
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(EX_OSERR, "standard output", "unable to dup()");
402
403         if (pipe(pd) == NOTOK)
404                 adios(EX_OSERR, "pipe", "unable to");
405
406         switch (m_pid = fork()) {
407         case NOTOK:
408                 adios(EX_OSERR, "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(EX_OSERR);
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(EX_OSERR, "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 }