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"
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 (?) */
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";
86 int debuglevel
= DEBUGLEVEL_NONE
;
87 int parser_debug
, yy_flex_debug
;
90 static int do_everything
= 1;
91 int preprocess_only
= 0;
98 int no_preprocess
= 0;
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 },
130 static void rm_tempfile(void);
131 static void segvhandler(int sig
);
133 static char *make_token(const char *name
)
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
]);
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
= '_';
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
[])
173 signal(SIGSEGV
, segvhandler
);
174 signal( SIGTERM
, exit_on_signal
);
175 signal( SIGINT
, exit_on_signal
);
177 signal( SIGHUP
, exit_on_signal
);
182 while((optc
= getopt_long(argc
, argv
, short_options
, long_options
, &opti
)) != EOF
) {
192 client_name
= xstrdup(optarg
);
195 debuglevel
= strtol(optarg
, NULL
, 0);
198 wpp_add_cmdline_define(optarg
);
209 header_name
= xstrdup(optarg
);
212 wpp_add_include_path(optarg
);
222 proxy_name
= xstrdup(optarg
);
229 server_name
= xstrdup(optarg
);
236 typelib_name
= xstrdup(optarg
);
243 idfile_name
= xstrdup(optarg
);
246 printf(version_string
);
252 fprintf(stderr
, usage
);
258 do_header
= do_typelib
= do_proxies
= do_client
= do_server
= do_idfile
= 1;
261 input_name
= xstrdup(argv
[optind
]);
264 fprintf(stderr
, usage
);
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 );
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__");
320 chat("Starting preprocess\n");
322 if (!preprocess_only
)
324 ret
= wpp_parse_temp( input_name
, header_name
, &temp_name
);
328 ret
= wpp_parse( input_name
, stdout
);
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
);
339 if(!(parser_in
= fopen(input_name
, "r"))) {
340 fprintf(stderr
, "Could not open %s for input\n", input_name
);
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
);
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");
363 idfile_token
= make_token(idfile_name
);
365 idfile
= fopen(idfile_name
, "w");
367 fprintf(stderr
, "Could not open %s for output\n", idfile_name
);
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");
383 ret
= parser_parse();
386 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
387 fprintf(header
, "\n");
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
);
400 fprintf(idfile
, "\n");
401 fprintf(idfile
, "#ifdef __cplusplus\n");
402 fprintf(idfile
, "}\n");
403 fprintf(idfile
, "#endif\n");
420 static void rm_tempfile(void)
433 static void segvhandler(int sig
)
435 fprintf(stderr
, "\n%s:%d: Oops, segment violation\n", input_name
, line_number
);