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>
19 static struct swit switches[] = {
27 { "outfile file", 0 },
31 { "type content", 0 },
42 int ebcdicsw = 0; /* hack for linking purposes */
45 extern char *tmp; /* directory to place temp files */
50 extern char *parts[NPARTS + 1];
51 extern char *types[NTYPES + 1];
55 ** This is currently needed to keep mhparse happy.
56 ** This needs to be changed.
63 #define quitser pipeser
66 CT parse_mime(char *);
69 int output_message(CT, char *);
74 void set_endian(void);
75 void flush_errors(void);
78 void free_content(CT);
80 void freects_done(int) NORETURN;
85 static int write_content(CT *, char *);
86 static void pipeser(int);
90 main(int argc, char **argv)
93 char *cp, *file = NULL, *folder = NULL;
94 char *maildir, buf[100], *outfile = NULL;
95 char **argp, **arguments;
96 struct msgs_array msgs = { 0, 0, NULL };
97 struct msgs *mp = NULL;
103 setlocale(LC_ALL, "");
105 invo_name = mhbasename(argv[0]);
107 /* read user profile/context */
110 arguments = getarguments(invo_name, argc, argv, 1);
116 while ((cp = *argp++)) {
118 switch (smatch(++cp, switches)) {
120 ambigsw(cp, switches);
123 adios(NULL, "-%s unknown", cp);
126 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
127 print_help(buf, switches, 1);
130 print_version(invo_name);
134 if (!(cp = *argp++) || *cp == '-')
135 adios(NULL, "missing argument to %s", argp[-2]);
137 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
142 if (!(cp = *argp++) || *cp == '-')
143 adios(NULL, "missing argument to %s", argp[-2]);
145 adios(NULL, "too many types (starting with %s), %d max",
151 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
152 adios(NULL, "missing argument to %s",
154 file = *cp == '-' ? cp : getcpy(expanddir(cp));
158 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
159 adios(NULL, "missing argument to %s",
161 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
175 if (*cp == '+' || *cp == '@') {
177 adios(NULL, "only one folder at a time!");
179 folder = getcpy(expandfol(cp));
181 app_msgarg(&msgs, cp);
184 /* null terminate the list of acceptable parts/types */
191 adios(NULL, "must specify output file");
194 ** Check for storage directory. If specified,
195 ** then store temporary files there. Else we
196 ** store them in standard nmh directory.
198 if ((cp = context_find(nmhstorage)) && *cp)
199 tmp = concat(cp, "/", invo_name, NULL);
201 tmp = getcpy(toabsdir(invo_name));
203 if (file && msgs.size)
204 adios(NULL, "cannot specify msg and file at same time!");
207 ** check if message is coming from file
210 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
211 adios(NULL, "out of memory");
214 if ((ct = parse_mime(file)))
218 ** message(s) are coming from a folder
221 app_msgarg(&msgs, seq_cur);
223 folder = getcurfol();
224 maildir = toabsdir(folder);
226 if (chdir(maildir) == NOTOK)
227 adios(maildir, "unable to change directory to");
229 /* read folder and create message structure */
230 if (!(mp = folder_read(folder)))
231 adios(NULL, "unable to read folder %s", folder);
233 /* check for empty folder */
235 adios(NULL, "no messages in %s", folder);
237 /* parse all the message ranges/sequences and set SELECTED */
238 for (msgnum = 0; msgnum < msgs.size; msgnum++)
239 if (!m_convert(mp, msgs.msgs[msgnum]))
241 seq_setprev(mp); /* set the previous-sequence */
243 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
245 adios(NULL, "out of memory");
248 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
249 if (is_selected(mp, msgnum)) {
252 msgnam = m_name(msgnum);
253 if ((ct = parse_mime(msgnam)))
263 SIGNAL(SIGQUIT, quitser);
264 SIGNAL(SIGPIPE, pipeser);
267 ** Get the associated umask for the relevant contents.
269 for (ctp = cts; *ctp; ctp++) {
273 if (type_ok(ct, 1) && !ct->c_umask) {
274 if (stat(ct->c_file, &st) != NOTOK)
275 ct->c_umask = ~(st.st_mode & 0777);
277 ct->c_umask = ~m_gmprot();
282 ** Write the content to a file
284 write_content(cts, outfile);
286 /* Now free all the structures for the content */
287 for (ctp = cts; *ctp; ctp++)
293 /* If reading from a folder, do some updating */
295 context_replace(curfolder, folder); /* update current folder */
296 seq_setcur(mp, mp->hghsel); /* update current message */
297 seq_save(mp); /* synchronize sequences */
298 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");