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