Added "generate headers only" command-line option. Implemented imports
[wine/wine-kai.git] / tools / widl / widl.c
blobd32ff9127a1eadcb3f3125090d96bf1d6b76e679
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
34 #define WIDL_FULLVERSION "0.1"
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "proxy.h"
40 #include "../wpp/wpp.h"
42 static char usage[] =
43 "Usage: widl [options...] infile.idl\n"
44 " -d n Set debug level to 'n'\n"
45 " -D id[=val] Define preprocessor identifier id=val\n"
46 " -E Preprocess only\n"
47 " -h Generate headers only\n"
48 " -H file Name of header file (default is infile.h)\n"
49 " -I path Set include search dir to path (multiple -I allowed)\n"
50 " -N Do not preprocess input\n"
51 " -V Print version and exit\n"
52 " -W Enable pedantic warnings\n"
53 "Debug level 'n' is a bitmask with following meaning:\n"
54 " * 0x01 Tell which resource is parsed (verbose mode)\n"
55 " * 0x02 Dump internal structures\n"
56 " * 0x04 Create a parser trace (yydebug=1)\n"
57 " * 0x08 Preprocessor messages\n"
58 " * 0x10 Preprocessor lex messages\n"
59 " * 0x20 Preprocessor yacc trace\n"
62 char version_string[] = "Wine IDL Compiler Version " WIDL_FULLVERSION "\n"
63 "Copyright 2002 Ove Kaaven\n";
65 int win32 = 1;
66 int debuglevel = DEBUGLEVEL_NONE;
68 int pedantic = 0;
69 int preprocess_only = 0;
70 int header_only = 0;
71 int no_preprocess = 0;
73 char *input_name;
74 char *header_name;
75 char *header_token;
76 char *proxy_name;
77 char *temp_name;
79 int line_number = 1;
80 int char_number = 1;
82 FILE *header;
83 FILE *proxy;
85 time_t now;
87 int getopt (int argc, char *const *argv, const char *optstring);
88 static void rm_tempfile(void);
89 static void segvhandler(int sig);
91 static char *make_token(const char *name)
93 char *token;
94 char *slash;
95 int i;
97 slash = strrchr(name, '/');
98 if (slash) name = slash + 1;
100 token = xstrdup(name);
101 for (i=0; token[i]; i++) {
102 if (!isalnum(token[i])) token[i] = '_';
103 else token[i] = toupper(token[i]);
105 return token;
108 int main(int argc,char *argv[])
110 extern char* optarg;
111 extern int optind;
112 int optc;
113 int ret;
115 signal(SIGSEGV, segvhandler);
117 now = time(NULL);
119 while((optc = getopt(argc, argv, "d:D:EhH:I:NVW")) != EOF) {
120 switch(optc) {
121 case 'd':
122 debuglevel = strtol(optarg, NULL, 0);
123 break;
124 case 'D':
125 wpp_add_cmdline_define(optarg);
126 break;
127 case 'E':
128 preprocess_only = 1;
129 break;
130 case 'h':
131 header_only = 1;
132 break;
133 case 'H':
134 header_name = strdup(optarg);
135 break;
136 case 'I':
137 wpp_add_include_path(optarg);
138 break;
139 case 'N':
140 no_preprocess = 1;
141 break;
142 case 'V':
143 printf(version_string);
144 return 0;
145 case 'W':
146 pedantic = 1;
147 break;
148 default:
149 fprintf(stderr, usage);
150 return 1;
154 if(optind < argc) {
155 input_name = argv[optind];
157 else {
158 fprintf(stderr, usage);
159 return 1;
162 if(debuglevel)
164 setbuf(stdout,0);
165 setbuf(stderr,0);
168 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
169 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
171 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
172 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
173 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
175 if (!header_name) {
176 header_name = dup_basename(input_name, ".idl");
177 strcat(header_name, ".h");
180 if (!proxy_name) {
181 proxy_name = dup_basename(input_name, ".idl");
182 strcat(proxy_name, "_p.c");
185 atexit(rm_tempfile);
186 if (!no_preprocess)
188 chat("Starting preprocess");
190 if (!preprocess_only)
192 ret = wpp_parse_temp( input_name, header_name, &temp_name );
194 else
196 ret = wpp_parse( input_name, stdout );
199 if(ret) exit(1);
200 if(preprocess_only) exit(0);
201 if(!(yyin = fopen(temp_name, "r"))) {
202 fprintf(stderr, "Could not open %s for input\n", temp_name);
203 return 1;
206 else {
207 if(!(yyin = fopen(input_name, "r"))) {
208 fprintf(stderr, "Could not open %s for input\n", input_name);
209 return 1;
213 header_token = make_token(header_name);
215 header = fopen(header_name, "w");
216 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
217 fprintf(header, "#include \"rpc.h\"\n" );
218 fprintf(header, "#include \"rpcndr.h\"\n\n" );
219 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
220 fprintf(header, "#define __WIDL_%s\n", header_token);
222 ret = yyparse();
224 finish_proxy();
225 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
226 fclose(header);
227 fclose(yyin);
229 if(ret) {
230 exit(1);
233 return 0;
236 static void rm_tempfile(void)
238 abort_import();
239 if(temp_name)
240 unlink(temp_name);
243 static void segvhandler(int sig)
245 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
246 fflush(stdout);
247 fflush(stderr);
248 abort();