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