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