mhbuild: Removed the -ebcdicsafe switch.
[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", 0 },
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(int) NORETURN;
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         done=freects_done;
99
100 #ifdef LOCALE
101         setlocale(LC_ALL, "");
102 #endif
103         invo_name = mhbasename(argv[0]);
104
105         /* read user profile/context */
106         context_read();
107
108         arguments = getarguments(invo_name, argc, argv, 1);
109         argp = arguments;
110
111         /*
112         ** Parse arguments
113         */
114         while ((cp = *argp++)) {
115                 if (*cp == '-') {
116                         switch (smatch(++cp, switches)) {
117                         case AMBIGSW:
118                                 ambigsw(cp, switches);
119                                 done(1);
120                         case UNKWNSW:
121                                 adios(NULL, "-%s unknown", cp);
122
123                         case HELPSW:
124                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
125                                 print_help(buf, switches, 1);
126                                 done(1);
127                         case VERSIONSW:
128                                 print_version(invo_name);
129                                 done(1);
130
131                         case PARTSW:
132                                 if (!(cp = *argp++) || *cp == '-')
133                                         adios(NULL, "missing argument to %s", argp[-2]);
134                                 if (npart >= NPARTS)
135                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
136                                 parts[npart++] = cp;
137                                 continue;
138
139                         case TYPESW:
140                                 if (!(cp = *argp++) || *cp == '-')
141                                         adios(NULL, "missing argument to %s", argp[-2]);
142                                 if (ntype >= NTYPES)
143                                         adios(NULL, "too many types (starting with %s), %d max",
144                                                    cp, NTYPES);
145                                 types[ntype++] = cp;
146                                 continue;
147
148                         case FILESW:
149                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
150                                         adios(NULL, "missing argument to %s",
151                                                         argp[-2]);
152                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
153                                 continue;
154
155                         case OUTFILESW:
156                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
157                                         adios(NULL, "missing argument to %s",
158                                                         argp[-2]);
159                                 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
160                                 continue;
161
162                         case VERBSW:
163                                 verbosw = 1;
164                                 continue;
165                         case NVERBSW:
166                                 verbosw = 0;
167                                 continue;
168                         case DEBUGSW:
169                                 debugsw = 1;
170                                 continue;
171                         }
172                 }
173                 if (*cp == '+' || *cp == '@') {
174                         if (folder)
175                                 adios(NULL, "only one folder at a time!");
176                         else
177                                 folder = getcpy(expandfol(cp));
178                 } else
179                         app_msgarg(&msgs, cp);
180         }
181
182         /* null terminate the list of acceptable parts/types */
183         parts[npart] = NULL;
184         types[ntype] = NULL;
185
186         set_endian();
187
188         if (outfile == NULL)
189                 adios(NULL, "must specify output file");
190
191         /*
192         ** Check for storage directory.  If specified,
193         ** then store temporary files there.  Else we
194         ** store them in standard nmh directory.
195         */
196         if ((cp = context_find(nmhstorage)) && *cp)
197                 tmp = concat(cp, "/", invo_name, NULL);
198         else
199                 tmp = getcpy(toabsdir(invo_name));
200
201         if (file && msgs.size)
202                 adios(NULL, "cannot specify msg and file at same time!");
203
204         /*
205         ** check if message is coming from file
206         */
207         if (file) {
208                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
209                         adios(NULL, "out of memory");
210                 ctp = cts;
211
212                 if ((ct = parse_mime(file)))
213                         *ctp++ = ct;
214         } else {
215                 /*
216                 ** message(s) are coming from a folder
217                 */
218                 if (!msgs.size)
219                         app_msgarg(&msgs, seq_cur);
220                 if (!folder)
221                         folder = getcurfol();
222                 maildir = toabsdir(folder);
223
224                 if (chdir(maildir) == NOTOK)
225                         adios(maildir, "unable to change directory to");
226
227                 /* read folder and create message structure */
228                 if (!(mp = folder_read(folder)))
229                         adios(NULL, "unable to read folder %s", folder);
230
231                 /* check for empty folder */
232                 if (mp->nummsg == 0)
233                         adios(NULL, "no messages in %s", folder);
234
235                 /* parse all the message ranges/sequences and set SELECTED */
236                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
237                         if (!m_convert(mp, msgs.msgs[msgnum]))
238                                 done(1);
239                 seq_setprev(mp);  /* set the previous-sequence */
240
241                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
242                                 sizeof(*cts))))
243                         adios(NULL, "out of memory");
244                 ctp = cts;
245
246                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
247                         if (is_selected(mp, msgnum)) {
248                                 char *msgnam;
249
250                                 msgnam = m_name(msgnum);
251                                 if ((ct = parse_mime(msgnam)))
252                                         *ctp++ = ct;
253                         }
254                 }
255         }
256
257         if (!*cts)
258                 done(1);
259
260         userrs = 1;
261         SIGNAL(SIGQUIT, quitser);
262         SIGNAL(SIGPIPE, pipeser);
263
264         /*
265         ** Get the associated umask for the relevant contents.
266         */
267         for (ctp = cts; *ctp; ctp++) {
268                 struct stat st;
269
270                 ct = *ctp;
271                 if (type_ok(ct, 1) && !ct->c_umask) {
272                         if (stat(ct->c_file, &st) != NOTOK)
273                                 ct->c_umask = ~(st.st_mode & 0777);
274                         else
275                                 ct->c_umask = ~m_gmprot();
276                 }
277         }
278
279         /*
280         ** Write the content to a file
281         */
282         write_content(cts, outfile);
283
284         /* Now free all the structures for the content */
285         for (ctp = cts; *ctp; ctp++)
286                 free_content(*ctp);
287
288         free((char *) cts);
289         cts = NULL;
290
291         /* If reading from a folder, do some updating */
292         if (mp) {
293                 context_replace(curfolder, folder); /* update current folder */
294                 seq_setcur(mp, mp->hghsel);  /* update current message */
295                 seq_save(mp);  /* synchronize sequences  */
296                 context_save();  /* save the context file  */
297         }
298
299         done(0);
300         return 1;
301 }
302
303
304 static int
305 write_content(CT *cts, char *outfile)
306 {
307         CT ct, *ctp;
308
309         for (ctp = cts; *ctp; ctp++) {
310                 ct = *ctp;
311                 output_message(ct, outfile);
312         }
313
314         flush_errors();
315         return OK;
316 }
317
318
319 static void
320 pipeser(int i)
321 {
322         if (i == SIGQUIT) {
323                 unlink("core");
324                 fflush(stdout);
325                 fprintf(stderr, "\n");
326                 fflush(stderr);
327         }
328
329         done(1);
330         /* NOTREACHED */
331 }