remove unnecessary casts
[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 <errno.h>
13 #include <signal.h>
14 #include <h/tws.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/utils.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <locale.h>
21 #include <sysexits.h>
22
23 static struct swit switches[] = {
24 #define VERBSW  0
25         { "verbose", 0 },
26 #define NVERBSW  1
27         { "noverbose", 2 },
28 #define FILESW  2  /* interface from show */
29         { "file file", 0 },
30 #define PARTSW  3
31         { "part number", 0 },
32 #define TYPESW  4
33         { "type content", 0 },
34 #define VERSIONSW  5
35         { "Version", 0 },
36 #define HELPSW  6
37         { "help", 0 },
38 #define DEBUGSW  7
39         { "debug", -5 },
40         { NULL, 0 }
41 };
42
43
44 /* mhparse.c */
45 extern char *tmp;  /* directory to place temp files */
46
47 /* mhmisc.c */
48 extern int npart;
49 extern int ntype;
50 extern char *parts[NPARTS + 1];
51 extern char *types[NTYPES + 1];
52 extern int userrs;
53
54 /*
55 ** This is currently needed to keep mhparse happy.
56 ** This needs to be changed.
57 */
58 pid_t xpid  = 0;
59
60 int debugsw = 0;
61 int verbosw = 0;
62
63 #define quitser pipeser
64
65 /* mhparse.c */
66 CT parse_mime(char *);
67
68 /* mhmisc.c */
69 int part_ok(CT, int);
70 int type_ok(CT, int);
71 void set_endian(void);
72 void flush_errors(void);
73
74 /* mhlistsbr.c */
75 void list_all_messages(CT *, int, int);
76
77 /* mhfree.c */
78 void free_content(CT);
79 extern CT *cts;
80 void freects_done();
81
82 /*
83 ** static prototypes
84 */
85 static void pipeser(int);
86
87
88 int
89 main(int argc, char **argv)
90 {
91         int msgnum;
92         char *cp, *file = NULL, *folder = NULL;
93         char *maildir, buf[100], **argp;
94         char **arguments;
95         struct msgs_array msgs = { 0, 0, NULL };
96         struct msgs *mp = NULL;
97         CT ct, *ctp;
98
99         if (atexit(freects_done) != 0) {
100                 adios(EX_OSERR, NULL, "atexit failed");
101         }
102
103         setlocale(LC_ALL, "");
104         invo_name = mhbasename(argv[0]);
105
106         /* read user profile/context */
107         context_read();
108
109         arguments = getarguments(invo_name, argc, argv, 1);
110         argp = arguments;
111
112         /*
113         ** Parse arguments
114         */
115         while ((cp = *argp++)) {
116                 if (*cp == '-') {
117                         switch (smatch(++cp, switches)) {
118                         case AMBIGSW:
119                                 ambigsw(cp, switches);
120                                 exit(EX_USAGE);
121                         case UNKWNSW:
122                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
123
124                         case HELPSW:
125                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
126                                 print_help(buf, switches, 1);
127                                 exit(argc == 2 ? EX_OK : EX_USAGE);
128                         case VERSIONSW:
129                                 print_version(invo_name);
130                                 exit(argc == 2 ? EX_OK : EX_USAGE);
131
132                         case PARTSW:
133                                 if (!(cp = *argp++) || *cp == '-')
134                                         adios(EX_USAGE, NULL, "missing argument to %s",
135                                                         argp[-2]);
136                                 if (npart >= NPARTS)
137                                         adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
138                                 parts[npart++] = cp;
139                                 continue;
140
141                         case TYPESW:
142                                 if (!(cp = *argp++) || *cp == '-')
143                                         adios(EX_USAGE, NULL, "missing argument to %s",
144                                                         argp[-2]);
145                                 if (ntype >= NTYPES)
146                                         adios(EX_USAGE, NULL, "too many types (starting with %s), %d max", cp, NTYPES);
147                                 types[ntype++] = cp;
148                                 continue;
149
150                         case FILESW:
151                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
152                                         adios(EX_USAGE, NULL, "missing argument to %s",
153                                                         argp[-2]);
154                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
155                                 continue;
156
157                         case VERBSW:
158                                 verbosw = 1;
159                                 continue;
160                         case NVERBSW:
161                                 verbosw = 0;
162                                 continue;
163                         case DEBUGSW:
164                                 debugsw = 1;
165                                 continue;
166                         }
167                 }
168                 if (*cp == '+' || *cp == '@') {
169                         if (folder)
170                                 adios(EX_USAGE, NULL, "only one folder at a time!");
171                         else
172                                 folder = getcpy(expandfol(cp));
173                 } else
174                         app_msgarg(&msgs, cp);
175         }
176
177         /* null terminate the list of acceptable parts/types */
178         parts[npart] = NULL;
179         types[ntype] = NULL;
180
181         set_endian();
182
183         /*
184         ** Check for storage directory.  If specified,
185         ** then store temporary files there.  Else we
186         ** store them in standard nmh directory.
187         */
188         if ((cp = context_find(nmhstorage)) && *cp)
189                 tmp = concat(cp, "/", invo_name, NULL);
190         else
191                 tmp = getcpy(toabsdir(invo_name));
192
193         if (file && msgs.size)
194                 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
195
196         /*
197         ** check if message is coming from file
198         */
199         if (file) {
200                 cts = mh_xcalloc(2, sizeof(*cts));
201                 ctp = cts;
202
203                 if ((ct = parse_mime(file)))
204                         *ctp++ = ct;
205         } else {
206                 /*
207                 ** message(s) are coming from a folder
208                 */
209                 if (!msgs.size)
210                         app_msgarg(&msgs, seq_cur);
211                 if (!folder)
212                         folder = getcurfol();
213                 maildir = toabsdir(folder);
214
215                 if (chdir(maildir) == NOTOK)
216                         adios(EX_OSERR, maildir, "unable to change directory to");
217
218                 /* read folder and create message structure */
219                 if (!(mp = folder_read(folder)))
220                         adios(EX_IOERR, NULL, "unable to read folder %s", folder);
221
222                 /* check for empty folder */
223                 if (mp->nummsg == 0)
224                         adios(EX_DATAERR, NULL, "no messages in %s", folder);
225
226                 /* parse all the message ranges/sequences and set SELECTED */
227                 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
228                         if (!m_convert(mp, msgs.msgs[msgnum])) {
229                                 exit(EX_SOFTWARE);
230                         }
231                 }
232                 seq_setprev(mp);  /* set the previous-sequence */
233
234                 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
235                 ctp = cts;
236
237                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
238                         if (is_selected(mp, msgnum)) {
239                                 char *msgnam;
240
241                                 msgnam = m_name(msgnum);
242                                 if ((ct = parse_mime(msgnam)))
243                                         *ctp++ = ct;
244                         }
245                 }
246         }
247
248         if (!*cts)
249                 exit(EX_SOFTWARE);
250
251         userrs = 1;
252         SIGNAL(SIGQUIT, quitser);
253         SIGNAL(SIGPIPE, pipeser);
254
255         /*
256         ** Get the associated umask for the relevant contents.
257         */
258         for (ctp = cts; *ctp; ctp++) {
259                 struct stat st;
260
261                 ct = *ctp;
262                 if (type_ok(ct, 1) && !ct->c_umask) {
263                         if (stat(ct->c_file, &st) != NOTOK)
264                                 ct->c_umask = ~(st.st_mode & 0777);
265                         else
266                                 ct->c_umask = ~m_gmprot();
267                 }
268         }
269
270         /*
271         ** List the message content
272         */
273         list_all_messages(cts, verbosw, debugsw);
274
275         /* Now free all the structures for the content */
276         for (ctp = cts; *ctp; ctp++)
277                 free_content(*ctp);
278
279         free((char *) cts);
280         cts = NULL;
281
282         /* If reading from a folder, do some updating */
283         if (mp) {
284                 context_replace(curfolder, folder); /* update current folder */
285                 seq_setcur(mp, mp->hghsel);  /* update current message */
286                 seq_save(mp);  /* synchronize sequences  */
287                 context_save();  /* save the context file  */
288         }
289
290         return EX_OK;
291 }
292
293
294 static void
295 pipeser(int i)
296 {
297         if (i == SIGQUIT) {
298                 unlink("core");
299                 fflush(stdout);
300                 fprintf(stderr, "\n");
301                 fflush(stderr);
302         }
303
304         _exit(EX_IOERR);
305         /* NOTREACHED */
306 }