5b9f669f22b1d4760997d6f72a2b10b2abd46a99
[mmh] / uip / mhtest.c
1 /*
2 ** mhtest.c -- test harness for MIME routines
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <h/tws.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/utils.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <locale.h>
21 #include <sysexits.h>
22
23 static struct swit switches[] = {
24 #define VERBSW  0
25         { "verbose", 0 },
26 #define NVERBSW  1
27         { "noverbose", 2 },
28 #define FILESW  2
29         { "file file", 0 },
30 #define OUTFILESW  3
31         { "outfile file", 0 },
32 #define PARTSW  4
33         { "part number", 0 },
34 #define TYPESW  5
35         { "type content", 0 },
36 #define VERSIONSW  6
37         { "Version", 0 },
38 #define HELPSW  7
39         { "help", 0 },
40 #define DEBUGSW  8
41         { "debug", -5 },
42         { NULL, 0 }
43 };
44
45
46 /* mhparse.c */
47 extern char *tmp;  /* directory to place temp files */
48
49 /* mhmisc.c */
50 extern int npart;
51 extern int ntype;
52 extern char *parts[NPARTS + 1];
53 extern char *types[NTYPES + 1];
54 extern int userrs;
55
56 /*
57 ** This is currently needed to keep mhparse happy.
58 ** This needs to be changed.
59 */
60 pid_t xpid  = 0;
61
62 int debugsw = 0;
63 int verbosw = 0;
64
65 #define quitser pipeser
66
67 /* mhparse.c */
68 CT parse_mime(char *);
69
70 /* mhoutsbr.c */
71 int output_message(CT, char *);
72
73 /* mhmisc.c */
74 int part_ok(CT, int);
75 int type_ok(CT, int);
76 void set_endian(void);
77 void flush_errors(void);
78
79 /* mhfree.c */
80 void free_content(CT);
81 extern CT *cts;
82 void freects_done();
83
84 /*
85 ** static prototypes
86 */
87 static int write_content(CT *, char *);
88 static void pipeser(int);
89
90
91 int
92 main(int argc, char **argv)
93 {
94         int msgnum;
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;
100         CT ct, *ctp;
101
102         if (atexit(freects_done) != 0) {
103                 adios(EX_OSERR, NULL, "atexit failed");
104         }
105
106         setlocale(LC_ALL, "");
107         invo_name = mhbasename(argv[0]);
108
109         /* read user profile/context */
110         context_read();
111
112         arguments = getarguments(invo_name, argc, argv, 1);
113         argp = arguments;
114
115         /*
116         ** Parse arguments
117         */
118         while ((cp = *argp++)) {
119                 if (*cp == '-') {
120                         switch (smatch(++cp, switches)) {
121                         case AMBIGSW:
122                                 ambigsw(cp, switches);
123                                 exit(EX_USAGE);
124                         case UNKWNSW:
125                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
126
127                         case HELPSW:
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);
131                         case VERSIONSW:
132                                 print_version(invo_name);
133                                 exit(argc == 2 ? EX_OK : EX_USAGE);
134
135                         case PARTSW:
136                                 if (!(cp = *argp++) || *cp == '-')
137                                         adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
138                                 if (npart >= NPARTS)
139                                         adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
140                                 parts[npart++] = cp;
141                                 continue;
142
143                         case TYPESW:
144                                 if (!(cp = *argp++) || *cp == '-')
145                                         adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
146                                 if (ntype >= NTYPES)
147                                         adios(EX_USAGE, NULL, "too many types (starting with %s), %d max",
148                                                    cp, NTYPES);
149                                 types[ntype++] = cp;
150                                 continue;
151
152                         case FILESW:
153                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
154                                         adios(EX_USAGE, NULL, "missing argument to %s",
155                                                         argp[-2]);
156                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
157                                 continue;
158
159                         case OUTFILESW:
160                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
161                                         adios(EX_USAGE, NULL, "missing argument to %s",
162                                                         argp[-2]);
163                                 outfile = *cp == '-' ? cp : getcpy(expanddir(cp));
164                                 continue;
165
166                         case VERBSW:
167                                 verbosw = 1;
168                                 continue;
169                         case NVERBSW:
170                                 verbosw = 0;
171                                 continue;
172                         case DEBUGSW:
173                                 debugsw = 1;
174                                 continue;
175                         }
176                 }
177                 if (*cp == '+' || *cp == '@') {
178                         if (folder)
179                                 adios(EX_USAGE, NULL, "only one folder at a time!");
180                         else
181                                 folder = getcpy(expandfol(cp));
182                 } else
183                         app_msgarg(&msgs, cp);
184         }
185
186         /* null terminate the list of acceptable parts/types */
187         parts[npart] = NULL;
188         types[ntype] = NULL;
189
190         set_endian();
191
192         if (outfile == NULL)
193                 adios(EX_USAGE, NULL, "must specify output file");
194
195         /*
196         ** Check for storage directory.  If specified,
197         ** then store temporary files there.  Else we
198         ** store them in standard nmh directory.
199         */
200         if ((cp = context_find(nmhstorage)) && *cp)
201                 tmp = concat(cp, "/", invo_name, NULL);
202         else
203                 tmp = getcpy(toabsdir(invo_name));
204
205         if (file && msgs.size)
206                 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
207
208         /*
209         ** check if message is coming from file
210         */
211         if (file) {
212                 cts = mh_xcalloc(2, sizeof(*cts));
213                 ctp = cts;
214
215                 if ((ct = parse_mime(file)))
216                         *ctp++ = ct;
217         } else {
218                 /*
219                 ** message(s) are coming from a folder
220                 */
221                 if (!msgs.size)
222                         app_msgarg(&msgs, seq_cur);
223                 if (!folder)
224                         folder = getcurfol();
225                 maildir = toabsdir(folder);
226
227                 if (chdir(maildir) == NOTOK)
228                         adios(EX_OSERR, maildir, "unable to change directory to");
229
230                 /* read folder and create message structure */
231                 if (!(mp = folder_read(folder)))
232                         adios(EX_IOERR, NULL, "unable to read folder %s", folder);
233
234                 /* check for empty folder */
235                 if (mp->nummsg == 0)
236                         adios(EX_DATAERR, NULL, "no messages in %s", folder);
237
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]))
241                                 exit(EX_USAGE);
242                 seq_setprev(mp);  /* set the previous-sequence */
243
244                 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
245                 ctp = cts;
246
247                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
248                         if (is_selected(mp, msgnum)) {
249                                 char *msgnam;
250
251                                 msgnam = m_name(msgnum);
252                                 if ((ct = parse_mime(msgnam)))
253                                         *ctp++ = ct;
254                         }
255                 }
256         }
257
258         if (!*cts)
259                 exit(EX_SOFTWARE);
260
261         userrs = 1;
262         SIGNAL(SIGQUIT, quitser);
263         SIGNAL(SIGPIPE, pipeser);
264
265         /*
266         ** Get the associated umask for the relevant contents.
267         */
268         for (ctp = cts; *ctp; ctp++) {
269                 struct stat st;
270
271                 ct = *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);
275                         else
276                                 ct->c_umask = ~m_gmprot();
277                 }
278         }
279
280         /*
281         ** Write the content to a file
282         */
283         write_content(cts, outfile);
284
285         /* Now free all the structures for the content */
286         for (ctp = cts; *ctp; ctp++)
287                 free_content(*ctp);
288
289         free((char *) cts);
290         cts = NULL;
291
292         /* If reading from a folder, do some updating */
293         if (mp) {
294                 context_replace(curfolder, folder); /* update current folder */
295                 seq_setcur(mp, mp->hghsel);  /* update current message */
296                 seq_save(mp);  /* synchronize sequences  */
297                 context_save();  /* save the context file  */
298         }
299
300         return EX_OK;
301 }
302
303
304 static int
305 write_content(CT *cts, char *outfile)
306 {
307         CT ct, *ctp;
308
309         for (ctp = cts; *ctp; ctp++) {
310                 ct = *ctp;
311                 output_message(ct, outfile);
312         }
313
314         flush_errors();
315         return OK;
316 }
317
318
319 static void
320 pipeser(int i)
321 {
322         if (i == SIGQUIT) {
323                 unlink("core");
324                 fflush(stdout);
325                 fprintf(stderr, "\n");
326                 fflush(stderr);
327         }
328
329         exit(EX_IOERR);
330         /* NOTREACHED */
331 }