2 ** mhtest.c -- test harness for MIME routines
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.
11 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
25 static struct swit switches[] = {
37 { "outfile file", 0 },
41 { "type content", 0 },
43 { "rcache policy", 0 },
45 { "wcache policy", 0 },
52 ** switches for debugging
60 int ebcdicsw = 0; /* hack for linking purposes */
63 extern char *tmp; /* directory to place temp files */
68 extern char *cache_public;
69 extern char *cache_private;
74 extern char *parts[NPARTS + 1];
75 extern char *types[NTYPES + 1];
79 ** This is currently needed to keep mhparse happy.
80 ** This needs to be changed.
87 #define quitser pipeser
90 CT parse_mime(char *);
93 int output_message(CT, char *);
98 void set_endian(void);
99 void flush_errors(void);
102 void free_content(CT);
104 void freects_done(int) NORETURN;
109 static int write_content(CT *, char *);
110 static RETSIGTYPE pipeser(int);
114 main(int argc, char **argv)
116 int msgnum, *icachesw;
117 char *cp, *file = NULL, *folder = NULL;
118 char *maildir, buf[100], *outfile = NULL;
119 char **argp, **arguments;
120 struct msgs_array msgs = { 0, 0, NULL };
121 struct msgs *mp = NULL;
127 setlocale(LC_ALL, "");
129 invo_name = mhbasename(argv[0]);
131 /* read user profile/context */
134 arguments = getarguments(invo_name, argc, argv, 1);
140 while ((cp = *argp++)) {
142 switch (smatch(++cp, switches)) {
144 ambigsw(cp, switches);
147 adios(NULL, "-%s unknown", cp);
150 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
151 print_help(buf, switches, 1);
154 print_version(invo_name);
158 icachesw = &rcachesw;
161 icachesw = &wcachesw;
163 if (!(cp = *argp++) || *cp == '-')
164 adios(NULL, "missing argument to %s",
166 switch (*icachesw = smatch(cp, caches)) {
171 adios(NULL, "%s unknown", cp);
185 if (!(cp = *argp++) || *cp == '-')
186 adios(NULL, "missing argument to %s", argp[-2]);
188 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
193 if (!(cp = *argp++) || *cp == '-')
194 adios(NULL, "missing argument to %s", argp[-2]);
196 adios(NULL, "too many types (starting with %s), %d max",
202 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
203 adios(NULL, "missing argument to %s",
205 file = *cp == '-' ? cp : getcpy(expanddir(cp));
209 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
210 adios(NULL, "missing argument to %s",
212 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
226 if (*cp == '+' || *cp == '@') {
228 adios(NULL, "only one folder at a time!");
230 folder = getcpy(expandfol(cp));
232 app_msgarg(&msgs, cp);
235 /* null terminate the list of acceptable parts/types */
242 adios(NULL, "must specify output file");
244 /* Check for public cache location */
245 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
248 /* Check for private cache location */
249 if (!(cache_private = context_find(nmhprivcache)))
250 cache_private = ".cache";
251 cache_private = getcpy(toabsdir(cache_private));
254 ** Check for storage directory. If specified,
255 ** then store temporary files there. Else we
256 ** store them in standard nmh directory.
258 if ((cp = context_find(nmhstorage)) && *cp)
259 tmp = concat(cp, "/", invo_name, NULL);
261 tmp = getcpy(toabsdir(invo_name));
263 if (file && msgs.size)
264 adios(NULL, "cannot specify msg and file at same time!");
267 ** check if message is coming from file
270 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
271 adios(NULL, "out of memory");
274 if ((ct = parse_mime(file)));
278 ** message(s) are coming from a folder
281 app_msgarg(&msgs, seq_cur);
283 folder = getcurfol();
284 maildir = toabsdir(folder);
286 if (chdir(maildir) == NOTOK)
287 adios(maildir, "unable to change directory to");
289 /* read folder and create message structure */
290 if (!(mp = folder_read(folder)))
291 adios(NULL, "unable to read folder %s", folder);
293 /* check for empty folder */
295 adios(NULL, "no messages in %s", folder);
297 /* parse all the message ranges/sequences and set SELECTED */
298 for (msgnum = 0; msgnum < msgs.size; msgnum++)
299 if (!m_convert(mp, msgs.msgs[msgnum]))
301 seq_setprev(mp); /* set the previous-sequence */
303 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
305 adios(NULL, "out of memory");
308 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
309 if (is_selected(mp, msgnum)) {
312 msgnam = m_name(msgnum);
313 if ((ct = parse_mime(msgnam)))
323 SIGNAL(SIGQUIT, quitser);
324 SIGNAL(SIGPIPE, pipeser);
327 ** Get the associated umask for the relevant contents.
329 for (ctp = cts; *ctp; ctp++) {
333 if (type_ok(ct, 1) && !ct->c_umask) {
334 if (stat(ct->c_file, &st) != NOTOK)
335 ct->c_umask = ~(st.st_mode & 0777);
337 ct->c_umask = ~m_gmprot();
342 ** Write the content to a file
344 write_content(cts, outfile);
346 /* Now free all the structures for the content */
347 for (ctp = cts; *ctp; ctp++)
353 /* If reading from a folder, do some updating */
355 context_replace(curfolder, folder); /* update current folder */
356 seq_setcur(mp, mp->hghsel); /* update current message */
357 seq_save(mp); /* synchronize sequences */
358 context_save(); /* save the context file */
367 write_content(CT *cts, char *outfile)
371 for (ctp = cts; *ctp; ctp++) {
373 output_message(ct, outfile);
387 fprintf(stderr, "\n");