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
23 #include "wine/port.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";
90 int debuglevel = DEBUGLEVEL_NONE;
91 int parser_debug, yy_flex_debug;
94 int do_everything = 1;
95 int preprocess_only = 0;
102 int no_preprocess = 0;
118 const char *prefix_client = "";
119 const char *prefix_server = "";
130 OLDNAMES_OPTION = CHAR_MAX + 1,
132 PREFIX_CLIENT_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 },
146 static void rm_tempfile(void);
148 static char *make_token(const char *name)
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]);
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 = '_';
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[])
188 signal( SIGTERM, exit_on_signal );
189 signal( SIGINT, exit_on_signal );
191 signal( SIGHUP, exit_on_signal );
196 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
198 case OLDNAMES_OPTION:
201 case PREFIX_ALL_OPTION:
202 prefix_client = xstrdup(optarg);
203 prefix_server = xstrdup(optarg);
205 case PREFIX_CLIENT_OPTION:
206 prefix_client = xstrdup(optarg);
208 case PREFIX_SERVER_OPTION:
209 prefix_server = xstrdup(optarg);
216 client_name = xstrdup(optarg);
219 debuglevel = strtol(optarg, NULL, 0);
222 wpp_add_cmdline_define(optarg);
233 header_name = xstrdup(optarg);
236 wpp_add_include_path(optarg);
246 proxy_name = xstrdup(optarg);
253 server_name = xstrdup(optarg);
260 typelib_name = xstrdup(optarg);
267 idfile_name = xstrdup(optarg);
270 printf(version_string);
276 fprintf(stderr, usage);
282 do_header = do_typelib = do_proxies = do_client = do_server = do_idfile = 1;
285 input_name = xstrdup(argv[optind]);
288 fprintf(stderr, usage);
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 );
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__");
344 chat("Starting preprocess\n");
346 if (!preprocess_only)
348 ret = wpp_parse_temp( input_name, header_name, &temp_name );
352 ret = wpp_parse( input_name, stdout );
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);
363 if(!(parser_in = fopen(input_name, "r"))) {
364 fprintf(stderr, "Could not open %s for input\n", input_name);
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);
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");
387 idfile_token = make_token(idfile_name);
389 idfile = fopen(idfile_name, "w");
391 fprintf(stderr, "Could not open %s for output\n", idfile_name);
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");
406 ret = parser_parse();
409 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
410 fprintf(header, "\n");
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);
423 fprintf(idfile, "\n");
424 fprintf(idfile, "#ifdef __cplusplus\n");
425 fprintf(idfile, "}\n");
426 fprintf(idfile, "#endif\n");
443 static void rm_tempfile(void)