include: Add d3dx9mesh.h X template extensions.
[wine] / tools / widl / widl.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2002 Ove Kaaven
5  * based on WRC code by Bertho Stultiens
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <signal.h>
36 #ifdef HAVE_GETOPT_H
37 # include <getopt.h>
38 #endif
39
40 #include "widl.h"
41 #include "utils.h"
42 #include "parser.h"
43 #include "wine/wpp.h"
44 #include "header.h"
45
46 /* future options to reserve characters for: */
47 /* A = ACF input filename */
48 /* J = do not search standard include path */
49 /* O = generate interpreted stubs */
50 /* w = select win16/win32 output (?) */
51
52 static const char usage[] =
53 "Usage: widl [options...] infile.idl\n"
54 "   or: widl [options...] --dlldata-only name1 [name2...]\n"
55 "   -b arch            Set the target architecture\n"
56 "   -c                 Generate client stub\n"
57 "   -d n               Set debug level to 'n'\n"
58 "   -D id[=val]        Define preprocessor identifier id=val\n"
59 "   -E                 Preprocess only\n"
60 "   -h                 Generate headers\n"
61 "   -H file            Name of header file (default is infile.h)\n"
62 "   -I path            Set include search dir to path (multiple -I allowed)\n"
63 "   --local-stubs=file Write empty stubs for call_as/local methods to file\n"
64 "   -m32, -m64         Set the kind of typelib to build (Win32 or Win64)\n"
65 "   -N                 Do not preprocess input\n"
66 "   --oldnames         Use old naming conventions\n"
67 "   -o, --output=NAME  Set the output file name\n"
68 "   -p                 Generate proxy\n"
69 "   --prefix-all=p     Prefix names of client stubs / server functions with 'p'\n"
70 "   --prefix-client=p  Prefix names of client stubs with 'p'\n"
71 "   --prefix-server=p  Prefix names of server functions with 'p'\n"
72 "   -r                 Generate registration script\n"
73 "   -s                 Generate server stub\n"
74 "   -t                 Generate typelib\n"
75 "   -u                 Generate interface identifiers file\n"
76 "   -V                 Print version and exit\n"
77 "   -W                 Enable pedantic warnings\n"
78 "   --win32            Only generate 32-bit code\n"
79 "   --win64            Only generate 64-bit code\n"
80 "   --win32-align n    Set win32 structure alignment to 'n'\n"
81 "   --win64-align n    Set win64 structure alignment to 'n'\n"
82 "Debug level 'n' is a bitmask with following meaning:\n"
83 "    * 0x01 Tell which resource is parsed (verbose mode)\n"
84 "    * 0x02 Dump internal structures\n"
85 "    * 0x04 Create a parser trace (yydebug=1)\n"
86 "    * 0x08 Preprocessor messages\n"
87 "    * 0x10 Preprocessor lex messages\n"
88 "    * 0x20 Preprocessor yacc trace\n"
89 ;
90
91 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
92                         "Copyright 2002 Ove Kaaven\n";
93
94 int debuglevel = DEBUGLEVEL_NONE;
95 int parser_debug, yy_flex_debug;
96
97 int pedantic = 0;
98 int do_everything = 1;
99 static int preprocess_only = 0;
100 int do_header = 0;
101 int do_typelib = 0;
102 int do_proxies = 0;
103 int do_client = 0;
104 int do_server = 0;
105 int do_regscript = 0;
106 int do_idfile = 0;
107 int do_dlldata = 0;
108 static int no_preprocess = 0;
109 int old_names = 0;
110 int do_win32 = 1;
111 int do_win64 = 1;
112 int win32_packing = 8;
113 int win64_packing = 8;
114
115 char *input_name;
116 char *header_name;
117 char *local_stubs_name;
118 char *header_token;
119 char *typelib_name;
120 char *dlldata_name;
121 char *proxy_name;
122 char *proxy_token;
123 char *client_name;
124 char *client_token;
125 char *server_name;
126 char *server_token;
127 char *regscript_name;
128 char *regscript_token;
129 static char *idfile_name;
130 static char *idfile_token;
131 char *temp_name;
132 const char *prefix_client = "";
133 const char *prefix_server = "";
134
135 int line_number = 1;
136
137 static FILE *idfile;
138
139 size_t pointer_size = 0;
140 syskind_t typelib_kind = sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32;
141
142 time_t now;
143
144 enum {
145     OLDNAMES_OPTION = CHAR_MAX + 1,
146     DLLDATA_OPTION,
147     DLLDATA_ONLY_OPTION,
148     LOCAL_STUBS_OPTION,
149     PREFIX_ALL_OPTION,
150     PREFIX_CLIENT_OPTION,
151     PREFIX_SERVER_OPTION,
152     WIN32_OPTION,
153     WIN64_OPTION,
154     WIN32_ALIGN_OPTION,
155     WIN64_ALIGN_OPTION
156 };
157
158 static const char short_options[] =
159     "b:cC:d:D:EhH:I:m:No:pP:rsS:tT:uU:VW";
160 static const struct option long_options[] = {
161     { "dlldata", 1, NULL, DLLDATA_OPTION },
162     { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
163     { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
164     { "oldnames", 0, NULL, OLDNAMES_OPTION },
165     { "output", 0, NULL, 'o' },
166     { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
167     { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
168     { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
169     { "win32", 0, NULL, WIN32_OPTION },
170     { "win64", 0, NULL, WIN64_OPTION },
171     { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
172     { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
173     { NULL, 0, NULL, 0 }
174 };
175
176 static void rm_tempfile(void);
177
178 static char *make_token(const char *name)
179 {
180   char *token;
181   char *slash;
182   int i;
183
184   slash = strrchr(name, '/');
185   if(!slash)
186     slash = strrchr(name, '\\');
187
188   if (slash) name = slash + 1;
189
190   token = xstrdup(name);
191   for (i=0; token[i]; i++) {
192     if (!isalnum(token[i])) token[i] = '_';
193     else token[i] = toupper(token[i]);
194   }
195   return token;
196 }
197
198 /* duplicate a basename into a valid C token */
199 static char *dup_basename_token(const char *name, const char *ext)
200 {
201     char *p, *ret = dup_basename( name, ext );
202     /* map invalid characters to '_' */
203     for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
204     return ret;
205 }
206
207 static void add_widl_version_define(void)
208 {
209     unsigned int version;
210     const char *p = PACKAGE_VERSION;
211
212     /* major */
213     version = atoi(p) * 0x10000;
214     p = strchr(p, '.');
215
216     /* minor */
217     if (p)
218     {
219         version += atoi(p + 1) * 0x100;
220         p = strchr(p + 1, '.');
221     }
222
223     /* build */
224     if (p)
225         version += atoi(p + 1);
226
227     if (version != 0)
228     {
229         char version_str[11];
230         snprintf(version_str, sizeof(version_str), "0x%x", version);
231         wpp_add_define("__WIDL__", version_str);
232     }
233     else
234         wpp_add_define("__WIDL__", NULL);
235 }
236
237 /* set the target platform */
238 static void set_target( const char *target )
239 {
240     static const struct
241     {
242         const char *name;
243         syskind_t   kind;
244     } cpu_names[] =
245     {
246         { "i386",    SYS_WIN32 },
247         { "i486",    SYS_WIN32 },
248         { "i586",    SYS_WIN32 },
249         { "i686",    SYS_WIN32 },
250         { "i786",    SYS_WIN32 },
251         { "amd64",   SYS_WIN64 },
252         { "x86_64",  SYS_WIN64 },
253         { "sparc",   SYS_WIN32 },
254         { "alpha",   SYS_WIN32 },
255         { "powerpc", SYS_WIN32 },
256         { "arm",     SYS_WIN32 }
257     };
258
259     unsigned int i;
260     char *p, *spec = xstrdup( target );
261
262     /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
263
264     if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
265     *p++ = 0;
266     for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
267     {
268         if (!strcmp( cpu_names[i].name, spec ))
269         {
270             typelib_kind = cpu_names[i].kind;
271             free( spec );
272             return;
273         }
274     }
275     error( "Unrecognized CPU '%s'\n", spec );
276 }
277
278 /* clean things up when aborting on a signal */
279 static void exit_on_signal( int sig )
280 {
281     exit(1);  /* this will call the atexit functions */
282 }
283
284 static void set_everything(int x)
285 {
286   do_header = x;
287   do_typelib = x;
288   do_proxies = x;
289   do_client = x;
290   do_server = x;
291   do_regscript = x;
292   do_idfile = x;
293   do_dlldata = x;
294 }
295
296 void start_cplusplus_guard(FILE *fp)
297 {
298   fprintf(fp, "#ifdef __cplusplus\n");
299   fprintf(fp, "extern \"C\" {\n");
300   fprintf(fp, "#endif\n\n");
301 }
302
303 void end_cplusplus_guard(FILE *fp)
304 {
305   fprintf(fp, "#ifdef __cplusplus\n");
306   fprintf(fp, "}\n");
307   fprintf(fp, "#endif\n\n");
308 }
309
310 typedef struct
311 {
312   char *filename;
313   struct list link;
314 } filename_node_t;
315
316 static void add_filename_node(struct list *list, const char *name)
317 {
318   filename_node_t *node = xmalloc(sizeof *node);
319   node->filename = dup_basename( name, ".idl" );
320   list_add_tail(list, &node->link);
321 }
322
323 static void free_filename_nodes(struct list *list)
324 {
325   filename_node_t *node, *next;
326   LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
327     list_remove(&node->link);
328     free(node->filename);
329     free(node);
330   }
331 }
332
333 static void write_dlldata_list(struct list *filenames)
334 {
335   FILE *dlldata;
336   filename_node_t *node;
337
338   dlldata = fopen(dlldata_name, "w");
339   if (!dlldata)
340     error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
341
342   fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
343   fprintf(dlldata, "- Do not edit ***/\n\n");
344   fprintf(dlldata, "#include <objbase.h>\n");
345   fprintf(dlldata, "#include <rpcproxy.h>\n\n");
346   start_cplusplus_guard(dlldata);
347
348   LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
349     fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
350
351   fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
352   fprintf(dlldata, "/* Start of list */\n");
353   LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
354     fprintf(dlldata, "  REFERENCE_PROXY_FILE(%s),\n", node->filename);
355   fprintf(dlldata, "/* End of list */\n");
356   fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
357
358   fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
359   end_cplusplus_guard(dlldata);
360   fclose(dlldata);
361 }
362
363 static char *eat_space(char *s)
364 {
365   while (isspace((unsigned char) *s))
366     ++s;
367   return s;
368 }
369
370 void write_dlldata(const statement_list_t *stmts)
371 {
372   struct list filenames = LIST_INIT(filenames);
373   filename_node_t *node;
374   FILE *dlldata;
375
376   if (!do_dlldata || !need_proxy_file(stmts))
377     return;
378
379   dlldata = fopen(dlldata_name, "r");
380   if (dlldata) {
381     static char marker[] = "REFERENCE_PROXY_FILE";
382     char *line = NULL;
383     size_t len = 0;
384
385     while (widl_getline(&line, &len, dlldata)) {
386       char *start, *end;
387       start = eat_space(line);
388       if (strncmp(start, marker, sizeof marker - 1) == 0) {
389         start = eat_space(start + sizeof marker - 1);
390         if (*start != '(')
391           continue;
392         end = start = eat_space(start + 1);
393         while (*end && *end != ')')
394           ++end;
395         if (*end != ')')
396           continue;
397         while (isspace((unsigned char) end[-1]))
398           --end;
399         *end = '\0';
400         if (start < end)
401           add_filename_node(&filenames, start);
402       }
403     }
404
405     if (ferror(dlldata))
406       error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
407
408     free(line);
409     fclose(dlldata);
410   }
411
412   LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
413     if (strcmp(proxy_token, node->filename) == 0) {
414       /* We're already in the list, no need to regenerate this file.  */
415       free_filename_nodes(&filenames);
416       return;
417     }
418
419   add_filename_node(&filenames, proxy_token);
420   write_dlldata_list(&filenames);
421   free_filename_nodes(&filenames);
422 }
423
424 static void write_id_data_stmts(const statement_list_t *stmts)
425 {
426   const statement_t *stmt;
427   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
428   {
429     if (stmt->type == STMT_TYPE)
430     {
431       const type_t *type = stmt->u.type;
432       if (type_get_type(type) == TYPE_INTERFACE)
433       {
434         const UUID *uuid;
435         if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
436           continue;
437         uuid = get_attrp(type->attrs, ATTR_UUID);
438         write_guid(idfile, is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
439                    type->name, uuid);
440       }
441       else if (type_get_type(type) == TYPE_COCLASS)
442       {
443         const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
444         write_guid(idfile, "CLSID", type->name, uuid);
445       }
446     }
447     else if (stmt->type == STMT_LIBRARY)
448     {
449       const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
450       write_guid(idfile, "LIBID", stmt->u.lib->name, uuid);
451       write_id_data_stmts(stmt->u.lib->stmts);
452     }
453   }
454 }
455
456 void write_id_data(const statement_list_t *stmts)
457 {
458   if (!do_idfile) return;
459
460   idfile_token = make_token(idfile_name);
461
462   idfile = fopen(idfile_name, "w");
463   if (! idfile) {
464     error("Could not open %s for output\n", idfile_name);
465     return;
466   }
467
468   fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
469   fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
470   fprintf(idfile, "#include <rpc.h>\n");
471   fprintf(idfile, "#include <rpcndr.h>\n\n");
472   fprintf(idfile, "#include <initguid.h>\n\n");
473   start_cplusplus_guard(idfile);
474
475   write_id_data_stmts(stmts);
476
477   fprintf(idfile, "\n");
478   end_cplusplus_guard(idfile);
479
480   fclose(idfile);
481 }
482
483 int main(int argc,char *argv[])
484 {
485   extern char* optarg;
486   extern int   optind;
487   int optc;
488   int ret = 0;
489   int opti = 0;
490   char *output_name = NULL;
491
492   signal( SIGTERM, exit_on_signal );
493   signal( SIGINT, exit_on_signal );
494 #ifdef SIGHUP
495   signal( SIGHUP, exit_on_signal );
496 #endif
497
498   now = time(NULL);
499
500   while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
501     switch(optc) {
502     case DLLDATA_OPTION:
503       dlldata_name = xstrdup(optarg);
504       break;
505     case DLLDATA_ONLY_OPTION:
506       do_everything = 0;
507       do_dlldata = 1;
508       break;
509     case LOCAL_STUBS_OPTION:
510       do_everything = 0;
511       local_stubs_name = xstrdup(optarg);
512       break;
513     case OLDNAMES_OPTION:
514       old_names = 1;
515       break;
516     case PREFIX_ALL_OPTION:
517       prefix_client = xstrdup(optarg);
518       prefix_server = xstrdup(optarg);
519       break;
520     case PREFIX_CLIENT_OPTION:
521       prefix_client = xstrdup(optarg);
522       break;
523     case PREFIX_SERVER_OPTION:
524       prefix_server = xstrdup(optarg);
525       break;
526     case WIN32_OPTION:
527       do_win32 = 1;
528       do_win64 = 0;
529       break;
530     case WIN64_OPTION:
531       do_win32 = 0;
532       do_win64 = 1;
533       break;
534     case WIN32_ALIGN_OPTION:
535       win32_packing = strtol(optarg, NULL, 0);
536       if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
537           error("Packing must be one of 2, 4 or 8\n");
538       break;
539     case WIN64_ALIGN_OPTION:
540       win64_packing = strtol(optarg, NULL, 0);
541       if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
542           error("Packing must be one of 2, 4 or 8\n");
543       break;
544     case 'b':
545       set_target( optarg );
546       break;
547     case 'c':
548       do_everything = 0;
549       do_client = 1;
550       break;
551     case 'C':
552       client_name = xstrdup(optarg);
553       break;
554     case 'd':
555       debuglevel = strtol(optarg, NULL, 0);
556       break;
557     case 'D':
558       wpp_add_cmdline_define(optarg);
559       break;
560     case 'E':
561       do_everything = 0;
562       preprocess_only = 1;
563       break;
564     case 'h':
565       do_everything = 0;
566       do_header = 1;
567       break;
568     case 'H':
569       header_name = xstrdup(optarg);
570       break;
571     case 'I':
572       wpp_add_include_path(optarg);
573       break;
574     case 'm':
575       if (!strcmp( optarg, "32" )) typelib_kind = SYS_WIN32;
576       else if (!strcmp( optarg, "64" )) typelib_kind = SYS_WIN64;
577       else error( "Invalid -m argument '%s'\n", optarg );
578       break;
579     case 'N':
580       no_preprocess = 1;
581       break;
582     case 'o':
583       output_name = xstrdup(optarg);
584       break;
585     case 'p':
586       do_everything = 0;
587       do_proxies = 1;
588       break;
589     case 'P':
590       proxy_name = xstrdup(optarg);
591       break;
592     case 'r':
593       do_everything = 0;
594       do_regscript = 1;
595       break;
596     case 's':
597       do_everything = 0;
598       do_server = 1;
599       break;
600     case 'S':
601       server_name = xstrdup(optarg);
602       break;
603     case 't':
604       do_everything = 0;
605       do_typelib = 1;
606       break;
607     case 'T':
608       typelib_name = xstrdup(optarg);
609       break;
610     case 'u':
611       do_everything = 0;
612       do_idfile = 1;
613       break;
614     case 'U':
615       idfile_name = xstrdup(optarg);
616       break;
617     case 'V':
618       printf("%s", version_string);
619       return 0;
620     case 'W':
621       pedantic = 1;
622       break;
623     default:
624       fprintf(stderr, "%s", usage);
625       return 1;
626     }
627   }
628
629   if(do_everything) {
630     set_everything(TRUE);
631   }
632
633   if (!output_name) output_name = dup_basename(input_name, ".idl");
634
635   if (!do_everything &&
636       do_header + do_typelib + do_proxies + do_client +
637       do_server + do_regscript + do_idfile + do_dlldata == 1)
638   {
639       if (do_header) header_name = output_name;
640       else if (do_typelib) typelib_name = output_name;
641       else if (do_proxies) proxy_name = output_name;
642       else if (do_client) client_name = output_name;
643       else if (do_server) server_name = output_name;
644       else if (do_regscript) regscript_name = output_name;
645       else if (do_idfile) idfile_name = output_name;
646       else if (do_dlldata) dlldata_name = output_name;
647   }
648
649   if (!dlldata_name && do_dlldata)
650     dlldata_name = xstrdup("dlldata.c");
651
652   if(optind < argc) {
653     if (do_dlldata && !do_everything) {
654       struct list filenames = LIST_INIT(filenames);
655       for ( ; optind < argc; ++optind)
656         add_filename_node(&filenames, argv[optind]);
657
658       write_dlldata_list(&filenames);
659       free_filename_nodes(&filenames);
660       return 0;
661     }
662     else if (optind != argc - 1) {
663       fprintf(stderr, "%s", usage);
664       return 1;
665     }
666     else
667       input_name = xstrdup(argv[optind]);
668   }
669   else {
670     fprintf(stderr, "%s", usage);
671     return 1;
672   }
673
674   if(debuglevel)
675   {
676     setbuf(stdout, NULL);
677     setbuf(stderr, NULL);
678   }
679
680   parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
681   yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
682
683   wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
684                  (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
685                  (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
686
687   if (!header_name) {
688     header_name = dup_basename(input_name, ".idl");
689     strcat(header_name, ".h");
690   }
691
692   if (!typelib_name && do_typelib) {
693     typelib_name = dup_basename(input_name, ".idl");
694     strcat(typelib_name, ".tlb");
695   }
696
697   if (!proxy_name && do_proxies) {
698     proxy_name = dup_basename(input_name, ".idl");
699     strcat(proxy_name, "_p.c");
700   }
701
702   if (!client_name && do_client) {
703     client_name = dup_basename(input_name, ".idl");
704     strcat(client_name, "_c.c");
705   }
706
707   if (!server_name && do_server) {
708     server_name = dup_basename(input_name, ".idl");
709     strcat(server_name, "_s.c");
710   }
711
712   if (!regscript_name && do_regscript) {
713     regscript_name = dup_basename(input_name, ".idl");
714     strcat(regscript_name, "_r.rgs");
715   }
716
717   if (!idfile_name && do_idfile) {
718     idfile_name = dup_basename(input_name, ".idl");
719     strcat(idfile_name, "_i.c");
720   }
721
722   if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
723   if (do_client) client_token = dup_basename_token(client_name,"_c.c");
724   if (do_server) server_token = dup_basename_token(server_name,"_s.c");
725   if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
726
727   add_widl_version_define();
728   wpp_add_define("_WIN32", NULL);
729
730   atexit(rm_tempfile);
731   if (!no_preprocess)
732   {
733     chat("Starting preprocess\n");
734
735     if (!preprocess_only)
736     {
737         FILE *output;
738         int fd;
739         char *name = xmalloc( strlen(header_name) + 8 );
740
741         strcpy( name, header_name );
742         strcat( name, ".XXXXXX" );
743
744         if ((fd = mkstemps( name, 0 )) == -1)
745             error("Could not generate a temp name from %s\n", name);
746
747         temp_name = name;
748         if (!(output = fdopen(fd, "wt")))
749             error("Could not open fd %s for writing\n", name);
750
751         ret = wpp_parse( input_name, output );
752         fclose( output );
753     }
754     else
755     {
756         ret = wpp_parse( input_name, stdout );
757     }
758
759     if(ret) exit(1);
760     if(preprocess_only) exit(0);
761     if(!(parser_in = fopen(temp_name, "r"))) {
762       fprintf(stderr, "Could not open %s for input\n", temp_name);
763       return 1;
764     }
765   }
766   else {
767     if(!(parser_in = fopen(input_name, "r"))) {
768       fprintf(stderr, "Could not open %s for input\n", input_name);
769       return 1;
770     }
771   }
772
773   header_token = make_token(header_name);
774
775   init_types();
776   ret = parser_parse();
777
778   fclose(parser_in);
779
780   if(ret) {
781     exit(1);
782   }
783
784   /* Everything has been done successfully, don't delete any files.  */
785   set_everything(FALSE);
786   local_stubs_name = NULL;
787
788   return 0;
789 }
790
791 static void rm_tempfile(void)
792 {
793   abort_import();
794   if(temp_name)
795     unlink(temp_name);
796   if (do_header)
797     unlink(header_name);
798   if (local_stubs_name)
799     unlink(local_stubs_name);
800   if (do_client)
801     unlink(client_name);
802   if (do_server)
803     unlink(server_name);
804   if (do_regscript)
805     unlink(regscript_name);
806   if (do_idfile)
807     unlink(idfile_name);
808   if (do_proxies)
809     unlink(proxy_name);
810   if (do_typelib)
811     unlink(typelib_name);
812 }