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 },
47 extern char *tmp; /* directory to place temp files */
52 extern char *parts[NPARTS + 1];
53 extern char *types[NTYPES + 1];
57 ** This is currently needed to keep mhparse happy.
58 ** This needs to be changed.
65 #define quitser pipeser
68 CT parse_mime(char *);
71 int output_message(CT, char *);
76 void set_endian(void);
77 void flush_errors(void);
80 void free_content(CT);
87 static int write_content(CT *, char *);
88 static void pipeser(int);
92 main(int argc, char **argv)
95 char *cp, *file = NULL, *folder = NULL;
96 char *maildir, buf[100], *outfile = NULL;
97 char **argp, **arguments;
98 struct msgs_array msgs = { 0, 0, NULL };
99 struct msgs *mp = NULL;
102 if (atexit(freects_done) != 0) {
103 adios(EX_OSERR, NULL, "atexit failed");
106 setlocale(LC_ALL, "");
107 invo_name = mhbasename(argv[0]);
109 /* read user profile/context */
112 arguments = getarguments(invo_name, argc, argv, 1);
118 while ((cp = *argp++)) {
120 switch (smatch(++cp, switches)) {
122 ambigsw(cp, switches);
125 adios(EX_USAGE, NULL, "-%s unknown", cp);
128 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
129 print_help(buf, switches, 1);
130 exit(argc == 2 ? EX_OK : EX_USAGE);
132 print_version(invo_name);
133 exit(argc == 2 ? EX_OK : EX_USAGE);
136 if (!(cp = *argp++) || *cp == '-')
137 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
139 adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
144 if (!(cp = *argp++) || *cp == '-')
145 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
147 adios(EX_USAGE, NULL, "too many types (starting with %s), %d max",
153 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
154 adios(EX_USAGE, NULL, "missing argument to %s",
156 file = *cp == '-' ? cp : getcpy(expanddir(cp));
160 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
161 adios(EX_USAGE, NULL, "missing argument to %s",
163 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
177 if (*cp == '+' || *cp == '@') {
179 adios(EX_USAGE, NULL, "only one folder at a time!");
181 folder = getcpy(expandfol(cp));
183 app_msgarg(&msgs, cp);
186 /* null terminate the list of acceptable parts/types */
193 adios(EX_USAGE, NULL, "must specify output file");
196 ** Check for storage directory. If specified,
197 ** then store temporary files there. Else we
198 ** store them in standard nmh directory.
200 if ((cp = context_find(nmhstorage)) && *cp)
201 tmp = concat(cp, "/", invo_name, NULL);
203 tmp = getcpy(toabsdir(invo_name));
205 if (file && msgs.size)
206 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
209 ** check if message is coming from file
212 if (!(cts = (CT *) mh_xcalloc((size_t) 2, sizeof(*cts))))
213 adios(EX_OSERR, NULL, "out of memory");
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]))
243 seq_setprev(mp); /* set the previous-sequence */
245 if (!(cts = (CT *) mh_xcalloc((size_t) (mp->numsel + 1),
247 adios(EX_OSERR, NULL, "out of memory");
250 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
251 if (is_selected(mp, msgnum)) {
254 msgnam = m_name(msgnum);
255 if ((ct = parse_mime(msgnam)))
265 SIGNAL(SIGQUIT, quitser);
266 SIGNAL(SIGPIPE, pipeser);
269 ** Get the associated umask for the relevant contents.
271 for (ctp = cts; *ctp; ctp++) {
275 if (type_ok(ct, 1) && !ct->c_umask) {
276 if (stat(ct->c_file, &st) != NOTOK)
277 ct->c_umask = ~(st.st_mode & 0777);
279 ct->c_umask = ~m_gmprot();
284 ** Write the content to a file
286 write_content(cts, outfile);
288 /* Now free all the structures for the content */
289 for (ctp = cts; *ctp; ctp++)
295 /* If reading from a folder, do some updating */
297 context_replace(curfolder, folder); /* update current folder */
298 seq_setcur(mp, mp->hghsel); /* update current message */
299 seq_save(mp); /* synchronize sequences */
300 context_save(); /* save the context file */
308 write_content(CT *cts, char *outfile)
312 for (ctp = cts; *ctp; ctp++) {
314 output_message(ct, outfile);
328 fprintf(stderr, "\n");