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 },
43 extern char *tmp; /* directory to place temp files */
48 extern char *parts[NPARTS + 1];
49 extern char *types[NTYPES + 1];
53 ** This is currently needed to keep mhparse happy.
54 ** This needs to be changed.
61 #define quitser pipeser
64 CT parse_mime(char *);
67 int output_message(CT, char *);
72 void set_endian(void);
73 void flush_errors(void);
76 void free_content(CT);
78 void freects_done(int) NORETURN;
83 static int write_content(CT *, char *);
84 static void pipeser(int);
88 main(int argc, char **argv)
91 char *cp, *file = NULL, *folder = NULL;
92 char *maildir, buf[100], *outfile = NULL;
93 char **argp, **arguments;
94 struct msgs_array msgs = { 0, 0, NULL };
95 struct msgs *mp = NULL;
100 setlocale(LC_ALL, "");
101 invo_name = mhbasename(argv[0]);
103 /* read user profile/context */
106 arguments = getarguments(invo_name, argc, argv, 1);
112 while ((cp = *argp++)) {
114 switch (smatch(++cp, switches)) {
116 ambigsw(cp, switches);
119 adios(NULL, "-%s unknown", cp);
122 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
123 print_help(buf, switches, 1);
126 print_version(invo_name);
130 if (!(cp = *argp++) || *cp == '-')
131 adios(NULL, "missing argument to %s", argp[-2]);
133 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
138 if (!(cp = *argp++) || *cp == '-')
139 adios(NULL, "missing argument to %s", argp[-2]);
141 adios(NULL, "too many types (starting with %s), %d max",
147 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
148 adios(NULL, "missing argument to %s",
150 file = *cp == '-' ? cp : getcpy(expanddir(cp));
154 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
155 adios(NULL, "missing argument to %s",
157 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
171 if (*cp == '+' || *cp == '@') {
173 adios(NULL, "only one folder at a time!");
175 folder = getcpy(expandfol(cp));
177 app_msgarg(&msgs, cp);
180 /* null terminate the list of acceptable parts/types */
187 adios(NULL, "must specify output file");
190 ** Check for storage directory. If specified,
191 ** then store temporary files there. Else we
192 ** store them in standard nmh directory.
194 if ((cp = context_find(nmhstorage)) && *cp)
195 tmp = concat(cp, "/", invo_name, NULL);
197 tmp = getcpy(toabsdir(invo_name));
199 if (file && msgs.size)
200 adios(NULL, "cannot specify msg and file at same time!");
203 ** check if message is coming from file
206 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
207 adios(NULL, "out of memory");
210 if ((ct = parse_mime(file)))
214 ** message(s) are coming from a folder
217 app_msgarg(&msgs, seq_cur);
219 folder = getcurfol();
220 maildir = toabsdir(folder);
222 if (chdir(maildir) == NOTOK)
223 adios(maildir, "unable to change directory to");
225 /* read folder and create message structure */
226 if (!(mp = folder_read(folder)))
227 adios(NULL, "unable to read folder %s", folder);
229 /* check for empty folder */
231 adios(NULL, "no messages in %s", folder);
233 /* parse all the message ranges/sequences and set SELECTED */
234 for (msgnum = 0; msgnum < msgs.size; msgnum++)
235 if (!m_convert(mp, msgs.msgs[msgnum]))
237 seq_setprev(mp); /* set the previous-sequence */
239 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
241 adios(NULL, "out of memory");
244 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
245 if (is_selected(mp, msgnum)) {
248 msgnam = m_name(msgnum);
249 if ((ct = parse_mime(msgnam)))
259 SIGNAL(SIGQUIT, quitser);
260 SIGNAL(SIGPIPE, pipeser);
263 ** Get the associated umask for the relevant contents.
265 for (ctp = cts; *ctp; ctp++) {
269 if (type_ok(ct, 1) && !ct->c_umask) {
270 if (stat(ct->c_file, &st) != NOTOK)
271 ct->c_umask = ~(st.st_mode & 0777);
273 ct->c_umask = ~m_gmprot();
278 ** Write the content to a file
280 write_content(cts, outfile);
282 /* Now free all the structures for the content */
283 for (ctp = cts; *ctp; ctp++)
289 /* If reading from a folder, do some updating */
291 context_replace(curfolder, folder); /* update current folder */
292 seq_setcur(mp, mp->hghsel); /* update current message */
293 seq_save(mp); /* synchronize sequences */
294 context_save(); /* save the context file */
303 write_content(CT *cts, char *outfile)
307 for (ctp = cts; *ctp; ctp++) {
309 output_message(ct, outfile);
323 fprintf(stderr, "\n");