widl: Replace erroneously removed current_func assignment.
[wine/winequartzdrv.git] / tools / widl / widl.c
blobed09b32089a5957f78d2f90b9844d122a3ae7342
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
5 * based on WRC code by Bertho Stultiens
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <string.h>
32 #include <assert.h>
33 #include <ctype.h>
34 #include <signal.h>
35 #ifdef HAVE_GETOPT_H
36 # include <getopt.h>
37 #endif
39 #include "widl.h"
40 #include "utils.h"
41 #include "parser.h"
42 #include "wine/wpp.h"
43 #include "header.h"
45 /* future options to reserve characters for: */
46 /* a = alignment of structures */
47 /* A = ACF input filename */
48 /* J = do not search standard include path */
49 /* O = generate interpreted stubs */
50 /* w = select win16/win32 output (?) */
52 static char usage[] =
53 "Usage: widl [options...] infile.idl\n"
54 " -c Generate client stub\n"
55 " -C file Name of client stub file (default is infile_c.c)\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 " -N Do not preprocess input\n"
63 " --oldnames Use old naming conventions\n"
64 " -p Generate proxy\n"
65 " -P file Name of proxy file (default is infile_p.c)\n"
66 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
67 " --prefix-client=p Prefix names of client stubs with 'p'\n"
68 " --prefix-server=p Prefix names of server functions with 'p'\n"
69 " -s Generate server stub\n"
70 " -S file Name of server stub file (default is infile_s.c)\n"
71 " -t Generate typelib\n"
72 " -T file Name of typelib file (default is infile.tlb)\n"
73 " -u Generate interface identifiers file\n"
74 " -U file Name of interface identifiers file (default is infile_i.c)\n"
75 " -V Print version and exit\n"
76 " -W Enable pedantic warnings\n"
77 "Debug level 'n' is a bitmask with following meaning:\n"
78 " * 0x01 Tell which resource is parsed (verbose mode)\n"
79 " * 0x02 Dump internal structures\n"
80 " * 0x04 Create a parser trace (yydebug=1)\n"
81 " * 0x08 Preprocessor messages\n"
82 " * 0x10 Preprocessor lex messages\n"
83 " * 0x20 Preprocessor yacc trace\n"
86 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
87 "Copyright 2002 Ove Kaaven\n";
89 int win32 = 1;
90 int debuglevel = DEBUGLEVEL_NONE;
91 int parser_debug, yy_flex_debug;
93 int pedantic = 0;
94 int do_everything = 1;
95 int preprocess_only = 0;
96 int do_header = 0;
97 int do_typelib = 0;
98 int do_proxies = 0;
99 int do_client = 0;
100 int do_server = 0;
101 int do_idfile = 0;
102 int no_preprocess = 0;
103 int old_names = 0;
105 char *input_name;
106 char *header_name;
107 char *header_token;
108 char *typelib_name;
109 char *proxy_name;
110 char *proxy_token;
111 char *client_name;
112 char *client_token;
113 char *server_name;
114 char *server_token;
115 char *idfile_name;
116 char *idfile_token;
117 char *temp_name;
118 const char *prefix_client = "";
119 const char *prefix_server = "";
121 int line_number = 1;
123 FILE *header;
124 FILE *proxy;
125 FILE *idfile;
127 time_t now;
129 enum {
130 OLDNAMES_OPTION = CHAR_MAX + 1,
131 PREFIX_ALL_OPTION,
132 PREFIX_CLIENT_OPTION,
133 PREFIX_SERVER_OPTION
136 static const char *short_options =
137 "cC:d:D:EhH:I:NpP:sS:tT:uU:VW";
138 static struct option long_options[] = {
139 { "oldnames", no_argument, 0, OLDNAMES_OPTION },
140 { "prefix-all", required_argument, 0, PREFIX_ALL_OPTION },
141 { "prefix-client", required_argument, 0, PREFIX_CLIENT_OPTION },
142 { "prefix-server", required_argument, 0, PREFIX_SERVER_OPTION },
143 { 0, 0, 0, 0 }
146 static void rm_tempfile(void);
147 static void segvhandler(int sig);
149 static char *make_token(const char *name)
151 char *token;
152 char *slash;
153 int i;
155 slash = strrchr(name, '/');
156 if (slash) name = slash + 1;
158 token = xstrdup(name);
159 for (i=0; token[i]; i++) {
160 if (!isalnum(token[i])) token[i] = '_';
161 else token[i] = toupper(token[i]);
163 return token;
166 /* duplicate a basename into a valid C token */
167 static char *dup_basename_token(const char *name, const char *ext)
169 char *p, *ret = dup_basename( name, ext );
170 /* map invalid characters to '_' */
171 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
172 return ret;
175 /* clean things up when aborting on a signal */
176 static void exit_on_signal( int sig )
178 exit(1); /* this will call the atexit functions */
181 int main(int argc,char *argv[])
183 extern char* optarg;
184 extern int optind;
185 int optc;
186 int ret = 0;
187 int opti = 0;
189 signal(SIGSEGV, segvhandler);
190 signal( SIGTERM, exit_on_signal );
191 signal( SIGINT, exit_on_signal );
192 #ifdef SIGHUP
193 signal( SIGHUP, exit_on_signal );
194 #endif
196 now = time(NULL);
198 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
199 switch(optc) {
200 case OLDNAMES_OPTION:
201 old_names = 1;
202 break;
203 case PREFIX_ALL_OPTION:
204 prefix_client = xstrdup(optarg);
205 prefix_server = xstrdup(optarg);
206 break;
207 case PREFIX_CLIENT_OPTION:
208 prefix_client = xstrdup(optarg);
209 break;
210 case PREFIX_SERVER_OPTION:
211 prefix_server = xstrdup(optarg);
212 break;
213 case 'c':
214 do_everything = 0;
215 do_client = 1;
216 break;
217 case 'C':
218 client_name = xstrdup(optarg);
219 break;
220 case 'd':
221 debuglevel = strtol(optarg, NULL, 0);
222 break;
223 case 'D':
224 wpp_add_cmdline_define(optarg);
225 break;
226 case 'E':
227 do_everything = 0;
228 preprocess_only = 1;
229 break;
230 case 'h':
231 do_everything = 0;
232 do_header = 1;
233 break;
234 case 'H':
235 header_name = xstrdup(optarg);
236 break;
237 case 'I':
238 wpp_add_include_path(optarg);
239 break;
240 case 'N':
241 no_preprocess = 1;
242 break;
243 case 'p':
244 do_everything = 0;
245 do_proxies = 1;
246 break;
247 case 'P':
248 proxy_name = xstrdup(optarg);
249 break;
250 case 's':
251 do_everything = 0;
252 do_server = 1;
253 break;
254 case 'S':
255 server_name = xstrdup(optarg);
256 break;
257 case 't':
258 do_everything = 0;
259 do_typelib = 1;
260 break;
261 case 'T':
262 typelib_name = xstrdup(optarg);
263 break;
264 case 'u':
265 do_everything = 0;
266 do_idfile = 1;
267 break;
268 case 'U':
269 idfile_name = xstrdup(optarg);
270 break;
271 case 'V':
272 printf(version_string);
273 return 0;
274 case 'W':
275 pedantic = 1;
276 break;
277 default:
278 fprintf(stderr, usage);
279 return 1;
283 if(do_everything) {
284 do_header = do_typelib = do_proxies = do_client = do_server = do_idfile = 1;
286 if(optind < argc) {
287 input_name = xstrdup(argv[optind]);
289 else {
290 fprintf(stderr, usage);
291 return 1;
294 if(debuglevel)
296 setbuf(stdout,0);
297 setbuf(stderr,0);
300 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
301 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
303 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
304 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
305 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
307 if (!header_name) {
308 header_name = dup_basename(input_name, ".idl");
309 strcat(header_name, ".h");
312 if (!typelib_name && do_typelib) {
313 typelib_name = dup_basename(input_name, ".idl");
314 strcat(typelib_name, ".tlb");
317 if (!proxy_name && do_proxies) {
318 proxy_name = dup_basename(input_name, ".idl");
319 strcat(proxy_name, "_p.c");
322 if (!client_name && do_client) {
323 client_name = dup_basename(input_name, ".idl");
324 strcat(client_name, "_c.c");
327 if (!server_name && do_server) {
328 server_name = dup_basename(input_name, ".idl");
329 strcat(server_name, "_s.c");
332 if (!idfile_name && do_idfile) {
333 idfile_name = dup_basename(input_name, ".idl");
334 strcat(idfile_name, "_i.c");
337 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
338 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
339 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
341 wpp_add_cmdline_define("__WIDL__");
343 atexit(rm_tempfile);
344 if (!no_preprocess)
346 chat("Starting preprocess\n");
348 if (!preprocess_only)
350 ret = wpp_parse_temp( input_name, header_name, &temp_name );
352 else
354 ret = wpp_parse( input_name, stdout );
357 if(ret) exit(1);
358 if(preprocess_only) exit(0);
359 if(!(parser_in = fopen(temp_name, "r"))) {
360 fprintf(stderr, "Could not open %s for input\n", temp_name);
361 return 1;
364 else {
365 if(!(parser_in = fopen(input_name, "r"))) {
366 fprintf(stderr, "Could not open %s for input\n", input_name);
367 return 1;
371 if(do_header) {
372 header_token = make_token(header_name);
374 if(!(header = fopen(header_name, "w"))) {
375 fprintf(stderr, "Could not open %s for output\n", header_name);
376 return 1;
378 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
379 fprintf(header, "#include <rpc.h>\n" );
380 fprintf(header, "#include <rpcndr.h>\n\n" );
381 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
382 fprintf(header, "#define __WIDL_%s\n", header_token);
383 fprintf(header, "#ifdef __cplusplus\n");
384 fprintf(header, "extern \"C\" {\n");
385 fprintf(header, "#endif\n");
388 if (do_idfile) {
389 idfile_token = make_token(idfile_name);
391 idfile = fopen(idfile_name, "w");
392 if (! idfile) {
393 fprintf(stderr, "Could not open %s for output\n", idfile_name);
394 return 1;
397 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
398 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
399 fprintf(idfile, "#include <rpc.h>\n");
400 fprintf(idfile, "#include <rpcndr.h>\n\n");
401 fprintf(idfile, "#define INITGUID\n");
402 fprintf(idfile, "#include <guiddef.h>\n\n");
403 fprintf(idfile, "#ifdef __cplusplus\n");
404 fprintf(idfile, "extern \"C\" {\n");
405 fprintf(idfile, "#endif\n\n");
408 init_types();
409 ret = parser_parse();
411 if(do_header) {
412 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
413 fprintf(header, "\n");
414 write_user_types();
415 fprintf(header, "\n");
416 fprintf(header, "/* End additional prototypes */\n");
417 fprintf(header, "\n");
418 fprintf(header, "#ifdef __cplusplus\n");
419 fprintf(header, "}\n");
420 fprintf(header, "#endif\n");
421 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
422 fclose(header);
425 if (do_idfile) {
426 fprintf(idfile, "\n");
427 fprintf(idfile, "#ifdef __cplusplus\n");
428 fprintf(idfile, "}\n");
429 fprintf(idfile, "#endif\n");
431 fclose(idfile);
434 fclose(parser_in);
436 if(ret) {
437 exit(1);
439 header_name = NULL;
440 client_name = NULL;
441 server_name = NULL;
442 idfile_name = NULL;
443 return 0;
446 static void rm_tempfile(void)
448 abort_import();
449 if(temp_name)
450 unlink(temp_name);
451 if (header_name)
452 unlink(header_name);
453 if (client_name)
454 unlink(client_name);
455 if (server_name)
456 unlink(server_name);
459 static void segvhandler(int sig)
461 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
462 fflush(stdout);
463 fflush(stderr);
464 abort();