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>
23 static struct swit switches[] = {
31 { "outfile file", 0 },
35 { "type content", 0 },
45 char *version=VERSION;
48 extern char *tmp; /* directory to place temp files */
53 extern char *parts[NPARTS + 1];
54 extern char *types[NTYPES + 1];
58 ** This is currently needed to keep mhparse happy.
59 ** This needs to be changed.
66 #define quitser pipeser
69 CT parse_mime(char *);
72 int output_message(CT, char *);
77 void set_endian(void);
78 void flush_errors(void);
81 void free_content(CT);
88 static int write_content(CT *, char *);
89 static void pipeser(int);
93 main(int argc, char **argv)
96 char *cp, *file = NULL, *folder = NULL;
97 char *maildir, buf[100], *outfile = NULL;
98 char **argp, **arguments;
99 struct msgs_array msgs = { 0, 0, NULL };
100 struct msgs *mp = NULL;
103 if (atexit(freects_done) != 0) {
104 adios(EX_OSERR, NULL, "atexit failed");
107 setlocale(LC_ALL, "");
108 invo_name = mhbasename(argv[0]);
110 /* read user profile/context */
113 arguments = getarguments(invo_name, argc, argv, 1);
119 while ((cp = *argp++)) {
121 switch (smatch(++cp, switches)) {
123 ambigsw(cp, switches);
126 adios(EX_USAGE, NULL, "-%s unknown", cp);
129 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
130 print_help(buf, switches, 1);
131 exit(argc == 2 ? EX_OK : EX_USAGE);
133 print_version(invo_name);
134 exit(argc == 2 ? EX_OK : EX_USAGE);
137 if (!(cp = *argp++) || *cp == '-')
138 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
140 adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
145 if (!(cp = *argp++) || *cp == '-')
146 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
148 adios(EX_USAGE, NULL, "too many types (starting with %s), %d max",
154 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
155 adios(EX_USAGE, NULL, "missing argument to %s",
157 file = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
161 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
162 adios(EX_USAGE, NULL, "missing argument to %s",
164 outfile = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
178 if (*cp == '+' || *cp == '@') {
180 adios(EX_USAGE, NULL, "only one folder at a time!");
182 folder = mh_xstrdup(expandfol(cp));
184 app_msgarg(&msgs, cp);
187 /* null terminate the list of acceptable parts/types */
194 adios(EX_USAGE, NULL, "must specify output file");
197 ** Check for storage directory. If specified,
198 ** then store temporary files there. Else we
199 ** store them in standard nmh directory.
201 if ((cp = context_find(nmhstorage)) && *cp)
202 tmp = concat(cp, "/", invo_name, NULL);
204 tmp = mh_xstrdup(toabsdir(invo_name));
206 if (file && msgs.size)
207 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
210 ** check if message is coming from file
213 cts = mh_xcalloc(2, sizeof(*cts));
216 if ((ct = parse_mime(file)))
220 ** message(s) are coming from a folder
223 app_msgarg(&msgs, seq_cur);
225 folder = getcurfol();
226 maildir = toabsdir(folder);
228 if (chdir(maildir) == NOTOK)
229 adios(EX_OSERR, maildir, "unable to change directory to");
231 /* read folder and create message structure */
232 if (!(mp = folder_read(folder)))
233 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
235 /* check for empty folder */
237 adios(EX_DATAERR, NULL, "no messages in %s", folder);
239 /* parse all the message ranges/sequences and set SELECTED */
240 for (msgnum = 0; msgnum < msgs.size; msgnum++)
241 if (!m_convert(mp, msgs.msgs[msgnum]))
244 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
247 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
248 if (is_selected(mp, msgnum)) {
251 msgnam = m_name(msgnum);
252 if ((ct = parse_mime(msgnam)))
262 SIGNAL(SIGQUIT, quitser);
263 SIGNAL(SIGPIPE, pipeser);
266 ** Get the associated umask for the relevant contents.
268 for (ctp = cts; *ctp; ctp++) {
272 if (type_ok(ct, 1) && !ct->c_umask) {
273 if (stat(ct->c_file, &st) != NOTOK)
274 ct->c_umask = ~(st.st_mode & 0777);
276 ct->c_umask = ~m_gmprot();
281 ** Write the content to a file
283 write_content(cts, outfile);
285 /* Now free all the structures for the content */
286 for (ctp = cts; *ctp; ctp++)
291 /* If reading from a folder, do some updating */
293 context_replace(curfolder, folder); /* update current folder */
294 seq_setcur(mp, mp->hghsel); /* update current message */
295 seq_save(mp); /* synchronize sequences */
296 context_save(); /* save the context file */
304 write_content(CT *cts, char *outfile)
308 for (ctp = cts; *ctp; ctp++) {
310 output_message(ct, outfile);
324 fprintf(stderr, "\n");