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