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