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