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 : mh_xstrdup(expanddir(cp));
160 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
161 adios(EX_USAGE, NULL, "missing argument to %s",
163 outfile = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
177 if (*cp == '+' || *cp == '@') {
179 adios(EX_USAGE, NULL, "only one folder at a time!");
181 folder = mh_xstrdup(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 = mh_xstrdup(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 cts = mh_xcalloc(2, sizeof(*cts));
215 if ((ct = parse_mime(file)))
219 ** message(s) are coming from a folder
222 app_msgarg(&msgs, seq_cur);
224 folder = getcurfol();
225 maildir = toabsdir(folder);
227 if (chdir(maildir) == NOTOK)
228 adios(EX_OSERR, maildir, "unable to change directory to");
230 /* read folder and create message structure */
231 if (!(mp = folder_read(folder)))
232 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
234 /* check for empty folder */
236 adios(EX_DATAERR, NULL, "no messages in %s", folder);
238 /* parse all the message ranges/sequences and set SELECTED */
239 for (msgnum = 0; msgnum < msgs.size; msgnum++)
240 if (!m_convert(mp, msgs.msgs[msgnum]))
242 seq_setprev(mp); /* set the previous-sequence */
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");