bb96b6e87b23a0dc65e066d15b899e0b177950fc
[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 "wine/wpp.h"
40
41 /* future options to reserve characters for: */
42 /* a = alignment of structures */
43 /* A = ACF input filename */
44 /* J = do not search standard include path */
45 /* O = generate interpreted stubs */
46 /* u = UUID file only? */
47 /* U = UUID filename */
48 /* w = select win16/win32 output (?) */
49
50 static char usage[] =
51 "Usage: widl [options...] infile.idl\n"
52 " -c Generate client stub\n"
53 " -C file Name of client stub file (default is infile_c.c)\n"
54 " -d n Set debug level to 'n'\n"
55 " -D id[=val] Define preprocessor identifier id=val\n"
56 " -E Preprocess only\n"
57 " -h Generate headers\n"
58 " -H file Name of header file (default is infile.h)\n"
59 " -I path Set include search dir to path (multiple -I allowed)\n"
60 " -N Do not preprocess input\n"
61 " -o Use old naming conventions\n"
62 " -p Generate proxy\n"
63 " -P file Name of proxy file (default is infile_p.c)\n"
64 " -s Generate server stub\n"
65 " -S file Name of server stub file (default is infile_s.c)\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 do_client = 0;
92 int do_server = 0;
93 int no_preprocess = 0;
94 int old_names = 0;
95
96 char *input_name;
97 char *header_name;
98 char *header_token;
99 char *typelib_name;
100 char *proxy_name;
101 char *proxy_token;
102 char *client_name;
103 char *client_token;
104 char *server_name;
105 char *server_token;
106 char *temp_name;
107
108 int line_number = 1;
109
110 FILE *header;
111 FILE *proxy;
112
113 time_t now;
114
115 int getopt (int argc, char *const *argv, const char *optstring);
116 static void rm_tempfile(void);
117 static void segvhandler(int sig);
118
119 static char *make_token(const char *name)
120 {
121 char *token;
122 char *slash;
123 int i;
124
125 slash = strrchr(name, '/');
126 if (slash) name = slash + 1;
127
128 token = xstrdup(name);
129 for (i=0; token[i]; i++) {
130 if (!isalnum(token[i])) token[i] = '_';
131 else token[i] = toupper(token[i]);
132 }
133 return token;
134 }
135
136 int main(int argc,char *argv[])
137 {
138 extern char* optarg;
139 extern int optind;
140 int optc;
141 int ret = 0;
142
143 signal(SIGSEGV, segvhandler);
144
145 now = time(NULL);
146
147 while((optc = getopt(argc, argv, "cC:d:D:EhH:I:NopP:sS:tT:VW")) != EOF) {
148 switch (optc) {
149 case 'c':
150 do_everything = 0;
151 do_client = 1;
152 break;
153 case 'C':
154 client_name = strdup(optarg);
155 break;
156 case 'd':
157 debuglevel = strtol(optarg, NULL, 0);
158 break;
159 case 'D':
160 wpp_add_cmdline_define(optarg);
161 break;
162 case 'E':
163 do_everything = 0;
164 preprocess_only = 1;
165 break;
166 case 'h':
167 do_everything = 0;
168 do_header = 1;
169 break;
170 case 'H':
171 header_name = strdup(optarg);
172 break;
173 case 'I':
174 wpp_add_include_path(optarg);
175 break;
176 case 'N':
177 no_preprocess = 1;
178 break;
179 case 'o':
180 old_names = 1;
181 break;
182 case 'p':
183 do_everything = 0;
184 do_proxies = 1;
185 break;
186 case 'P':
187 proxy_name = strdup(optarg);
188 break;
189 case 's':
190 do_everything = 0;
191 do_server = 1;
192 break;
193 case 'S':
194 server_name = strdup(optarg);
195 break;
196 case 't':
197 do_everything = 0;
198 do_typelib = 1;
199 break;
200 case 'T':
201 typelib_name = strdup(optarg);
202 break;
203 case 'V':
204 printf(version_string);
205 return 0;
206 case 'W':
207 pedantic = 1;
208 break;
209 default:
210 fprintf(stderr, usage);
211 return 1;
212 }
213 }
214
215 if (do_everything) {
216 do_header = do_typelib = do_proxies = do_client = do_server = 1;
217 }
218 if (optind < argc) {
219 input_name = xstrdup(argv[optind]);
220 }
221 else {
222 fprintf(stderr, usage);
223 return 1;
224 }
225
226 if (debuglevel)
227 {
228 setbuf(stdout,0);
229 setbuf(stderr,0);
230 }
231
232 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
233 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
234
235 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
236 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
237 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
238
239 if (!header_name) {
240 header_name = dup_basename(input_name, ".idl");
241 strcat(header_name, ".h");
242 }
243
244 if (!typelib_name && do_typelib) {
245 typelib_name = dup_basename(input_name, ".idl");
246 strcat(typelib_name, ".tlb");
247 }
248
249 if (!proxy_name && do_proxies) {
250 proxy_name = dup_basename(input_name, ".idl");
251 proxy_token = xstrdup(proxy_name);
252 strcat(proxy_name, "_p.c");
253 }
254
255 if (!client_name && do_client) {
256 client_name = dup_basename(input_name, ".idl");
257 client_token = xstrdup(client_name);
258 strcat(client_name, "_c.c");
259 }
260
261 if (!server_name && do_server) {
262 server_name = dup_basename(input_name, ".idl");
263 server_token = xstrdup(server_name);
264 strcat(server_name, "_s.c");
265 }
266
267 wpp_add_cmdline_define("__WIDL__");
268
269 atexit(rm_tempfile);
270 if (!no_preprocess)
271 {
272 chat("Starting preprocess\n");
273
274 if (!preprocess_only)
275 {
276 ret = wpp_parse_temp( input_name, header_name, &temp_name );
277 }
278 else
279 {
280 ret = wpp_parse( input_name, stdout );
281 }
282
283 if(ret) exit(1);
284 if(preprocess_only) exit(0);
285 if(!(yyin = fopen(temp_name, "r"))) {
286 fprintf(stderr, "Could not open %s for input\n", temp_name);
287 return 1;
288 }
289 }
290 else {
291 if(!(yyin = fopen(input_name, "r"))) {
292 fprintf(stderr, "Could not open %s for input\n", input_name);
293 return 1;
294 }
295 }
296
297 if(do_header) {
298 header_token = make_token(header_name);
299
300 if(!(header = fopen(header_name, "w"))) {
301 fprintf(stderr, "Could not open %s for output\n", header_name);
302 return 1;
303 }
304 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
305 fprintf(header, "#include <rpc.h>\n" );
306 fprintf(header, "#include <rpcndr.h>\n\n" );
307 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
308 fprintf(header, "#define __WIDL_%s\n", header_token);
309 fprintf(header, "#ifdef __cplusplus\n");
310 fprintf(header, "extern \"C\" {\n");
311 fprintf(header, "#endif\n");
312 }
313
314 ret = yyparse();
315
316 if(do_header) {
317 fprintf(header, "#ifdef __cplusplus\n");
318 fprintf(header, "}\n");
319 fprintf(header, "#endif\n");
320 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
321 fclose(header);
322 }
323
324 fclose(yyin);
325
326 if(ret) {
327 exit(1);
328 }
329 header_name = NULL;
330 client_name = NULL;
331 server_name = NULL;
332 return 0;
333 }
334
335 static void rm_tempfile(void)
336 {
337 abort_import();
338 if(temp_name)
339 unlink(temp_name);
340 if (header_name)
341 unlink(header_name);
342 if (client_name)
343 unlink(client_name);
344 if (server_name)
345 unlink(server_name);
346 }
347
348 static void segvhandler(int sig)
349 {
350 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
351 fflush(stdout);
352 fflush(stderr);
353 abort();
354 }