3 * mhtest.c -- test harness for MIME routines
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
27 static struct swit switches[] = {
39 { "outfile file", 0 },
43 { "type content", 0 },
45 { "rcache policy", 0 },
47 { "wcache policy", 0 },
54 * switches for debugging
62 int ebcdicsw = 0; /* hack for linking purposes */
65 extern char *tmp; /* directory to place temp files */
70 extern char *cache_public;
71 extern char *cache_private;
76 extern char *parts[NPARTS + 1];
77 extern char *types[NTYPES + 1];
81 * This is currently needed to keep mhparse happy.
82 * This needs to be changed.
89 #define quitser pipeser
92 CT parse_mime (char *);
95 int output_message (CT, char *);
98 int part_ok (CT, int);
99 int type_ok (CT, int);
100 void set_endian (void);
101 void flush_errors (void);
104 void free_content (CT);
106 void freects_done (int) NORETURN;
111 static int write_content (CT *, char *);
112 static RETSIGTYPE pipeser (int);
116 main (int argc, char **argv)
118 int msgnum, *icachesw;
119 char *cp, *file = NULL, *folder = NULL;
120 char *maildir, buf[100], *outfile = NULL;
121 char **argp, **arguments;
122 struct msgs_array msgs = { 0, 0, NULL };
123 struct msgs *mp = NULL;
129 setlocale(LC_ALL, "");
131 invo_name = r1bindex (argv[0], '/');
133 /* read user profile/context */
136 arguments = getarguments (invo_name, argc, argv, 1);
142 while ((cp = *argp++)) {
144 switch (smatch (++cp, switches)) {
146 ambigsw (cp, switches);
149 adios (NULL, "-%s unknown", cp);
152 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
154 print_help (buf, switches, 1);
157 print_version(invo_name);
161 icachesw = &rcachesw;
164 icachesw = &wcachesw;
166 if (!(cp = *argp++) || *cp == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
168 switch (*icachesw = smatch (cp, caches)) {
170 ambigsw (cp, caches);
173 adios (NULL, "%s unknown", cp);
187 if (!(cp = *argp++) || *cp == '-')
188 adios (NULL, "missing argument to %s", argp[-2]);
190 adios (NULL, "too many parts (starting with %s), %d max",
196 if (!(cp = *argp++) || *cp == '-')
197 adios (NULL, "missing argument to %s", argp[-2]);
199 adios (NULL, "too many types (starting with %s), %d max",
205 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
206 adios (NULL, "missing argument to %s", argp[-2]);
207 file = *cp == '-' ? cp : path (cp, TFILE);
211 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
212 adios (NULL, "missing argument to %s", argp[-2]);
213 outfile = *cp == '-' ? cp : path (cp, TFILE);
227 if (*cp == '+' || *cp == '@') {
229 adios (NULL, "only one folder at a time!");
231 folder = pluspath (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 (m_maildir (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 = add (m_maildir (invo_name), NULL);
264 if (!context_find ("path"))
265 free (path ("./", TFOLDER));
267 if (file && msgs.size)
268 adios (NULL, "cannot specify msg and file at same time!");
271 * check if message is coming from file
274 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
275 adios (NULL, "out of memory");
278 if ((ct = parse_mime (file)));
282 * message(s) are coming from a folder
285 app_msgarg(&msgs, "cur");
287 folder = getfolder (1);
288 maildir = m_maildir (folder);
290 if (chdir (maildir) == NOTOK)
291 adios (maildir, "unable to change directory to");
293 /* read folder and create message structure */
294 if (!(mp = folder_read (folder)))
295 adios (NULL, "unable to read folder %s", folder);
297 /* check for empty folder */
299 adios (NULL, "no messages in %s", folder);
301 /* parse all the message ranges/sequences and set SELECTED */
302 for (msgnum = 0; msgnum < msgs.size; msgnum++)
303 if (!m_convert (mp, msgs.msgs[msgnum]))
305 seq_setprev (mp); /* set the previous-sequence */
307 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
308 adios (NULL, "out of memory");
311 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
312 if (is_selected(mp, msgnum)) {
315 msgnam = m_name (msgnum);
316 if ((ct = parse_mime (msgnam)))
326 SIGNAL (SIGQUIT, quitser);
327 SIGNAL (SIGPIPE, pipeser);
330 * Get the associated umask for the relevant contents.
332 for (ctp = cts; *ctp; ctp++) {
336 if (type_ok (ct, 1) && !ct->c_umask) {
337 if (stat (ct->c_file, &st) != NOTOK)
338 ct->c_umask = ~(st.st_mode & 0777);
340 ct->c_umask = ~m_gmprot();
345 * Write the content to a file
347 write_content (cts, outfile);
349 /* Now free all the structures for the content */
350 for (ctp = cts; *ctp; ctp++)
356 /* If reading from a folder, do some updating */
358 context_replace (pfolder, folder);/* update current folder */
359 seq_setcur (mp, mp->hghsel); /* update current message */
360 seq_save (mp); /* synchronize sequences */
361 context_save (); /* save the context file */
370 write_content (CT *cts, char *outfile)
374 for (ctp = cts; *ctp; ctp++) {
376 output_message (ct, outfile);
390 fprintf (stderr, "\n");