Authors: Mike McCormack <mike@codeweavers.com>, Aric Stewart <aric@codeweavers.com...
[wine/multimedia.git] / tools / widl / widl.c
blob119f58237a357a1cb0c1ebda23d962dc88ac416e
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 "wine/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 /* u = UUID file only? */
54 /* U = UUID filename */
55 /* w = select win16/win32 output (?) */
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 only\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 only\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"
79 static const char version_string[] = "Wine IDL Compiler Version " WIDL_FULLVERSION "\n"
80 "Copyright 2002 Ove Kaaven\n";
82 int win32 = 1;
83 int debuglevel = DEBUGLEVEL_NONE;
85 int pedantic = 0;
86 int do_everything = 1;
87 int preprocess_only = 0;
88 int header_only = 0;
89 int typelib_only = 0;
90 int no_preprocess = 0;
92 char *input_name;
93 char *header_name;
94 char *header_token;
95 char *typelib_name;
96 char *proxy_name;
97 char *proxy_token;
98 char *temp_name;
100 int line_number = 1;
102 FILE *header;
103 FILE *proxy;
105 time_t now;
107 int getopt (int argc, char *const *argv, const char *optstring);
108 static void rm_tempfile(void);
109 static void segvhandler(int sig);
111 static char *make_token(const char *name)
113 char *token;
114 char *slash;
115 int i;
117 slash = strrchr(name, '/');
118 if (slash) name = slash + 1;
120 token = xstrdup(name);
121 for (i=0; token[i]; i++) {
122 if (!isalnum(token[i])) token[i] = '_';
123 else token[i] = toupper(token[i]);
125 return token;
128 int main(int argc,char *argv[])
130 extern char* optarg;
131 extern int optind;
132 int optc;
133 int ret = 0;
135 signal(SIGSEGV, segvhandler);
137 now = time(NULL);
139 while((optc = getopt(argc, argv, "d:D:EhH:I:NtT:VW")) != EOF) {
140 switch(optc) {
141 case 'd':
142 debuglevel = strtol(optarg, NULL, 0);
143 break;
144 case 'D':
145 wpp_add_cmdline_define(optarg);
146 break;
147 case 'E':
148 do_everything = 0;
149 preprocess_only = 1;
150 break;
151 case 'h':
152 do_everything = 0;
153 header_only = 1;
154 break;
155 case 'H':
156 header_name = strdup(optarg);
157 break;
158 case 'I':
159 wpp_add_include_path(optarg);
160 break;
161 case 'N':
162 no_preprocess = 1;
163 break;
164 case 't':
165 do_everything = 0;
166 typelib_only = 1;
167 break;
168 case 'T':
169 typelib_name = strdup(optarg);
170 break;
171 case 'V':
172 printf(version_string);
173 return 0;
174 case 'W':
175 pedantic = 1;
176 break;
177 default:
178 fprintf(stderr, usage);
179 return 1;
183 if(optind < argc) {
184 input_name = xstrdup(argv[optind]);
186 else {
187 fprintf(stderr, usage);
188 return 1;
191 if(debuglevel)
193 setbuf(stdout,0);
194 setbuf(stderr,0);
197 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
198 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
200 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
201 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
202 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
204 if (!header_name && (do_everything || header_only)) {
205 header_name = dup_basename(input_name, ".idl");
206 strcat(header_name, ".h");
209 if (!typelib_name && (do_everything || typelib_only)) {
210 typelib_name = dup_basename(input_name, ".idl");
211 strcat(typelib_name, ".tlb");
214 if (!proxy_name && do_everything) {
215 proxy_name = dup_basename(input_name, ".idl");
216 proxy_token = xstrdup(proxy_name);
217 strcat(proxy_name, "_p.c");
220 wpp_add_cmdline_define("__WIDL__");
222 atexit(rm_tempfile);
223 if (!no_preprocess)
225 chat("Starting preprocess\n");
227 if (!preprocess_only)
229 ret = wpp_parse_temp( input_name, header_name, &temp_name );
231 else
233 ret = wpp_parse( input_name, stdout );
236 if(ret) exit(1);
237 if(preprocess_only) exit(0);
238 if(!(yyin = fopen(temp_name, "r"))) {
239 fprintf(stderr, "Could not open %s for input\n", temp_name);
240 return 1;
243 else {
244 if(!(yyin = fopen(input_name, "r"))) {
245 fprintf(stderr, "Could not open %s for input\n", input_name);
246 return 1;
250 if(do_everything || header_only) {
251 header_token = make_token(header_name);
253 if(!(header = fopen(header_name, "w"))) {
254 fprintf(stderr, "Could not open %s for output\n", header_name);
255 return 1;
257 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
258 fprintf(header, "#include <rpc.h>\n" );
259 fprintf(header, "#include <rpcndr.h>\n\n" );
260 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
261 fprintf(header, "#define __WIDL_%s\n", header_token);
262 fprintf(header, "#ifdef __cplusplus\n");
263 fprintf(header, "extern \"C\" {\n");
264 fprintf(header, "#endif\n");
266 ret = yyparse();
268 fprintf(header, "#ifdef __cplusplus\n");
269 fprintf(header, "}\n");
270 fprintf(header, "#endif\n");
271 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
272 fclose(header);
275 fclose(yyin);
277 if(ret) {
278 exit(1);
280 header_name = NULL;
281 return 0;
284 static void rm_tempfile(void)
286 abort_import();
287 if(temp_name)
288 unlink(temp_name);
289 if (header_name)
290 unlink( header_name );
293 static void segvhandler(int sig)
295 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
296 fflush(stdout);
297 fflush(stderr);
298 abort();