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>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
38 { "outfile file", 0 },
42 { "type content", 0 },
44 { "rcache policy", 0 },
46 { "wcache policy", 0 },
53 ** switches for debugging
61 int ebcdicsw = 0; /* hack for linking purposes */
64 extern char *tmp; /* directory to place temp files */
69 extern char *cache_public;
70 extern char *cache_private;
75 extern char *parts[NPARTS + 1];
76 extern char *types[NTYPES + 1];
80 ** This is currently needed to keep mhparse happy.
81 ** This needs to be changed.
88 #define quitser pipeser
91 CT parse_mime(char *);
94 int output_message(CT, char *);
99 void set_endian(void);
100 void flush_errors(void);
103 void free_content(CT);
105 void freects_done(int) NORETURN;
110 static int write_content(CT *, char *);
111 static RETSIGTYPE pipeser(int);
115 main(int argc, char **argv)
117 int msgnum, *icachesw;
118 char *cp, *file = NULL, *folder = NULL;
119 char *maildir, buf[100], *outfile = NULL;
120 char **argp, **arguments;
121 struct msgs_array msgs = { 0, 0, NULL };
122 struct msgs *mp = NULL;
128 setlocale(LC_ALL, "");
130 invo_name = mhbasename(argv[0]);
132 /* read user profile/context */
135 arguments = getarguments(invo_name, argc, argv, 1);
141 while ((cp = *argp++)) {
143 switch (smatch(++cp, switches)) {
145 ambigsw(cp, switches);
148 adios(NULL, "-%s unknown", cp);
151 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
152 print_help(buf, switches, 1);
155 print_version(invo_name);
159 icachesw = &rcachesw;
162 icachesw = &wcachesw;
164 if (!(cp = *argp++) || *cp == '-')
165 adios(NULL, "missing argument to %s",
167 switch (*icachesw = smatch(cp, caches)) {
172 adios(NULL, "%s unknown", cp);
186 if (!(cp = *argp++) || *cp == '-')
187 adios(NULL, "missing argument to %s", argp[-2]);
189 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
194 if (!(cp = *argp++) || *cp == '-')
195 adios(NULL, "missing argument to %s", argp[-2]);
197 adios(NULL, "too many types (starting with %s), %d max",
203 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
204 adios(NULL, "missing argument to %s",
206 file = *cp == '-' ? cp : getcpy(expanddir(cp));
210 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
211 adios(NULL, "missing argument to %s",
213 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
227 if (*cp == '+' || *cp == '@') {
229 adios(NULL, "only one folder at a time!");
231 folder = getcpy(expandfol(cp));
233 app_msgarg(&msgs, cp);
236 /* null terminate the list of acceptable parts/types */
243 adios(NULL, "must specify output file");
245 /* Check for public cache location */
246 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
249 /* Check for private cache location */
250 if (!(cache_private = context_find(nmhprivcache)))
251 cache_private = ".cache";
252 cache_private = getcpy(toabsdir(cache_private));
255 ** Check for storage directory. If specified,
256 ** then store temporary files there. Else we
257 ** store them in standard nmh directory.
259 if ((cp = context_find(nmhstorage)) && *cp)
260 tmp = concat(cp, "/", invo_name, NULL);
262 tmp = getcpy(toabsdir(invo_name));
264 if (file && msgs.size)
265 adios(NULL, "cannot specify msg and file at same time!");
268 ** check if message is coming from file
271 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
272 adios(NULL, "out of memory");
275 if ((ct = parse_mime(file)));
279 ** message(s) are coming from a folder
282 app_msgarg(&msgs, seq_cur);
284 folder = getcurfol();
285 maildir = toabsdir(folder);
287 if (chdir(maildir) == NOTOK)
288 adios(maildir, "unable to change directory to");
290 /* read folder and create message structure */
291 if (!(mp = folder_read(folder)))
292 adios(NULL, "unable to read folder %s", folder);
294 /* check for empty folder */
296 adios(NULL, "no messages in %s", folder);
298 /* parse all the message ranges/sequences and set SELECTED */
299 for (msgnum = 0; msgnum < msgs.size; msgnum++)
300 if (!m_convert(mp, msgs.msgs[msgnum]))
302 seq_setprev(mp); /* set the previous-sequence */
304 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
306 adios(NULL, "out of memory");
309 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
310 if (is_selected(mp, msgnum)) {
313 msgnam = m_name(msgnum);
314 if ((ct = parse_mime(msgnam)))
324 SIGNAL(SIGQUIT, quitser);
325 SIGNAL(SIGPIPE, pipeser);
328 ** Get the associated umask for the relevant contents.
330 for (ctp = cts; *ctp; ctp++) {
334 if (type_ok(ct, 1) && !ct->c_umask) {
335 if (stat(ct->c_file, &st) != NOTOK)
336 ct->c_umask = ~(st.st_mode & 0777);
338 ct->c_umask = ~m_gmprot();
343 ** Write the content to a file
345 write_content(cts, outfile);
347 /* Now free all the structures for the content */
348 for (ctp = cts; *ctp; ctp++)
354 /* If reading from a folder, do some updating */
356 context_replace(curfolder, folder); /* update current folder */
357 seq_setcur(mp, mp->hghsel); /* update current message */
358 seq_save(mp); /* synchronize sequences */
359 context_save(); /* save the context file */
368 write_content(CT *cts, char *outfile)
372 for (ctp = cts; *ctp; ctp++) {
374 output_message(ct, outfile);
388 fprintf(stderr, "\n");