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>
16 #include <h/mhparse.h>
22 static struct swit switches[] = {
30 { "outfile file", 0 },
34 { "type content", 0 },
46 extern char *tmp; /* directory to place temp files */
51 extern char *parts[NPARTS + 1];
52 extern char *types[NTYPES + 1];
56 ** This is currently needed to keep mhparse happy.
57 ** This needs to be changed.
64 #define quitser pipeser
67 CT parse_mime(char *);
70 int output_message(CT, char *);
75 void set_endian(void);
76 void flush_errors(void);
79 void free_content(CT);
86 static int write_content(CT *, char *);
87 static void pipeser(int);
91 main(int argc, char **argv)
94 char *cp, *file = NULL, *folder = NULL;
95 char *maildir, buf[100], *outfile = NULL;
96 char **argp, **arguments;
97 struct msgs_array msgs = { 0, 0, NULL };
98 struct msgs *mp = NULL;
101 if (atexit(freects_done) != 0) {
102 adios(NULL, "atexit failed");
105 setlocale(LC_ALL, "");
106 invo_name = mhbasename(argv[0]);
108 /* read user profile/context */
111 arguments = getarguments(invo_name, argc, argv, 1);
117 while ((cp = *argp++)) {
119 switch (smatch(++cp, switches)) {
121 ambigsw(cp, switches);
124 adios(NULL, "-%s unknown", cp);
127 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
128 print_help(buf, switches, 1);
131 print_version(invo_name);
135 if (!(cp = *argp++) || *cp == '-')
136 adios(NULL, "missing argument to %s", argp[-2]);
138 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
143 if (!(cp = *argp++) || *cp == '-')
144 adios(NULL, "missing argument to %s", argp[-2]);
146 adios(NULL, "too many types (starting with %s), %d max",
152 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
153 adios(NULL, "missing argument to %s",
155 file = *cp == '-' ? cp : getcpy(expanddir(cp));
159 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
160 adios(NULL, "missing argument to %s",
162 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
176 if (*cp == '+' || *cp == '@') {
178 adios(NULL, "only one folder at a time!");
180 folder = getcpy(expandfol(cp));
182 app_msgarg(&msgs, cp);
185 /* null terminate the list of acceptable parts/types */
192 adios(NULL, "must specify output file");
195 ** Check for storage directory. If specified,
196 ** then store temporary files there. Else we
197 ** store them in standard nmh directory.
199 if ((cp = context_find(nmhstorage)) && *cp)
200 tmp = concat(cp, "/", invo_name, NULL);
202 tmp = getcpy(toabsdir(invo_name));
204 if (file && msgs.size)
205 adios(NULL, "cannot specify msg and file at same time!");
208 ** check if message is coming from file
211 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
212 adios(NULL, "out of memory");
215 if ((ct = parse_mime(file)))
219 ** message(s) are coming from a folder
222 app_msgarg(&msgs, seq_cur);
224 folder = getcurfol();
225 maildir = toabsdir(folder);
227 if (chdir(maildir) == NOTOK)
228 adios(maildir, "unable to change directory to");
230 /* read folder and create message structure */
231 if (!(mp = folder_read(folder)))
232 adios(NULL, "unable to read folder %s", folder);
234 /* check for empty folder */
236 adios(NULL, "no messages in %s", folder);
238 /* parse all the message ranges/sequences and set SELECTED */
239 for (msgnum = 0; msgnum < msgs.size; msgnum++)
240 if (!m_convert(mp, msgs.msgs[msgnum]))
242 seq_setprev(mp); /* set the previous-sequence */
244 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
246 adios(NULL, "out of memory");
249 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
250 if (is_selected(mp, msgnum)) {
253 msgnam = m_name(msgnum);
254 if ((ct = parse_mime(msgnam)))
264 SIGNAL(SIGQUIT, quitser);
265 SIGNAL(SIGPIPE, pipeser);
268 ** Get the associated umask for the relevant contents.
270 for (ctp = cts; *ctp; ctp++) {
274 if (type_ok(ct, 1) && !ct->c_umask) {
275 if (stat(ct->c_file, &st) != NOTOK)
276 ct->c_umask = ~(st.st_mode & 0777);
278 ct->c_umask = ~m_gmprot();
283 ** Write the content to a file
285 write_content(cts, outfile);
287 /* Now free all the structures for the content */
288 for (ctp = cts; *ctp; ctp++)
294 /* If reading from a folder, do some updating */
296 context_replace(curfolder, folder); /* update current folder */
297 seq_setcur(mp, mp->hghsel); /* update current message */
298 seq_save(mp); /* synchronize sequences */
299 context_save(); /* save the context file */
307 write_content(CT *cts, char *outfile)
311 for (ctp = cts; *ctp; ctp++) {
313 output_message(ct, outfile);
327 fprintf(stderr, "\n");