Get rid of the almost empty tmarshal.h file.
[wine/wine-kai.git] / tools / widl / widl.c
blob9022179f48ceba2f2c6ceaafad1d60fe313d50fc
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"
23 #include "wine/port.h"
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>
35 #define WIDL_FULLVERSION "0.1"
37 #include "widl.h"
38 #include "utils.h"
39 #include "parser.h"
40 #include "wine/wpp.h"
42 /* future options to reserve characters for: */
43 /* a = alignment of structures */
44 /* A = ACF input filename */
45 /* J = do not search standard include path */
46 /* O = generate interpreted stubs */
47 /* u = UUID file only? */
48 /* U = UUID filename */
49 /* w = select win16/win32 output (?) */
51 static char usage[] =
52 "Usage: widl [options...] infile.idl\n"
53 " -c Generate client stub\n"
54 " -C file Name of client stub file (default is infile_c.c)\n"
55 " -d n Set debug level to 'n'\n"
56 " -D id[=val] Define preprocessor identifier id=val\n"
57 " -E Preprocess only\n"
58 " -h Generate headers\n"
59 " -H file Name of header file (default is infile.h)\n"
60 " -I path Set include search dir to path (multiple -I allowed)\n"
61 " -N Do not preprocess input\n"
62 " -p Generate proxy\n"
63 " -P file Name of proxy file (default is infile_p.c)\n"
64 " -s Generate server stub\n"
65 " -S file Name of server stub file (default is infile_s.c)\n"
66 " -t Generate typelib\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 static int do_everything = 1;
87 int preprocess_only = 0;
88 int do_header = 0;
89 int do_typelib = 0;
90 int do_proxies = 0;
91 int do_client = 0;
92 int do_server = 0;
93 int no_preprocess = 0;
95 char *input_name;
96 char *header_name;
97 char *header_token;
98 char *typelib_name;
99 char *proxy_name;
100 char *proxy_token;
101 char *client_name;
102 char *client_token;
103 char *server_name;
104 char *server_token;
105 char *temp_name;
107 int line_number = 1;
109 FILE *header;
110 FILE *proxy;
112 time_t now;
114 int getopt (int argc, char *const *argv, const char *optstring);
115 static void rm_tempfile(void);
116 static void segvhandler(int sig);
118 static char *make_token(const char *name)
120 char *token;
121 char *slash;
122 int i;
124 slash = strrchr(name, '/');
125 if (slash) name = slash + 1;
127 token = xstrdup(name);
128 for (i=0; token[i]; i++) {
129 if (!isalnum(token[i])) token[i] = '_';
130 else token[i] = toupper(token[i]);
132 return token;
135 int main(int argc,char *argv[])
137 extern char* optarg;
138 extern int optind;
139 int optc;
140 int ret = 0;
142 signal(SIGSEGV, segvhandler);
144 now = time(NULL);
146 while((optc = getopt(argc, argv, "cC:d:D:EhH:I:NpP:sS:tT:VW")) != EOF) {
147 switch(optc) {
148 case 'c':
149 do_everything = 0;
150 do_client = 1;
151 break;
152 case 'C':
153 client_name = strdup(optarg);
154 break;
155 case 'd':
156 debuglevel = strtol(optarg, NULL, 0);
157 break;
158 case 'D':
159 wpp_add_cmdline_define(optarg);
160 break;
161 case 'E':
162 do_everything = 0;
163 preprocess_only = 1;
164 break;
165 case 'h':
166 do_everything = 0;
167 do_header = 1;
168 break;
169 case 'H':
170 header_name = strdup(optarg);
171 break;
172 case 'I':
173 wpp_add_include_path(optarg);
174 break;
175 case 'N':
176 no_preprocess = 1;
177 break;
178 case 'p':
179 do_everything = 0;
180 do_proxies = 1;
181 break;
182 case 'P':
183 proxy_name = strdup(optarg);
184 break;
185 case 's':
186 do_everything = 0;
187 do_server = 1;
188 break;
189 case 'S':
190 server_name = strdup(optarg);
191 break;
192 case 't':
193 do_everything = 0;
194 do_typelib = 1;
195 break;
196 case 'T':
197 typelib_name = strdup(optarg);
198 break;
199 case 'V':
200 printf(version_string);
201 return 0;
202 case 'W':
203 pedantic = 1;
204 break;
205 default:
206 fprintf(stderr, usage);
207 return 1;
211 if(do_everything) {
212 do_header = do_typelib = do_proxies = do_client = do_server = 1;
214 if(optind < argc) {
215 input_name = xstrdup(argv[optind]);
217 else {
218 fprintf(stderr, usage);
219 return 1;
222 if(debuglevel)
224 setbuf(stdout,0);
225 setbuf(stderr,0);
228 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
229 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
231 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
232 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
233 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
235 if (!header_name) {
236 header_name = dup_basename(input_name, ".idl");
237 strcat(header_name, ".h");
240 if (!typelib_name && do_typelib) {
241 typelib_name = dup_basename(input_name, ".idl");
242 strcat(typelib_name, ".tlb");
245 if (!proxy_name && do_proxies) {
246 proxy_name = dup_basename(input_name, ".idl");
247 proxy_token = xstrdup(proxy_name);
248 strcat(proxy_name, "_p.c");
251 if (!client_name && do_client) {
252 client_name = dup_basename(input_name, ".idl");
253 client_token = xstrdup(client_name);
254 strcat(client_name, "_c.c");
257 if (!server_name && do_server) {
258 server_name = dup_basename(input_name, ".idl");
259 server_token = xstrdup(server_name);
260 strcat(server_name, "_s.c");
263 wpp_add_cmdline_define("__WIDL__");
265 atexit(rm_tempfile);
266 if (!no_preprocess)
268 chat("Starting preprocess\n");
270 if (!preprocess_only)
272 ret = wpp_parse_temp( input_name, header_name, &temp_name );
274 else
276 ret = wpp_parse( input_name, stdout );
279 if(ret) exit(1);
280 if(preprocess_only) exit(0);
281 if(!(yyin = fopen(temp_name, "r"))) {
282 fprintf(stderr, "Could not open %s for input\n", temp_name);
283 return 1;
286 else {
287 if(!(yyin = fopen(input_name, "r"))) {
288 fprintf(stderr, "Could not open %s for input\n", input_name);
289 return 1;
293 if(do_header) {
294 header_token = make_token(header_name);
296 if(!(header = fopen(header_name, "w"))) {
297 fprintf(stderr, "Could not open %s for output\n", header_name);
298 return 1;
300 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
301 fprintf(header, "#include <rpc.h>\n" );
302 fprintf(header, "#include <rpcndr.h>\n\n" );
303 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
304 fprintf(header, "#define __WIDL_%s\n", header_token);
305 fprintf(header, "#ifdef __cplusplus\n");
306 fprintf(header, "extern \"C\" {\n");
307 fprintf(header, "#endif\n");
310 ret = yyparse();
312 if(do_header) {
313 fprintf(header, "#ifdef __cplusplus\n");
314 fprintf(header, "}\n");
315 fprintf(header, "#endif\n");
316 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
317 fclose(header);
320 fclose(yyin);
322 if(ret) {
323 exit(1);
325 header_name = NULL;
326 client_name = NULL;
327 server_name = NULL;
328 return 0;
331 static void rm_tempfile(void)
333 abort_import();
334 if(temp_name)
335 unlink(temp_name);
336 if (header_name)
337 unlink(header_name);
338 if (client_name)
339 unlink(client_name);
340 if (server_name)
341 unlink(server_name);
344 static void segvhandler(int sig)
346 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
347 fflush(stdout);
348 fflush(stderr);
349 abort();