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