3 * mhtest.c -- test harness for MIME routines
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
29 static struct swit switches[] = {
41 { "outfile file", 0 },
45 { "type content", 0 },
47 { "rcache policy", 0 },
49 { "wcache policy", 0 },
56 * switches for debugging
64 int ebcdicsw = 0; /* hack for linking purposes */
67 extern char *tmp; /* directory to place temp files */
72 extern char *cache_public;
73 extern char *cache_private;
78 extern char *parts[NPARTS + 1];
79 extern char *types[NTYPES + 1];
83 * This is currently needed to keep mhparse happy.
84 * This needs to be changed.
91 #define quitser pipeser
94 CT parse_mime (char *);
97 int output_message (CT, char *);
100 int part_ok (CT, int);
101 int type_ok (CT, int);
102 void set_endian (void);
103 void flush_errors (void);
106 void free_content (CT);
108 void freects_done (int) NORETURN;
113 static int write_content (CT *, char *);
114 static RETSIGTYPE pipeser (int);
118 main (int argc, char **argv)
120 int msgnum, *icachesw;
121 char *cp, *file = NULL, *folder = NULL;
122 char *maildir, buf[100], *outfile = NULL;
123 char **argp, **arguments;
124 struct msgs_array msgs = { 0, 0, NULL };
125 struct msgs *mp = NULL;
131 setlocale(LC_ALL, "");
133 invo_name = r1bindex (argv[0], '/');
135 /* read user profile/context */
138 arguments = getarguments (invo_name, argc, argv, 1);
144 while ((cp = *argp++)) {
146 switch (smatch (++cp, switches)) {
148 ambigsw (cp, switches);
151 adios (NULL, "-%s unknown", cp);
154 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
156 print_help (buf, switches, 1);
159 print_version(invo_name);
163 icachesw = &rcachesw;
166 icachesw = &wcachesw;
168 if (!(cp = *argp++) || *cp == '-')
169 adios (NULL, "missing argument to %s", argp[-2]);
170 switch (*icachesw = smatch (cp, caches)) {
172 ambigsw (cp, caches);
175 adios (NULL, "%s unknown", cp);
189 if (!(cp = *argp++) || *cp == '-')
190 adios (NULL, "missing argument to %s", argp[-2]);
192 adios (NULL, "too many parts (starting with %s), %d max",
198 if (!(cp = *argp++) || *cp == '-')
199 adios (NULL, "missing argument to %s", argp[-2]);
201 adios (NULL, "too many types (starting with %s), %d max",
207 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
208 adios (NULL, "missing argument to %s", argp[-2]);
209 file = *cp == '-' ? cp : path (cp, TFILE);
213 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
214 adios (NULL, "missing argument to %s", argp[-2]);
215 outfile = *cp == '-' ? cp : path (cp, TFILE);
229 if (*cp == '+' || *cp == '@') {
231 adios (NULL, "only one folder at a time!");
233 folder = pluspath (cp);
235 app_msgarg(&msgs, cp);
238 /* null terminate the list of acceptable parts/types */
245 adios (NULL, "must specify output file");
247 /* Check for public cache location */
248 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
251 /* Check for private cache location */
252 if (!(cache_private = context_find (nmhprivcache)))
253 cache_private = ".cache";
254 cache_private = getcpy (m_maildir (cache_private));
257 * Check for storage directory. If specified,
258 * then store temporary files there. Else we
259 * store them in standard nmh directory.
261 if ((cp = context_find (nmhstorage)) && *cp)
262 tmp = concat (cp, "/", invo_name, NULL);
264 tmp = add (m_maildir (invo_name), NULL);
266 if (!context_find ("path"))
267 free (path ("./", TFOLDER));
269 if (file && msgs.size)
270 adios (NULL, "cannot specify msg and file at same time!");
273 * check if message is coming from file
276 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
277 adios (NULL, "out of memory");
280 if ((ct = parse_mime (file)));
284 * message(s) are coming from a folder
287 app_msgarg(&msgs, "cur");
289 folder = getfolder (1);
290 maildir = m_maildir (folder);
292 if (chdir (maildir) == NOTOK)
293 adios (maildir, "unable to change directory to");
295 /* read folder and create message structure */
296 if (!(mp = folder_read (folder)))
297 adios (NULL, "unable to read folder %s", folder);
299 /* check for empty folder */
301 adios (NULL, "no messages in %s", folder);
303 /* parse all the message ranges/sequences and set SELECTED */
304 for (msgnum = 0; msgnum < msgs.size; msgnum++)
305 if (!m_convert (mp, msgs.msgs[msgnum]))
307 seq_setprev (mp); /* set the previous-sequence */
309 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
310 adios (NULL, "out of memory");
313 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
314 if (is_selected(mp, msgnum)) {
317 msgnam = m_name (msgnum);
318 if ((ct = parse_mime (msgnam)))
328 SIGNAL (SIGQUIT, quitser);
329 SIGNAL (SIGPIPE, pipeser);
332 * Get the associated umask for the relevant contents.
334 for (ctp = cts; *ctp; ctp++) {
338 if (type_ok (ct, 1) && !ct->c_umask) {
339 if (stat (ct->c_file, &st) != NOTOK)
340 ct->c_umask = ~(st.st_mode & 0777);
342 ct->c_umask = ~m_gmprot();
347 * Write the content to a file
349 write_content (cts, outfile);
351 /* Now free all the structures for the content */
352 for (ctp = cts; *ctp; ctp++)
358 /* If reading from a folder, do some updating */
360 context_replace (pfolder, folder);/* update current folder */
361 seq_setcur (mp, mp->hghsel); /* update current message */
362 seq_save (mp); /* synchronize sequences */
363 context_save (); /* save the context file */
372 write_content (CT *cts, char *outfile)
376 for (ctp = cts; *ctp; ctp++) {
378 output_message (ct, outfile);
392 fprintf (stderr, "\n");