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