push ab3cc77eb114d2bd400734a9dccad24563f936a6
[wine/hacks.git] / tools / widl / widl.c
blobca88958708eaac8856c652104bbfe447bd0eb4e6
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 const 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 const 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);
148 static char *make_token(const char *name)
150 char *token;
151 char *slash;
152 int i;
154 slash = strrchr(name, '/');
155 if (slash) name = slash + 1;
157 token = xstrdup(name);
158 for (i=0; token[i]; i++) {
159 if (!isalnum(token[i])) token[i] = '_';
160 else token[i] = toupper(token[i]);
162 return token;
165 /* duplicate a basename into a valid C token */
166 static char *dup_basename_token(const char *name, const char *ext)
168 char *p, *ret = dup_basename( name, ext );
169 /* map invalid characters to '_' */
170 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
171 return ret;
174 /* clean things up when aborting on a signal */
175 static void exit_on_signal( int sig )
177 exit(1); /* this will call the atexit functions */
180 int main(int argc,char *argv[])
182 extern char* optarg;
183 extern int optind;
184 int optc;
185 int ret = 0;
186 int opti = 0;
188 signal( SIGTERM, exit_on_signal );
189 signal( SIGINT, exit_on_signal );
190 #ifdef SIGHUP
191 signal( SIGHUP, exit_on_signal );
192 #endif
194 now = time(NULL);
196 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
197 switch(optc) {
198 case OLDNAMES_OPTION:
199 old_names = 1;
200 break;
201 case PREFIX_ALL_OPTION:
202 prefix_client = xstrdup(optarg);
203 prefix_server = xstrdup(optarg);
204 break;
205 case PREFIX_CLIENT_OPTION:
206 prefix_client = xstrdup(optarg);
207 break;
208 case PREFIX_SERVER_OPTION:
209 prefix_server = xstrdup(optarg);
210 break;
211 case 'c':
212 do_everything = 0;
213 do_client = 1;
214 break;
215 case 'C':
216 client_name = xstrdup(optarg);
217 break;
218 case 'd':
219 debuglevel = strtol(optarg, NULL, 0);
220 break;
221 case 'D':
222 wpp_add_cmdline_define(optarg);
223 break;
224 case 'E':
225 do_everything = 0;
226 preprocess_only = 1;
227 break;
228 case 'h':
229 do_everything = 0;
230 do_header = 1;
231 break;
232 case 'H':
233 header_name = xstrdup(optarg);
234 break;
235 case 'I':
236 wpp_add_include_path(optarg);
237 break;
238 case 'N':
239 no_preprocess = 1;
240 break;
241 case 'p':
242 do_everything = 0;
243 do_proxies = 1;
244 break;
245 case 'P':
246 proxy_name = xstrdup(optarg);
247 break;
248 case 's':
249 do_everything = 0;
250 do_server = 1;
251 break;
252 case 'S':
253 server_name = xstrdup(optarg);
254 break;
255 case 't':
256 do_everything = 0;
257 do_typelib = 1;
258 break;
259 case 'T':
260 typelib_name = xstrdup(optarg);
261 break;
262 case 'u':
263 do_everything = 0;
264 do_idfile = 1;
265 break;
266 case 'U':
267 idfile_name = xstrdup(optarg);
268 break;
269 case 'V':
270 printf(version_string);
271 return 0;
272 case 'W':
273 pedantic = 1;
274 break;
275 default:
276 fprintf(stderr, usage);
277 return 1;
281 if(do_everything) {
282 do_header = do_typelib = do_proxies = do_client = do_server = do_idfile = 1;
284 if(optind < argc) {
285 input_name = xstrdup(argv[optind]);
287 else {
288 fprintf(stderr, usage);
289 return 1;
292 if(debuglevel)
294 setbuf(stdout,0);
295 setbuf(stderr,0);
298 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
299 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
301 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
302 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
303 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
305 if (!header_name) {
306 header_name = dup_basename(input_name, ".idl");
307 strcat(header_name, ".h");
310 if (!typelib_name && do_typelib) {
311 typelib_name = dup_basename(input_name, ".idl");
312 strcat(typelib_name, ".tlb");
315 if (!proxy_name && do_proxies) {
316 proxy_name = dup_basename(input_name, ".idl");
317 strcat(proxy_name, "_p.c");
320 if (!client_name && do_client) {
321 client_name = dup_basename(input_name, ".idl");
322 strcat(client_name, "_c.c");
325 if (!server_name && do_server) {
326 server_name = dup_basename(input_name, ".idl");
327 strcat(server_name, "_s.c");
330 if (!idfile_name && do_idfile) {
331 idfile_name = dup_basename(input_name, ".idl");
332 strcat(idfile_name, "_i.c");
335 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
336 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
337 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
339 wpp_add_cmdline_define("__WIDL__");
341 atexit(rm_tempfile);
342 if (!no_preprocess)
344 chat("Starting preprocess\n");
346 if (!preprocess_only)
348 ret = wpp_parse_temp( input_name, header_name, &temp_name );
350 else
352 ret = wpp_parse( input_name, stdout );
355 if(ret) exit(1);
356 if(preprocess_only) exit(0);
357 if(!(parser_in = fopen(temp_name, "r"))) {
358 fprintf(stderr, "Could not open %s for input\n", temp_name);
359 return 1;
362 else {
363 if(!(parser_in = fopen(input_name, "r"))) {
364 fprintf(stderr, "Could not open %s for input\n", input_name);
365 return 1;
369 if(do_header) {
370 header_token = make_token(header_name);
372 if(!(header = fopen(header_name, "w"))) {
373 fprintf(stderr, "Could not open %s for output\n", header_name);
374 return 1;
376 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
377 fprintf(header, "#include <rpc.h>\n" );
378 fprintf(header, "#include <rpcndr.h>\n\n" );
379 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
380 fprintf(header, "#define __WIDL_%s\n", header_token);
381 fprintf(header, "#ifdef __cplusplus\n");
382 fprintf(header, "extern \"C\" {\n");
383 fprintf(header, "#endif\n");
386 if (do_idfile) {
387 idfile_token = make_token(idfile_name);
389 idfile = fopen(idfile_name, "w");
390 if (! idfile) {
391 fprintf(stderr, "Could not open %s for output\n", idfile_name);
392 return 1;
395 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
396 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
397 fprintf(idfile, "#include <rpc.h>\n");
398 fprintf(idfile, "#include <rpcndr.h>\n\n");
399 fprintf(idfile, "#include <initguid.h>\n\n");
400 fprintf(idfile, "#ifdef __cplusplus\n");
401 fprintf(idfile, "extern \"C\" {\n");
402 fprintf(idfile, "#endif\n\n");
405 init_types();
406 ret = parser_parse();
408 if(do_header) {
409 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
410 fprintf(header, "\n");
411 write_user_types();
412 fprintf(header, "\n");
413 fprintf(header, "/* End additional prototypes */\n");
414 fprintf(header, "\n");
415 fprintf(header, "#ifdef __cplusplus\n");
416 fprintf(header, "}\n");
417 fprintf(header, "#endif\n");
418 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
419 fclose(header);
422 if (do_idfile) {
423 fprintf(idfile, "\n");
424 fprintf(idfile, "#ifdef __cplusplus\n");
425 fprintf(idfile, "}\n");
426 fprintf(idfile, "#endif\n");
428 fclose(idfile);
431 fclose(parser_in);
433 if(ret) {
434 exit(1);
436 header_name = NULL;
437 client_name = NULL;
438 server_name = NULL;
439 idfile_name = NULL;
440 return 0;
443 static void rm_tempfile(void)
445 abort_import();
446 if(temp_name)
447 unlink(temp_name);
448 if (header_name)
449 unlink(header_name);
450 if (client_name)
451 unlink(client_name);
452 if (server_name)
453 unlink(server_name);