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