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;
101 setlocale(LC_ALL, "");
103 invo_name = mhbasename(argv[0]);
105 /* read user profile/context */
108 arguments = getarguments(invo_name, argc, argv, 1);
114 while ((cp = *argp++)) {
116 switch (smatch(++cp, switches)) {
118 ambigsw(cp, switches);
121 adios(NULL, "-%s unknown", cp);
124 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
125 print_help(buf, switches, 1);
128 print_version(invo_name);
132 if (!(cp = *argp++) || *cp == '-')
133 adios(NULL, "missing argument to %s", argp[-2]);
135 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
140 if (!(cp = *argp++) || *cp == '-')
141 adios(NULL, "missing argument to %s", argp[-2]);
143 adios(NULL, "too many types (starting with %s), %d max",
149 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
150 adios(NULL, "missing argument to %s",
152 file = *cp == '-' ? cp : getcpy(expanddir(cp));
156 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
157 adios(NULL, "missing argument to %s",
159 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
173 if (*cp == '+' || *cp == '@') {
175 adios(NULL, "only one folder at a time!");
177 folder = getcpy(expandfol(cp));
179 app_msgarg(&msgs, cp);
182 /* null terminate the list of acceptable parts/types */
189 adios(NULL, "must specify output file");
192 ** Check for storage directory. If specified,
193 ** then store temporary files there. Else we
194 ** store them in standard nmh directory.
196 if ((cp = context_find(nmhstorage)) && *cp)
197 tmp = concat(cp, "/", invo_name, NULL);
199 tmp = getcpy(toabsdir(invo_name));
201 if (file && msgs.size)
202 adios(NULL, "cannot specify msg and file at same time!");
205 ** check if message is coming from file
208 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
209 adios(NULL, "out of memory");
212 if ((ct = parse_mime(file)))
216 ** message(s) are coming from a folder
219 app_msgarg(&msgs, seq_cur);
221 folder = getcurfol();
222 maildir = toabsdir(folder);
224 if (chdir(maildir) == NOTOK)
225 adios(maildir, "unable to change directory to");
227 /* read folder and create message structure */
228 if (!(mp = folder_read(folder)))
229 adios(NULL, "unable to read folder %s", folder);
231 /* check for empty folder */
233 adios(NULL, "no messages in %s", folder);
235 /* parse all the message ranges/sequences and set SELECTED */
236 for (msgnum = 0; msgnum < msgs.size; msgnum++)
237 if (!m_convert(mp, msgs.msgs[msgnum]))
239 seq_setprev(mp); /* set the previous-sequence */
241 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
243 adios(NULL, "out of memory");
246 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
247 if (is_selected(mp, msgnum)) {
250 msgnam = m_name(msgnum);
251 if ((ct = parse_mime(msgnam)))
261 SIGNAL(SIGQUIT, quitser);
262 SIGNAL(SIGPIPE, pipeser);
265 ** Get the associated umask for the relevant contents.
267 for (ctp = cts; *ctp; ctp++) {
271 if (type_ok(ct, 1) && !ct->c_umask) {
272 if (stat(ct->c_file, &st) != NOTOK)
273 ct->c_umask = ~(st.st_mode & 0777);
275 ct->c_umask = ~m_gmprot();
280 ** Write the content to a file
282 write_content(cts, outfile);
284 /* Now free all the structures for the content */
285 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 */
305 write_content(CT *cts, char *outfile)
309 for (ctp = cts; *ctp; ctp++) {
311 output_message(ct, outfile);
325 fprintf(stderr, "\n");