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 *);
97 int part_ok (CT, int);
98 int type_ok (CT, int);
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 = r1bindex (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", argp[-2]);
166 switch (*icachesw = smatch (cp, caches)) {
168 ambigsw (cp, caches);
171 adios (NULL, "%s unknown", cp);
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
188 adios (NULL, "too many parts (starting with %s), %d max",
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", argp[-2]);
205 file = *cp == '-' ? cp : path (cp, TFILE);
209 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
210 adios (NULL, "missing argument to %s", argp[-2]);
211 outfile = *cp == '-' ? cp : path (cp, TFILE);
225 if (*cp == '+' || *cp == '@') {
227 adios (NULL, "only one folder at a time!");
229 folder = pluspath (cp);
231 app_msgarg(&msgs, cp);
234 /* null terminate the list of acceptable parts/types */
241 adios (NULL, "must specify output file");
243 /* Check for public cache location */
244 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
247 /* Check for private cache location */
248 if (!(cache_private = context_find (nmhprivcache)))
249 cache_private = ".cache";
250 cache_private = getcpy (m_maildir (cache_private));
253 ** Check for storage directory. If specified,
254 ** then store temporary files there. Else we
255 ** store them in standard nmh directory.
257 if ((cp = context_find (nmhstorage)) && *cp)
258 tmp = concat (cp, "/", invo_name, NULL);
260 tmp = add (m_maildir (invo_name), NULL);
262 if (!context_find ("path"))
263 free (path ("./", TFOLDER));
265 if (file && msgs.size)
266 adios (NULL, "cannot specify msg and file at same time!");
269 ** check if message is coming from file
272 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
273 adios (NULL, "out of memory");
276 if ((ct = parse_mime (file)));
280 ** message(s) are coming from a folder
283 app_msgarg(&msgs, "cur");
285 folder = getfolder (1);
286 maildir = m_maildir (folder);
288 if (chdir (maildir) == NOTOK)
289 adios (maildir, "unable to change directory to");
291 /* read folder and create message structure */
292 if (!(mp = folder_read (folder)))
293 adios (NULL, "unable to read folder %s", folder);
295 /* check for empty folder */
297 adios (NULL, "no messages in %s", folder);
299 /* parse all the message ranges/sequences and set SELECTED */
300 for (msgnum = 0; msgnum < msgs.size; msgnum++)
301 if (!m_convert (mp, msgs.msgs[msgnum]))
303 seq_setprev (mp); /* set the previous-sequence */
305 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
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 (pfolder, 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");