winuser.h: Add some missing virtual key symbols.
[wine] / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #ifdef HAVE_GETOPT_H
35 # include <getopt.h>
36 #endif
37
38 #include "widl.h"
39 #include "utils.h"
40 #include "parser.h"
41 #include "wine/wpp.h"
42 #include "header.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 " PACKAGE_VERSION "\n"
83                         "Copyright 2002 Ove Kaaven\n";
84
85 int win32 = 1;
86 int debuglevel = DEBUGLEVEL_NONE;
87 int yy_flex_debug;
88
89 int pedantic = 0;
90 static int do_everything = 1;
91 int preprocess_only = 0;
92 int do_header = 0;
93 int do_typelib = 0;
94 int do_proxies = 0;
95 int do_client = 0;
96 int do_server = 0;
97 int no_preprocess = 0;
98 int old_names = 0;
99
100 char *input_name;
101 char *header_name;
102 char *header_token;
103 char *typelib_name;
104 char *proxy_name;
105 char *proxy_token;
106 char *client_name;
107 char *client_token;
108 char *server_name;
109 char *server_token;
110 char *temp_name;
111
112 int line_number = 1;
113
114 FILE *header;
115 FILE *proxy;
116
117 time_t now;
118
119 static const char *short_options =
120     "cC:d:D:EhH:I:NpP:sS:tT:VW";
121 static struct option long_options[] = {
122     { "oldnames", 0, 0, 1 },
123     { 0, 0, 0, 0 }
124 };
125
126 static void rm_tempfile(void);
127 static void segvhandler(int sig);
128
129 static char *make_token(const char *name)
130 {
131   char *token;
132   char *slash;
133   int i;
134
135   slash = strrchr(name, '/');
136   if (slash) name = slash + 1;
137
138   token = xstrdup(name);
139   for (i=0; token[i]; i++) {
140     if (!isalnum(token[i])) token[i] = '_';
141     else token[i] = toupper(token[i]);
142   }
143   return token;
144 }
145
146 /* duplicate a basename into a valid C token */
147 static char *dup_basename_token(const char *name, const char *ext)
148 {
149     char *p, *ret = dup_basename( name, ext );
150     /* map invalid characters to '_' */
151     for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
152     return ret;
153 }
154
155 /* clean things up when aborting on a signal */
156 static void exit_on_signal( int sig )
157 {
158     exit(1);  /* this will call the atexit functions */
159 }
160
161 int main(int argc,char *argv[])
162 {
163   extern char* optarg;
164   extern int   optind;
165   int optc;
166   int ret = 0;
167   int opti = 0;
168
169   signal(SIGSEGV, segvhandler);
170   signal( SIGTERM, exit_on_signal );
171   signal( SIGINT, exit_on_signal );
172 #ifdef SIGHUP
173   signal( SIGHUP, exit_on_signal );
174 #endif
175
176   now = time(NULL);
177
178   while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
179     switch(optc) {
180     case 1:
181       old_names = 1;
182       break;
183     case 'c':
184       do_everything = 0;
185       do_client = 1;
186       break;
187     case 'C':
188       client_name = strdup(optarg);
189       break;
190     case 'd':
191       debuglevel = strtol(optarg, NULL, 0);
192       break;
193     case 'D':
194       wpp_add_cmdline_define(optarg);
195       break;
196     case 'E':
197       do_everything = 0;
198       preprocess_only = 1;
199       break;
200     case 'h':
201       do_everything = 0;
202       do_header = 1;
203       break;
204     case 'H':
205       header_name = strdup(optarg);
206       break;
207     case 'I':
208       wpp_add_include_path(optarg);
209       break;
210     case 'N':
211       no_preprocess = 1;
212       break;
213     case 'p':
214       do_everything = 0;
215       do_proxies = 1;
216       break;
217     case 'P':
218       proxy_name = strdup(optarg);
219       break;
220     case 's':
221       do_everything = 0;
222       do_server = 1;
223       break;
224     case 'S':
225       server_name = strdup(optarg);
226       break;
227     case 't':
228       do_everything = 0;
229       do_typelib = 1;
230       break;
231     case 'T':
232       typelib_name = strdup(optarg);
233       break;
234     case 'V':
235       printf(version_string);
236       return 0;
237     case 'W':
238       pedantic = 1;
239       break;
240     default:
241       fprintf(stderr, usage);
242       return 1;
243     }
244   }
245
246   if(do_everything) {
247       do_header = do_typelib = do_proxies = do_client = do_server = 1;
248   }
249   if(optind < argc) {
250     input_name = xstrdup(argv[optind]);
251   }
252   else {
253     fprintf(stderr, usage);
254     return 1;
255   }
256
257   if(debuglevel)
258   {
259     setbuf(stdout,0);
260     setbuf(stderr,0);
261   }
262
263   yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
264   yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
265
266   wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
267                  (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
268                  (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
269
270   if (!header_name) {
271     header_name = dup_basename(input_name, ".idl");
272     strcat(header_name, ".h");
273   }
274
275   if (!typelib_name && do_typelib) {
276     typelib_name = dup_basename(input_name, ".idl");
277     strcat(typelib_name, ".tlb");
278   }
279
280   if (!proxy_name && do_proxies) {
281     proxy_name = dup_basename(input_name, ".idl");
282     strcat(proxy_name, "_p.c");
283   }
284
285   if (!client_name && do_client) {
286     client_name = dup_basename(input_name, ".idl");
287     strcat(client_name, "_c.c");
288   }
289
290   if (!server_name && do_server) {
291     server_name = dup_basename(input_name, ".idl");
292     strcat(server_name, "_s.c");
293   }
294
295   if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
296   if (do_client) client_token = dup_basename_token(client_name,"_c.c");
297   if (do_server) server_token = dup_basename_token(server_name,"_s.c");
298
299   wpp_add_cmdline_define("__WIDL__");
300
301   atexit(rm_tempfile);
302   if (!no_preprocess)
303   {
304     chat("Starting preprocess\n");
305
306     if (!preprocess_only)
307     {
308         ret = wpp_parse_temp( input_name, header_name, &temp_name );
309     }
310     else
311     {
312         ret = wpp_parse( input_name, stdout );
313     }
314
315     if(ret) exit(1);
316     if(preprocess_only) exit(0);
317     if(!(yyin = fopen(temp_name, "r"))) {
318       fprintf(stderr, "Could not open %s for input\n", temp_name);
319       return 1;
320     }
321   }
322   else {
323     if(!(yyin = fopen(input_name, "r"))) {
324       fprintf(stderr, "Could not open %s for input\n", input_name);
325       return 1;
326     }
327   }
328
329   if(do_header) {
330     header_token = make_token(header_name);
331
332     if(!(header = fopen(header_name, "w"))) {
333       fprintf(stderr, "Could not open %s for output\n", header_name);
334       return 1;
335     }
336     fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
337     fprintf(header, "#include <rpc.h>\n" );
338     fprintf(header, "#include <rpcndr.h>\n\n" );
339     fprintf(header, "#ifndef __WIDL_%s\n", header_token);
340     fprintf(header, "#define __WIDL_%s\n", header_token);
341     fprintf(header, "#ifdef __cplusplus\n");
342     fprintf(header, "extern \"C\" {\n");
343     fprintf(header, "#endif\n");
344   }
345
346   ret = yyparse();
347
348   if(do_header) {
349     fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
350     fprintf(header, "\n");
351     write_user_types();
352     fprintf(header, "\n");
353     fprintf(header, "/* End additional prototypes */\n");
354     fprintf(header, "\n");
355     fprintf(header, "#ifdef __cplusplus\n");
356     fprintf(header, "}\n");
357     fprintf(header, "#endif\n");
358     fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
359     fclose(header);
360   }
361
362   fclose(yyin);
363
364   if(ret) {
365     exit(1);
366   }
367   header_name = NULL;
368   client_name = NULL;
369   server_name = NULL;
370   return 0;
371 }
372
373 static void rm_tempfile(void)
374 {
375   abort_import();
376   if(temp_name)
377     unlink(temp_name);
378   if (header_name)
379     unlink(header_name);
380   if (client_name)
381     unlink(client_name);
382   if (server_name)
383     unlink(server_name);
384 }
385
386 static void segvhandler(int sig)
387 {
388   fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
389   fflush(stdout);
390   fflush(stderr);
391   abort();
392 }