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 " --win32 Only generate 32-bit code\n"
82 " --win64 Only generate 64-bit code\n"
83 "Debug level 'n' is a bitmask with following meaning:\n"
84 " * 0x01 Tell which resource is parsed (verbose mode)\n"
85 " * 0x02 Dump internal structures\n"
86 " * 0x04 Create a parser trace (yydebug=1)\n"
87 " * 0x08 Preprocessor messages\n"
88 " * 0x10 Preprocessor lex messages\n"
89 " * 0x20 Preprocessor yacc trace\n"
92 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
93 "Copyright 2002 Ove Kaaven\n";
96 int debuglevel = DEBUGLEVEL_NONE;
97 int parser_debug, yy_flex_debug;
100 int do_everything = 1;
101 int preprocess_only = 0;
109 int no_preprocess = 0;
116 char *local_stubs_name;
129 const char *prefix_client = "";
130 const char *prefix_server = "";
137 size_t pointer_size = 0;
142 OLDNAMES_OPTION = CHAR_MAX + 1,
147 PREFIX_CLIENT_OPTION,
148 PREFIX_SERVER_OPTION,
153 static const char short_options[] =
154 "cC:d:D:EhH:I:NpP:sS:tT:uU:VW";
155 static const struct option long_options[] = {
156 { "dlldata", 1, 0, DLLDATA_OPTION },
157 { "dlldata-only", 0, 0, DLLDATA_ONLY_OPTION },
158 { "local-stubs", 1, 0, LOCAL_STUBS_OPTION },
159 { "oldnames", 0, 0, OLDNAMES_OPTION },
160 { "prefix-all", 1, 0, PREFIX_ALL_OPTION },
161 { "prefix-client", 1, 0, PREFIX_CLIENT_OPTION },
162 { "prefix-server", 1, 0, PREFIX_SERVER_OPTION },
163 { "win32", 0, 0, WIN32_OPTION },
164 { "win64", 0, 0, WIN64_OPTION },
168 static void rm_tempfile(void);
170 static char *make_token(const char *name)
176 slash = strrchr(name, '/');
178 slash = strrchr(name, '\\');
180 if (slash) name = slash + 1;
182 token = xstrdup(name);
183 for (i=0; token[i]; i++) {
184 if (!isalnum(token[i])) token[i] = '_';
185 else token[i] = toupper(token[i]);
190 /* duplicate a basename into a valid C token */
191 static char *dup_basename_token(const char *name, const char *ext)
193 char *p, *ret = dup_basename( name, ext );
194 /* map invalid characters to '_' */
195 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
199 static void add_widl_version_define(void)
201 unsigned int version;
202 const char *p = PACKAGE_VERSION;
205 version = atoi(p) * 0x10000;
211 version += atoi(p + 1) * 0x100;
212 p = strchr(p + 1, '.');
217 version += atoi(p + 1);
221 char version_str[11];
222 snprintf(version_str, sizeof(version_str), "0x%x", version);
223 wpp_add_define("__WIDL__", version_str);
226 wpp_add_define("__WIDL__", NULL);
229 /* clean things up when aborting on a signal */
230 static void exit_on_signal( int sig )
232 exit(1); /* this will call the atexit functions */
235 static void set_everything(int x)
246 void start_cplusplus_guard(FILE *fp)
248 fprintf(fp, "#ifdef __cplusplus\n");
249 fprintf(fp, "extern \"C\" {\n");
250 fprintf(fp, "#endif\n\n");
253 void end_cplusplus_guard(FILE *fp)
255 fprintf(fp, "#ifdef __cplusplus\n");
257 fprintf(fp, "#endif\n\n");
266 static void add_filename_node(struct list *list, const char *name)
268 filename_node_t *node = xmalloc(sizeof *node);
269 node->filename = dup_basename( name, ".idl" );
270 list_add_tail(list, &node->link);
273 static void free_filename_nodes(struct list *list)
275 filename_node_t *node, *next;
276 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
277 list_remove(&node->link);
278 free(node->filename);
283 static void write_dlldata_list(struct list *filenames)
286 filename_node_t *node;
288 dlldata = fopen(dlldata_name, "w");
290 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
292 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
293 fprintf(dlldata, "- Do not edit ***/\n\n");
294 fprintf(dlldata, "#include <objbase.h>\n");
295 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
296 start_cplusplus_guard(dlldata);
298 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
299 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
301 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
302 fprintf(dlldata, "/* Start of list */\n");
303 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
304 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
305 fprintf(dlldata, "/* End of list */\n");
306 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
308 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
309 end_cplusplus_guard(dlldata);
313 static char *eat_space(char *s)
315 while (isspace((unsigned char) *s))
320 void write_dlldata(const statement_list_t *stmts)
322 struct list filenames = LIST_INIT(filenames);
323 filename_node_t *node;
326 if (!do_dlldata || !need_proxy_file(stmts))
329 dlldata = fopen(dlldata_name, "r");
331 static char marker[] = "REFERENCE_PROXY_FILE";
335 while (widl_getline(&line, &len, dlldata)) {
337 start = eat_space(line);
338 if (strncmp(start, marker, sizeof marker - 1) == 0) {
339 start = eat_space(start + sizeof marker - 1);
342 end = start = eat_space(start + 1);
343 while (*end && *end != ')')
347 while (isspace((unsigned char) end[-1]))
351 add_filename_node(&filenames, start);
356 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
362 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
363 if (strcmp(proxy_token, node->filename) == 0) {
364 /* We're already in the list, no need to regenerate this file. */
365 free_filename_nodes(&filenames);
369 add_filename_node(&filenames, proxy_token);
370 write_dlldata_list(&filenames);
371 free_filename_nodes(&filenames);
374 static void write_id_data_stmts(const statement_list_t *stmts)
376 const statement_t *stmt;
377 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
379 if (stmt->type == STMT_TYPE)
381 const type_t *type = stmt->u.type;
382 if (type_get_type(type) == TYPE_INTERFACE)
385 if (!is_object(type->attrs) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
387 uuid = get_attrp(type->attrs, ATTR_UUID);
388 write_guid(idfile, is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
391 else if (type_get_type(type) == TYPE_COCLASS)
393 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
394 write_guid(idfile, "CLSID", type->name, uuid);
397 else if (stmt->type == STMT_LIBRARY)
399 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
400 write_guid(idfile, "LIBID", stmt->u.lib->name, uuid);
401 write_id_data_stmts(stmt->u.lib->stmts);
406 void write_id_data(const statement_list_t *stmts)
408 if (!do_idfile) return;
410 idfile_token = make_token(idfile_name);
412 idfile = fopen(idfile_name, "w");
414 error("Could not open %s for output\n", idfile_name);
418 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
419 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
420 fprintf(idfile, "#include <rpc.h>\n");
421 fprintf(idfile, "#include <rpcndr.h>\n\n");
422 fprintf(idfile, "#include <initguid.h>\n\n");
423 start_cplusplus_guard(idfile);
425 write_id_data_stmts(stmts);
427 fprintf(idfile, "\n");
428 end_cplusplus_guard(idfile);
433 int main(int argc,char *argv[])
441 signal( SIGTERM, exit_on_signal );
442 signal( SIGINT, exit_on_signal );
444 signal( SIGHUP, exit_on_signal );
449 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
452 dlldata_name = xstrdup(optarg);
454 case DLLDATA_ONLY_OPTION:
458 case LOCAL_STUBS_OPTION:
460 local_stubs_name = xstrdup(optarg);
462 case OLDNAMES_OPTION:
465 case PREFIX_ALL_OPTION:
466 prefix_client = xstrdup(optarg);
467 prefix_server = xstrdup(optarg);
469 case PREFIX_CLIENT_OPTION:
470 prefix_client = xstrdup(optarg);
472 case PREFIX_SERVER_OPTION:
473 prefix_server = xstrdup(optarg);
488 client_name = xstrdup(optarg);
491 debuglevel = strtol(optarg, NULL, 0);
494 wpp_add_cmdline_define(optarg);
505 header_name = xstrdup(optarg);
508 wpp_add_include_path(optarg);
518 proxy_name = xstrdup(optarg);
525 server_name = xstrdup(optarg);
532 typelib_name = xstrdup(optarg);
539 idfile_name = xstrdup(optarg);
542 printf("%s", version_string);
548 fprintf(stderr, "%s", usage);
554 set_everything(TRUE);
557 if (!dlldata_name && do_dlldata)
558 dlldata_name = xstrdup("dlldata.c");
561 if (do_dlldata && !do_everything) {
562 struct list filenames = LIST_INIT(filenames);
563 for ( ; optind < argc; ++optind)
564 add_filename_node(&filenames, argv[optind]);
566 write_dlldata_list(&filenames);
567 free_filename_nodes(&filenames);
570 else if (optind != argc - 1) {
571 fprintf(stderr, "%s", usage);
575 input_name = xstrdup(argv[optind]);
578 fprintf(stderr, "%s", usage);
588 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
589 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
591 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
592 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
593 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
596 header_name = dup_basename(input_name, ".idl");
597 strcat(header_name, ".h");
600 if (!typelib_name && do_typelib) {
601 typelib_name = dup_basename(input_name, ".idl");
602 strcat(typelib_name, ".tlb");
605 if (!proxy_name && do_proxies) {
606 proxy_name = dup_basename(input_name, ".idl");
607 strcat(proxy_name, "_p.c");
610 if (!client_name && do_client) {
611 client_name = dup_basename(input_name, ".idl");
612 strcat(client_name, "_c.c");
615 if (!server_name && do_server) {
616 server_name = dup_basename(input_name, ".idl");
617 strcat(server_name, "_s.c");
620 if (!idfile_name && do_idfile) {
621 idfile_name = dup_basename(input_name, ".idl");
622 strcat(idfile_name, "_i.c");
625 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
626 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
627 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
629 add_widl_version_define();
634 chat("Starting preprocess\n");
636 if (!preprocess_only)
638 ret = wpp_parse_temp( input_name, header_name, &temp_name );
642 ret = wpp_parse( input_name, stdout );
646 if(preprocess_only) exit(0);
647 if(!(parser_in = fopen(temp_name, "r"))) {
648 fprintf(stderr, "Could not open %s for input\n", temp_name);
653 if(!(parser_in = fopen(input_name, "r"))) {
654 fprintf(stderr, "Could not open %s for input\n", input_name);
659 header_token = make_token(header_name);
662 ret = parser_parse();
670 /* Everything has been done successfully, don't delete any files. */
671 set_everything(FALSE);
672 local_stubs_name = NULL;
677 static void rm_tempfile(void)
684 if (local_stubs_name)
685 unlink(local_stubs_name);
695 unlink(typelib_name);