Release 20030408.
[wine/gsoc-2012-control.git] / tools / widl / widl.c
blob69bb83221ce7d5b86616fd7a980a034d24031fe0
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 /* 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 /* t = typelib only? */
54 /* T = typelib filename */
55 /* u = UUID file only? */
56 /* U = UUID filename */
57 /* w = select win16/win32 output (?) */
59 static char usage[] =
60 "Usage: widl [options...] infile.idl\n"
61 " -b Make headers compatible with ICOM macros\n"
62 " -B Make headers use ICOM macros\n"
63 " -d n Set debug level to 'n'\n"
64 " -D id[=val] Define preprocessor identifier id=val\n"
65 " -E Preprocess only\n"
66 " -h Generate headers only\n"
67 " -H file Name of header file (default is infile.h)\n"
68 " -I path Set include search dir to path (multiple -I allowed)\n"
69 " -N Do not preprocess input\n"
70 " -V Print version and exit\n"
71 " -W Enable pedantic warnings\n"
72 "Debug level 'n' is a bitmask with following meaning:\n"
73 " * 0x01 Tell which resource is parsed (verbose mode)\n"
74 " * 0x02 Dump internal structures\n"
75 " * 0x04 Create a parser trace (yydebug=1)\n"
76 " * 0x08 Preprocessor messages\n"
77 " * 0x10 Preprocessor lex messages\n"
78 " * 0x20 Preprocessor yacc trace\n"
81 char version_string[] = "Wine IDL Compiler Version " WIDL_FULLVERSION "\n"
82 "Copyright 2002 Ove Kaaven\n";
84 int win32 = 1;
85 int debuglevel = DEBUGLEVEL_NONE;
87 int pedantic = 0;
88 int preprocess_only = 0;
89 int header_only = 0;
90 int no_preprocess = 0;
91 int compat_icom = 0;
92 int use_icom = 0;
94 char *input_name;
95 char *header_name;
96 char *header_token;
97 char *proxy_name;
98 char *proxy_token;
99 char *temp_name;
101 int line_number = 1;
103 FILE *header;
104 FILE *proxy;
106 time_t now;
108 int getopt (int argc, char *const *argv, const char *optstring);
109 static void rm_tempfile(void);
110 static void segvhandler(int sig);
112 static char *make_token(const char *name)
114 char *token;
115 char *slash;
116 int i;
118 slash = strrchr(name, '/');
119 if (slash) name = slash + 1;
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]);
126 return token;
129 int main(int argc,char *argv[])
131 extern char* optarg;
132 extern int optind;
133 int optc;
134 int ret;
136 signal(SIGSEGV, segvhandler);
138 now = time(NULL);
140 while((optc = getopt(argc, argv, "bBd:D:EhH:I:NVW")) != EOF) {
141 switch(optc) {
142 case 'b':
143 compat_icom = 1;
144 break;
145 case 'B':
146 use_icom = 1;
147 break;
148 case 'd':
149 debuglevel = strtol(optarg, NULL, 0);
150 break;
151 case 'D':
152 wpp_add_cmdline_define(optarg);
153 break;
154 case 'E':
155 preprocess_only = 1;
156 break;
157 case 'h':
158 header_only = 1;
159 break;
160 case 'H':
161 header_name = strdup(optarg);
162 break;
163 case 'I':
164 wpp_add_include_path(optarg);
165 break;
166 case 'N':
167 no_preprocess = 1;
168 break;
169 case 'V':
170 printf(version_string);
171 return 0;
172 case 'W':
173 pedantic = 1;
174 break;
175 default:
176 fprintf(stderr, usage);
177 return 1;
181 if(optind < argc) {
182 input_name = xstrdup(argv[optind]);
184 else {
185 fprintf(stderr, usage);
186 return 1;
189 if(debuglevel)
191 setbuf(stdout,0);
192 setbuf(stderr,0);
195 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
196 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
198 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
199 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
200 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
202 if (!header_name) {
203 header_name = dup_basename(input_name, ".idl");
204 strcat(header_name, ".h");
207 if (!proxy_name) {
208 proxy_name = dup_basename(input_name, ".idl");
209 proxy_token = xstrdup(proxy_name);
210 strcat(proxy_name, "_p.c");
213 wpp_add_cmdline_define("__WIDL__");
215 atexit(rm_tempfile);
216 if (!no_preprocess)
218 chat("Starting preprocess");
220 if (!preprocess_only)
222 ret = wpp_parse_temp( input_name, header_name, &temp_name );
224 else
226 ret = wpp_parse( input_name, stdout );
229 if(ret) exit(1);
230 if(preprocess_only) exit(0);
231 if(!(yyin = fopen(temp_name, "r"))) {
232 fprintf(stderr, "Could not open %s for input\n", temp_name);
233 return 1;
236 else {
237 if(!(yyin = fopen(input_name, "r"))) {
238 fprintf(stderr, "Could not open %s for input\n", input_name);
239 return 1;
243 header_token = make_token(header_name);
245 header = fopen(header_name, "w");
246 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
247 fprintf(header, "#include \"rpc.h\"\n" );
248 fprintf(header, "#include \"rpcndr.h\"\n\n" );
249 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
250 fprintf(header, "#define __WIDL_%s\n", header_token);
251 fprintf(header, "#ifdef __cplusplus\n");
252 fprintf(header, "extern \"C\" {\n");
253 fprintf(header, "#endif\n");
255 ret = yyparse();
257 finish_proxy();
258 fprintf(header, "#ifdef __cplusplus\n");
259 fprintf(header, "}\n");
260 fprintf(header, "#endif\n");
261 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
262 fclose(header);
263 fclose(yyin);
265 if(ret) {
266 exit(1);
268 header_name = NULL;
269 return 0;
272 static void rm_tempfile(void)
274 abort_import();
275 if(temp_name)
276 unlink(temp_name);
277 if (header_name)
278 unlink( header_name );
281 static void segvhandler(int sig)
283 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
284 fflush(stdout);
285 fflush(stderr);
286 abort();