comctl32: A couple fixes for tab icon offsets.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "wine/wpp.h"
39 #include "header.h"
40
41 /* future options to reserve characters for: */
42 /* a = alignment of structures */
43 /* A = ACF input filename */
44 /* J = do not search standard include path */
45 /* O = generate interpreted stubs */
46 /* u = UUID file only? */
47 /* U = UUID filename */
48 /* w = select win16/win32 output (?) */
49
50 static char usage[] =
51 "Usage: widl [options...] infile.idl\n"
52 "   -c          Generate client stub\n"
53 "   -C file     Name of client stub file (default is infile_c.c)\n"
54 "   -d n        Set debug level to 'n'\n"
55 "   -D id[=val] Define preprocessor identifier id=val\n"
56 "   -E          Preprocess only\n"
57 "   -h          Generate headers\n"
58 "   -H file     Name of header file (default is infile.h)\n"
59 "   -I path     Set include search dir to path (multiple -I allowed)\n"
60 "   -N          Do not preprocess input\n"
61 "   -p          Generate proxy\n"
62 "   -P file     Name of proxy file (default is infile_p.c)\n"
63 "   -s          Generate server stub\n"
64 "   -S file     Name of server stub file (default is infile_s.c)\n"
65 "   -t          Generate typelib\n"
66 "   -T file     Name of typelib file (default is infile.tlb)\n"
67 "   -V          Print version and exit\n"
68 "   -W          Enable pedantic warnings\n"
69 "Debug level 'n' is a bitmask with following meaning:\n"
70 "    * 0x01 Tell which resource is parsed (verbose mode)\n"
71 "    * 0x02 Dump internal structures\n"
72 "    * 0x04 Create a parser trace (yydebug=1)\n"
73 "    * 0x08 Preprocessor messages\n"
74 "    * 0x10 Preprocessor lex messages\n"
75 "    * 0x20 Preprocessor yacc trace\n"
76 ;
77
78 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
79                         "Copyright 2002 Ove Kaaven\n";
80
81 int win32 = 1;
82 int debuglevel = DEBUGLEVEL_NONE;
83 int yy_flex_debug;
84
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;
94
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;
106
107 int line_number = 1;
108
109 FILE *header;
110 FILE *proxy;
111
112 time_t now;
113
114 int getopt (int argc, char *const *argv, const char *optstring);
115 static void rm_tempfile(void);
116 static void segvhandler(int sig);
117
118 static char *make_token(const char *name)
119 {
120   char *token;
121   char *slash;
122   int i;
123
124   slash = strrchr(name, '/');
125   if (slash) name = slash + 1;
126
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]);
131   }
132   return token;
133 }
134
135 /* duplicate a basename into a valid C token */
136 static char *dup_basename_token(const char *name, const char *ext)
137 {
138     char *p, *ret = dup_basename( name, ext );
139     /* map invalid characters to '_' */
140     for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
141     return ret;
142 }
143
144 int main(int argc,char *argv[])
145 {
146   extern char* optarg;
147   extern int   optind;
148   int optc;
149   int ret = 0;
150
151   signal(SIGSEGV, segvhandler);
152
153   now = time(NULL);
154
155   while((optc = getopt(argc, argv, "cC:d:D:EhH:I:NpP:sS:tT:VW")) != EOF) {
156     switch(optc) {
157     case 'c':
158       do_everything = 0;
159       do_client = 1;
160       break;
161     case 'C':
162       client_name = strdup(optarg);
163       break;
164     case 'd':
165       debuglevel = strtol(optarg, NULL, 0);
166       break;
167     case 'D':
168       wpp_add_cmdline_define(optarg);
169       break;
170     case 'E':
171       do_everything = 0;
172       preprocess_only = 1;
173       break;
174     case 'h':
175       do_everything = 0;
176       do_header = 1;
177       break;
178     case 'H':
179       header_name = strdup(optarg);
180       break;
181     case 'I':
182       wpp_add_include_path(optarg);
183       break;
184     case 'N':
185       no_preprocess = 1;
186       break;
187     case 'p':
188       do_everything = 0;
189       do_proxies = 1;
190       break;
191     case 'P':
192       proxy_name = strdup(optarg);
193       break;
194     case 's':
195       do_everything = 0;
196       do_server = 1;
197       break;
198     case 'S':
199       server_name = strdup(optarg);
200       break;
201     case 't':
202       do_everything = 0;
203       do_typelib = 1;
204       break;
205     case 'T':
206       typelib_name = strdup(optarg);
207       break;
208     case 'V':
209       printf(version_string);
210       return 0;
211     case 'W':
212       pedantic = 1;
213       break;
214     default:
215       fprintf(stderr, usage);
216       return 1;
217     }
218   }
219
220   if(do_everything) {
221       do_header = do_typelib = do_proxies = do_client = do_server = 1;
222   }
223   if(optind < argc) {
224     input_name = xstrdup(argv[optind]);
225   }
226   else {
227     fprintf(stderr, usage);
228     return 1;
229   }
230
231   if(debuglevel)
232   {
233     setbuf(stdout,0);
234     setbuf(stderr,0);
235   }
236
237   yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
238   yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
239
240   wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
241                  (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
242                  (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
243
244   if (!header_name) {
245     header_name = dup_basename(input_name, ".idl");
246     strcat(header_name, ".h");
247   }
248
249   if (!typelib_name && do_typelib) {
250     typelib_name = dup_basename(input_name, ".idl");
251     strcat(typelib_name, ".tlb");
252   }
253
254   if (!proxy_name && do_proxies) {
255     proxy_name = dup_basename(input_name, ".idl");
256     strcat(proxy_name, "_p.c");
257   }
258
259   if (!client_name && do_client) {
260     client_name = dup_basename(input_name, ".idl");
261     strcat(client_name, "_c.c");
262   }
263
264   if (!server_name && do_server) {
265     server_name = dup_basename(input_name, ".idl");
266     strcat(server_name, "_s.c");
267   }
268
269   if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
270   if (do_client) client_token = dup_basename_token(client_name,"_c.c");
271   if (do_server) server_token = dup_basename_token(server_name,"_s.c");
272
273   wpp_add_cmdline_define("__WIDL__");
274
275   atexit(rm_tempfile);
276   if (!no_preprocess)
277   {
278     chat("Starting preprocess\n");
279
280     if (!preprocess_only)
281     {
282         ret = wpp_parse_temp( input_name, header_name, &temp_name );
283     }
284     else
285     {
286         ret = wpp_parse( input_name, stdout );
287     }
288
289     if(ret) exit(1);
290     if(preprocess_only) exit(0);
291     if(!(yyin = fopen(temp_name, "r"))) {
292       fprintf(stderr, "Could not open %s for input\n", temp_name);
293       return 1;
294     }
295   }
296   else {
297     if(!(yyin = fopen(input_name, "r"))) {
298       fprintf(stderr, "Could not open %s for input\n", input_name);
299       return 1;
300     }
301   }
302
303   if(do_header) {
304     header_token = make_token(header_name);
305
306     if(!(header = fopen(header_name, "w"))) {
307       fprintf(stderr, "Could not open %s for output\n", header_name);
308       return 1;
309     }
310     fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
311     fprintf(header, "#include <rpc.h>\n" );
312     fprintf(header, "#include <rpcndr.h>\n\n" );
313     fprintf(header, "#ifndef __WIDL_%s\n", header_token);
314     fprintf(header, "#define __WIDL_%s\n", header_token);
315     fprintf(header, "#ifdef __cplusplus\n");
316     fprintf(header, "extern \"C\" {\n");
317     fprintf(header, "#endif\n");
318   }
319
320   ret = yyparse();
321
322   if(do_header) {
323     fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
324     fprintf(header, "\n");
325     write_user_types();
326     fprintf(header, "\n");
327     fprintf(header, "/* End additional prototypes */\n");
328     fprintf(header, "\n");
329     fprintf(header, "#ifdef __cplusplus\n");
330     fprintf(header, "}\n");
331     fprintf(header, "#endif\n");
332     fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
333     fclose(header);
334   }
335
336   fclose(yyin);
337
338   if(ret) {
339     exit(1);
340   }
341   header_name = NULL;
342   client_name = NULL;
343   server_name = NULL;
344   return 0;
345 }
346
347 static void rm_tempfile(void)
348 {
349   abort_import();
350   if(temp_name)
351     unlink(temp_name);
352   if (header_name)
353     unlink(header_name);
354   if (client_name)
355     unlink(client_name);
356   if (server_name)
357     unlink(server_name);
358 }
359
360 static void segvhandler(int sig)
361 {
362   fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
363   fflush(stdout);
364   fflush(stderr);
365   abort();
366 }