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"
46 /* future options to reserve characters for: */
47 /* a = alignment of structures */
48 /* A = ACF input filename */
49 /* J = do not search standard include path */
50 /* O = generate interpreted stubs */
51 /* w = select win16/win32 output (?) */
53 static const char usage[] =
54 "Usage: widl [options...] infile.idl\n"
55 " or: widl [options...] --dlldata-only name1 [name2...]\n"
56 " -c Generate client stub\n"
57 " -C file Name of client stub file (default is infile_c.c)\n"
58 " -d n Set debug level to 'n'\n"
59 " -D id[=val] Define preprocessor identifier id=val\n"
60 " --dlldata=file Name of the dlldata file (default is dlldata.c)\n"
61 " -E Preprocess only\n"
62 " -h Generate headers\n"
63 " -H file Name of header file (default is infile.h)\n"
64 " -I path Set include search dir to path (multiple -I allowed)\n"
65 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
66 " -N Do not preprocess input\n"
67 " --oldnames Use old naming conventions\n"
68 " -p Generate proxy\n"
69 " -P file Name of proxy file (default is infile_p.c)\n"
70 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
71 " --prefix-client=p Prefix names of client stubs with 'p'\n"
72 " --prefix-server=p Prefix names of server functions with 'p'\n"
73 " -s Generate server stub\n"
74 " -S file Name of server stub file (default is infile_s.c)\n"
75 " -t Generate typelib\n"
76 " -T file Name of typelib file (default is infile.tlb)\n"
77 " -u Generate interface identifiers file\n"
78 " -U file Name of interface identifiers file (default is infile_i.c)\n"
79 " -V Print version and exit\n"
80 " -W Enable pedantic warnings\n"
81 "Debug level 'n' is a bitmask with following meaning:\n"
82 " * 0x01 Tell which resource is parsed (verbose mode)\n"
83 " * 0x02 Dump internal structures\n"
84 " * 0x04 Create a parser trace (yydebug=1)\n"
85 " * 0x08 Preprocessor messages\n"
86 " * 0x10 Preprocessor lex messages\n"
87 " * 0x20 Preprocessor yacc trace\n"
90 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
91 "Copyright 2002 Ove Kaaven\n";
94 int debuglevel = DEBUGLEVEL_NONE;
95 int parser_debug, yy_flex_debug;
98 int do_everything = 1;
99 int preprocess_only = 0;
107 int no_preprocess = 0;
112 char *local_stubs_name;
125 const char *prefix_client = "";
126 const char *prefix_server = "";
138 OLDNAMES_OPTION = CHAR_MAX + 1,
143 PREFIX_CLIENT_OPTION,
147 static const char short_options[] =
148 "cC:d:D:EhH:I:NpP:sS:tT:uU:VW";
149 static const struct option long_options[] = {
150 { "dlldata", 1, 0, DLLDATA_OPTION },
151 { "dlldata-only", 0, 0, DLLDATA_ONLY_OPTION },
152 { "local-stubs", 1, 0, LOCAL_STUBS_OPTION },
153 { "oldnames", 0, 0, OLDNAMES_OPTION },
154 { "prefix-all", 1, 0, PREFIX_ALL_OPTION },
155 { "prefix-client", 1, 0, PREFIX_CLIENT_OPTION },
156 { "prefix-server", 1, 0, PREFIX_SERVER_OPTION },
160 static void rm_tempfile(void);
162 static char *make_token(const char *name)
168 slash = strrchr(name, '/');
170 slash = strrchr(name, '\\');
172 if (slash) name = slash + 1;
174 token = xstrdup(name);
175 for (i=0; token[i]; i++) {
176 if (!isalnum(token[i])) token[i] = '_';
177 else token[i] = toupper(token[i]);
182 /* duplicate a basename into a valid C token */
183 static char *dup_basename_token(const char *name, const char *ext)
185 char *p, *ret = dup_basename( name, ext );
186 /* map invalid characters to '_' */
187 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
191 /* clean things up when aborting on a signal */
192 static void exit_on_signal( int sig )
194 exit(1); /* this will call the atexit functions */
197 static void set_everything(int x)
208 static void start_cplusplus_guard(FILE *fp)
210 fprintf(fp, "#ifdef __cplusplus\n");
211 fprintf(fp, "extern \"C\" {\n");
212 fprintf(fp, "#endif\n\n");
215 static void end_cplusplus_guard(FILE *fp)
217 fprintf(fp, "#ifdef __cplusplus\n");
219 fprintf(fp, "#endif\n\n");
228 static void add_filename_node(struct list *list, const char *name)
230 filename_node_t *node = xmalloc(sizeof *node);
231 node->filename = dup_basename( name, ".idl" );
232 list_add_tail(list, &node->link);
235 static void free_filename_nodes(struct list *list)
237 filename_node_t *node, *next;
238 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
239 list_remove(&node->link);
240 free(node->filename);
245 static void write_dlldata_list(struct list *filenames)
248 filename_node_t *node;
250 dlldata = fopen(dlldata_name, "w");
252 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
254 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
255 fprintf(dlldata, "- Do not edit ***/\n\n");
256 fprintf(dlldata, "#include <objbase.h>\n");
257 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
258 start_cplusplus_guard(dlldata);
260 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
261 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
263 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
264 fprintf(dlldata, "/* Start of list */\n");
265 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
266 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
267 fprintf(dlldata, "/* End of list */\n");
268 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
270 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
271 end_cplusplus_guard(dlldata);
275 static char *eat_space(char *s)
277 while (isspace((unsigned char) *s))
282 void write_dlldata(const statement_list_t *stmts)
284 struct list filenames = LIST_INIT(filenames);
285 filename_node_t *node;
288 if (!do_dlldata || !need_proxy_file(stmts))
291 dlldata = fopen(dlldata_name, "r");
293 static char marker[] = "REFERENCE_PROXY_FILE";
297 while (widl_getline(&line, &len, dlldata)) {
299 start = eat_space(line);
300 if (strncmp(start, marker, sizeof marker - 1) == 0) {
301 start = eat_space(start + sizeof marker - 1);
304 end = start = eat_space(start + 1);
305 while (*end && *end != ')')
309 while (isspace((unsigned char) end[-1]))
313 add_filename_node(&filenames, start);
318 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
324 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
325 if (strcmp(proxy_token, node->filename) == 0) {
326 /* We're already in the list, no need to regenerate this file. */
327 free_filename_nodes(&filenames);
331 add_filename_node(&filenames, proxy_token);
332 write_dlldata_list(&filenames);
333 free_filename_nodes(&filenames);
336 int main(int argc,char *argv[])
344 signal( SIGTERM, exit_on_signal );
345 signal( SIGINT, exit_on_signal );
347 signal( SIGHUP, exit_on_signal );
352 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
355 dlldata_name = xstrdup(optarg);
357 case DLLDATA_ONLY_OPTION:
361 case LOCAL_STUBS_OPTION:
363 local_stubs_name = xstrdup(optarg);
365 case OLDNAMES_OPTION:
368 case PREFIX_ALL_OPTION:
369 prefix_client = xstrdup(optarg);
370 prefix_server = xstrdup(optarg);
372 case PREFIX_CLIENT_OPTION:
373 prefix_client = xstrdup(optarg);
375 case PREFIX_SERVER_OPTION:
376 prefix_server = xstrdup(optarg);
383 client_name = xstrdup(optarg);
386 debuglevel = strtol(optarg, NULL, 0);
389 wpp_add_cmdline_define(optarg);
400 header_name = xstrdup(optarg);
403 wpp_add_include_path(optarg);
413 proxy_name = xstrdup(optarg);
420 server_name = xstrdup(optarg);
427 typelib_name = xstrdup(optarg);
434 idfile_name = xstrdup(optarg);
437 printf("%s", version_string);
443 fprintf(stderr, "%s", usage);
449 set_everything(TRUE);
452 if (!dlldata_name && do_dlldata)
453 dlldata_name = xstrdup("dlldata.c");
456 if (do_dlldata && !do_everything) {
457 struct list filenames = LIST_INIT(filenames);
458 for ( ; optind < argc; ++optind)
459 add_filename_node(&filenames, argv[optind]);
461 write_dlldata_list(&filenames);
462 free_filename_nodes(&filenames);
465 else if (optind != argc - 1) {
466 fprintf(stderr, "%s", usage);
470 input_name = xstrdup(argv[optind]);
473 fprintf(stderr, "%s", usage);
483 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
484 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
486 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
487 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
488 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
491 header_name = dup_basename(input_name, ".idl");
492 strcat(header_name, ".h");
495 if (!typelib_name && do_typelib) {
496 typelib_name = dup_basename(input_name, ".idl");
497 strcat(typelib_name, ".tlb");
500 if (!proxy_name && do_proxies) {
501 proxy_name = dup_basename(input_name, ".idl");
502 strcat(proxy_name, "_p.c");
505 if (!client_name && do_client) {
506 client_name = dup_basename(input_name, ".idl");
507 strcat(client_name, "_c.c");
510 if (!server_name && do_server) {
511 server_name = dup_basename(input_name, ".idl");
512 strcat(server_name, "_s.c");
515 if (!idfile_name && do_idfile) {
516 idfile_name = dup_basename(input_name, ".idl");
517 strcat(idfile_name, "_i.c");
520 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
521 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
522 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
524 wpp_add_cmdline_define("__WIDL__");
529 chat("Starting preprocess\n");
531 if (!preprocess_only)
533 ret = wpp_parse_temp( input_name, header_name, &temp_name );
537 ret = wpp_parse( input_name, stdout );
541 if(preprocess_only) exit(0);
542 if(!(parser_in = fopen(temp_name, "r"))) {
543 fprintf(stderr, "Could not open %s for input\n", temp_name);
548 if(!(parser_in = fopen(input_name, "r"))) {
549 fprintf(stderr, "Could not open %s for input\n", input_name);
555 header_token = make_token(header_name);
557 if(!(header = fopen(header_name, "w"))) {
558 fprintf(stderr, "Could not open %s for output\n", header_name);
561 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
562 fprintf(header, "#include <rpc.h>\n" );
563 fprintf(header, "#include <rpcndr.h>\n\n" );
564 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
565 fprintf(header, "#define __WIDL_%s\n", header_token);
566 start_cplusplus_guard(header);
569 if (local_stubs_name) {
570 local_stubs = fopen(local_stubs_name, "w");
572 fprintf(stderr, "Could not open %s for output\n", local_stubs_name);
575 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
576 fprintf(local_stubs, "#include <objbase.h>\n");
577 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
581 idfile_token = make_token(idfile_name);
583 idfile = fopen(idfile_name, "w");
585 fprintf(stderr, "Could not open %s for output\n", idfile_name);
589 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
590 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
591 fprintf(idfile, "#include <rpc.h>\n");
592 fprintf(idfile, "#include <rpcndr.h>\n\n");
593 fprintf(idfile, "#include <initguid.h>\n\n");
594 start_cplusplus_guard(idfile);
598 ret = parser_parse();
601 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
602 fprintf(header, "\n");
604 write_generic_handle_routines();
605 write_context_handle_rundowns();
606 fprintf(header, "\n");
607 fprintf(header, "/* End additional prototypes */\n");
608 fprintf(header, "\n");
609 end_cplusplus_guard(header);
610 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
619 fprintf(idfile, "\n");
620 end_cplusplus_guard(idfile);
631 /* Everything has been done successfully, don't delete any files. */
632 set_everything(FALSE);
633 local_stubs_name = NULL;
638 static void rm_tempfile(void)
645 if (local_stubs_name)
646 unlink(local_stubs_name);
656 unlink(typelib_name);