Renamed all standard sequences (e.g. cur->c) and made them globally changeable
[mmh] / uip / mhlist.c
1 /*
2 ** mhlist.c -- list 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 HEADSW  2
32         { "headers", 0 },
33 #define NHEADSW  3
34         { "noheaders", 0 },
35 #define SIZESW  4
36         { "realsize", 0 },
37 #define NSIZESW  5
38         { "norealsize", 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 PARTSW  9
46         { "part number", 0 },
47 #define TYPESW  10
48         { "type content", 0 },
49 #define RCACHESW  11
50         { "rcache policy", 0 },
51 #define WCACHESW  12
52         { "wcache policy", 0 },
53 #define VERSIONSW  13
54         { "version", 0 },
55 #define HELPSW  14
56         { "help", 0 },
57
58 /*
59 ** switches for debugging
60 */
61 #define DEBUGSW  15
62         { "debug", -5 },
63         { NULL, 0 }
64 };
65
66
67 /* mhparse.c */
68 extern char *tmp;  /* directory to place temp files */
69
70 /* mhcachesbr.c */
71 extern int rcachesw;
72 extern int wcachesw;
73 extern char *cache_public;
74 extern char *cache_private;
75
76 /* mhmisc.c */
77 extern int npart;
78 extern int ntype;
79 extern char *parts[NPARTS + 1];
80 extern char *types[NTYPES + 1];
81 extern int userrs;
82
83 /*
84 ** This is currently needed to keep mhparse happy.
85 ** This needs to be changed.
86 */
87 pid_t xpid  = 0;
88
89 int debugsw = 0;
90 int verbosw = 0;
91
92 #define quitser pipeser
93
94 /* mhparse.c */
95 CT parse_mime(char *);
96
97 /* mhmisc.c */
98 int part_ok(CT, int);
99 int type_ok(CT, int);
100 void set_endian(void);
101 void flush_errors(void);
102
103 /* mhlistsbr.c */
104 void list_all_messages(CT *, int, int, int, int);
105
106 /* mhfree.c */
107 void free_content(CT);
108 extern CT *cts;
109 void freects_done(int) NORETURN;
110
111 /*
112 ** static prototypes
113 */
114 static RETSIGTYPE pipeser(int);
115
116
117 int
118 main(int argc, char **argv)
119 {
120         int sizesw = 1, headsw = 1;
121         int msgnum, *icachesw;
122         char *cp, *file = NULL, *folder = NULL;
123         char *maildir, buf[100], **argp;
124         char **arguments;
125         struct msgs_array msgs = { 0, 0, NULL };
126         struct msgs *mp = NULL;
127         CT ct, *ctp;
128
129         done=freects_done;
130
131 #ifdef LOCALE
132         setlocale(LC_ALL, "");
133 #endif
134         invo_name = mhbasename(argv[0]);
135
136         /* read user profile/context */
137         context_read();
138
139         arguments = getarguments(invo_name, argc, argv, 1);
140         argp = arguments;
141
142         /*
143         ** Parse arguments
144         */
145         while ((cp = *argp++)) {
146                 if (*cp == '-') {
147                         switch (smatch(++cp, switches)) {
148                         case AMBIGSW:
149                                 ambigsw(cp, switches);
150                                 done(1);
151                         case UNKWNSW:
152                                 adios(NULL, "-%s unknown", cp);
153
154                         case HELPSW:
155                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
156                                 print_help(buf, switches, 1);
157                                 done(1);
158                         case VERSIONSW:
159                                 print_version(invo_name);
160                                 done(1);
161
162                         case RCACHESW:
163                                 icachesw = &rcachesw;
164                                 goto do_cache;
165                         case WCACHESW:
166                                 icachesw = &wcachesw;
167 do_cache:
168                                 if (!(cp = *argp++) || *cp == '-')
169                                         adios(NULL, "missing argument to %s", argp[-2]);
170                                 switch (*icachesw = smatch(cp, caches)) {
171                                 case AMBIGSW:
172                                         ambigsw(cp, caches);
173                                         done(1);
174                                 case UNKWNSW:
175                                         adios(NULL, "%s unknown", cp);
176                                 default:
177                                         break;
178                                 }
179                                 continue;
180
181                         case CHECKSW:
182                                 checksw++;
183                                 continue;
184                         case NCHECKSW:
185                                 checksw = 0;
186                                 continue;
187
188                         case HEADSW:
189                                 headsw = 1;
190                                 continue;
191                         case NHEADSW:
192                                 headsw = 0;
193                                 continue;
194
195                         case SIZESW:
196                                 sizesw = 1;
197                                 continue;
198                         case NSIZESW:
199                                 sizesw = 0;
200                                 continue;
201
202                         case PARTSW:
203                                 if (!(cp = *argp++) || *cp == '-')
204                                         adios(NULL, "missing argument to %s",
205                                                         argp[-2]);
206                                 if (npart >= NPARTS)
207                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
208                                 parts[npart++] = cp;
209                                 continue;
210
211                         case TYPESW:
212                                 if (!(cp = *argp++) || *cp == '-')
213                                         adios(NULL, "missing argument to %s",
214                                                         argp[-2]);
215                                 if (ntype >= NTYPES)
216                                         adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
217                                 types[ntype++] = cp;
218                                 continue;
219
220                         case FILESW:
221                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
222                                         adios(NULL, "missing argument to %s",
223                                                         argp[-2]);
224                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
225                                 continue;
226
227                         case VERBSW:
228                                 verbosw = 1;
229                                 continue;
230                         case NVERBSW:
231                                 verbosw = 0;
232                                 continue;
233                         case DEBUGSW:
234                                 debugsw = 1;
235                                 continue;
236                         }
237                 }
238                 if (*cp == '+' || *cp == '@') {
239                         if (folder)
240                                 adios(NULL, "only one folder at a time!");
241                         else
242                                 folder = getcpy(expandfol(cp));
243                 } else
244                         app_msgarg(&msgs, cp);
245         }
246
247         /* null terminate the list of acceptable parts/types */
248         parts[npart] = NULL;
249         types[ntype] = NULL;
250
251         set_endian();
252
253         /* Check for public cache location */
254         if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
255                 cache_public = NULL;
256
257         /* Check for private cache location */
258         if (!(cache_private = context_find(nmhprivcache)))
259                 cache_private = ".cache";
260         cache_private = getcpy(toabsdir(cache_private));
261
262         /*
263         ** Check for storage directory.  If specified,
264         ** then store temporary files there.  Else we
265         ** store them in standard nmh directory.
266         */
267         if ((cp = context_find(nmhstorage)) && *cp)
268                 tmp = concat(cp, "/", invo_name, NULL);
269         else
270                 tmp = getcpy(toabsdir(invo_name));
271
272         if (file && msgs.size)
273                 adios(NULL, "cannot specify msg and file at same time!");
274
275         /*
276         ** check if message is coming from file
277         */
278         if (file) {
279                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
280                         adios(NULL, "out of memory");
281                 ctp = cts;
282
283                 if ((ct = parse_mime(file)));
284                         *ctp++ = ct;
285         } else {
286                 /*
287                 ** message(s) are coming from a folder
288                 */
289                 if (!msgs.size)
290                         app_msgarg(&msgs, seq_cur);
291                 if (!folder)
292                         folder = getcurfol();
293                 maildir = toabsdir(folder);
294
295                 if (chdir(maildir) == NOTOK)
296                         adios(maildir, "unable to change directory to");
297
298                 /* read folder and create message structure */
299                 if (!(mp = folder_read(folder)))
300                         adios(NULL, "unable to read folder %s", folder);
301
302                 /* check for empty folder */
303                 if (mp->nummsg == 0)
304                         adios(NULL, "no messages in %s", folder);
305
306                 /* parse all the message ranges/sequences and set SELECTED */
307                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
308                         if (!m_convert(mp, msgs.msgs[msgnum]))
309                                 done (1);
310                 seq_setprev(mp);  /* set the previous-sequence */
311
312                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
313                                 sizeof(*cts))))
314                         adios(NULL, "out of memory");
315                 ctp = cts;
316
317                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
318                         if (is_selected(mp, msgnum)) {
319                                 char *msgnam;
320
321                                 msgnam = m_name(msgnum);
322                                 if ((ct = parse_mime(msgnam)))
323                                         *ctp++ = ct;
324                         }
325                 }
326         }
327
328         if (!*cts)
329                 done(1);
330
331         userrs = 1;
332         SIGNAL(SIGQUIT, quitser);
333         SIGNAL(SIGPIPE, pipeser);
334
335         /*
336         ** Get the associated umask for the relevant contents.
337         */
338         for (ctp = cts; *ctp; ctp++) {
339                 struct stat st;
340
341                 ct = *ctp;
342                 if (type_ok(ct, 1) && !ct->c_umask) {
343                         if (stat(ct->c_file, &st) != NOTOK)
344                                 ct->c_umask = ~(st.st_mode & 0777);
345                         else
346                                 ct->c_umask = ~m_gmprot();
347                 }
348         }
349
350         /*
351         ** List the message content
352         */
353         list_all_messages(cts, headsw, sizesw, verbosw, debugsw);
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 RETSIGTYPE
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 }