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