Renamed all standard sequences (e.g. cur->c) and made them globally changeable
[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 = mhbasename(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 : getcpy(expanddir(cp));
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 = getcpy(expandfol(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(toabsdir(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 = getcpy(toabsdir(invo_name));
342
343         if (file && msgs.size)
344                 adios(NULL, "cannot specify msg and file at same time!");
345
346         /*
347         ** check if message is coming from file
348         */
349         if (file) {
350                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
351                         adios(NULL, "out of memory");
352                 ctp = cts;
353
354                 if ((ct = parse_mime(file)));
355                         *ctp++ = ct;
356         } else {
357                 /*
358                 ** message(s) are coming from a folder
359                 */
360                 if (!msgs.size)
361                         app_msgarg(&msgs, seq_cur);
362                 if (!folder)
363                         folder = getcurfol();
364                 maildir = toabsdir(folder);
365
366                 if (chdir(maildir) == NOTOK)
367                         adios(maildir, "unable to change directory to");
368
369                 /* read folder and create message structure */
370                 if (!(mp = folder_read(folder)))
371                         adios(NULL, "unable to read folder %s", folder);
372
373                 /* check for empty folder */
374                 if (mp->nummsg == 0)
375                         adios(NULL, "no messages in %s", folder);
376
377                 /* parse all the message ranges/sequences and set SELECTED */
378                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
379                         if (!m_convert(mp, msgs.msgs[msgnum]))
380                                 done(1);
381
382                 /*
383                 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
384                 ** since we will use that as a tag to know which messages
385                 ** to remove from the "unseen" sequence.
386                 */
387                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
388                         if (is_selected(mp, msgnum))
389                                 set_unseen(mp, msgnum);
390
391                 seq_setprev(mp);  /* set the Previous-Sequence */
392                 seq_setunseen(mp, 0);  /* unset unseen seqs for shown msgs */
393
394                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
395                                 sizeof(*cts))))
396                         adios(NULL, "out of memory");
397                 ctp = cts;
398
399                 /*
400                 ** Parse all the SELECTED messages.
401                 */
402                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
403                         if (is_selected(mp, msgnum)) {
404                                 char *msgnam;
405
406                                 msgnam = m_name(msgnum);
407                                 if ((ct = parse_mime(msgnam)))
408                                         *ctp++ = ct;
409                         }
410                 }
411         }
412
413         if (!*cts)
414                 done(1);
415
416         userrs = 1;
417         SIGNAL(SIGQUIT, quitser);
418         SIGNAL(SIGPIPE, pipeser);
419
420         /*
421         ** Get the associated umask for the relevant contents.
422         */
423         for (ctp = cts; *ctp; ctp++) {
424                 struct stat st;
425
426                 ct = *ctp;
427                 if (type_ok(ct, 1) && !ct->c_umask) {
428                         if (stat(ct->c_file, &st) != NOTOK)
429                                 ct->c_umask = ~(st.st_mode & 0777);
430                         else
431                                 ct->c_umask = ~m_gmprot();
432                 }
433         }
434
435         /*
436         ** Show the message content
437         */
438         show_all_messages(cts);
439
440         /* Now free all the structures for the content */
441         for (ctp = cts; *ctp; ctp++)
442                 free_content(*ctp);
443
444         free((char *) cts);
445         cts = NULL;
446
447         /* If reading from a folder, do some updating */
448         if (mp) {
449                 context_replace(curfolder, folder); /* update current folder */
450                 seq_setcur(mp, mp->hghsel);        /* update current message */
451                 seq_save(mp);                      /* synchronize sequences */
452                 context_save();                    /* save the context file */
453         }
454
455         done(0);
456         return 1;
457 }
458
459
460 static RETSIGTYPE
461 pipeser(int i)
462 {
463         if (i == SIGQUIT) {
464                 unlink("core");
465                 fflush(stdout);
466                 fprintf(stderr, "\n");
467                 fflush(stderr);
468         }
469
470         done(1);
471         /* NOTREACHED */
472 }