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