push f98ef5dce4886db1f9801e7c454145e1b633b4ff
[wine/hacks.git] / tools / widl / widl.c
blob79456942609a34d28fe817ac4e443eb650de96d6
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 = alignment of structures */
48 /* A = ACF input filename */
49 /* J = do not search standard include path */
50 /* O = generate interpreted stubs */
51 /* w = select win16/win32 output (?) */
53 static const char usage[] =
54 "Usage: widl [options...] infile.idl\n"
55 " or: widl [options...] --dlldata-only name1 [name2...]\n"
56 " -c Generate client stub\n"
57 " -C file Name of client stub file (default is infile_c.c)\n"
58 " -d n Set debug level to 'n'\n"
59 " -D id[=val] Define preprocessor identifier id=val\n"
60 " --dlldata=file Name of the dlldata file (default is dlldata.c)\n"
61 " -E Preprocess only\n"
62 " -h Generate headers\n"
63 " -H file Name of header file (default is infile.h)\n"
64 " -I path Set include search dir to path (multiple -I allowed)\n"
65 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
66 " -N Do not preprocess input\n"
67 " --oldnames Use old naming conventions\n"
68 " -p Generate proxy\n"
69 " -P file Name of proxy file (default is infile_p.c)\n"
70 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
71 " --prefix-client=p Prefix names of client stubs with 'p'\n"
72 " --prefix-server=p Prefix names of server functions with 'p'\n"
73 " -s Generate server stub\n"
74 " -S file Name of server stub file (default is infile_s.c)\n"
75 " -t Generate typelib\n"
76 " -T file Name of typelib file (default is infile.tlb)\n"
77 " -u Generate interface identifiers file\n"
78 " -U file Name of interface identifiers file (default is infile_i.c)\n"
79 " -V Print version and exit\n"
80 " -W Enable pedantic warnings\n"
81 "Debug level 'n' is a bitmask with following meaning:\n"
82 " * 0x01 Tell which resource is parsed (verbose mode)\n"
83 " * 0x02 Dump internal structures\n"
84 " * 0x04 Create a parser trace (yydebug=1)\n"
85 " * 0x08 Preprocessor messages\n"
86 " * 0x10 Preprocessor lex messages\n"
87 " * 0x20 Preprocessor yacc trace\n"
90 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
91 "Copyright 2002 Ove Kaaven\n";
93 int win32 = 1;
94 int debuglevel = DEBUGLEVEL_NONE;
95 int parser_debug, yy_flex_debug;
97 int pedantic = 0;
98 int do_everything = 1;
99 int preprocess_only = 0;
100 int do_header = 0;
101 int do_typelib = 0;
102 int do_proxies = 0;
103 int do_client = 0;
104 int do_server = 0;
105 int do_idfile = 0;
106 int do_dlldata = 0;
107 int no_preprocess = 0;
108 int old_names = 0;
110 char *input_name;
111 char *header_name;
112 char *local_stubs_name;
113 char *header_token;
114 char *typelib_name;
115 char *dlldata_name;
116 char *proxy_name;
117 char *proxy_token;
118 char *client_name;
119 char *client_token;
120 char *server_name;
121 char *server_token;
122 char *idfile_name;
123 char *idfile_token;
124 char *temp_name;
125 const char *prefix_client = "";
126 const char *prefix_server = "";
128 int line_number = 1;
130 FILE *header;
131 FILE *local_stubs;
132 FILE *proxy;
133 FILE *idfile;
135 time_t now;
137 enum {
138 OLDNAMES_OPTION = CHAR_MAX + 1,
139 DLLDATA_OPTION,
140 DLLDATA_ONLY_OPTION,
141 LOCAL_STUBS_OPTION,
142 PREFIX_ALL_OPTION,
143 PREFIX_CLIENT_OPTION,
144 PREFIX_SERVER_OPTION
147 static const char short_options[] =
148 "cC:d:D:EhH:I:NpP:sS:tT:uU:VW";
149 static const struct option long_options[] = {
150 { "dlldata", required_argument, 0, DLLDATA_OPTION },
151 { "dlldata-only", no_argument, 0, DLLDATA_ONLY_OPTION },
152 { "local-stubs", required_argument, 0, LOCAL_STUBS_OPTION },
153 { "oldnames", no_argument, 0, OLDNAMES_OPTION },
154 { "prefix-all", required_argument, 0, PREFIX_ALL_OPTION },
155 { "prefix-client", required_argument, 0, PREFIX_CLIENT_OPTION },
156 { "prefix-server", required_argument, 0, PREFIX_SERVER_OPTION },
157 { 0, 0, 0, 0 }
160 static void rm_tempfile(void);
162 static char *make_token(const char *name)
164 char *token;
165 char *slash;
166 int i;
168 slash = strrchr(name, '/');
169 if (slash) name = slash + 1;
171 token = xstrdup(name);
172 for (i=0; token[i]; i++) {
173 if (!isalnum(token[i])) token[i] = '_';
174 else token[i] = toupper(token[i]);
176 return token;
179 /* duplicate a basename into a valid C token */
180 static char *dup_basename_token(const char *name, const char *ext)
182 char *p, *ret = dup_basename( name, ext );
183 /* map invalid characters to '_' */
184 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
185 return ret;
188 /* clean things up when aborting on a signal */
189 static void exit_on_signal( int sig )
191 exit(1); /* this will call the atexit functions */
194 static void set_everything(int x)
196 do_header = x;
197 do_typelib = x;
198 do_proxies = x;
199 do_client = x;
200 do_server = x;
201 do_idfile = x;
202 do_dlldata = x;
205 static void start_cplusplus_guard(FILE *fp)
207 fprintf(fp, "#ifdef __cplusplus\n");
208 fprintf(fp, "extern \"C\" {\n");
209 fprintf(fp, "#endif\n\n");
212 static void end_cplusplus_guard(FILE *fp)
214 fprintf(fp, "#ifdef __cplusplus\n");
215 fprintf(fp, "}\n");
216 fprintf(fp, "#endif\n\n");
219 typedef struct
221 char *filename;
222 struct list link;
223 } filename_node_t;
225 static void add_filename_node(struct list *list, const char *name)
227 filename_node_t *node = xmalloc(sizeof *node);
228 node->filename = dup_basename( name, ".idl" );
229 list_add_tail(list, &node->link);
232 static void free_filename_nodes(struct list *list)
234 filename_node_t *node, *next;
235 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
236 list_remove(&node->link);
237 free(node->filename);
238 free(node);
242 static void write_dlldata_list(struct list *filenames)
244 FILE *dlldata;
245 filename_node_t *node;
247 dlldata = fopen(dlldata_name, "w");
248 if (!dlldata)
249 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
251 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
252 fprintf(dlldata, "- Do not edit ***/\n\n");
253 fprintf(dlldata, "#include <objbase.h>\n");
254 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
255 start_cplusplus_guard(dlldata);
257 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
258 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
260 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
261 fprintf(dlldata, "/* Start of list */\n");
262 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
263 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
264 fprintf(dlldata, "/* End of list */\n");
265 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
267 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
268 end_cplusplus_guard(dlldata);
269 fclose(dlldata);
272 static char *eat_space(char *s)
274 while (isspace((unsigned char) *s))
275 ++s;
276 return s;
279 void write_dlldata(ifref_list_t *ifaces)
281 struct list filenames = LIST_INIT(filenames);
282 filename_node_t *node;
283 FILE *dlldata;
285 if (!do_dlldata || !need_proxy_file(ifaces))
286 return;
288 dlldata = fopen(dlldata_name, "r");
289 if (dlldata) {
290 static char marker[] = "REFERENCE_PROXY_FILE";
291 char *line = NULL;
292 size_t len = 0;
294 while (widl_getline(&line, &len, dlldata)) {
295 char *start, *end;
296 start = eat_space(line);
297 if (strncmp(start, marker, sizeof marker - 1) == 0) {
298 start = eat_space(start + sizeof marker - 1);
299 if (*start != '(')
300 continue;
301 end = start = eat_space(start + 1);
302 while (*end && *end != ')')
303 ++end;
304 if (*end != ')')
305 continue;
306 while (isspace((unsigned char) end[-1]))
307 --end;
308 *end = '\0';
309 if (start < end)
310 add_filename_node(&filenames, start);
314 if (ferror(dlldata))
315 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
317 free(line);
318 fclose(dlldata);
321 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
322 if (strcmp(proxy_token, node->filename) == 0) {
323 /* We're already in the list, no need to regenerate this file. */
324 free_filename_nodes(&filenames);
325 return;
328 add_filename_node(&filenames, proxy_token);
329 write_dlldata_list(&filenames);
330 free_filename_nodes(&filenames);
333 int main(int argc,char *argv[])
335 extern char* optarg;
336 extern int optind;
337 int optc;
338 int ret = 0;
339 int opti = 0;
341 signal( SIGTERM, exit_on_signal );
342 signal( SIGINT, exit_on_signal );
343 #ifdef SIGHUP
344 signal( SIGHUP, exit_on_signal );
345 #endif
347 now = time(NULL);
349 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
350 switch(optc) {
351 case DLLDATA_OPTION:
352 dlldata_name = xstrdup(optarg);
353 break;
354 case DLLDATA_ONLY_OPTION:
355 do_everything = 0;
356 do_dlldata = 1;
357 break;
358 case LOCAL_STUBS_OPTION:
359 do_everything = 0;
360 local_stubs_name = xstrdup(optarg);
361 break;
362 case OLDNAMES_OPTION:
363 old_names = 1;
364 break;
365 case PREFIX_ALL_OPTION:
366 prefix_client = xstrdup(optarg);
367 prefix_server = xstrdup(optarg);
368 break;
369 case PREFIX_CLIENT_OPTION:
370 prefix_client = xstrdup(optarg);
371 break;
372 case PREFIX_SERVER_OPTION:
373 prefix_server = xstrdup(optarg);
374 break;
375 case 'c':
376 do_everything = 0;
377 do_client = 1;
378 break;
379 case 'C':
380 client_name = xstrdup(optarg);
381 break;
382 case 'd':
383 debuglevel = strtol(optarg, NULL, 0);
384 break;
385 case 'D':
386 wpp_add_cmdline_define(optarg);
387 break;
388 case 'E':
389 do_everything = 0;
390 preprocess_only = 1;
391 break;
392 case 'h':
393 do_everything = 0;
394 do_header = 1;
395 break;
396 case 'H':
397 header_name = xstrdup(optarg);
398 break;
399 case 'I':
400 wpp_add_include_path(optarg);
401 break;
402 case 'N':
403 no_preprocess = 1;
404 break;
405 case 'p':
406 do_everything = 0;
407 do_proxies = 1;
408 break;
409 case 'P':
410 proxy_name = xstrdup(optarg);
411 break;
412 case 's':
413 do_everything = 0;
414 do_server = 1;
415 break;
416 case 'S':
417 server_name = xstrdup(optarg);
418 break;
419 case 't':
420 do_everything = 0;
421 do_typelib = 1;
422 break;
423 case 'T':
424 typelib_name = xstrdup(optarg);
425 break;
426 case 'u':
427 do_everything = 0;
428 do_idfile = 1;
429 break;
430 case 'U':
431 idfile_name = xstrdup(optarg);
432 break;
433 case 'V':
434 printf("%s", version_string);
435 return 0;
436 case 'W':
437 pedantic = 1;
438 break;
439 default:
440 fprintf(stderr, "%s", usage);
441 return 1;
445 if(do_everything) {
446 set_everything(TRUE);
449 if (!dlldata_name && do_dlldata)
450 dlldata_name = xstrdup("dlldata.c");
452 if(optind < argc) {
453 if (do_dlldata && !do_everything) {
454 struct list filenames = LIST_INIT(filenames);
455 for ( ; optind < argc; ++optind)
456 add_filename_node(&filenames, argv[optind]);
458 write_dlldata_list(&filenames);
459 free_filename_nodes(&filenames);
460 return 0;
462 else if (optind != argc - 1) {
463 fprintf(stderr, "%s", usage);
464 return 1;
466 else
467 input_name = xstrdup(argv[optind]);
469 else {
470 fprintf(stderr, "%s", usage);
471 return 1;
474 if(debuglevel)
476 setbuf(stdout,0);
477 setbuf(stderr,0);
480 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
481 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
483 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
484 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
485 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
487 if (!header_name) {
488 header_name = dup_basename(input_name, ".idl");
489 strcat(header_name, ".h");
492 if (!typelib_name && do_typelib) {
493 typelib_name = dup_basename(input_name, ".idl");
494 strcat(typelib_name, ".tlb");
497 if (!proxy_name && do_proxies) {
498 proxy_name = dup_basename(input_name, ".idl");
499 strcat(proxy_name, "_p.c");
502 if (!client_name && do_client) {
503 client_name = dup_basename(input_name, ".idl");
504 strcat(client_name, "_c.c");
507 if (!server_name && do_server) {
508 server_name = dup_basename(input_name, ".idl");
509 strcat(server_name, "_s.c");
512 if (!idfile_name && do_idfile) {
513 idfile_name = dup_basename(input_name, ".idl");
514 strcat(idfile_name, "_i.c");
517 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
518 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
519 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
521 wpp_add_cmdline_define("__WIDL__");
523 atexit(rm_tempfile);
524 if (!no_preprocess)
526 chat("Starting preprocess\n");
528 if (!preprocess_only)
530 ret = wpp_parse_temp( input_name, header_name, &temp_name );
532 else
534 ret = wpp_parse( input_name, stdout );
537 if(ret) exit(1);
538 if(preprocess_only) exit(0);
539 if(!(parser_in = fopen(temp_name, "r"))) {
540 fprintf(stderr, "Could not open %s for input\n", temp_name);
541 return 1;
544 else {
545 if(!(parser_in = fopen(input_name, "r"))) {
546 fprintf(stderr, "Could not open %s for input\n", input_name);
547 return 1;
551 if(do_header) {
552 header_token = make_token(header_name);
554 if(!(header = fopen(header_name, "w"))) {
555 fprintf(stderr, "Could not open %s for output\n", header_name);
556 return 1;
558 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
559 fprintf(header, "#include <rpc.h>\n" );
560 fprintf(header, "#include <rpcndr.h>\n\n" );
561 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
562 fprintf(header, "#define __WIDL_%s\n", header_token);
563 start_cplusplus_guard(header);
566 if (local_stubs_name) {
567 local_stubs = fopen(local_stubs_name, "w");
568 if (!local_stubs) {
569 fprintf(stderr, "Could not open %s for output\n", local_stubs_name);
570 return 1;
572 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
573 fprintf(local_stubs, "#include <objbase.h>\n");
574 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
577 if (do_idfile) {
578 idfile_token = make_token(idfile_name);
580 idfile = fopen(idfile_name, "w");
581 if (! idfile) {
582 fprintf(stderr, "Could not open %s for output\n", idfile_name);
583 return 1;
586 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
587 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
588 fprintf(idfile, "#include <rpc.h>\n");
589 fprintf(idfile, "#include <rpcndr.h>\n\n");
590 fprintf(idfile, "#include <initguid.h>\n\n");
591 start_cplusplus_guard(idfile);
594 init_types();
595 ret = parser_parse();
597 if(do_header) {
598 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
599 fprintf(header, "\n");
600 write_user_types();
601 write_context_handle_rundowns();
602 fprintf(header, "\n");
603 fprintf(header, "/* End additional prototypes */\n");
604 fprintf(header, "\n");
605 end_cplusplus_guard(header);
606 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
607 fclose(header);
610 if (local_stubs) {
611 fclose(local_stubs);
614 if (do_idfile) {
615 fprintf(idfile, "\n");
616 end_cplusplus_guard(idfile);
618 fclose(idfile);
621 fclose(parser_in);
623 if(ret) {
624 exit(1);
627 /* Everything has been done successfully, don't delete any files. */
628 set_everything(FALSE);
629 local_stubs_name = NULL;
631 return 0;
634 static void rm_tempfile(void)
636 abort_import();
637 if(temp_name)
638 unlink(temp_name);
639 if (do_header)
640 unlink(header_name);
641 if (local_stubs_name)
642 unlink(local_stubs_name);
643 if (do_client)
644 unlink(client_name);
645 if (do_server)
646 unlink(server_name);
647 if (do_idfile)
648 unlink(idfile_name);
649 if (do_proxies)
650 unlink(proxy_name);
651 if (do_typelib)
652 unlink(typelib_name);