d3dxof: Do not expect a separator when there is no element.
[wine/multimedia.git] / tools / widl / widl.c
blob8c76c24e08ebf2aa674c95a86069f026cfc904f8
1 /*
2 * IDL Compiler
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <signal.h>
36 #ifdef HAVE_GETOPT_H
37 # include <getopt.h>
38 #endif
40 #include "widl.h"
41 #include "utils.h"
42 #include "parser.h"
43 #include "wine/wpp.h"
44 #include "header.h"
46 /* future options to reserve characters for: */
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 " or: widl [options...] --dlldata-only name1 [name2...]\n"
55 " -b arch Set the target architecture\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 " -m32, -m64 Set the kind of typelib to build (Win32 or Win64)\n"
67 " -N Do not preprocess input\n"
68 " --oldnames Use old naming conventions\n"
69 " -p Generate proxy\n"
70 " -P file Name of proxy file (default is infile_p.c)\n"
71 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
72 " --prefix-client=p Prefix names of client stubs with 'p'\n"
73 " --prefix-server=p Prefix names of server functions with 'p'\n"
74 " -s Generate server stub\n"
75 " -S file Name of server stub file (default is infile_s.c)\n"
76 " -t Generate typelib\n"
77 " -T file Name of typelib file (default is infile.tlb)\n"
78 " -u Generate interface identifiers file\n"
79 " -U file Name of interface identifiers file (default is infile_i.c)\n"
80 " -V Print version and exit\n"
81 " -W Enable pedantic warnings\n"
82 " --win32 Only generate 32-bit code\n"
83 " --win64 Only generate 64-bit code\n"
84 " --win32-align n Set win32 structure alignment to 'n'\n"
85 " --win64-align n Set win64 structure alignment to 'n'\n"
86 "Debug level 'n' is a bitmask with following meaning:\n"
87 " * 0x01 Tell which resource is parsed (verbose mode)\n"
88 " * 0x02 Dump internal structures\n"
89 " * 0x04 Create a parser trace (yydebug=1)\n"
90 " * 0x08 Preprocessor messages\n"
91 " * 0x10 Preprocessor lex messages\n"
92 " * 0x20 Preprocessor yacc trace\n"
95 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
96 "Copyright 2002 Ove Kaaven\n";
98 int debuglevel = DEBUGLEVEL_NONE;
99 int parser_debug, yy_flex_debug;
101 int pedantic = 0;
102 int do_everything = 1;
103 static int preprocess_only = 0;
104 int do_header = 0;
105 int do_typelib = 0;
106 int do_proxies = 0;
107 int do_client = 0;
108 int do_server = 0;
109 int do_idfile = 0;
110 int do_dlldata = 0;
111 static int no_preprocess = 0;
112 int old_names = 0;
113 int do_win32 = 1;
114 int do_win64 = 1;
115 int win32_packing = 8;
116 int win64_packing = 8;
118 char *input_name;
119 char *header_name;
120 char *local_stubs_name;
121 char *header_token;
122 char *typelib_name;
123 char *dlldata_name;
124 char *proxy_name;
125 char *proxy_token;
126 char *client_name;
127 char *client_token;
128 char *server_name;
129 char *server_token;
130 static char *idfile_name;
131 static char *idfile_token;
132 char *temp_name;
133 const char *prefix_client = "";
134 const char *prefix_server = "";
136 int line_number = 1;
138 static FILE *idfile;
140 size_t pointer_size = 0;
141 syskind_t typelib_kind = sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32;
143 time_t now;
145 enum {
146 OLDNAMES_OPTION = CHAR_MAX + 1,
147 DLLDATA_OPTION,
148 DLLDATA_ONLY_OPTION,
149 LOCAL_STUBS_OPTION,
150 PREFIX_ALL_OPTION,
151 PREFIX_CLIENT_OPTION,
152 PREFIX_SERVER_OPTION,
153 WIN32_OPTION,
154 WIN64_OPTION,
155 WIN32_ALIGN_OPTION,
156 WIN64_ALIGN_OPTION
159 static const char short_options[] =
160 "b:cC:d:D:EhH:I:m:NpP:sS:tT:uU:VW";
161 static const struct option long_options[] = {
162 { "dlldata", 1, NULL, DLLDATA_OPTION },
163 { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
164 { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
165 { "oldnames", 0, NULL, OLDNAMES_OPTION },
166 { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
167 { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
168 { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
169 { "win32", 0, NULL, WIN32_OPTION },
170 { "win64", 0, NULL, WIN64_OPTION },
171 { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
172 { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
173 { NULL, 0, NULL, 0 }
176 static void rm_tempfile(void);
178 static char *make_token(const char *name)
180 char *token;
181 char *slash;
182 int i;
184 slash = strrchr(name, '/');
185 if(!slash)
186 slash = strrchr(name, '\\');
188 if (slash) name = slash + 1;
190 token = xstrdup(name);
191 for (i=0; token[i]; i++) {
192 if (!isalnum(token[i])) token[i] = '_';
193 else token[i] = toupper(token[i]);
195 return token;
198 /* duplicate a basename into a valid C token */
199 static char *dup_basename_token(const char *name, const char *ext)
201 char *p, *ret = dup_basename( name, ext );
202 /* map invalid characters to '_' */
203 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
204 return ret;
207 static void add_widl_version_define(void)
209 unsigned int version;
210 const char *p = PACKAGE_VERSION;
212 /* major */
213 version = atoi(p) * 0x10000;
214 p = strchr(p, '.');
216 /* minor */
217 if (p)
219 version += atoi(p + 1) * 0x100;
220 p = strchr(p + 1, '.');
223 /* build */
224 if (p)
225 version += atoi(p + 1);
227 if (version != 0)
229 char version_str[11];
230 snprintf(version_str, sizeof(version_str), "0x%x", version);
231 wpp_add_define("__WIDL__", version_str);
233 else
234 wpp_add_define("__WIDL__", NULL);
237 /* set the target platform */
238 static void set_target( const char *target )
240 static const struct
242 const char *name;
243 syskind_t kind;
244 } cpu_names[] =
246 { "i386", SYS_WIN32 },
247 { "i486", SYS_WIN32 },
248 { "i586", SYS_WIN32 },
249 { "i686", SYS_WIN32 },
250 { "i786", SYS_WIN32 },
251 { "x86_64", SYS_WIN64 },
252 { "sparc", SYS_WIN32 },
253 { "alpha", SYS_WIN32 },
254 { "powerpc", SYS_WIN32 },
255 { "arm", SYS_WIN32 }
258 unsigned int i;
259 char *p, *spec = xstrdup( target );
261 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
263 if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
264 *p++ = 0;
265 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
267 if (!strcmp( cpu_names[i].name, spec ))
269 typelib_kind = cpu_names[i].kind;
270 free( spec );
271 return;
274 error( "Unrecognized CPU '%s'\n", spec );
277 /* clean things up when aborting on a signal */
278 static void exit_on_signal( int sig )
280 exit(1); /* this will call the atexit functions */
283 static void set_everything(int x)
285 do_header = x;
286 do_typelib = x;
287 do_proxies = x;
288 do_client = x;
289 do_server = x;
290 do_idfile = x;
291 do_dlldata = x;
294 void start_cplusplus_guard(FILE *fp)
296 fprintf(fp, "#ifdef __cplusplus\n");
297 fprintf(fp, "extern \"C\" {\n");
298 fprintf(fp, "#endif\n\n");
301 void end_cplusplus_guard(FILE *fp)
303 fprintf(fp, "#ifdef __cplusplus\n");
304 fprintf(fp, "}\n");
305 fprintf(fp, "#endif\n\n");
308 typedef struct
310 char *filename;
311 struct list link;
312 } filename_node_t;
314 static void add_filename_node(struct list *list, const char *name)
316 filename_node_t *node = xmalloc(sizeof *node);
317 node->filename = dup_basename( name, ".idl" );
318 list_add_tail(list, &node->link);
321 static void free_filename_nodes(struct list *list)
323 filename_node_t *node, *next;
324 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
325 list_remove(&node->link);
326 free(node->filename);
327 free(node);
331 static void write_dlldata_list(struct list *filenames)
333 FILE *dlldata;
334 filename_node_t *node;
336 dlldata = fopen(dlldata_name, "w");
337 if (!dlldata)
338 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
340 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
341 fprintf(dlldata, "- Do not edit ***/\n\n");
342 fprintf(dlldata, "#include <objbase.h>\n");
343 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
344 start_cplusplus_guard(dlldata);
346 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
347 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
349 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
350 fprintf(dlldata, "/* Start of list */\n");
351 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
352 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
353 fprintf(dlldata, "/* End of list */\n");
354 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
356 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
357 end_cplusplus_guard(dlldata);
358 fclose(dlldata);
361 static char *eat_space(char *s)
363 while (isspace((unsigned char) *s))
364 ++s;
365 return s;
368 void write_dlldata(const statement_list_t *stmts)
370 struct list filenames = LIST_INIT(filenames);
371 filename_node_t *node;
372 FILE *dlldata;
374 if (!do_dlldata || !need_proxy_file(stmts))
375 return;
377 dlldata = fopen(dlldata_name, "r");
378 if (dlldata) {
379 static char marker[] = "REFERENCE_PROXY_FILE";
380 char *line = NULL;
381 size_t len = 0;
383 while (widl_getline(&line, &len, dlldata)) {
384 char *start, *end;
385 start = eat_space(line);
386 if (strncmp(start, marker, sizeof marker - 1) == 0) {
387 start = eat_space(start + sizeof marker - 1);
388 if (*start != '(')
389 continue;
390 end = start = eat_space(start + 1);
391 while (*end && *end != ')')
392 ++end;
393 if (*end != ')')
394 continue;
395 while (isspace((unsigned char) end[-1]))
396 --end;
397 *end = '\0';
398 if (start < end)
399 add_filename_node(&filenames, start);
403 if (ferror(dlldata))
404 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
406 free(line);
407 fclose(dlldata);
410 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
411 if (strcmp(proxy_token, node->filename) == 0) {
412 /* We're already in the list, no need to regenerate this file. */
413 free_filename_nodes(&filenames);
414 return;
417 add_filename_node(&filenames, proxy_token);
418 write_dlldata_list(&filenames);
419 free_filename_nodes(&filenames);
422 static void write_id_data_stmts(const statement_list_t *stmts)
424 const statement_t *stmt;
425 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
427 if (stmt->type == STMT_TYPE)
429 const type_t *type = stmt->u.type;
430 if (type_get_type(type) == TYPE_INTERFACE)
432 const UUID *uuid;
433 if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
434 continue;
435 uuid = get_attrp(type->attrs, ATTR_UUID);
436 write_guid(idfile, is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
437 type->name, uuid);
439 else if (type_get_type(type) == TYPE_COCLASS)
441 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
442 write_guid(idfile, "CLSID", type->name, uuid);
445 else if (stmt->type == STMT_LIBRARY)
447 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
448 write_guid(idfile, "LIBID", stmt->u.lib->name, uuid);
449 write_id_data_stmts(stmt->u.lib->stmts);
454 void write_id_data(const statement_list_t *stmts)
456 if (!do_idfile) return;
458 idfile_token = make_token(idfile_name);
460 idfile = fopen(idfile_name, "w");
461 if (! idfile) {
462 error("Could not open %s for output\n", idfile_name);
463 return;
466 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
467 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
468 fprintf(idfile, "#include <rpc.h>\n");
469 fprintf(idfile, "#include <rpcndr.h>\n\n");
470 fprintf(idfile, "#include <initguid.h>\n\n");
471 start_cplusplus_guard(idfile);
473 write_id_data_stmts(stmts);
475 fprintf(idfile, "\n");
476 end_cplusplus_guard(idfile);
478 fclose(idfile);
481 int main(int argc,char *argv[])
483 extern char* optarg;
484 extern int optind;
485 int optc;
486 int ret = 0;
487 int opti = 0;
489 signal( SIGTERM, exit_on_signal );
490 signal( SIGINT, exit_on_signal );
491 #ifdef SIGHUP
492 signal( SIGHUP, exit_on_signal );
493 #endif
495 now = time(NULL);
497 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
498 switch(optc) {
499 case DLLDATA_OPTION:
500 dlldata_name = xstrdup(optarg);
501 break;
502 case DLLDATA_ONLY_OPTION:
503 do_everything = 0;
504 do_dlldata = 1;
505 break;
506 case LOCAL_STUBS_OPTION:
507 do_everything = 0;
508 local_stubs_name = xstrdup(optarg);
509 break;
510 case OLDNAMES_OPTION:
511 old_names = 1;
512 break;
513 case PREFIX_ALL_OPTION:
514 prefix_client = xstrdup(optarg);
515 prefix_server = xstrdup(optarg);
516 break;
517 case PREFIX_CLIENT_OPTION:
518 prefix_client = xstrdup(optarg);
519 break;
520 case PREFIX_SERVER_OPTION:
521 prefix_server = xstrdup(optarg);
522 break;
523 case WIN32_OPTION:
524 do_win32 = 1;
525 do_win64 = 0;
526 break;
527 case WIN64_OPTION:
528 do_win32 = 0;
529 do_win64 = 1;
530 break;
531 case WIN32_ALIGN_OPTION:
532 win32_packing = strtol(optarg, NULL, 0);
533 if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
534 error("Packing must be one of 2, 4 or 8\n");
535 break;
536 case WIN64_ALIGN_OPTION:
537 win64_packing = strtol(optarg, NULL, 0);
538 if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
539 error("Packing must be one of 2, 4 or 8\n");
540 break;
541 case 'b':
542 set_target( optarg );
543 break;
544 case 'c':
545 do_everything = 0;
546 do_client = 1;
547 break;
548 case 'C':
549 client_name = xstrdup(optarg);
550 break;
551 case 'd':
552 debuglevel = strtol(optarg, NULL, 0);
553 break;
554 case 'D':
555 wpp_add_cmdline_define(optarg);
556 break;
557 case 'E':
558 do_everything = 0;
559 preprocess_only = 1;
560 break;
561 case 'h':
562 do_everything = 0;
563 do_header = 1;
564 break;
565 case 'H':
566 header_name = xstrdup(optarg);
567 break;
568 case 'I':
569 wpp_add_include_path(optarg);
570 break;
571 case 'm':
572 if (!strcmp( optarg, "32" )) typelib_kind = SYS_WIN32;
573 else if (!strcmp( optarg, "64" )) typelib_kind = SYS_WIN64;
574 else error( "Invalid -m argument '%s'\n", optarg );
575 break;
576 case 'N':
577 no_preprocess = 1;
578 break;
579 case 'p':
580 do_everything = 0;
581 do_proxies = 1;
582 break;
583 case 'P':
584 proxy_name = xstrdup(optarg);
585 break;
586 case 's':
587 do_everything = 0;
588 do_server = 1;
589 break;
590 case 'S':
591 server_name = xstrdup(optarg);
592 break;
593 case 't':
594 do_everything = 0;
595 do_typelib = 1;
596 break;
597 case 'T':
598 typelib_name = xstrdup(optarg);
599 break;
600 case 'u':
601 do_everything = 0;
602 do_idfile = 1;
603 break;
604 case 'U':
605 idfile_name = xstrdup(optarg);
606 break;
607 case 'V':
608 printf("%s", version_string);
609 return 0;
610 case 'W':
611 pedantic = 1;
612 break;
613 default:
614 fprintf(stderr, "%s", usage);
615 return 1;
619 if(do_everything) {
620 set_everything(TRUE);
623 if (!dlldata_name && do_dlldata)
624 dlldata_name = xstrdup("dlldata.c");
626 if(optind < argc) {
627 if (do_dlldata && !do_everything) {
628 struct list filenames = LIST_INIT(filenames);
629 for ( ; optind < argc; ++optind)
630 add_filename_node(&filenames, argv[optind]);
632 write_dlldata_list(&filenames);
633 free_filename_nodes(&filenames);
634 return 0;
636 else if (optind != argc - 1) {
637 fprintf(stderr, "%s", usage);
638 return 1;
640 else
641 input_name = xstrdup(argv[optind]);
643 else {
644 fprintf(stderr, "%s", usage);
645 return 1;
648 if(debuglevel)
650 setbuf(stdout, NULL);
651 setbuf(stderr, NULL);
654 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
655 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
657 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
658 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
659 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
661 if (!header_name) {
662 header_name = dup_basename(input_name, ".idl");
663 strcat(header_name, ".h");
666 if (!typelib_name && do_typelib) {
667 typelib_name = dup_basename(input_name, ".idl");
668 strcat(typelib_name, ".tlb");
671 if (!proxy_name && do_proxies) {
672 proxy_name = dup_basename(input_name, ".idl");
673 strcat(proxy_name, "_p.c");
676 if (!client_name && do_client) {
677 client_name = dup_basename(input_name, ".idl");
678 strcat(client_name, "_c.c");
681 if (!server_name && do_server) {
682 server_name = dup_basename(input_name, ".idl");
683 strcat(server_name, "_s.c");
686 if (!idfile_name && do_idfile) {
687 idfile_name = dup_basename(input_name, ".idl");
688 strcat(idfile_name, "_i.c");
691 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
692 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
693 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
695 add_widl_version_define();
697 atexit(rm_tempfile);
698 if (!no_preprocess)
700 chat("Starting preprocess\n");
702 if (!preprocess_only)
704 ret = wpp_parse_temp( input_name, header_name, &temp_name );
706 else
708 ret = wpp_parse( input_name, stdout );
711 if(ret) exit(1);
712 if(preprocess_only) exit(0);
713 if(!(parser_in = fopen(temp_name, "r"))) {
714 fprintf(stderr, "Could not open %s for input\n", temp_name);
715 return 1;
718 else {
719 if(!(parser_in = fopen(input_name, "r"))) {
720 fprintf(stderr, "Could not open %s for input\n", input_name);
721 return 1;
725 header_token = make_token(header_name);
727 init_types();
728 ret = parser_parse();
730 fclose(parser_in);
732 if(ret) {
733 exit(1);
736 /* Everything has been done successfully, don't delete any files. */
737 set_everything(FALSE);
738 local_stubs_name = NULL;
740 return 0;
743 static void rm_tempfile(void)
745 abort_import();
746 if(temp_name)
747 unlink(temp_name);
748 if (do_header)
749 unlink(header_name);
750 if (local_stubs_name)
751 unlink(local_stubs_name);
752 if (do_client)
753 unlink(client_name);
754 if (do_server)
755 unlink(server_name);
756 if (do_idfile)
757 unlink(idfile_name);
758 if (do_proxies)
759 unlink(proxy_name);
760 if (do_typelib)
761 unlink(typelib_name);