include: Fix out of place comment for DLGC_* codes.
[wine/dibdrv.git] / tools / widl / widl.c
blobc04d07f8e5097618f9a3b1ac443e41d9877d4de5
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 <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #ifdef HAVE_GETOPT_H
35 # include <getopt.h>
36 #endif
38 #include "widl.h"
39 #include "utils.h"
40 #include "parser.h"
41 #include "wine/wpp.h"
42 #include "header.h"
44 /* future options to reserve characters for: */
45 /* a = alignment of structures */
46 /* A = ACF input filename */
47 /* J = do not search standard include path */
48 /* O = generate interpreted stubs */
49 /* w = select win16/win32 output (?) */
51 static char usage[] =
52 "Usage: widl [options...] infile.idl\n"
53 " -c Generate client stub\n"
54 " -C file Name of client stub file (default is infile_c.c)\n"
55 " -d n Set debug level to 'n'\n"
56 " -D id[=val] Define preprocessor identifier id=val\n"
57 " -E Preprocess only\n"
58 " -h Generate headers\n"
59 " -H file Name of header file (default is infile.h)\n"
60 " -I path Set include search dir to path (multiple -I allowed)\n"
61 " -N Do not preprocess input\n"
62 " --oldnames Use old naming conventions\n"
63 " -p Generate proxy\n"
64 " -P file Name of proxy file (default is infile_p.c)\n"
65 " -s Generate server stub\n"
66 " -S file Name of server stub file (default is infile_s.c)\n"
67 " -t Generate typelib\n"
68 " -T file Name of typelib file (default is infile.tlb)\n"
69 " -u Generate interface identifiers file\n"
70 " -U file Name of interface identifiers file (default is infile_i.c)\n"
71 " -V Print version and exit\n"
72 " -W Enable pedantic warnings\n"
73 "Debug level 'n' is a bitmask with following meaning:\n"
74 " * 0x01 Tell which resource is parsed (verbose mode)\n"
75 " * 0x02 Dump internal structures\n"
76 " * 0x04 Create a parser trace (yydebug=1)\n"
77 " * 0x08 Preprocessor messages\n"
78 " * 0x10 Preprocessor lex messages\n"
79 " * 0x20 Preprocessor yacc trace\n"
82 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
83 "Copyright 2002 Ove Kaaven\n";
85 int win32 = 1;
86 int debuglevel = DEBUGLEVEL_NONE;
87 int parser_debug, yy_flex_debug;
89 int pedantic = 0;
90 static int do_everything = 1;
91 int preprocess_only = 0;
92 int do_header = 0;
93 int do_typelib = 0;
94 int do_proxies = 0;
95 int do_client = 0;
96 int do_server = 0;
97 int do_idfile = 0;
98 int no_preprocess = 0;
99 int old_names = 0;
101 char *input_name;
102 char *header_name;
103 char *header_token;
104 char *typelib_name;
105 char *proxy_name;
106 char *proxy_token;
107 char *client_name;
108 char *client_token;
109 char *server_name;
110 char *server_token;
111 char *idfile_name;
112 char *idfile_token;
113 char *temp_name;
115 int line_number = 1;
117 FILE *header;
118 FILE *proxy;
119 FILE *idfile;
121 time_t now;
123 static const char *short_options =
124 "cC:d:D:EhH:I:NpP:sS:tT:uU:VW";
125 static struct option long_options[] = {
126 { "oldnames", 0, 0, 1 },
127 { 0, 0, 0, 0 }
130 static void rm_tempfile(void);
131 static void segvhandler(int sig);
133 static char *make_token(const char *name)
135 char *token;
136 char *slash;
137 int i;
139 slash = strrchr(name, '/');
140 if (slash) name = slash + 1;
142 token = xstrdup(name);
143 for (i=0; token[i]; i++) {
144 if (!isalnum(token[i])) token[i] = '_';
145 else token[i] = toupper(token[i]);
147 return token;
150 /* duplicate a basename into a valid C token */
151 static char *dup_basename_token(const char *name, const char *ext)
153 char *p, *ret = dup_basename( name, ext );
154 /* map invalid characters to '_' */
155 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
156 return ret;
159 /* clean things up when aborting on a signal */
160 static void exit_on_signal( int sig )
162 exit(1); /* this will call the atexit functions */
165 int main(int argc,char *argv[])
167 extern char* optarg;
168 extern int optind;
169 int optc;
170 int ret = 0;
171 int opti = 0;
173 signal(SIGSEGV, segvhandler);
174 signal( SIGTERM, exit_on_signal );
175 signal( SIGINT, exit_on_signal );
176 #ifdef SIGHUP
177 signal( SIGHUP, exit_on_signal );
178 #endif
180 now = time(NULL);
182 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
183 switch(optc) {
184 case 1:
185 old_names = 1;
186 break;
187 case 'c':
188 do_everything = 0;
189 do_client = 1;
190 break;
191 case 'C':
192 client_name = xstrdup(optarg);
193 break;
194 case 'd':
195 debuglevel = strtol(optarg, NULL, 0);
196 break;
197 case 'D':
198 wpp_add_cmdline_define(optarg);
199 break;
200 case 'E':
201 do_everything = 0;
202 preprocess_only = 1;
203 break;
204 case 'h':
205 do_everything = 0;
206 do_header = 1;
207 break;
208 case 'H':
209 header_name = xstrdup(optarg);
210 break;
211 case 'I':
212 wpp_add_include_path(optarg);
213 break;
214 case 'N':
215 no_preprocess = 1;
216 break;
217 case 'p':
218 do_everything = 0;
219 do_proxies = 1;
220 break;
221 case 'P':
222 proxy_name = xstrdup(optarg);
223 break;
224 case 's':
225 do_everything = 0;
226 do_server = 1;
227 break;
228 case 'S':
229 server_name = xstrdup(optarg);
230 break;
231 case 't':
232 do_everything = 0;
233 do_typelib = 1;
234 break;
235 case 'T':
236 typelib_name = xstrdup(optarg);
237 break;
238 case 'u':
239 do_everything = 0;
240 do_idfile = 1;
241 break;
242 case 'U':
243 idfile_name = xstrdup(optarg);
244 break;
245 case 'V':
246 printf(version_string);
247 return 0;
248 case 'W':
249 pedantic = 1;
250 break;
251 default:
252 fprintf(stderr, usage);
253 return 1;
257 if(do_everything) {
258 do_header = do_typelib = do_proxies = do_client = do_server = do_idfile = 1;
260 if(optind < argc) {
261 input_name = xstrdup(argv[optind]);
263 else {
264 fprintf(stderr, usage);
265 return 1;
268 if(debuglevel)
270 setbuf(stdout,0);
271 setbuf(stderr,0);
274 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
275 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
277 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
278 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
279 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
281 if (!header_name) {
282 header_name = dup_basename(input_name, ".idl");
283 strcat(header_name, ".h");
286 if (!typelib_name && do_typelib) {
287 typelib_name = dup_basename(input_name, ".idl");
288 strcat(typelib_name, ".tlb");
291 if (!proxy_name && do_proxies) {
292 proxy_name = dup_basename(input_name, ".idl");
293 strcat(proxy_name, "_p.c");
296 if (!client_name && do_client) {
297 client_name = dup_basename(input_name, ".idl");
298 strcat(client_name, "_c.c");
301 if (!server_name && do_server) {
302 server_name = dup_basename(input_name, ".idl");
303 strcat(server_name, "_s.c");
306 if (!idfile_name && do_idfile) {
307 idfile_name = dup_basename(input_name, ".idl");
308 strcat(idfile_name, "_i.c");
311 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
312 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
313 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
315 wpp_add_cmdline_define("__WIDL__");
317 atexit(rm_tempfile);
318 if (!no_preprocess)
320 chat("Starting preprocess\n");
322 if (!preprocess_only)
324 ret = wpp_parse_temp( input_name, header_name, &temp_name );
326 else
328 ret = wpp_parse( input_name, stdout );
331 if(ret) exit(1);
332 if(preprocess_only) exit(0);
333 if(!(parser_in = fopen(temp_name, "r"))) {
334 fprintf(stderr, "Could not open %s for input\n", temp_name);
335 return 1;
338 else {
339 if(!(parser_in = fopen(input_name, "r"))) {
340 fprintf(stderr, "Could not open %s for input\n", input_name);
341 return 1;
345 if(do_header) {
346 header_token = make_token(header_name);
348 if(!(header = fopen(header_name, "w"))) {
349 fprintf(stderr, "Could not open %s for output\n", header_name);
350 return 1;
352 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
353 fprintf(header, "#include <rpc.h>\n" );
354 fprintf(header, "#include <rpcndr.h>\n\n" );
355 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
356 fprintf(header, "#define __WIDL_%s\n", header_token);
357 fprintf(header, "#ifdef __cplusplus\n");
358 fprintf(header, "extern \"C\" {\n");
359 fprintf(header, "#endif\n");
362 if (do_idfile) {
363 idfile_token = make_token(idfile_name);
365 idfile = fopen(idfile_name, "w");
366 if (! idfile) {
367 fprintf(stderr, "Could not open %s for output\n", idfile_name);
368 return 1;
371 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
372 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
373 fprintf(idfile, "#include <rpc.h>\n");
374 fprintf(idfile, "#include <rpcndr.h>\n\n");
375 fprintf(idfile, "#define INITGUID\n");
376 fprintf(idfile, "#include <guiddef.h>\n\n");
377 fprintf(idfile, "#ifdef __cplusplus\n");
378 fprintf(idfile, "extern \"C\" {\n");
379 fprintf(idfile, "#endif\n\n");
382 init_types();
383 ret = parser_parse();
385 if(do_header) {
386 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
387 fprintf(header, "\n");
388 write_user_types();
389 fprintf(header, "\n");
390 fprintf(header, "/* End additional prototypes */\n");
391 fprintf(header, "\n");
392 fprintf(header, "#ifdef __cplusplus\n");
393 fprintf(header, "}\n");
394 fprintf(header, "#endif\n");
395 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
396 fclose(header);
399 if (do_idfile) {
400 fprintf(idfile, "\n");
401 fprintf(idfile, "#ifdef __cplusplus\n");
402 fprintf(idfile, "}\n");
403 fprintf(idfile, "#endif\n");
405 fclose(idfile);
408 fclose(parser_in);
410 if(ret) {
411 exit(1);
413 header_name = NULL;
414 client_name = NULL;
415 server_name = NULL;
416 idfile_name = NULL;
417 return 0;
420 static void rm_tempfile(void)
422 abort_import();
423 if(temp_name)
424 unlink(temp_name);
425 if (header_name)
426 unlink(header_name);
427 if (client_name)
428 unlink(client_name);
429 if (server_name)
430 unlink(server_name);
433 static void segvhandler(int sig)
435 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
436 fflush(stdout);
437 fflush(stderr);
438 abort();