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