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 (?) */
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 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);
147 static void segvhandler(int sig
);
149 static char *make_token(const char *name
)
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
]);
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
= '_';
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
[])
189 signal(SIGSEGV
, segvhandler
);
190 signal( SIGTERM
, exit_on_signal
);
191 signal( SIGINT
, exit_on_signal
);
193 signal( SIGHUP
, exit_on_signal
);
198 while((optc
= getopt_long(argc
, argv
, short_options
, long_options
, &opti
)) != EOF
) {
200 case OLDNAMES_OPTION
:
203 case PREFIX_ALL_OPTION
:
204 prefix_client
= xstrdup(optarg
);
205 prefix_server
= xstrdup(optarg
);
207 case PREFIX_CLIENT_OPTION
:
208 prefix_client
= xstrdup(optarg
);
210 case PREFIX_SERVER_OPTION
:
211 prefix_server
= xstrdup(optarg
);
218 client_name
= xstrdup(optarg
);
221 debuglevel
= strtol(optarg
, NULL
, 0);
224 wpp_add_cmdline_define(optarg
);
235 header_name
= xstrdup(optarg
);
238 wpp_add_include_path(optarg
);
248 proxy_name
= xstrdup(optarg
);
255 server_name
= xstrdup(optarg
);
262 typelib_name
= xstrdup(optarg
);
269 idfile_name
= xstrdup(optarg
);
272 printf(version_string
);
278 fprintf(stderr
, usage
);
284 do_header
= do_typelib
= do_proxies
= do_client
= do_server
= do_idfile
= 1;
287 input_name
= xstrdup(argv
[optind
]);
290 fprintf(stderr
, usage
);
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 );
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__");
346 chat("Starting preprocess\n");
348 if (!preprocess_only
)
350 ret
= wpp_parse_temp( input_name
, header_name
, &temp_name
);
354 ret
= wpp_parse( input_name
, stdout
);
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
);
365 if(!(parser_in
= fopen(input_name
, "r"))) {
366 fprintf(stderr
, "Could not open %s for input\n", input_name
);
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
);
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");
389 idfile_token
= make_token(idfile_name
);
391 idfile
= fopen(idfile_name
, "w");
393 fprintf(stderr
, "Could not open %s for output\n", idfile_name
);
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");
409 ret
= parser_parse();
412 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
413 fprintf(header
, "\n");
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
);
426 fprintf(idfile
, "\n");
427 fprintf(idfile
, "#ifdef __cplusplus\n");
428 fprintf(idfile
, "}\n");
429 fprintf(idfile
, "#endif\n");
446 static void rm_tempfile(void)
459 static void segvhandler(int sig
)
461 fprintf(stderr
, "\n%s:%d: Oops, segment violation\n", input_name
, line_number
);