Import from Wine
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23
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>
33
34 #define WIDL_FULLVERSION "0.1"
35
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "proxy.h"
40 #include "wine/wpp.h"
41
42 /* future options to reserve characters for: */
43 /* a = alignment of structures */
44 /* A = ACF input filename */
45 /* c = client stub only? */
46 /* C = client stub filename */
47 /* J = do not search standard include path */
48 /* O = generate interpreted stubs */
49 /* p = proxy only? */
50 /* P = proxy filename */
51 /* s = server stub only? */
52 /* S = server stub filename */
53 /* u = UUID file only? */
54 /* U = UUID filename */
55 /* w = select win16/win32 output (?) */
56
57 static char usage[] =
58 "Usage: widl [options...] infile.idl\n"
59 " -d n Set debug level to 'n'\n"
60 " -D id[=val] Define preprocessor identifier id=val\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 " -N Do not preprocess input\n"
66 " -t Generate typelib\n"
67 " -T file Name of typelib file (default is infile.tlb)\n"
68 " -V Print version and exit\n"
69 " -W Enable pedantic warnings\n"
70 "Debug level 'n' is a bitmask with following meaning:\n"
71 " * 0x01 Tell which resource is parsed (verbose mode)\n"
72 " * 0x02 Dump internal structures\n"
73 " * 0x04 Create a parser trace (yydebug=1)\n"
74 " * 0x08 Preprocessor messages\n"
75 " * 0x10 Preprocessor lex messages\n"
76 " * 0x20 Preprocessor yacc trace\n"
77 ;
78
79 static const char version_string[] = "Wine IDL Compiler Version " WIDL_FULLVERSION "\n"
80 "Copyright 2002 Ove Kaaven\n";
81
82 int win32 = 1;
83 int debuglevel = DEBUGLEVEL_NONE;
84
85 int pedantic = 0;
86 static int do_everything = 1;
87 int preprocess_only = 0;
88 int do_header = 0;
89 int do_typelib = 0;
90 int do_proxies = 0;
91 int no_preprocess = 0;
92
93 char *input_name;
94 char *header_name;
95 char *header_token;
96 char *typelib_name;
97 char *proxy_name;
98 char *proxy_token;
99 char *temp_name;
100
101 int line_number = 1;
102
103 FILE *header;
104 FILE *proxy;
105
106 time_t now;
107
108 int getopt (int argc, char *const *argv, const char *optstring);
109 static void rm_tempfile(void);
110 static void segvhandler(int sig);
111
112 static char *make_token(const char *name)
113 {
114 char *token;
115 char *slash;
116 int i;
117
118 slash = strrchr(name, '/');
119 if (slash) name = slash + 1;
120
121 token = xstrdup(name);
122 for (i=0; token[i]; i++) {
123 if (!isalnum(token[i])) token[i] = '_';
124 else token[i] = toupper(token[i]);
125 }
126 return token;
127 }
128
129 int main(int argc,char *argv[])
130 {
131 extern char* optarg;
132 extern int optind;
133 int optc;
134 int ret = 0;
135
136 signal(SIGSEGV, segvhandler);
137
138 now = time(NULL);
139
140 while((optc = getopt(argc, argv, "d:D:EhH:I:NtT:VW")) != EOF) {
141 switch(optc) {
142 case 'd':
143 debuglevel = strtol(optarg, NULL, 0);
144 break;
145 case 'D':
146 wpp_add_cmdline_define(optarg);
147 break;
148 case 'E':
149 do_everything = 0;
150 preprocess_only = 1;
151 break;
152 case 'h':
153 do_everything = 0;
154 do_header = 1;
155 break;
156 case 'H':
157 header_name = strdup(optarg);
158 break;
159 case 'I':
160 wpp_add_include_path(optarg);
161 break;
162 case 'N':
163 no_preprocess = 1;
164 break;
165 case 't':
166 do_everything = 0;
167 do_typelib = 1;
168 break;
169 case 'T':
170 typelib_name = strdup(optarg);
171 break;
172 case 'V':
173 printf(version_string);
174 return 0;
175 case 'W':
176 pedantic = 1;
177 break;
178 default:
179 fprintf(stderr, usage);
180 return 1;
181 }
182 }
183
184 if(do_everything) {
185 do_header = do_typelib = do_proxies = 1;
186 }
187 if(optind < argc) {
188 input_name = xstrdup(argv[optind]);
189 }
190 else {
191 fprintf(stderr, usage);
192 return 1;
193 }
194
195 if(debuglevel)
196 {
197 setbuf(stdout,0);
198 setbuf(stderr,0);
199 }
200
201 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
202 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
203
204 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
205 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
206 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
207
208 if (!header_name && do_header) {
209 header_name = dup_basename(input_name, ".idl");
210 strcat(header_name, ".h");
211 }
212
213 if (!typelib_name && do_typelib) {
214 typelib_name = dup_basename(input_name, ".idl");
215 strcat(typelib_name, ".tlb");
216 }
217
218 if (!proxy_name && do_proxies) {
219 proxy_name = dup_basename(input_name, ".idl");
220 proxy_token = xstrdup(proxy_name);
221 strcat(proxy_name, "_p.c");
222 }
223
224 wpp_add_cmdline_define("__WIDL__");
225
226 atexit(rm_tempfile);
227 if (!no_preprocess)
228 {
229 chat("Starting preprocess\n");
230
231 if (!preprocess_only)
232 {
233 ret = wpp_parse_temp( input_name, header_name, &temp_name );
234 }
235 else
236 {
237 ret = wpp_parse( input_name, stdout );
238 }
239
240 if(ret) exit(1);
241 if(preprocess_only) exit(0);
242 if(!(yyin = fopen(temp_name, "r"))) {
243 fprintf(stderr, "Could not open %s for input\n", temp_name);
244 return 1;
245 }
246 }
247 else {
248 if(!(yyin = fopen(input_name, "r"))) {
249 fprintf(stderr, "Could not open %s for input\n", input_name);
250 return 1;
251 }
252 }
253
254 if(do_header) {
255 header_token = make_token(header_name);
256
257 if(!(header = fopen(header_name, "w"))) {
258 fprintf(stderr, "Could not open %s for output\n", header_name);
259 return 1;
260 }
261 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
262 fprintf(header, "#include <rpc.h>\n" );
263 fprintf(header, "#include <rpcndr.h>\n\n" );
264 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
265 fprintf(header, "#define __WIDL_%s\n", header_token);
266 fprintf(header, "#ifdef __cplusplus\n");
267 fprintf(header, "extern \"C\" {\n");
268 fprintf(header, "#endif\n");
269 }
270
271 ret = yyparse();
272
273 if(do_header) {
274 fprintf(header, "#ifdef __cplusplus\n");
275 fprintf(header, "}\n");
276 fprintf(header, "#endif\n");
277 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
278 fclose(header);
279 }
280
281 fclose(yyin);
282
283 if(ret) {
284 exit(1);
285 }
286 header_name = NULL;
287 return 0;
288 }
289
290 static void rm_tempfile(void)
291 {
292 abort_import();
293 if(temp_name)
294 unlink(temp_name);
295 if (header_name)
296 unlink( header_name );
297 }
298
299 static void segvhandler(int sig)
300 {
301 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
302 fflush(stdout);
303 fflush(stderr);
304 abort();
305 }