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