3 * mhtest.c -- test harness for MIME routines
10 #include <h/signals.h>
14 #include <zotnet/mts/mts.h>
15 #include <zotnet/tws/tws.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
25 * We allocate space for message names (msgs array)
26 * this number of elements at a time.
31 static struct swit switches[] = {
43 { "outfile file", 0 },
47 { "type content", 0 },
49 { "rcache policy", 0 },
51 { "wcache policy", 0 },
58 * switches for debugging
68 int ebcdicsw = 0; /* hack for linking purposes */
72 extern char *tmp; /* directory to place temp files */
77 extern char *cache_public;
78 extern char *cache_private;
83 extern char *parts[NPARTS + 1];
84 extern char *types[NTYPES + 1];
88 * This is currently needed to keep mhparse happy.
89 * This needs to be changed.
96 /* The list of top-level contents to display */
99 #define quitser pipeser
102 CT parse_mime (char *);
105 int output_message (CT, char *);
108 int part_ok (CT, int);
109 int type_ok (CT, int);
110 void set_endian (void);
111 void flush_errors (void);
114 void free_content (CT);
119 static int write_content (CT *, char *);
120 static RETSIGTYPE pipeser (int);
124 main (int argc, char **argv)
126 int nummsgs, maxmsgs, msgnum, *icachesw;
127 char *cp, *file = NULL, *folder = NULL;
128 char *maildir, buf[100], *outfile = NULL;
129 char **argp, **arguments, **msgs;
130 struct msgs *mp = NULL;
134 setlocale(LC_ALL, "");
136 invo_name = r1bindex (argv[0], '/');
138 /* read user profile/context */
141 arguments = getarguments (invo_name, argc, argv, 1);
145 * Allocate the initial space to record message
146 * names, ranges, and sequences.
150 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
151 adios (NULL, "unable to allocate storage");
156 while ((cp = *argp++)) {
158 switch (smatch (++cp, switches)) {
160 ambigsw (cp, switches);
163 adios (NULL, "-%s unknown", cp);
166 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
168 print_help (buf, switches, 1);
171 print_version(invo_name);
175 icachesw = &rcachesw;
178 icachesw = &wcachesw;
180 if (!(cp = *argp++) || *cp == '-')
181 adios (NULL, "missing argument to %s", argp[-2]);
182 switch (*icachesw = smatch (cp, caches)) {
184 ambigsw (cp, caches);
187 adios (NULL, "%s unknown", cp);
201 if (!(cp = *argp++) || *cp == '-')
202 adios (NULL, "missing argument to %s", argp[-2]);
204 adios (NULL, "too many parts (starting with %s), %d max",
210 if (!(cp = *argp++) || *cp == '-')
211 adios (NULL, "missing argument to %s", argp[-2]);
213 adios (NULL, "too many types (starting with %s), %d max",
219 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
220 adios (NULL, "missing argument to %s", argp[-2]);
221 file = *cp == '-' ? cp : path (cp, TFILE);
225 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
226 adios (NULL, "missing argument to %s", argp[-2]);
227 outfile = *cp == '-' ? cp : path (cp, TFILE);
241 if (*cp == '+' || *cp == '@') {
243 adios (NULL, "only one folder at a time!");
245 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
248 * Check if we need to allocate more space
249 * for message names/ranges/sequences.
251 if (nummsgs >= maxmsgs) {
253 if (!(msgs = (char **) realloc (msgs,
254 (size_t) (maxmsgs * sizeof(*msgs)))))
255 adios (NULL, "unable to reallocate msgs storage");
257 msgs[nummsgs++] = cp;
261 /* null terminate the list of acceptable parts/types */
268 adios (NULL, "must specify output file");
270 /* Check for public cache location */
271 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
274 /* Check for private cache location */
275 if (!(cache_private = context_find (nmhprivcache)))
276 cache_private = ".cache";
277 cache_private = getcpy (m_maildir (cache_private));
280 * Check for storage directory. If specified,
281 * then store temporary files there. Else we
282 * store them in standard nmh directory.
284 if ((cp = context_find (nmhstorage)) && *cp)
285 tmp = concat (cp, "/", invo_name, NULL);
287 tmp = add (m_maildir (invo_name), NULL);
289 if (!context_find ("path"))
290 free (path ("./", TFOLDER));
293 adios (NULL, "cannot specify msg and file at same time!");
296 * check if message is coming from file
299 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
300 adios (NULL, "out of memory");
303 if ((ct = parse_mime (file)));
307 * message(s) are coming from a folder
310 msgs[nummsgs++] = "cur";
312 folder = getfolder (1);
313 maildir = m_maildir (folder);
315 if (chdir (maildir) == NOTOK)
316 adios (maildir, "unable to change directory to");
318 /* read folder and create message structure */
319 if (!(mp = folder_read (folder)))
320 adios (NULL, "unable to read folder %s", folder);
322 /* check for empty folder */
324 adios (NULL, "no messages in %s", folder);
326 /* parse all the message ranges/sequences and set SELECTED */
327 for (msgnum = 0; msgnum < nummsgs; msgnum++)
328 if (!m_convert (mp, msgs[msgnum]))
330 seq_setprev (mp); /* set the previous-sequence */
332 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
333 adios (NULL, "out of memory");
336 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
337 if (is_selected(mp, msgnum)) {
340 msgnam = m_name (msgnum);
341 if ((ct = parse_mime (msgnam)))
351 SIGNAL (SIGQUIT, quitser);
352 SIGNAL (SIGPIPE, pipeser);
355 * Get the associated umask for the relevant contents.
357 for (ctp = cts; *ctp; ctp++) {
361 if (type_ok (ct, 1) && !ct->c_umask) {
362 if (stat (ct->c_file, &st) != NOTOK)
363 ct->c_umask = ~(st.st_mode & 0777);
365 ct->c_umask = ~m_gmprot();
370 * Write the content to a file
372 write_content (cts, outfile);
374 /* Now free all the structures for the content */
375 for (ctp = cts; *ctp; ctp++)
381 /* If reading from a folder, do some updating */
383 context_replace (pfolder, folder);/* update current folder */
384 seq_setcur (mp, mp->hghsel); /* update current message */
385 seq_save (mp); /* synchronize sequences */
386 context_save (); /* save the context file */
395 write_content (CT *cts, char *outfile)
399 for (ctp = cts; *ctp; ctp++) {
401 output_message (ct, outfile);
415 fprintf (stderr, "\n");