dmsynth: Drop "synth" from the synth_sink field.
[wine.git] / tools / widl / widl.c
blobf696a73bd2f9d57c292d50e7c0d9a54231d69fb7
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"
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <limits.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SYSCTL_H
35 # include <sys/sysctl.h>
36 #endif
38 #include "widl.h"
39 #include "utils.h"
40 #include "parser.h"
41 #include "wpp_private.h"
42 #include "header.h"
44 static const char usage[] =
45 "Usage: widl [options...] infile.idl\n"
46 " or: widl [options...] --dlldata-only name1 [name2...]\n"
47 " --acf=file Use ACF file\n"
48 " -app_config Ignored, present for midl compatibility\n"
49 " -b arch Set the target architecture\n"
50 " -c Generate client stub\n"
51 " -d n Set debug level to 'n'\n"
52 " -D id[=val] Define preprocessor identifier id=val\n"
53 " -E Preprocess only\n"
54 " --help Display this help and exit\n"
55 " -h Generate headers\n"
56 " -H file Name of header file (default is infile.h)\n"
57 " -I directory Add directory to the include search path (multiple -I allowed)\n"
58 " -L directory Add directory to the library search path (multiple -L allowed)\n"
59 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
60 " -m32, -m64 Set the target architecture (Win32 or Win64)\n"
61 " -N Do not preprocess input\n"
62 " --nostdinc Do not search the standard include path\n"
63 " --ns_prefix Prefix namespaces with ABI namespace\n"
64 " --oldnames Use old naming conventions\n"
65 " -o, --output=NAME Set the output file name\n"
66 " -Otype Type of stubs to generate (-Os, -Oi, -Oif)\n"
67 " -p Generate proxy\n"
68 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
69 " --prefix-client=p Prefix names of client stubs with 'p'\n"
70 " --prefix-server=p Prefix names of server functions with 'p'\n"
71 " -r Generate registration script\n"
72 " -robust Ignored, present for midl compatibility\n"
73 " --sysroot=DIR Prefix include paths with DIR\n"
74 " -s Generate server stub\n"
75 " -t Generate typelib\n"
76 " -u Generate interface identifiers file\n"
77 " -V Print version and exit\n"
78 " -W Enable pedantic warnings\n"
79 " --win32, --win64 Set the target architecture (Win32 or Win64)\n"
80 " --win32-align n Set win32 structure alignment to 'n'\n"
81 " --win64-align n Set win64 structure alignment to 'n'\n"
82 " --winrt Enable Windows Runtime mode\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";
95 static struct target target;
97 int debuglevel = DEBUGLEVEL_NONE;
98 int parser_debug, yy_flex_debug;
100 int pedantic = 0;
101 int do_everything = 1;
102 static int preprocess_only = 0;
103 int do_header = 0;
104 int do_typelib = 0;
105 int do_proxies = 0;
106 int do_client = 0;
107 int do_server = 0;
108 int do_regscript = 0;
109 int do_idfile = 0;
110 int do_dlldata = 0;
111 static int no_preprocess = 0;
112 int old_names = 0;
113 int win32_packing = 8;
114 int win64_packing = 8;
115 int winrt_mode = 0;
116 int use_abi_namespace = 0;
117 static int stdinc = 1;
118 static enum stub_mode stub_mode = MODE_Os;
120 char *input_name;
121 char *input_idl_name;
122 char *acf_name;
123 char *header_name;
124 char *local_stubs_name;
125 char *header_token;
126 char *typelib_name;
127 char *dlldata_name;
128 char *proxy_name;
129 char *proxy_token;
130 char *client_name;
131 char *client_token;
132 char *server_name;
133 char *server_token;
134 char *regscript_name;
135 char *regscript_token;
136 static char *idfile_name;
137 char *temp_name;
138 const char *prefix_client = "";
139 const char *prefix_server = "";
140 static const char *includedir;
141 static const char *dlldir;
142 static struct strarray dlldirs;
143 static char *output_name;
144 static const char *sysroot = "";
146 int line_number = 1;
148 static FILE *idfile;
150 unsigned int pointer_size = 0;
152 time_t now;
154 enum {
155 OLDNAMES_OPTION = CHAR_MAX + 1,
156 ACF_OPTION,
157 APP_CONFIG_OPTION,
158 DLLDATA_OPTION,
159 DLLDATA_ONLY_OPTION,
160 LOCAL_STUBS_OPTION,
161 NOSTDINC_OPTION,
162 PREFIX_ALL_OPTION,
163 PREFIX_CLIENT_OPTION,
164 PREFIX_SERVER_OPTION,
165 PRINT_HELP,
166 RT_NS_PREFIX,
167 RT_OPTION,
168 ROBUST_OPTION,
169 SYSROOT_OPTION,
170 WIN32_OPTION,
171 WIN64_OPTION,
172 WIN32_ALIGN_OPTION,
173 WIN64_ALIGN_OPTION
176 static const char short_options[] =
177 "b:cC:d:D:EhH:I:L:m:No:O:pP:rsS:tT:uU:VW";
178 static const struct long_option long_options[] = {
179 { "acf", 1, ACF_OPTION },
180 { "app_config", 0, APP_CONFIG_OPTION },
181 { "dlldata", 1, DLLDATA_OPTION },
182 { "dlldata-only", 0, DLLDATA_ONLY_OPTION },
183 { "help", 0, PRINT_HELP },
184 { "local-stubs", 1, LOCAL_STUBS_OPTION },
185 { "nostdinc", 0, NOSTDINC_OPTION },
186 { "ns_prefix", 0, RT_NS_PREFIX },
187 { "oldnames", 0, OLDNAMES_OPTION },
188 { "output", 0, 'o' },
189 { "prefix-all", 1, PREFIX_ALL_OPTION },
190 { "prefix-client", 1, PREFIX_CLIENT_OPTION },
191 { "prefix-server", 1, PREFIX_SERVER_OPTION },
192 { "robust", 0, ROBUST_OPTION },
193 { "sysroot", 1, SYSROOT_OPTION },
194 { "target", 0, 'b' },
195 { "winrt", 0, RT_OPTION },
196 { "win32", 0, WIN32_OPTION },
197 { "win64", 0, WIN64_OPTION },
198 { "win32-align", 1, WIN32_ALIGN_OPTION },
199 { "win64-align", 1, WIN64_ALIGN_OPTION },
200 { NULL }
203 static void rm_tempfile(void);
205 enum stub_mode get_stub_mode(void)
207 /* old-style interpreted stubs are not supported on 64-bit */
208 if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
209 return stub_mode;
212 static char *make_token(const char *name)
214 char *token;
215 int i;
217 token = get_basename( name );
218 for (i=0; token[i]; i++) {
219 if (!isalnum(token[i])) token[i] = '_';
220 else token[i] = tolower(token[i]);
222 return token;
225 /* duplicate a basename into a valid C token */
226 static char *dup_basename_token(const char *name, const char *ext)
228 char *p, *ret = replace_extension( get_basename(name), ext, "" );
229 /* map invalid characters to '_' */
230 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
231 return ret;
234 static void add_widl_version_define(void)
236 char version_str[32];
237 unsigned int version;
238 const char *p = PACKAGE_VERSION;
240 /* major */
241 version = atoi(p) * 0x10000;
242 p = strchr(p, '.');
244 /* minor */
245 if (p)
247 version += atoi(p + 1) * 0x100;
248 p = strchr(p + 1, '.');
251 /* build */
252 if (p)
253 version += atoi(p + 1);
255 sprintf(version_str, "__WIDL__=0x%x", version);
256 wpp_add_cmdline_define(version_str);
259 /* clean things up when aborting on a signal */
260 static void exit_on_signal( int sig )
262 exit(1); /* this will call the atexit functions */
265 static void set_everything(int x)
267 do_header = x;
268 do_typelib = x;
269 do_proxies = x;
270 do_client = x;
271 do_server = x;
272 do_regscript = x;
273 do_idfile = x;
274 do_dlldata = x;
277 void start_cplusplus_guard(FILE *fp)
279 fprintf(fp, "#ifdef __cplusplus\n");
280 fprintf(fp, "extern \"C\" {\n");
281 fprintf(fp, "#endif\n\n");
284 void end_cplusplus_guard(FILE *fp)
286 fprintf(fp, "#ifdef __cplusplus\n");
287 fprintf(fp, "}\n");
288 fprintf(fp, "#endif\n\n");
291 static void write_dlldata_list( struct strarray filenames, int define_proxy_delegation)
293 FILE *dlldata;
294 unsigned int i;
296 dlldata = fopen(dlldata_name, "w");
297 if (!dlldata)
298 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
300 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
301 fprintf(dlldata, "- Do not edit ***/\n\n");
302 if (define_proxy_delegation)
303 fprintf(dlldata, "#define PROXY_DELEGATION\n");
304 fprintf(dlldata, "#include <objbase.h>\n");
305 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
306 start_cplusplus_guard(dlldata);
308 for (i = 0; i < filenames.count; i++)
309 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", filenames.str[i]);
311 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
312 fprintf(dlldata, "/* Start of list */\n");
313 for (i = 0; i < filenames.count; i++)
314 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", filenames.str[i]);
315 fprintf(dlldata, "/* End of list */\n");
316 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
318 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
319 end_cplusplus_guard(dlldata);
320 fclose(dlldata);
323 static char *eat_space(char *s)
325 while (isspace((unsigned char) *s))
326 ++s;
327 return s;
330 void write_dlldata(const statement_list_t *stmts)
332 struct strarray filenames = empty_strarray;
333 int define_proxy_delegation = 0;
334 FILE *dlldata;
336 if (!do_dlldata || !need_proxy_file(stmts))
337 return;
339 define_proxy_delegation = need_proxy_delegation(stmts);
341 dlldata = fopen(dlldata_name, "r");
342 if (dlldata) {
343 static const char marker[] = "REFERENCE_PROXY_FILE";
344 static const char delegation_define[] = "#define PROXY_DELEGATION";
345 char *line = NULL;
346 size_t len = 0;
348 while (widl_getline(&line, &len, dlldata)) {
349 char *start, *end;
350 start = eat_space(line);
351 if (strncmp(start, marker, sizeof marker - 1) == 0) {
352 start = eat_space(start + sizeof marker - 1);
353 if (*start != '(')
354 continue;
355 end = start = eat_space(start + 1);
356 while (*end && *end != ')')
357 ++end;
358 if (*end != ')')
359 continue;
360 while (isspace((unsigned char) end[-1]))
361 --end;
362 *end = '\0';
363 if (start < end)
364 strarray_add(&filenames, replace_extension( get_basename( start ), ".idl", "" ));
365 }else if (!define_proxy_delegation && strncmp(start, delegation_define, sizeof(delegation_define)-1)) {
366 define_proxy_delegation = 1;
370 if (ferror(dlldata))
371 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
373 free(line);
374 fclose(dlldata);
377 if (strarray_exists( &filenames, proxy_token ))
378 /* We're already in the list, no need to regenerate this file. */
379 return;
381 strarray_add(&filenames, proxy_token);
382 write_dlldata_list(filenames, define_proxy_delegation);
385 static void write_id_guid(FILE *f, const char *type, const char *guid_prefix, const char *name, const UUID *uuid)
387 if (!uuid) return;
388 fprintf(f, "MIDL_DEFINE_GUID(%s, %s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
389 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
390 type, guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
391 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
392 uuid->Data4[6], uuid->Data4[7]);
395 static void write_id_data_stmts(const statement_list_t *stmts)
397 const statement_t *stmt;
398 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
400 if (stmt->type == STMT_TYPE)
402 const type_t *type = stmt->u.type;
403 if (type_get_type(type) == TYPE_INTERFACE)
405 const UUID *uuid;
406 if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
407 continue;
408 uuid = get_attrp(type->attrs, ATTR_UUID);
409 write_id_guid(idfile, "IID", is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
410 type->name, uuid);
411 if (type_iface_get_async_iface(type))
413 uuid = get_attrp(type_iface_get_async_iface(type)->attrs, ATTR_UUID);
414 write_id_guid(idfile, "IID", "IID", type_iface_get_async_iface(type)->name, uuid);
417 else if (type_get_type(type) == TYPE_COCLASS)
419 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
420 write_id_guid(idfile, "CLSID", "CLSID", type->name, uuid);
423 else if (stmt->type == STMT_LIBRARY)
425 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
426 write_id_guid(idfile, "IID", "LIBID", stmt->u.lib->name, uuid);
427 write_id_data_stmts(stmt->u.lib->stmts);
432 void write_id_data(const statement_list_t *stmts)
434 if (!do_idfile) return;
436 idfile = fopen(idfile_name, "w");
437 if (! idfile) {
438 error("Could not open %s for output\n", idfile_name);
439 return;
442 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
443 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_idl_name);
444 fprintf(idfile, "#include <rpc.h>\n");
445 fprintf(idfile, "#include <rpcndr.h>\n\n");
447 fprintf(idfile, "#ifdef _MIDL_USE_GUIDDEF_\n\n");
449 fprintf(idfile, "#ifndef INITGUID\n");
450 fprintf(idfile, "#define INITGUID\n");
451 fprintf(idfile, "#include <guiddef.h>\n");
452 fprintf(idfile, "#undef INITGUID\n");
453 fprintf(idfile, "#else\n");
454 fprintf(idfile, "#include <guiddef.h>\n");
455 fprintf(idfile, "#endif\n\n");
457 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
458 fprintf(idfile, " DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)\n\n");
460 fprintf(idfile, "#elif defined(__cplusplus)\n\n");
462 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
463 fprintf(idfile, " EXTERN_C const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
465 fprintf(idfile, "#else\n\n");
467 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
468 fprintf(idfile, " const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
470 fprintf(idfile, "#endif\n\n");
471 start_cplusplus_guard(idfile);
473 write_id_data_stmts(stmts);
475 fprintf(idfile, "\n");
476 end_cplusplus_guard(idfile);
477 fprintf(idfile, "#undef MIDL_DEFINE_GUID\n" );
479 fclose(idfile);
482 static void init_argv0_dir( const char *argv0 )
484 #ifndef _WIN32
485 char *dir;
487 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
488 dir = realpath( "/proc/self/exe", NULL );
489 #elif defined (__FreeBSD__) || defined(__DragonFly__)
490 static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
491 size_t path_size = PATH_MAX;
492 char *path = malloc( path_size );
493 if (path && !sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
494 dir = realpath( path, NULL );
495 free( path );
496 #else
497 dir = realpath( argv0, NULL );
498 #endif
499 if (!dir) return;
500 includedir = strmake( "%s/%s", get_dirname( dir ), BIN_TO_INCLUDEDIR );
501 dlldir = strmake( "%s/%s", get_dirname( dir ), BIN_TO_DLLDIR );
502 #endif
505 static void option_callback( int optc, char *optarg )
507 switch (optc)
509 case DLLDATA_OPTION:
510 dlldata_name = xstrdup(optarg);
511 break;
512 case DLLDATA_ONLY_OPTION:
513 do_everything = 0;
514 do_dlldata = 1;
515 break;
516 case LOCAL_STUBS_OPTION:
517 do_everything = 0;
518 local_stubs_name = xstrdup(optarg);
519 break;
520 case NOSTDINC_OPTION:
521 stdinc = 0;
522 break;
523 case OLDNAMES_OPTION:
524 old_names = 1;
525 break;
526 case PREFIX_ALL_OPTION:
527 prefix_client = xstrdup(optarg);
528 prefix_server = xstrdup(optarg);
529 break;
530 case PREFIX_CLIENT_OPTION:
531 prefix_client = xstrdup(optarg);
532 break;
533 case PREFIX_SERVER_OPTION:
534 prefix_server = xstrdup(optarg);
535 break;
536 case PRINT_HELP:
537 fprintf(stderr, "%s", usage);
538 exit(0);
539 case RT_OPTION:
540 winrt_mode = 1;
541 break;
542 case RT_NS_PREFIX:
543 use_abi_namespace = 1;
544 break;
545 case SYSROOT_OPTION:
546 sysroot = xstrdup(optarg);
547 break;
548 case WIN32_OPTION:
549 pointer_size = 4;
550 break;
551 case WIN64_OPTION:
552 pointer_size = 8;
553 break;
554 case WIN32_ALIGN_OPTION:
555 win32_packing = strtol(optarg, NULL, 0);
556 if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
557 error("Packing must be one of 2, 4 or 8\n");
558 break;
559 case WIN64_ALIGN_OPTION:
560 win64_packing = strtol(optarg, NULL, 0);
561 if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
562 error("Packing must be one of 2, 4 or 8\n");
563 break;
564 case ACF_OPTION:
565 acf_name = xstrdup(optarg);
566 break;
567 case APP_CONFIG_OPTION:
568 /* widl does not distinguish between app_mode and default mode,
569 but we ignore this option for midl compatibility */
570 break;
571 case ROBUST_OPTION:
572 /* FIXME: Support robust option */
573 break;
574 case 'b':
575 if (!parse_target( optarg, &target ))
576 error( "Invalid target specification '%s'\n", optarg );
577 break;
578 case 'c':
579 do_everything = 0;
580 do_client = 1;
581 break;
582 case 'C':
583 client_name = xstrdup(optarg);
584 break;
585 case 'd':
586 debuglevel = strtol(optarg, NULL, 0);
587 break;
588 case 'D':
589 wpp_add_cmdline_define(optarg);
590 break;
591 case 'E':
592 do_everything = 0;
593 preprocess_only = 1;
594 break;
595 case 'h':
596 do_everything = 0;
597 do_header = 1;
598 break;
599 case 'H':
600 header_name = xstrdup(optarg);
601 break;
602 case 'I':
603 wpp_add_include_path(optarg);
604 break;
605 case 'L':
606 strarray_add( &dlldirs, optarg );
607 break;
608 case 'm':
609 if (!strcmp( optarg, "32" )) pointer_size = 4;
610 else if (!strcmp( optarg, "64" )) pointer_size = 8;
611 break;
612 case 'N':
613 no_preprocess = 1;
614 break;
615 case 'o':
616 output_name = xstrdup(optarg);
617 break;
618 case 'O':
619 if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
620 else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
621 else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
622 else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
623 else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
624 else error( "Invalid argument '-O%s'\n", optarg );
625 break;
626 case 'p':
627 do_everything = 0;
628 do_proxies = 1;
629 break;
630 case 'P':
631 proxy_name = xstrdup(optarg);
632 break;
633 case 'r':
634 do_everything = 0;
635 do_regscript = 1;
636 break;
637 case 's':
638 do_everything = 0;
639 do_server = 1;
640 break;
641 case 'S':
642 server_name = xstrdup(optarg);
643 break;
644 case 't':
645 do_everything = 0;
646 do_typelib = 1;
647 break;
648 case 'T':
649 typelib_name = xstrdup(optarg);
650 break;
651 case 'u':
652 do_everything = 0;
653 do_idfile = 1;
654 break;
655 case 'U':
656 idfile_name = xstrdup(optarg);
657 break;
658 case 'V':
659 printf("%s", version_string);
660 exit(0);
661 case 'W':
662 pedantic = 1;
663 break;
664 case '?':
665 fprintf(stderr, "widl: %s\n\n%s", optarg, usage);
666 exit(1);
670 int open_typelib( const char *name )
672 static const char *default_dirs[] = { DLLDIR, "/usr/lib/wine", "/usr/local/lib/wine" };
673 struct target win_target = { target.cpu, PLATFORM_WINDOWS };
674 const char *pe_dir = get_arch_dir( win_target );
675 int fd;
676 unsigned int i;
678 #define TRYOPEN(str) do { \
679 char *file = str; \
680 if ((fd = open( file, O_RDONLY | O_BINARY )) != -1) return fd; \
681 free( file ); } while(0)
683 for (i = 0; i < dlldirs.count; i++)
685 if (strendswith( dlldirs.str[i], "/*" )) /* special case for wine build tree */
687 int namelen = strlen( name );
688 if (strendswith( name, ".dll" )) namelen -= 4;
689 TRYOPEN( strmake( "%.*s/%.*s/%s", (int)strlen(dlldirs.str[i]) - 2, dlldirs.str[i],
690 namelen, name, name ));
692 else
694 TRYOPEN( strmake( "%s%s/%s", dlldirs.str[i], pe_dir, name ));
695 TRYOPEN( strmake( "%s/%s", dlldirs.str[i], name ));
699 if (stdinc)
701 if (dlldir)
703 TRYOPEN( strmake( "%s%s/%s", dlldir, pe_dir, name ));
704 TRYOPEN( strmake( "%s/%s", dlldir, name ));
706 for (i = 0; i < ARRAY_SIZE(default_dirs); i++)
708 if (i && !strcmp( default_dirs[i], default_dirs[0] )) continue;
709 TRYOPEN( strmake( "%s%s/%s", default_dirs[i], pe_dir, name ));
712 error( "cannot find %s\n", name );
713 #undef TRYOPEN
716 int main(int argc,char *argv[])
718 int i;
719 int ret = 0;
720 struct strarray files;
722 signal( SIGTERM, exit_on_signal );
723 signal( SIGINT, exit_on_signal );
724 #ifdef SIGHUP
725 signal( SIGHUP, exit_on_signal );
726 #endif
727 init_argv0_dir( argv[0] );
728 target = init_argv0_target( argv[0] );
730 now = time(NULL);
732 files = parse_options( argc, argv, short_options, long_options, 1, option_callback );
734 if (stdinc)
736 static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
738 if (includedir)
740 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
741 wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
743 for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
745 if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
746 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
747 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
751 if (pointer_size)
752 set_target_ptr_size( &target, pointer_size );
753 else
754 pointer_size = get_target_ptr_size( target );
756 /* if nothing specified, try to guess output type from the output file name */
757 if (output_name && do_everything && !do_header && !do_typelib && !do_proxies &&
758 !do_client && !do_server && !do_regscript && !do_idfile && !do_dlldata)
760 do_everything = 0;
761 if (strendswith( output_name, ".h" )) do_header = 1;
762 else if (strendswith( output_name, ".tlb" )) do_typelib = 1;
763 else if (strendswith( output_name, "_p.c" )) do_proxies = 1;
764 else if (strendswith( output_name, "_c.c" )) do_client = 1;
765 else if (strendswith( output_name, "_s.c" )) do_server = 1;
766 else if (strendswith( output_name, "_i.c" )) do_idfile = 1;
767 else if (strendswith( output_name, "_r.res" )) do_regscript = 1;
768 else if (strendswith( output_name, "_t.res" )) do_typelib = 1;
769 else if (strendswith( output_name, "_l.res" )) do_typelib = 1;
770 else if (strendswith( output_name, "dlldata.c" )) do_dlldata = 1;
771 else do_everything = 1;
774 if(do_everything) {
775 set_everything(TRUE);
778 if (do_header + do_typelib + do_proxies + do_client +
779 do_server + do_regscript + do_idfile + do_dlldata == 1 && output_name)
781 if (do_header && !header_name) header_name = output_name;
782 else if (do_typelib && !typelib_name) typelib_name = output_name;
783 else if (do_proxies && !proxy_name) proxy_name = output_name;
784 else if (do_client && !client_name) client_name = output_name;
785 else if (do_server && !server_name) server_name = output_name;
786 else if (do_regscript && !regscript_name) regscript_name = output_name;
787 else if (do_idfile && !idfile_name) idfile_name = output_name;
788 else if (do_dlldata && !dlldata_name) dlldata_name = output_name;
791 if (!dlldata_name && do_dlldata)
792 dlldata_name = xstrdup("dlldata.c");
794 if (files.count) {
795 if (do_dlldata && !do_everything) {
796 struct strarray filenames = empty_strarray;
797 for (i = 0; i < files.count; i++)
798 strarray_add(&filenames, replace_extension( get_basename( files.str[i] ), ".idl", "" ));
800 write_dlldata_list(filenames, 0 /* FIXME */ );
801 return 0;
803 else if (files.count > 1) {
804 fprintf(stderr, "%s", usage);
805 return 1;
807 else
808 input_idl_name = input_name = xstrdup(files.str[0]);
810 else {
811 fprintf(stderr, "%s", usage);
812 return 1;
815 if(debuglevel)
817 setbuf(stdout, NULL);
818 setbuf(stderr, NULL);
821 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
822 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
824 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
825 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
826 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
828 if (!header_name)
829 header_name = replace_extension( get_basename(input_name), ".idl", ".h" );
831 if (!typelib_name && do_typelib)
832 typelib_name = replace_extension( get_basename(input_name), ".idl", ".tlb" );
834 if (!proxy_name && do_proxies)
835 proxy_name = replace_extension( get_basename(input_name), ".idl", "_p.c" );
837 if (!client_name && do_client)
838 client_name = replace_extension( get_basename(input_name), ".idl", "_c.c" );
840 if (!server_name && do_server)
841 server_name = replace_extension( get_basename(input_name), ".idl", "_s.c" );
843 if (!regscript_name && do_regscript)
844 regscript_name = replace_extension( get_basename(input_name), ".idl", "_r.rgs" );
846 if (!idfile_name && do_idfile)
847 idfile_name = replace_extension( get_basename(input_name), ".idl", "_i.c" );
849 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
850 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
851 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
852 if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
854 add_widl_version_define();
855 wpp_add_cmdline_define("_WIN32=1");
857 atexit(rm_tempfile);
858 if (!no_preprocess)
860 chat("Starting preprocess\n");
862 if (!preprocess_only)
864 FILE *output;
865 int fd;
866 char *name;
868 fd = make_temp_file( header_name, NULL, &name );
869 temp_name = name;
870 if (!(output = fdopen(fd, "wt")))
871 error("Could not open fd %s for writing\n", name);
873 ret = wpp_parse( input_name, output );
874 fclose( output );
876 else
878 ret = wpp_parse( input_name, stdout );
881 if(ret) exit(1);
882 if(preprocess_only) exit(0);
883 if(!(parser_in = fopen(temp_name, "r"))) {
884 fprintf(stderr, "Could not open %s for input\n", temp_name);
885 return 1;
888 else {
889 if(!(parser_in = fopen(input_name, "r"))) {
890 fprintf(stderr, "Could not open %s for input\n", input_name);
891 return 1;
895 header_token = make_token(header_name);
897 init_types();
898 ret = parser_parse();
900 fclose(parser_in);
902 if(ret) {
903 exit(1);
906 /* Everything has been done successfully, don't delete any files. */
907 set_everything(FALSE);
908 local_stubs_name = NULL;
910 return 0;
913 static void rm_tempfile(void)
915 abort_import();
916 if(temp_name)
917 unlink(temp_name);
918 if (do_header)
919 unlink(header_name);
920 if (local_stubs_name)
921 unlink(local_stubs_name);
922 if (do_client)
923 unlink(client_name);
924 if (do_server)
925 unlink(server_name);
926 if (do_regscript)
927 unlink(regscript_name);
928 if (do_idfile)
929 unlink(idfile_name);
930 if (do_proxies)
931 unlink(proxy_name);
932 if (do_typelib)
933 unlink(typelib_name);