- implemented support for function debug start/stop and labels (msc.c
[wine.git] / tools / widl / widl.c
blob37368d8069b633cd777e705ed2914f8874fc3379
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 " -b Make headers compatible with ICOM macros\n"
60 " -d n Set debug level to 'n'\n"
61 " -D id[=val] Define preprocessor identifier id=val\n"
62 " -E Preprocess only\n"
63 " -h Generate headers only\n"
64 " -H file Name of header file (default is infile.h)\n"
65 " -I path Set include search dir to path (multiple -I allowed)\n"
66 " -N Do not preprocess input\n"
67 " -t Generate typelib only\n"
68 " -T file Name of typelib file (default is infile.tlb)\n"
69 " -V Print version and exit\n"
70 " -W Enable pedantic warnings\n"
71 "Debug level 'n' is a bitmask with following meaning:\n"
72 " * 0x01 Tell which resource is parsed (verbose mode)\n"
73 " * 0x02 Dump internal structures\n"
74 " * 0x04 Create a parser trace (yydebug=1)\n"
75 " * 0x08 Preprocessor messages\n"
76 " * 0x10 Preprocessor lex messages\n"
77 " * 0x20 Preprocessor yacc trace\n"
80 char version_string[] = "Wine IDL Compiler Version " WIDL_FULLVERSION "\n"
81 "Copyright 2002 Ove Kaaven\n";
83 int win32 = 1;
84 int debuglevel = DEBUGLEVEL_NONE;
86 int pedantic = 0;
87 int do_everything = 1;
88 int preprocess_only = 0;
89 int header_only = 0;
90 int typelib_only = 0;
91 int no_preprocess = 0;
92 int compat_icom = 0;
94 char *input_name;
95 char *header_name;
96 char *header_token;
97 char *typelib_name;
98 char *proxy_name;
99 char *proxy_token;
100 char *temp_name;
102 int line_number = 1;
104 FILE *header;
105 FILE *proxy;
107 time_t now;
109 int getopt (int argc, char *const *argv, const char *optstring);
110 static void rm_tempfile(void);
111 static void segvhandler(int sig);
113 static char *make_token(const char *name)
115 char *token;
116 char *slash;
117 int i;
119 slash = strrchr(name, '/');
120 if (slash) name = slash + 1;
122 token = xstrdup(name);
123 for (i=0; token[i]; i++) {
124 if (!isalnum(token[i])) token[i] = '_';
125 else token[i] = toupper(token[i]);
127 return token;
130 int main(int argc,char *argv[])
132 extern char* optarg;
133 extern int optind;
134 int optc;
135 int ret;
137 signal(SIGSEGV, segvhandler);
139 now = time(NULL);
141 while((optc = getopt(argc, argv, "bd:D:EhH:I:NtT:VW")) != EOF) {
142 switch(optc) {
143 case 'b':
144 compat_icom = 1;
145 break;
146 case 'd':
147 debuglevel = strtol(optarg, NULL, 0);
148 break;
149 case 'D':
150 wpp_add_cmdline_define(optarg);
151 break;
152 case 'E':
153 do_everything = 0;
154 preprocess_only = 1;
155 break;
156 case 'h':
157 do_everything = 0;
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 't':
170 do_everything = 0;
171 typelib_only = 1;
172 break;
173 case 'T':
174 typelib_name = strdup(optarg);
175 break;
176 case 'V':
177 printf(version_string);
178 return 0;
179 case 'W':
180 pedantic = 1;
181 break;
182 default:
183 fprintf(stderr, usage);
184 return 1;
188 if(optind < argc) {
189 input_name = xstrdup(argv[optind]);
191 else {
192 fprintf(stderr, usage);
193 return 1;
196 if(debuglevel)
198 setbuf(stdout,0);
199 setbuf(stderr,0);
202 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
203 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
205 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
206 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
207 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
209 if (!header_name) {
210 header_name = dup_basename(input_name, ".idl");
211 strcat(header_name, ".h");
214 if (!typelib_name) {
215 typelib_name = dup_basename(input_name, ".idl");
216 strcat(typelib_name, ".tlb");
219 if (!proxy_name) {
220 proxy_name = dup_basename(input_name, ".idl");
221 proxy_token = xstrdup(proxy_name);
222 strcat(proxy_name, "_p.c");
225 wpp_add_cmdline_define("__WIDL__");
227 atexit(rm_tempfile);
228 if (!no_preprocess)
230 chat("Starting preprocess");
232 if (!preprocess_only)
234 ret = wpp_parse_temp( input_name, header_name, &temp_name );
236 else
238 ret = wpp_parse( input_name, stdout );
241 if(ret) exit(1);
242 if(preprocess_only) exit(0);
243 if(!(yyin = fopen(temp_name, "r"))) {
244 fprintf(stderr, "Could not open %s for input\n", temp_name);
245 return 1;
248 else {
249 if(!(yyin = fopen(input_name, "r"))) {
250 fprintf(stderr, "Could not open %s for input\n", input_name);
251 return 1;
255 header_token = make_token(header_name);
257 header = fopen(header_name, "w");
258 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
259 fprintf(header, "#include <rpc.h>\n" );
260 fprintf(header, "#include <rpcndr.h>\n\n" );
261 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
262 fprintf(header, "#define __WIDL_%s\n", header_token);
263 fprintf(header, "#ifdef __cplusplus\n");
264 fprintf(header, "extern \"C\" {\n");
265 fprintf(header, "#endif\n");
267 ret = yyparse();
269 fprintf(header, "#ifdef __cplusplus\n");
270 fprintf(header, "}\n");
271 fprintf(header, "#endif\n");
272 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
273 fclose(header);
274 fclose(yyin);
276 if(ret) {
277 exit(1);
279 header_name = NULL;
280 return 0;
283 static void rm_tempfile(void)
285 abort_import();
286 if(temp_name)
287 unlink(temp_name);
288 if (header_name)
289 unlink( header_name );
292 static void segvhandler(int sig)
294 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
295 fflush(stdout);
296 fflush(stderr);
297 abort();