add irc channel to README
[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 char *version=VERSION;
46
47 /* mhparse.c */
48 extern char *tmp;  /* directory to place temp files */
49
50 /* mhmisc.c */
51 extern int npart;
52 extern int ntype;
53 extern char *parts[NPARTS + 1];
54 extern char *types[NTYPES + 1];
55 extern int userrs;
56
57 /*
58 ** This is currently needed to keep mhparse happy.
59 ** This needs to be changed.
60 */
61 pid_t xpid  = 0;
62
63 int debugsw = 0;
64 int verbosw = 0;
65
66 #define quitser pipeser
67
68 /* mhparse.c */
69 CT parse_mime(char *);
70
71 /* mhoutsbr.c */
72 int output_message(CT, char *);
73
74 /* mhmisc.c */
75 int part_ok(CT, int);
76 int type_ok(CT, int);
77 void set_endian(void);
78 void flush_errors(void);
79
80 /* mhfree.c */
81 void free_content(CT);
82 extern CT *cts;
83 void freects_done();
84
85 /*
86 ** static prototypes
87 */
88 static int write_content(CT *, char *);
89 static void pipeser(int);
90
91
92 int
93 main(int argc, char **argv)
94 {
95         int msgnum;
96         char *cp, *file = NULL, *folder = NULL;
97         char *maildir, buf[100], *outfile = NULL;
98         char **argp, **arguments;
99         struct msgs_array msgs = { 0, 0, NULL };
100         struct msgs *mp = NULL;
101         CT ct, *ctp;
102
103         if (atexit(freects_done) != 0) {
104                 adios(EX_OSERR, NULL, "atexit failed");
105         }
106
107         setlocale(LC_ALL, "");
108         invo_name = mhbasename(argv[0]);
109
110         /* read user profile/context */
111         context_read();
112
113         arguments = getarguments(invo_name, argc, argv, 1);
114         argp = arguments;
115
116         /*
117         ** Parse arguments
118         */
119         while ((cp = *argp++)) {
120                 if (*cp == '-') {
121                         switch (smatch(++cp, switches)) {
122                         case AMBIGSW:
123                                 ambigsw(cp, switches);
124                                 exit(EX_USAGE);
125                         case UNKWNSW:
126                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
127
128                         case HELPSW:
129                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
130                                 print_help(buf, switches, 1);
131                                 exit(argc == 2 ? EX_OK : EX_USAGE);
132                         case VERSIONSW:
133                                 print_version(invo_name);
134                                 exit(argc == 2 ? EX_OK : EX_USAGE);
135
136                         case PARTSW:
137                                 if (!(cp = *argp++) || *cp == '-')
138                                         adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
139                                 if (npart >= NPARTS)
140                                         adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
141                                 parts[npart++] = cp;
142                                 continue;
143
144                         case TYPESW:
145                                 if (!(cp = *argp++) || *cp == '-')
146                                         adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
147                                 if (ntype >= NTYPES)
148                                         adios(EX_USAGE, NULL, "too many types (starting with %s), %d max",
149                                                    cp, NTYPES);
150                                 types[ntype++] = cp;
151                                 continue;
152
153                         case FILESW:
154                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
155                                         adios(EX_USAGE, NULL, "missing argument to %s",
156                                                         argp[-2]);
157                                 file = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
158                                 continue;
159
160                         case OUTFILESW:
161                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
162                                         adios(EX_USAGE, NULL, "missing argument to %s",
163                                                         argp[-2]);
164                                 outfile = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
165                                 continue;
166
167                         case VERBSW:
168                                 verbosw = 1;
169                                 continue;
170                         case NVERBSW:
171                                 verbosw = 0;
172                                 continue;
173                         case DEBUGSW:
174                                 debugsw = 1;
175                                 continue;
176                         }
177                 }
178                 if (*cp == '+' || *cp == '@') {
179                         if (folder)
180                                 adios(EX_USAGE, NULL, "only one folder at a time!");
181                         else
182                                 folder = mh_xstrdup(expandfol(cp));
183                 } else
184                         app_msgarg(&msgs, cp);
185         }
186
187         /* null terminate the list of acceptable parts/types */
188         parts[npart] = NULL;
189         types[ntype] = NULL;
190
191         set_endian();
192
193         if (outfile == NULL)
194                 adios(EX_USAGE, NULL, "must specify output file");
195
196         /*
197         ** Check for storage directory.  If specified,
198         ** then store temporary files there.  Else we
199         ** store them in standard nmh directory.
200         */
201         if ((cp = context_find(nmhstorage)) && *cp)
202                 tmp = concat(cp, "/", invo_name, NULL);
203         else
204                 tmp = mh_xstrdup(toabsdir(invo_name));
205
206         if (file && msgs.size)
207                 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
208
209         /*
210         ** check if message is coming from file
211         */
212         if (file) {
213                 cts = mh_xcalloc(2, sizeof(*cts));
214                 ctp = cts;
215
216                 if ((ct = parse_mime(file)))
217                         *ctp++ = ct;
218         } else {
219                 /*
220                 ** message(s) are coming from a folder
221                 */
222                 if (!msgs.size)
223                         app_msgarg(&msgs, seq_cur);
224                 if (!folder)
225                         folder = getcurfol();
226                 maildir = toabsdir(folder);
227
228                 if (chdir(maildir) == NOTOK)
229                         adios(EX_OSERR, maildir, "unable to change directory to");
230
231                 /* read folder and create message structure */
232                 if (!(mp = folder_read(folder)))
233                         adios(EX_IOERR, NULL, "unable to read folder %s", folder);
234
235                 /* check for empty folder */
236                 if (mp->nummsg == 0)
237                         adios(EX_DATAERR, NULL, "no messages in %s", folder);
238
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]))
242                                 exit(EX_USAGE);
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         mh_free0(&cts);
290
291         /* If reading from a folder, do some updating */
292         if (mp) {
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  */
297         }
298
299         return EX_OK;
300 }
301
302
303 static int
304 write_content(CT *cts, char *outfile)
305 {
306         CT ct, *ctp;
307
308         for (ctp = cts; *ctp; ctp++) {
309                 ct = *ctp;
310                 output_message(ct, outfile);
311         }
312
313         flush_errors();
314         return OK;
315 }
316
317
318 static void
319 pipeser(int i)
320 {
321         if (i == SIGQUIT) {
322                 unlink("core");
323                 fflush(stdout);
324                 fprintf(stderr, "\n");
325                 fflush(stderr);
326         }
327
328         exit(EX_IOERR);
329         /* NOTREACHED */
330 }