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 static struct swit switches[] = {
33 { "outfile file", 0 },
37 { "type content", 0 },
39 { "rcache policy", 0 },
41 { "wcache policy", 0 },
48 ** switches for debugging
56 int ebcdicsw = 0; /* hack for linking purposes */
59 extern char *tmp; /* directory to place temp files */
64 extern char *cache_public;
65 extern char *cache_private;
70 extern char *parts[NPARTS + 1];
71 extern char *types[NTYPES + 1];
75 ** This is currently needed to keep mhparse happy.
76 ** This needs to be changed.
83 #define quitser pipeser
86 CT parse_mime(char *);
89 int output_message(CT, char *);
94 void set_endian(void);
95 void flush_errors(void);
98 void free_content(CT);
100 void freects_done(int) NORETURN;
105 static int write_content(CT *, char *);
106 static RETSIGTYPE pipeser(int);
110 main(int argc, char **argv)
112 int msgnum, *icachesw;
113 char *cp, *file = NULL, *folder = NULL;
114 char *maildir, buf[100], *outfile = NULL;
115 char **argp, **arguments;
116 struct msgs_array msgs = { 0, 0, NULL };
117 struct msgs *mp = NULL;
123 setlocale(LC_ALL, "");
125 invo_name = mhbasename(argv[0]);
127 /* read user profile/context */
130 arguments = getarguments(invo_name, argc, argv, 1);
136 while ((cp = *argp++)) {
138 switch (smatch(++cp, switches)) {
140 ambigsw(cp, switches);
143 adios(NULL, "-%s unknown", cp);
146 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
147 print_help(buf, switches, 1);
150 print_version(invo_name);
154 icachesw = &rcachesw;
157 icachesw = &wcachesw;
159 if (!(cp = *argp++) || *cp == '-')
160 adios(NULL, "missing argument to %s",
162 switch (*icachesw = smatch(cp, caches)) {
167 adios(NULL, "%s unknown", cp);
181 if (!(cp = *argp++) || *cp == '-')
182 adios(NULL, "missing argument to %s", argp[-2]);
184 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
189 if (!(cp = *argp++) || *cp == '-')
190 adios(NULL, "missing argument to %s", argp[-2]);
192 adios(NULL, "too many types (starting with %s), %d max",
198 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
199 adios(NULL, "missing argument to %s",
201 file = *cp == '-' ? cp : getcpy(expanddir(cp));
205 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
206 adios(NULL, "missing argument to %s",
208 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
222 if (*cp == '+' || *cp == '@') {
224 adios(NULL, "only one folder at a time!");
226 folder = getcpy(expandfol(cp));
228 app_msgarg(&msgs, cp);
231 /* null terminate the list of acceptable parts/types */
238 adios(NULL, "must specify output file");
240 /* Check for public cache location */
241 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
244 /* Check for private cache location */
245 if (!(cache_private = context_find(nmhprivcache)))
246 cache_private = ".cache";
247 cache_private = getcpy(toabsdir(cache_private));
250 ** Check for storage directory. If specified,
251 ** then store temporary files there. Else we
252 ** store them in standard nmh directory.
254 if ((cp = context_find(nmhstorage)) && *cp)
255 tmp = concat(cp, "/", invo_name, NULL);
257 tmp = getcpy(toabsdir(invo_name));
259 if (file && msgs.size)
260 adios(NULL, "cannot specify msg and file at same time!");
263 ** check if message is coming from file
266 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
267 adios(NULL, "out of memory");
270 if ((ct = parse_mime(file)))
274 ** message(s) are coming from a folder
277 app_msgarg(&msgs, seq_cur);
279 folder = getcurfol();
280 maildir = toabsdir(folder);
282 if (chdir(maildir) == NOTOK)
283 adios(maildir, "unable to change directory to");
285 /* read folder and create message structure */
286 if (!(mp = folder_read(folder)))
287 adios(NULL, "unable to read folder %s", folder);
289 /* check for empty folder */
291 adios(NULL, "no messages in %s", folder);
293 /* parse all the message ranges/sequences and set SELECTED */
294 for (msgnum = 0; msgnum < msgs.size; msgnum++)
295 if (!m_convert(mp, msgs.msgs[msgnum]))
297 seq_setprev(mp); /* set the previous-sequence */
299 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
301 adios(NULL, "out of memory");
304 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
305 if (is_selected(mp, msgnum)) {
308 msgnam = m_name(msgnum);
309 if ((ct = parse_mime(msgnam)))
319 SIGNAL(SIGQUIT, quitser);
320 SIGNAL(SIGPIPE, pipeser);
323 ** Get the associated umask for the relevant contents.
325 for (ctp = cts; *ctp; ctp++) {
329 if (type_ok(ct, 1) && !ct->c_umask) {
330 if (stat(ct->c_file, &st) != NOTOK)
331 ct->c_umask = ~(st.st_mode & 0777);
333 ct->c_umask = ~m_gmprot();
338 ** Write the content to a file
340 write_content(cts, outfile);
342 /* Now free all the structures for the content */
343 for (ctp = cts; *ctp; ctp++)
349 /* If reading from a folder, do some updating */
351 context_replace(curfolder, folder); /* update current folder */
352 seq_setcur(mp, mp->hghsel); /* update current message */
353 seq_save(mp); /* synchronize sequences */
354 context_save(); /* save the context file */
363 write_content(CT *cts, char *outfile)
367 for (ctp = cts; *ctp; ctp++) {
369 output_message(ct, outfile);
383 fprintf(stderr, "\n");