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 },
52 int ebcdicsw = 0; /* hack for linking purposes */
55 extern char *tmp; /* directory to place temp files */
60 extern char *cache_public;
61 extern char *cache_private;
66 extern char *parts[NPARTS + 1];
67 extern char *types[NTYPES + 1];
71 ** This is currently needed to keep mhparse happy.
72 ** This needs to be changed.
79 #define quitser pipeser
82 CT parse_mime(char *);
85 int output_message(CT, char *);
90 void set_endian(void);
91 void flush_errors(void);
94 void free_content(CT);
96 void freects_done(int) NORETURN;
101 static int write_content(CT *, char *);
102 static void pipeser(int);
106 main(int argc, char **argv)
108 int msgnum, *icachesw;
109 char *cp, *file = NULL, *folder = NULL;
110 char *maildir, buf[100], *outfile = NULL;
111 char **argp, **arguments;
112 struct msgs_array msgs = { 0, 0, NULL };
113 struct msgs *mp = NULL;
119 setlocale(LC_ALL, "");
121 invo_name = mhbasename(argv[0]);
123 /* read user profile/context */
126 arguments = getarguments(invo_name, argc, argv, 1);
132 while ((cp = *argp++)) {
134 switch (smatch(++cp, switches)) {
136 ambigsw(cp, switches);
139 adios(NULL, "-%s unknown", cp);
142 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
143 print_help(buf, switches, 1);
146 print_version(invo_name);
150 icachesw = &rcachesw;
153 icachesw = &wcachesw;
155 if (!(cp = *argp++) || *cp == '-')
156 adios(NULL, "missing argument to %s",
158 switch (*icachesw = smatch(cp, caches)) {
163 adios(NULL, "%s unknown", cp);
177 if (!(cp = *argp++) || *cp == '-')
178 adios(NULL, "missing argument to %s", argp[-2]);
180 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
185 if (!(cp = *argp++) || *cp == '-')
186 adios(NULL, "missing argument to %s", argp[-2]);
188 adios(NULL, "too many types (starting with %s), %d max",
194 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
195 adios(NULL, "missing argument to %s",
197 file = *cp == '-' ? cp : getcpy(expanddir(cp));
201 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
202 adios(NULL, "missing argument to %s",
204 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
218 if (*cp == '+' || *cp == '@') {
220 adios(NULL, "only one folder at a time!");
222 folder = getcpy(expandfol(cp));
224 app_msgarg(&msgs, cp);
227 /* null terminate the list of acceptable parts/types */
234 adios(NULL, "must specify output file");
236 /* Check for public cache location */
237 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
240 /* Check for private cache location */
241 if (!(cache_private = context_find(nmhprivcache)))
242 cache_private = ".cache";
243 cache_private = getcpy(toabsdir(cache_private));
246 ** Check for storage directory. If specified,
247 ** then store temporary files there. Else we
248 ** store them in standard nmh directory.
250 if ((cp = context_find(nmhstorage)) && *cp)
251 tmp = concat(cp, "/", invo_name, NULL);
253 tmp = getcpy(toabsdir(invo_name));
255 if (file && msgs.size)
256 adios(NULL, "cannot specify msg and file at same time!");
259 ** check if message is coming from file
262 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
263 adios(NULL, "out of memory");
266 if ((ct = parse_mime(file)))
270 ** message(s) are coming from a folder
273 app_msgarg(&msgs, seq_cur);
275 folder = getcurfol();
276 maildir = toabsdir(folder);
278 if (chdir(maildir) == NOTOK)
279 adios(maildir, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp = folder_read(folder)))
283 adios(NULL, "unable to read folder %s", folder);
285 /* check for empty folder */
287 adios(NULL, "no messages in %s", folder);
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum = 0; msgnum < msgs.size; msgnum++)
291 if (!m_convert(mp, msgs.msgs[msgnum]))
293 seq_setprev(mp); /* set the previous-sequence */
295 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
297 adios(NULL, "out of memory");
300 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
301 if (is_selected(mp, msgnum)) {
304 msgnam = m_name(msgnum);
305 if ((ct = parse_mime(msgnam)))
315 SIGNAL(SIGQUIT, quitser);
316 SIGNAL(SIGPIPE, pipeser);
319 ** Get the associated umask for the relevant contents.
321 for (ctp = cts; *ctp; ctp++) {
325 if (type_ok(ct, 1) && !ct->c_umask) {
326 if (stat(ct->c_file, &st) != NOTOK)
327 ct->c_umask = ~(st.st_mode & 0777);
329 ct->c_umask = ~m_gmprot();
334 ** Write the content to a file
336 write_content(cts, outfile);
338 /* Now free all the structures for the content */
339 for (ctp = cts; *ctp; ctp++)
345 /* If reading from a folder, do some updating */
347 context_replace(curfolder, folder); /* update current folder */
348 seq_setcur(mp, mp->hghsel); /* update current message */
349 seq_save(mp); /* synchronize sequences */
350 context_save(); /* save the context file */
359 write_content(CT *cts, char *outfile)
363 for (ctp = cts; *ctp; ctp++) {
365 output_message(ct, outfile);
379 fprintf(stderr, "\n");