[WIDL]
[reactos.git] / reactos / tools / widl / widl.c
1 /*
2 * IDL Compiler
3 *
4 * Copyright 2002 Ove Kaaven
5 * based on WRC code by Bertho Stultiens
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
39
40 #include "widl.h"
41 #include "utils.h"
42 #include "parser.h"
43 #include "wine/wpp.h"
44 #include "header.h"
45
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 (?) */
50
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 " --rt Enable WinRT's language extensions for IDL\n"
76 " -s Generate server stub\n"
77 " -t Generate typelib\n"
78 " -u Generate interface identifiers file\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 " --win32-align n Set win32 structure alignment to 'n'\n"
84 " --win64-align n Set win64 structure alignment to 'n'\n"
85 "Debug level 'n' is a bitmask with following meaning:\n"
86 " * 0x01 Tell which resource is parsed (verbose mode)\n"
87 " * 0x02 Dump internal structures\n"
88 " * 0x04 Create a parser trace (yydebug=1)\n"
89 " * 0x08 Preprocessor messages\n"
90 " * 0x10 Preprocessor lex messages\n"
91 " * 0x20 Preprocessor yacc trace\n"
92 ;
93
94 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
95 "Copyright 2002 Ove Kaaven\n";
96
97 int debuglevel = DEBUGLEVEL_NONE;
98 int parser_debug, yy_flex_debug;
99
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 do_win32 = 1;
114 int do_win64 = 1;
115 int win32_packing = 8;
116 int win64_packing = 8;
117 int do_rt_extension = 0;
118 static enum stub_mode stub_mode = MODE_Os;
119
120 char *input_name;
121 char *input_idl_name;
122 char *header_name;
123 char *local_stubs_name;
124 char *header_token;
125 char *typelib_name;
126 char *dlldata_name;
127 char *proxy_name;
128 char *proxy_token;
129 char *client_name;
130 char *client_token;
131 char *server_name;
132 char *server_token;
133 char *regscript_name;
134 char *regscript_token;
135 static char *idfile_name;
136 char *temp_name;
137 const char *prefix_client = "";
138 const char *prefix_server = "";
139
140 int line_number = 1;
141
142 static FILE *idfile;
143
144 unsigned int pointer_size = 0;
145 syskind_t typelib_kind = sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32;
146
147 time_t now;
148
149 enum {
150 OLDNAMES_OPTION = CHAR_MAX + 1,
151 DLLDATA_OPTION,
152 DLLDATA_ONLY_OPTION,
153 LOCAL_STUBS_OPTION,
154 PREFIX_ALL_OPTION,
155 PREFIX_CLIENT_OPTION,
156 PREFIX_SERVER_OPTION,
157 PRINT_HELP,
158 RT_OPTION,
159 WIN32_OPTION,
160 WIN64_OPTION,
161 WIN32_ALIGN_OPTION,
162 WIN64_ALIGN_OPTION,
163 APP_CONFIG_OPTION
164 };
165
166 static const char short_options[] =
167 "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
168 static const struct option long_options[] = {
169 { "dlldata", 1, NULL, DLLDATA_OPTION },
170 { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
171 { "help", 0, NULL, PRINT_HELP },
172 { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
173 { "oldnames", 0, NULL, OLDNAMES_OPTION },
174 { "output", 0, NULL, 'o' },
175 { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
176 { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
177 { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
178 { "rt", 0, NULL, RT_OPTION },
179 { "win32", 0, NULL, WIN32_OPTION },
180 { "win64", 0, NULL, WIN64_OPTION },
181 { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
182 { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
183 { "app_config", 0, NULL, APP_CONFIG_OPTION },
184 { NULL, 0, NULL, 0 }
185 };
186
187 static void rm_tempfile(void);
188
189 enum stub_mode get_stub_mode(void)
190 {
191 /* old-style interpreted stubs are not supported on 64-bit */
192 if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
193 return stub_mode;
194 }
195
196 static char *make_token(const char *name)
197 {
198 char *token;
199 char *slash;
200 int i;
201
202 slash = strrchr(name, '/');
203 if(!slash)
204 slash = strrchr(name, '\\');
205
206 if (slash) name = slash + 1;
207
208 token = xstrdup(name);
209 for (i=0; token[i]; i++) {
210 if (!isalnum(token[i])) token[i] = '_';
211 else token[i] = tolower(token[i]);
212 }
213 return token;
214 }
215
216 /* duplicate a basename into a valid C token */
217 static char *dup_basename_token(const char *name, const char *ext)
218 {
219 char *p, *ret = dup_basename( name, ext );
220 /* map invalid characters to '_' */
221 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
222 return ret;
223 }
224
225 static void add_widl_version_define(void)
226 {
227 unsigned int version;
228 const char *p = PACKAGE_VERSION;
229
230 /* major */
231 version = atoi(p) * 0x10000;
232 p = strchr(p, '.');
233
234 /* minor */
235 if (p)
236 {
237 version += atoi(p + 1) * 0x100;
238 p = strchr(p + 1, '.');
239 }
240
241 /* build */
242 if (p)
243 version += atoi(p + 1);
244
245 if (version != 0)
246 {
247 char version_str[11];
248 snprintf(version_str, sizeof(version_str), "0x%x", version);
249 wpp_add_define("__WIDL__", version_str);
250 }
251 else
252 wpp_add_define("__WIDL__", NULL);
253 }
254
255 /* set the target platform */
256 static void set_target( const char *target )
257 {
258 static const struct
259 {
260 const char *name;
261 syskind_t kind;
262 } cpu_names[] =
263 {
264 { "i386", SYS_WIN32 },
265 { "i486", SYS_WIN32 },
266 { "i586", SYS_WIN32 },
267 { "i686", SYS_WIN32 },
268 { "i786", SYS_WIN32 },
269 { "amd64", SYS_WIN64 },
270 { "x86_64", SYS_WIN64 },
271 { "powerpc", SYS_WIN32 },
272 { "arm", SYS_WIN32 },
273 { "aarch64", SYS_WIN64 }
274 };
275
276 unsigned int i;
277 char *p, *spec = xstrdup( target );
278
279 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
280
281 if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
282 *p++ = 0;
283 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
284 {
285 if (!strcmp( cpu_names[i].name, spec ))
286 {
287 typelib_kind = cpu_names[i].kind;
288 free( spec );
289 return;
290 }
291 }
292 error( "Unrecognized CPU '%s'\n", spec );
293 }
294
295 /* clean things up when aborting on a signal */
296 static void exit_on_signal( int sig )
297 {
298 exit(1); /* this will call the atexit functions */
299 }
300
301 static void set_everything(int x)
302 {
303 do_header = x;
304 do_typelib = x;
305 do_proxies = x;
306 do_client = x;
307 do_server = x;
308 do_regscript = x;
309 do_idfile = x;
310 do_dlldata = x;
311 }
312
313 void start_cplusplus_guard(FILE *fp)
314 {
315 fprintf(fp, "#ifdef __cplusplus\n");
316 fprintf(fp, "extern \"C\" {\n");
317 fprintf(fp, "#endif\n\n");
318 }
319
320 void end_cplusplus_guard(FILE *fp)
321 {
322 fprintf(fp, "#ifdef __cplusplus\n");
323 fprintf(fp, "}\n");
324 fprintf(fp, "#endif\n\n");
325 }
326
327 typedef struct
328 {
329 char *filename;
330 struct list link;
331 } filename_node_t;
332
333 static void add_filename_node(struct list *list, const char *name)
334 {
335 filename_node_t *node = xmalloc(sizeof *node);
336 node->filename = dup_basename( name, ".idl" );
337 list_add_tail(list, &node->link);
338 }
339
340 static void free_filename_nodes(struct list *list)
341 {
342 filename_node_t *node, *next;
343 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
344 list_remove(&node->link);
345 free(node->filename);
346 free(node);
347 }
348 }
349
350 static void write_dlldata_list(struct list *filenames, int define_proxy_delegation)
351 {
352 FILE *dlldata;
353 filename_node_t *node;
354
355 dlldata = fopen(dlldata_name, "w");
356 if (!dlldata)
357 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
358
359 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
360 fprintf(dlldata, "- Do not edit ***/\n\n");
361 if (define_proxy_delegation)
362 fprintf(dlldata, "#define PROXY_DELEGATION\n");
363
364 fprintf(dlldata, "#ifdef __REACTOS__\n");
365 fprintf(dlldata, "#define WIN32_NO_STATUS\n");
366 fprintf(dlldata, "#define WIN32_LEAN_AND_MEAN\n");
367 fprintf(dlldata, "#endif\n\n");
368
369 fprintf(dlldata, "#include <objbase.h>\n");
370 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
371 start_cplusplus_guard(dlldata);
372
373 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
374 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
375
376 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
377 fprintf(dlldata, "/* Start of list */\n");
378 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
379 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
380 fprintf(dlldata, "/* End of list */\n");
381 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
382
383 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
384 end_cplusplus_guard(dlldata);
385 fclose(dlldata);
386 }
387
388 static char *eat_space(char *s)
389 {
390 while (isspace((unsigned char) *s))
391 ++s;
392 return s;
393 }
394
395 void write_dlldata(const statement_list_t *stmts)
396 {
397 struct list filenames = LIST_INIT(filenames);
398 int define_proxy_delegation = 0;
399 filename_node_t *node;
400 FILE *dlldata;
401
402 if (!do_dlldata || !need_proxy_file(stmts))
403 return;
404
405 define_proxy_delegation = need_proxy_delegation(stmts);
406
407 dlldata = fopen(dlldata_name, "r");
408 if (dlldata) {
409 static const char marker[] = "REFERENCE_PROXY_FILE";
410 static const char delegation_define[] = "#define PROXY_DELEGATION";
411 char *line = NULL;
412 size_t len = 0;
413
414 while (widl_getline(&line, &len, dlldata)) {
415 char *start, *end;
416 start = eat_space(line);
417 if (strncmp(start, marker, sizeof marker - 1) == 0) {
418 start = eat_space(start + sizeof marker - 1);
419 if (*start != '(')
420 continue;
421 end = start = eat_space(start + 1);
422 while (*end && *end != ')')
423 ++end;
424 if (*end != ')')
425 continue;
426 while (isspace((unsigned char) end[-1]))
427 --end;
428 *end = '\0';
429 if (start < end)
430 add_filename_node(&filenames, start);
431 }else if (!define_proxy_delegation && strncmp(start, delegation_define, sizeof(delegation_define)-1)) {
432 define_proxy_delegation = 1;
433 }
434 }
435
436 if (ferror(dlldata))
437 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
438
439 free(line);
440 fclose(dlldata);
441 }
442
443 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
444 if (strcmp(proxy_token, node->filename) == 0) {
445 /* We're already in the list, no need to regenerate this file. */
446 free_filename_nodes(&filenames);
447 return;
448 }
449
450 add_filename_node(&filenames, proxy_token);
451 write_dlldata_list(&filenames, define_proxy_delegation);
452 free_filename_nodes(&filenames);
453 }
454
455 static void write_id_guid(FILE *f, const char *type, const char *guid_prefix, const char *name, const UUID *uuid)
456 {
457 if (!uuid) return;
458 fprintf(f, "MIDL_DEFINE_GUID(%s, %s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
459 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
460 type, guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
461 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
462 uuid->Data4[6], uuid->Data4[7]);
463 }
464
465 static void write_id_data_stmts(const statement_list_t *stmts)
466 {
467 const statement_t *stmt;
468 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
469 {
470 if (stmt->type == STMT_TYPE)
471 {
472 const type_t *type = stmt->u.type;
473 if (type_get_type(type) == TYPE_INTERFACE)
474 {
475 const UUID *uuid;
476 if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
477 continue;
478 uuid = get_attrp(type->attrs, ATTR_UUID);
479 write_id_guid(idfile, "IID", is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
480 type->name, uuid);
481 }
482 else if (type_get_type(type) == TYPE_COCLASS)
483 {
484 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
485 write_id_guid(idfile, "CLSID", "CLSID", type->name, uuid);
486 }
487 }
488 else if (stmt->type == STMT_LIBRARY)
489 {
490 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
491 write_id_guid(idfile, "IID", "LIBID", stmt->u.lib->name, uuid);
492 write_id_data_stmts(stmt->u.lib->stmts);
493 }
494 }
495 }
496
497 void write_id_data(const statement_list_t *stmts)
498 {
499 if (!do_idfile) return;
500
501 idfile = fopen(idfile_name, "w");
502 if (! idfile) {
503 error("Could not open %s for output\n", idfile_name);
504 return;
505 }
506
507 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
508 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_idl_name);
509
510 fprintf(idfile, "#ifdef __REACTOS__\n");
511 fprintf(idfile, "#define WIN32_NO_STATUS\n");
512 fprintf(idfile, "#define WIN32_LEAN_AND_MEAN\n");
513 fprintf(idfile, "#endif\n\n");
514
515 fprintf(idfile, "#include <rpc.h>\n");
516 fprintf(idfile, "#include <rpcndr.h>\n\n");
517
518 fprintf(idfile, "#ifdef _MIDL_USE_GUIDDEF_\n\n");
519
520 fprintf(idfile, "#ifndef INITGUID\n");
521 fprintf(idfile, "#define INITGUID\n");
522 fprintf(idfile, "#include <guiddef.h>\n");
523 fprintf(idfile, "#undef INITGUID\n");
524 fprintf(idfile, "#else\n");
525 fprintf(idfile, "#include <guiddef.h>\n");
526 fprintf(idfile, "#endif\n\n");
527
528 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
529 fprintf(idfile, " DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)\n\n");
530
531 fprintf(idfile, "#else\n\n");
532
533 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
534 fprintf(idfile, " const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
535
536 fprintf(idfile, "#endif\n\n");
537 start_cplusplus_guard(idfile);
538
539 write_id_data_stmts(stmts);
540
541 fprintf(idfile, "\n");
542 end_cplusplus_guard(idfile);
543 fprintf(idfile, "#undef MIDL_DEFINE_GUID\n" );
544
545 fclose(idfile);
546 }
547
548 int main(int argc,char *argv[])
549 {
550 int optc;
551 int ret = 0;
552 int opti = 0;
553 char *output_name = NULL;
554
555 signal( SIGTERM, exit_on_signal );
556 signal( SIGINT, exit_on_signal );
557 #ifdef SIGHUP
558 signal( SIGHUP, exit_on_signal );
559 #endif
560
561 now = time(NULL);
562
563 while((optc = getopt_long_only(argc, argv, short_options, long_options, &opti)) != EOF) {
564 switch(optc) {
565 case DLLDATA_OPTION:
566 dlldata_name = xstrdup(optarg);
567 break;
568 case DLLDATA_ONLY_OPTION:
569 do_everything = 0;
570 do_dlldata = 1;
571 break;
572 case LOCAL_STUBS_OPTION:
573 do_everything = 0;
574 local_stubs_name = xstrdup(optarg);
575 break;
576 case OLDNAMES_OPTION:
577 old_names = 1;
578 break;
579 case PREFIX_ALL_OPTION:
580 prefix_client = xstrdup(optarg);
581 prefix_server = xstrdup(optarg);
582 break;
583 case PREFIX_CLIENT_OPTION:
584 prefix_client = xstrdup(optarg);
585 break;
586 case PREFIX_SERVER_OPTION:
587 prefix_server = xstrdup(optarg);
588 break;
589 case PRINT_HELP:
590 fprintf(stderr, "%s", usage);
591 return 0;
592 case RT_OPTION:
593 do_rt_extension = 1;
594 break;
595 case WIN32_OPTION:
596 do_win32 = 1;
597 do_win64 = 0;
598 break;
599 case WIN64_OPTION:
600 do_win32 = 0;
601 do_win64 = 1;
602 break;
603 case WIN32_ALIGN_OPTION:
604 win32_packing = strtol(optarg, NULL, 0);
605 if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
606 error("Packing must be one of 2, 4 or 8\n");
607 break;
608 case WIN64_ALIGN_OPTION:
609 win64_packing = strtol(optarg, NULL, 0);
610 if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
611 error("Packing must be one of 2, 4 or 8\n");
612 break;
613 case APP_CONFIG_OPTION:
614 /* widl does not distinguish between app_mode and default mode,
615 but we ignore this option for midl compatibility */
616 break;
617 case 'b':
618 set_target( optarg );
619 break;
620 case 'c':
621 do_everything = 0;
622 do_client = 1;
623 break;
624 case 'C':
625 client_name = xstrdup(optarg);
626 break;
627 case 'd':
628 debuglevel = strtol(optarg, NULL, 0);
629 break;
630 case 'D':
631 wpp_add_cmdline_define(optarg);
632 break;
633 case 'E':
634 do_everything = 0;
635 preprocess_only = 1;
636 break;
637 case 'h':
638 do_everything = 0;
639 do_header = 1;
640 break;
641 case 'H':
642 header_name = xstrdup(optarg);
643 break;
644 case 'I':
645 wpp_add_include_path(optarg);
646 break;
647 case 'm':
648 if (!strcmp( optarg, "32" )) typelib_kind = SYS_WIN32;
649 else if (!strcmp( optarg, "64" )) typelib_kind = SYS_WIN64;
650 break;
651 case 'N':
652 no_preprocess = 1;
653 break;
654 case 'o':
655 output_name = xstrdup(optarg);
656 break;
657 case 'O':
658 if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
659 else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
660 else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
661 else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
662 else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
663 else error( "Invalid argument '-O%s'\n", optarg );
664 break;
665 case 'p':
666 do_everything = 0;
667 do_proxies = 1;
668 break;
669 case 'P':
670 proxy_name = xstrdup(optarg);
671 break;
672 case 'r':
673 do_everything = 0;
674 do_regscript = 1;
675 break;
676 case 's':
677 do_everything = 0;
678 do_server = 1;
679 break;
680 case 'S':
681 server_name = xstrdup(optarg);
682 break;
683 case 't':
684 do_everything = 0;
685 do_typelib = 1;
686 break;
687 case 'T':
688 typelib_name = xstrdup(optarg);
689 break;
690 case 'u':
691 do_everything = 0;
692 do_idfile = 1;
693 break;
694 case 'U':
695 idfile_name = xstrdup(optarg);
696 break;
697 case 'V':
698 printf("%s", version_string);
699 return 0;
700 case 'W':
701 pedantic = 1;
702 break;
703 default:
704 fprintf(stderr, "%s", usage);
705 return 1;
706 }
707 }
708
709 #ifdef DEFAULT_INCLUDE_DIR
710 wpp_add_include_path(DEFAULT_INCLUDE_DIR);
711 #endif
712
713 /* if nothing specified, try to guess output type from the output file name */
714 if (output_name && do_everything && !do_header && !do_typelib && !do_proxies &&
715 !do_client && !do_server && !do_regscript && !do_idfile && !do_dlldata)
716 {
717 do_everything = 0;
718 if (strendswith( output_name, ".h" )) do_header = 1;
719 else if (strendswith( output_name, ".tlb" )) do_typelib = 1;
720 else if (strendswith( output_name, "_p.c" )) do_proxies = 1;
721 else if (strendswith( output_name, "_c.c" )) do_client = 1;
722 else if (strendswith( output_name, "_s.c" )) do_server = 1;
723 else if (strendswith( output_name, "_i.c" )) do_idfile = 1;
724 else if (strendswith( output_name, "_r.res" )) do_regscript = 1;
725 else if (strendswith( output_name, "_t.res" )) do_typelib = 1;
726 else if (strendswith( output_name, "dlldata.c" )) do_dlldata = 1;
727 else do_everything = 1;
728 }
729
730 if(do_everything) {
731 set_everything(TRUE);
732 }
733
734 if (!output_name) output_name = dup_basename(input_name, ".idl");
735
736 if (do_header + do_typelib + do_proxies + do_client +
737 do_server + do_regscript + do_idfile + do_dlldata == 1)
738 {
739 if (do_header) header_name = output_name;
740 else if (do_typelib) typelib_name = output_name;
741 else if (do_proxies) proxy_name = output_name;
742 else if (do_client) client_name = output_name;
743 else if (do_server) server_name = output_name;
744 else if (do_regscript) regscript_name = output_name;
745 else if (do_idfile) idfile_name = output_name;
746 else if (do_dlldata) dlldata_name = output_name;
747 }
748
749 if (!dlldata_name && do_dlldata)
750 dlldata_name = xstrdup("dlldata.c");
751
752 if(optind < argc) {
753 if (do_dlldata && !do_everything) {
754 struct list filenames = LIST_INIT(filenames);
755 for ( ; optind < argc; ++optind)
756 add_filename_node(&filenames, argv[optind]);
757
758 write_dlldata_list(&filenames, 0 /* FIXME */ );
759 free_filename_nodes(&filenames);
760 return 0;
761 }
762 else if (optind != argc - 1) {
763 fprintf(stderr, "%s", usage);
764 return 1;
765 }
766 else
767 input_idl_name = input_name = xstrdup(argv[optind]);
768 }
769 else {
770 fprintf(stderr, "%s", usage);
771 return 1;
772 }
773
774 if(debuglevel)
775 {
776 setbuf(stdout, NULL);
777 setbuf(stderr, NULL);
778 }
779
780 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
781 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
782
783 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
784 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
785 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
786
787 if (!header_name) {
788 header_name = dup_basename(input_name, ".idl");
789 strcat(header_name, ".h");
790 }
791
792 if (!typelib_name && do_typelib) {
793 typelib_name = dup_basename(input_name, ".idl");
794 strcat(typelib_name, ".tlb");
795 }
796
797 if (!proxy_name && do_proxies) {
798 proxy_name = dup_basename(input_name, ".idl");
799 strcat(proxy_name, "_p.c");
800 }
801
802 if (!client_name && do_client) {
803 client_name = dup_basename(input_name, ".idl");
804 strcat(client_name, "_c.c");
805 }
806
807 if (!server_name && do_server) {
808 server_name = dup_basename(input_name, ".idl");
809 strcat(server_name, "_s.c");
810 }
811
812 if (!regscript_name && do_regscript) {
813 regscript_name = dup_basename(input_name, ".idl");
814 strcat(regscript_name, "_r.rgs");
815 }
816
817 if (!idfile_name && do_idfile) {
818 idfile_name = dup_basename(input_name, ".idl");
819 strcat(idfile_name, "_i.c");
820 }
821
822 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
823 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
824 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
825 if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
826
827 add_widl_version_define();
828 wpp_add_define("_WIN32", NULL);
829
830 atexit(rm_tempfile);
831 if (!no_preprocess)
832 {
833 chat("Starting preprocess\n");
834
835 if (!preprocess_only)
836 {
837 FILE *output;
838 int fd;
839 char *name = xmalloc( strlen(header_name) + 8 );
840
841 strcpy( name, header_name );
842 strcat( name, ".XXXXXX" );
843
844 if ((fd = mkstemps( name, 0 )) == -1)
845 error("Could not generate a temp name from %s\n", name);
846
847 temp_name = name;
848 if (!(output = fdopen(fd, "wt")))
849 error("Could not open fd %s for writing\n", name);
850
851 ret = wpp_parse( input_name, output );
852 fclose( output );
853 }
854 else
855 {
856 ret = wpp_parse( input_name, stdout );
857 }
858
859 if(ret) exit(1);
860 if(preprocess_only) exit(0);
861 if(!(parser_in = fopen(temp_name, "r"))) {
862 fprintf(stderr, "Could not open %s for input\n", temp_name);
863 return 1;
864 }
865 }
866 else {
867 if(!(parser_in = fopen(input_name, "r"))) {
868 fprintf(stderr, "Could not open %s for input\n", input_name);
869 return 1;
870 }
871 }
872
873 header_token = make_token(header_name);
874
875 init_types();
876 ret = parser_parse();
877
878 fclose(parser_in);
879
880 if(ret) {
881 exit(1);
882 }
883
884 /* Everything has been done successfully, don't delete any files. */
885 set_everything(FALSE);
886 local_stubs_name = NULL;
887
888 return 0;
889 }
890
891 static void rm_tempfile(void)
892 {
893 abort_import();
894 if(temp_name)
895 unlink(temp_name);
896 if (do_header)
897 unlink(header_name);
898 if (local_stubs_name)
899 unlink(local_stubs_name);
900 if (do_client)
901 unlink(client_name);
902 if (do_server)
903 unlink(server_name);
904 if (do_regscript)
905 unlink(regscript_name);
906 if (do_idfile)
907 unlink(idfile_name);
908 if (do_proxies)
909 unlink(proxy_name);
910 if (do_typelib)
911 unlink(typelib_name);
912 }