msacm32: Add invalid parameter checks for acmFormatDetails().
[wine.git] / tools / widl / widl.c
bloba38f9170ead6ccae4f63f78102e02a7bba5786dc
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 /* w = select win16/win32 output (?) */
51 static const char usage[] =
52 "Usage: widl [options...] infile.idl\n"
53 " or: widl [options...] --dlldata-only name1 [name2...]\n"
54 " -app_config Ignored, present for midl compatibility\n"
55 " -b arch Set the target architecture\n"
56 " -c Generate client stub\n"
57 " -d n Set debug level to 'n'\n"
58 " -D id[=val] Define preprocessor identifier id=val\n"
59 " -E Preprocess only\n"
60 " --help Display this help and exit\n"
61 " -h Generate headers\n"
62 " -H file Name of header file (default is infile.h)\n"
63 " -I path Set include search dir to path (multiple -I allowed)\n"
64 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
65 " -m32, -m64 Set the kind of typelib to build (Win32 or Win64)\n"
66 " -N Do not preprocess input\n"
67 " --oldnames Use old naming conventions\n"
68 " -o, --output=NAME Set the output file name\n"
69 " -Otype Type of stubs to generate (-Os, -Oi, -Oif)\n"
70 " -p Generate proxy\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 " -r Generate registration script\n"
75 " --winrt Enable Windows Runtime mode\n"
76 " --ns_prefix Prefix namespaces with ABI namespace\n"
77 " -s Generate server stub\n"
78 " -t Generate typelib\n"
79 " -u Generate interface identifiers file\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_regscript = 0;
110 int do_idfile = 0;
111 int do_dlldata = 0;
112 static int no_preprocess = 0;
113 int old_names = 0;
114 int do_win32 = 1;
115 int do_win64 = 1;
116 int win32_packing = 8;
117 int win64_packing = 8;
118 int winrt_mode = 0;
119 int use_abi_namespace = 0;
120 static enum stub_mode stub_mode = MODE_Os;
122 char *input_name;
123 char *input_idl_name;
124 char *header_name;
125 char *local_stubs_name;
126 char *header_token;
127 char *typelib_name;
128 char *dlldata_name;
129 char *proxy_name;
130 char *proxy_token;
131 char *client_name;
132 char *client_token;
133 char *server_name;
134 char *server_token;
135 char *regscript_name;
136 char *regscript_token;
137 static char *idfile_name;
138 char *temp_name;
139 const char *prefix_client = "";
140 const char *prefix_server = "";
142 int line_number = 1;
144 static FILE *idfile;
146 unsigned int pointer_size = 0;
147 syskind_t typelib_kind = sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32;
149 time_t now;
151 enum {
152 OLDNAMES_OPTION = CHAR_MAX + 1,
153 DLLDATA_OPTION,
154 DLLDATA_ONLY_OPTION,
155 LOCAL_STUBS_OPTION,
156 PREFIX_ALL_OPTION,
157 PREFIX_CLIENT_OPTION,
158 PREFIX_SERVER_OPTION,
159 PRINT_HELP,
160 RT_NS_PREFIX,
161 RT_OPTION,
162 WIN32_OPTION,
163 WIN64_OPTION,
164 WIN32_ALIGN_OPTION,
165 WIN64_ALIGN_OPTION,
166 APP_CONFIG_OPTION
169 static const char short_options[] =
170 "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
171 static const struct option long_options[] = {
172 { "dlldata", 1, NULL, DLLDATA_OPTION },
173 { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
174 { "help", 0, NULL, PRINT_HELP },
175 { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
176 { "ns_prefix", 0, NULL, RT_NS_PREFIX },
177 { "oldnames", 0, NULL, OLDNAMES_OPTION },
178 { "output", 0, NULL, 'o' },
179 { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
180 { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
181 { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
182 { "winrt", 0, NULL, RT_OPTION },
183 { "win32", 0, NULL, WIN32_OPTION },
184 { "win64", 0, NULL, WIN64_OPTION },
185 { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
186 { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
187 { "app_config", 0, NULL, APP_CONFIG_OPTION },
188 { NULL, 0, NULL, 0 }
191 static void rm_tempfile(void);
193 enum stub_mode get_stub_mode(void)
195 /* old-style interpreted stubs are not supported on 64-bit */
196 if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
197 return stub_mode;
200 static char *make_token(const char *name)
202 char *token;
203 char *slash;
204 int i;
206 slash = strrchr(name, '/');
207 if(!slash)
208 slash = strrchr(name, '\\');
210 if (slash) name = slash + 1;
212 token = xstrdup(name);
213 for (i=0; token[i]; i++) {
214 if (!isalnum(token[i])) token[i] = '_';
215 else token[i] = tolower(token[i]);
217 return token;
220 /* duplicate a basename into a valid C token */
221 static char *dup_basename_token(const char *name, const char *ext)
223 char *p, *ret = dup_basename( name, ext );
224 /* map invalid characters to '_' */
225 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
226 return ret;
229 static void add_widl_version_define(void)
231 unsigned int version;
232 const char *p = PACKAGE_VERSION;
234 /* major */
235 version = atoi(p) * 0x10000;
236 p = strchr(p, '.');
238 /* minor */
239 if (p)
241 version += atoi(p + 1) * 0x100;
242 p = strchr(p + 1, '.');
245 /* build */
246 if (p)
247 version += atoi(p + 1);
249 if (version != 0)
251 char version_str[11];
252 snprintf(version_str, sizeof(version_str), "0x%x", version);
253 wpp_add_define("__WIDL__", version_str);
255 else
256 wpp_add_define("__WIDL__", NULL);
259 /* set the target platform */
260 static void set_target( const char *target )
262 static const struct
264 const char *name;
265 syskind_t kind;
266 } cpu_names[] =
268 { "i386", SYS_WIN32 },
269 { "i486", SYS_WIN32 },
270 { "i586", SYS_WIN32 },
271 { "i686", SYS_WIN32 },
272 { "i786", SYS_WIN32 },
273 { "amd64", SYS_WIN64 },
274 { "x86_64", SYS_WIN64 },
275 { "powerpc", SYS_WIN32 },
276 { "arm", SYS_WIN32 },
277 { "aarch64", SYS_WIN64 }
280 unsigned int i;
281 char *p, *spec = xstrdup( target );
283 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
285 if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
286 *p++ = 0;
287 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
289 if (!strcmp( cpu_names[i].name, spec ))
291 typelib_kind = cpu_names[i].kind;
292 free( spec );
293 return;
296 error( "Unrecognized CPU '%s'\n", spec );
299 /* clean things up when aborting on a signal */
300 static void exit_on_signal( int sig )
302 exit(1); /* this will call the atexit functions */
305 static void set_everything(int x)
307 do_header = x;
308 do_typelib = x;
309 do_proxies = x;
310 do_client = x;
311 do_server = x;
312 do_regscript = x;
313 do_idfile = x;
314 do_dlldata = x;
317 void start_cplusplus_guard(FILE *fp)
319 fprintf(fp, "#ifdef __cplusplus\n");
320 fprintf(fp, "extern \"C\" {\n");
321 fprintf(fp, "#endif\n\n");
324 void end_cplusplus_guard(FILE *fp)
326 fprintf(fp, "#ifdef __cplusplus\n");
327 fprintf(fp, "}\n");
328 fprintf(fp, "#endif\n\n");
331 typedef struct
333 char *filename;
334 struct list link;
335 } filename_node_t;
337 static void add_filename_node(struct list *list, const char *name)
339 filename_node_t *node = xmalloc(sizeof *node);
340 node->filename = dup_basename( name, ".idl" );
341 list_add_tail(list, &node->link);
344 static void free_filename_nodes(struct list *list)
346 filename_node_t *node, *next;
347 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
348 list_remove(&node->link);
349 free(node->filename);
350 free(node);
354 static void write_dlldata_list(struct list *filenames, int define_proxy_delegation)
356 FILE *dlldata;
357 filename_node_t *node;
359 dlldata = fopen(dlldata_name, "w");
360 if (!dlldata)
361 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
363 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
364 fprintf(dlldata, "- Do not edit ***/\n\n");
365 if (define_proxy_delegation)
366 fprintf(dlldata, "#define PROXY_DELEGATION\n");
367 fprintf(dlldata, "#include <objbase.h>\n");
368 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
369 start_cplusplus_guard(dlldata);
371 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
372 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
374 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
375 fprintf(dlldata, "/* Start of list */\n");
376 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
377 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
378 fprintf(dlldata, "/* End of list */\n");
379 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
381 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
382 end_cplusplus_guard(dlldata);
383 fclose(dlldata);
386 static char *eat_space(char *s)
388 while (isspace((unsigned char) *s))
389 ++s;
390 return s;
393 void write_dlldata(const statement_list_t *stmts)
395 struct list filenames = LIST_INIT(filenames);
396 int define_proxy_delegation = 0;
397 filename_node_t *node;
398 FILE *dlldata;
400 if (!do_dlldata || !need_proxy_file(stmts))
401 return;
403 define_proxy_delegation = need_proxy_delegation(stmts);
405 dlldata = fopen(dlldata_name, "r");
406 if (dlldata) {
407 static const char marker[] = "REFERENCE_PROXY_FILE";
408 static const char delegation_define[] = "#define PROXY_DELEGATION";
409 char *line = NULL;
410 size_t len = 0;
412 while (widl_getline(&line, &len, dlldata)) {
413 char *start, *end;
414 start = eat_space(line);
415 if (strncmp(start, marker, sizeof marker - 1) == 0) {
416 start = eat_space(start + sizeof marker - 1);
417 if (*start != '(')
418 continue;
419 end = start = eat_space(start + 1);
420 while (*end && *end != ')')
421 ++end;
422 if (*end != ')')
423 continue;
424 while (isspace((unsigned char) end[-1]))
425 --end;
426 *end = '\0';
427 if (start < end)
428 add_filename_node(&filenames, start);
429 }else if (!define_proxy_delegation && strncmp(start, delegation_define, sizeof(delegation_define)-1)) {
430 define_proxy_delegation = 1;
434 if (ferror(dlldata))
435 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
437 free(line);
438 fclose(dlldata);
441 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
442 if (strcmp(proxy_token, node->filename) == 0) {
443 /* We're already in the list, no need to regenerate this file. */
444 free_filename_nodes(&filenames);
445 return;
448 add_filename_node(&filenames, proxy_token);
449 write_dlldata_list(&filenames, define_proxy_delegation);
450 free_filename_nodes(&filenames);
453 static void write_id_guid(FILE *f, const char *type, const char *guid_prefix, const char *name, const UUID *uuid)
455 if (!uuid) return;
456 fprintf(f, "MIDL_DEFINE_GUID(%s, %s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
457 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
458 type, guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
459 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
460 uuid->Data4[6], uuid->Data4[7]);
463 static void write_id_data_stmts(const statement_list_t *stmts)
465 const statement_t *stmt;
466 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
468 if (stmt->type == STMT_TYPE)
470 const type_t *type = stmt->u.type;
471 if (type_get_type(type) == TYPE_INTERFACE)
473 const UUID *uuid;
474 if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
475 continue;
476 uuid = get_attrp(type->attrs, ATTR_UUID);
477 write_id_guid(idfile, "IID", is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
478 type->name, uuid);
480 else if (type_get_type(type) == TYPE_COCLASS)
482 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
483 write_id_guid(idfile, "CLSID", "CLSID", type->name, uuid);
486 else if (stmt->type == STMT_LIBRARY)
488 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
489 write_id_guid(idfile, "IID", "LIBID", stmt->u.lib->name, uuid);
490 write_id_data_stmts(stmt->u.lib->stmts);
495 void write_id_data(const statement_list_t *stmts)
497 if (!do_idfile) return;
499 idfile = fopen(idfile_name, "w");
500 if (! idfile) {
501 error("Could not open %s for output\n", idfile_name);
502 return;
505 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
506 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_idl_name);
507 fprintf(idfile, "#include <rpc.h>\n");
508 fprintf(idfile, "#include <rpcndr.h>\n\n");
510 fprintf(idfile, "#ifdef _MIDL_USE_GUIDDEF_\n\n");
512 fprintf(idfile, "#ifndef INITGUID\n");
513 fprintf(idfile, "#define INITGUID\n");
514 fprintf(idfile, "#include <guiddef.h>\n");
515 fprintf(idfile, "#undef INITGUID\n");
516 fprintf(idfile, "#else\n");
517 fprintf(idfile, "#include <guiddef.h>\n");
518 fprintf(idfile, "#endif\n\n");
520 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
521 fprintf(idfile, " DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)\n\n");
523 fprintf(idfile, "#else\n\n");
525 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
526 fprintf(idfile, " const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
528 fprintf(idfile, "#endif\n\n");
529 start_cplusplus_guard(idfile);
531 write_id_data_stmts(stmts);
533 fprintf(idfile, "\n");
534 end_cplusplus_guard(idfile);
535 fprintf(idfile, "#undef MIDL_DEFINE_GUID\n" );
537 fclose(idfile);
540 int main(int argc,char *argv[])
542 int optc;
543 int ret = 0;
544 int opti = 0;
545 char *output_name = NULL;
547 signal( SIGTERM, exit_on_signal );
548 signal( SIGINT, exit_on_signal );
549 #ifdef SIGHUP
550 signal( SIGHUP, exit_on_signal );
551 #endif
553 now = time(NULL);
555 while((optc = getopt_long_only(argc, argv, short_options, long_options, &opti)) != EOF) {
556 switch(optc) {
557 case DLLDATA_OPTION:
558 dlldata_name = xstrdup(optarg);
559 break;
560 case DLLDATA_ONLY_OPTION:
561 do_everything = 0;
562 do_dlldata = 1;
563 break;
564 case LOCAL_STUBS_OPTION:
565 do_everything = 0;
566 local_stubs_name = xstrdup(optarg);
567 break;
568 case OLDNAMES_OPTION:
569 old_names = 1;
570 break;
571 case PREFIX_ALL_OPTION:
572 prefix_client = xstrdup(optarg);
573 prefix_server = xstrdup(optarg);
574 break;
575 case PREFIX_CLIENT_OPTION:
576 prefix_client = xstrdup(optarg);
577 break;
578 case PREFIX_SERVER_OPTION:
579 prefix_server = xstrdup(optarg);
580 break;
581 case PRINT_HELP:
582 fprintf(stderr, "%s", usage);
583 return 0;
584 case RT_OPTION:
585 winrt_mode = 1;
586 break;
587 case RT_NS_PREFIX:
588 use_abi_namespace = 1;
589 break;
590 case WIN32_OPTION:
591 do_win32 = 1;
592 do_win64 = 0;
593 break;
594 case WIN64_OPTION:
595 do_win32 = 0;
596 do_win64 = 1;
597 break;
598 case WIN32_ALIGN_OPTION:
599 win32_packing = strtol(optarg, NULL, 0);
600 if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
601 error("Packing must be one of 2, 4 or 8\n");
602 break;
603 case WIN64_ALIGN_OPTION:
604 win64_packing = strtol(optarg, NULL, 0);
605 if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
606 error("Packing must be one of 2, 4 or 8\n");
607 break;
608 case APP_CONFIG_OPTION:
609 /* widl does not distinguish between app_mode and default mode,
610 but we ignore this option for midl compatibility */
611 break;
612 case 'b':
613 set_target( optarg );
614 break;
615 case 'c':
616 do_everything = 0;
617 do_client = 1;
618 break;
619 case 'C':
620 client_name = xstrdup(optarg);
621 break;
622 case 'd':
623 debuglevel = strtol(optarg, NULL, 0);
624 break;
625 case 'D':
626 wpp_add_cmdline_define(optarg);
627 break;
628 case 'E':
629 do_everything = 0;
630 preprocess_only = 1;
631 break;
632 case 'h':
633 do_everything = 0;
634 do_header = 1;
635 break;
636 case 'H':
637 header_name = xstrdup(optarg);
638 break;
639 case 'I':
640 wpp_add_include_path(optarg);
641 break;
642 case 'm':
643 if (!strcmp( optarg, "32" )) typelib_kind = SYS_WIN32;
644 else if (!strcmp( optarg, "64" )) typelib_kind = SYS_WIN64;
645 break;
646 case 'N':
647 no_preprocess = 1;
648 break;
649 case 'o':
650 output_name = xstrdup(optarg);
651 break;
652 case 'O':
653 if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
654 else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
655 else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
656 else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
657 else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
658 else error( "Invalid argument '-O%s'\n", optarg );
659 break;
660 case 'p':
661 do_everything = 0;
662 do_proxies = 1;
663 break;
664 case 'P':
665 proxy_name = xstrdup(optarg);
666 break;
667 case 'r':
668 do_everything = 0;
669 do_regscript = 1;
670 break;
671 case 's':
672 do_everything = 0;
673 do_server = 1;
674 break;
675 case 'S':
676 server_name = xstrdup(optarg);
677 break;
678 case 't':
679 do_everything = 0;
680 do_typelib = 1;
681 break;
682 case 'T':
683 typelib_name = xstrdup(optarg);
684 break;
685 case 'u':
686 do_everything = 0;
687 do_idfile = 1;
688 break;
689 case 'U':
690 idfile_name = xstrdup(optarg);
691 break;
692 case 'V':
693 printf("%s", version_string);
694 return 0;
695 case 'W':
696 pedantic = 1;
697 break;
698 default:
699 fprintf(stderr, "%s", usage);
700 return 1;
704 #ifdef DEFAULT_INCLUDE_DIR
705 wpp_add_include_path(DEFAULT_INCLUDE_DIR);
706 #endif
708 /* if nothing specified, try to guess output type from the output file name */
709 if (output_name && do_everything && !do_header && !do_typelib && !do_proxies &&
710 !do_client && !do_server && !do_regscript && !do_idfile && !do_dlldata)
712 do_everything = 0;
713 if (strendswith( output_name, ".h" )) do_header = 1;
714 else if (strendswith( output_name, ".tlb" )) do_typelib = 1;
715 else if (strendswith( output_name, "_p.c" )) do_proxies = 1;
716 else if (strendswith( output_name, "_c.c" )) do_client = 1;
717 else if (strendswith( output_name, "_s.c" )) do_server = 1;
718 else if (strendswith( output_name, "_i.c" )) do_idfile = 1;
719 else if (strendswith( output_name, "_r.res" )) do_regscript = 1;
720 else if (strendswith( output_name, "_t.res" )) do_typelib = 1;
721 else if (strendswith( output_name, "dlldata.c" )) do_dlldata = 1;
722 else do_everything = 1;
725 if(do_everything) {
726 set_everything(TRUE);
729 if (!output_name) output_name = dup_basename(input_name, ".idl");
731 if (do_header + do_typelib + do_proxies + do_client +
732 do_server + do_regscript + do_idfile + do_dlldata == 1)
734 if (do_header) header_name = output_name;
735 else if (do_typelib) typelib_name = output_name;
736 else if (do_proxies) proxy_name = output_name;
737 else if (do_client) client_name = output_name;
738 else if (do_server) server_name = output_name;
739 else if (do_regscript) regscript_name = output_name;
740 else if (do_idfile) idfile_name = output_name;
741 else if (do_dlldata) dlldata_name = output_name;
744 if (!dlldata_name && do_dlldata)
745 dlldata_name = xstrdup("dlldata.c");
747 if(optind < argc) {
748 if (do_dlldata && !do_everything) {
749 struct list filenames = LIST_INIT(filenames);
750 for ( ; optind < argc; ++optind)
751 add_filename_node(&filenames, argv[optind]);
753 write_dlldata_list(&filenames, 0 /* FIXME */ );
754 free_filename_nodes(&filenames);
755 return 0;
757 else if (optind != argc - 1) {
758 fprintf(stderr, "%s", usage);
759 return 1;
761 else
762 input_idl_name = input_name = xstrdup(argv[optind]);
764 else {
765 fprintf(stderr, "%s", usage);
766 return 1;
769 if(debuglevel)
771 setbuf(stdout, NULL);
772 setbuf(stderr, NULL);
775 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
776 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
778 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
779 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
780 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
782 if (!header_name) {
783 header_name = dup_basename(input_name, ".idl");
784 strcat(header_name, ".h");
787 if (!typelib_name && do_typelib) {
788 typelib_name = dup_basename(input_name, ".idl");
789 strcat(typelib_name, ".tlb");
792 if (!proxy_name && do_proxies) {
793 proxy_name = dup_basename(input_name, ".idl");
794 strcat(proxy_name, "_p.c");
797 if (!client_name && do_client) {
798 client_name = dup_basename(input_name, ".idl");
799 strcat(client_name, "_c.c");
802 if (!server_name && do_server) {
803 server_name = dup_basename(input_name, ".idl");
804 strcat(server_name, "_s.c");
807 if (!regscript_name && do_regscript) {
808 regscript_name = dup_basename(input_name, ".idl");
809 strcat(regscript_name, "_r.rgs");
812 if (!idfile_name && do_idfile) {
813 idfile_name = dup_basename(input_name, ".idl");
814 strcat(idfile_name, "_i.c");
817 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
818 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
819 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
820 if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
822 add_widl_version_define();
823 wpp_add_define("_WIN32", NULL);
825 atexit(rm_tempfile);
826 if (!no_preprocess)
828 chat("Starting preprocess\n");
830 if (!preprocess_only)
832 FILE *output;
833 int fd;
834 char *name = xmalloc( strlen(header_name) + 8 );
836 strcpy( name, header_name );
837 strcat( name, ".XXXXXX" );
839 if ((fd = mkstemps( name, 0 )) == -1)
840 error("Could not generate a temp name from %s\n", name);
842 temp_name = name;
843 if (!(output = fdopen(fd, "wt")))
844 error("Could not open fd %s for writing\n", name);
846 ret = wpp_parse( input_name, output );
847 fclose( output );
849 else
851 ret = wpp_parse( input_name, stdout );
854 if(ret) exit(1);
855 if(preprocess_only) exit(0);
856 if(!(parser_in = fopen(temp_name, "r"))) {
857 fprintf(stderr, "Could not open %s for input\n", temp_name);
858 return 1;
861 else {
862 if(!(parser_in = fopen(input_name, "r"))) {
863 fprintf(stderr, "Could not open %s for input\n", input_name);
864 return 1;
868 header_token = make_token(header_name);
870 init_types();
871 ret = parser_parse();
873 fclose(parser_in);
875 if(ret) {
876 exit(1);
879 /* Everything has been done successfully, don't delete any files. */
880 set_everything(FALSE);
881 local_stubs_name = NULL;
883 return 0;
886 static void rm_tempfile(void)
888 abort_import();
889 if(temp_name)
890 unlink(temp_name);
891 if (do_header)
892 unlink(header_name);
893 if (local_stubs_name)
894 unlink(local_stubs_name);
895 if (do_client)
896 unlink(client_name);
897 if (do_server)
898 unlink(server_name);
899 if (do_regscript)
900 unlink(regscript_name);
901 if (do_idfile)
902 unlink(idfile_name);
903 if (do_proxies)
904 unlink(proxy_name);
905 if (do_typelib)
906 unlink(typelib_name);