Fix the case of product and company names.
[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
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>
33
34 #define WIDL_FULLVERSION "0.1"
35
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "proxy.h"
40 #include "wine/wpp.h"
41
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 (?) */
58
59 static char usage[] =
60 "Usage: widl [options...] infile.idl\n"
61 "   -b          Make headers compatible with ICOM macros\n"
62 "   -d n        Set debug level to 'n'\n"
63 "   -D id[=val] Define preprocessor identifier id=val\n"
64 "   -E          Preprocess only\n"
65 "   -h          Generate headers only\n"
66 "   -H file     Name of header file (default is infile.h)\n"
67 "   -I path     Set include search dir to path (multiple -I allowed)\n"
68 "   -N          Do not preprocess input\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"
78 ;
79
80 char version_string[] = "Wine IDL Compiler Version " WIDL_FULLVERSION "\n"
81                         "Copyright 2002 Ove Kaaven\n";
82
83 int win32 = 1;
84 int debuglevel = DEBUGLEVEL_NONE;
85
86 int pedantic = 0;
87 int preprocess_only = 0;
88 int header_only = 0;
89 int no_preprocess = 0;
90 int compat_icom = 0;
91
92 char *input_name;
93 char *header_name;
94 char *header_token;
95 char *proxy_name;
96 char *proxy_token;
97 char *temp_name;
98
99 int line_number = 1;
100
101 FILE *header;
102 FILE *proxy;
103
104 time_t now;
105
106 int getopt (int argc, char *const *argv, const char *optstring);
107 static void rm_tempfile(void);
108 static void segvhandler(int sig);
109
110 static char *make_token(const char *name)
111 {
112   char *token;
113   char *slash;
114   int i;
115
116   slash = strrchr(name, '/');
117   if (slash) name = slash + 1;
118
119   token = xstrdup(name);
120   for (i=0; token[i]; i++) {
121     if (!isalnum(token[i])) token[i] = '_';
122     else token[i] = toupper(token[i]);
123   }
124   return token;
125 }
126
127 int main(int argc,char *argv[])
128 {
129   extern char* optarg;
130   extern int   optind;
131   int optc;
132   int ret;
133
134   signal(SIGSEGV, segvhandler);
135
136   now = time(NULL);
137
138   while((optc = getopt(argc, argv, "bd:D:EhH:I:NVW")) != EOF) {
139     switch(optc) {
140     case 'b':
141       compat_icom = 1;
142       break;
143     case 'd':
144       debuglevel = strtol(optarg, NULL, 0);
145       break;
146     case 'D':
147       wpp_add_cmdline_define(optarg);
148       break;
149     case 'E':
150       preprocess_only = 1;
151       break;
152     case 'h':
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 'V':
165       printf(version_string);
166       return 0;
167     case 'W':
168       pedantic = 1;
169       break;
170     default:
171       fprintf(stderr, usage);
172       return 1;
173     }
174   }
175
176   if(optind < argc) {
177     input_name = xstrdup(argv[optind]);
178   }
179   else {
180     fprintf(stderr, usage);
181     return 1;
182   }
183
184   if(debuglevel)
185   {
186     setbuf(stdout,0);
187     setbuf(stderr,0);
188   }
189
190   yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
191   yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
192
193   wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
194                  (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
195                  (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
196
197   if (!header_name) {
198     header_name = dup_basename(input_name, ".idl");
199     strcat(header_name, ".h");
200   }
201
202   if (!proxy_name) {
203     proxy_name = dup_basename(input_name, ".idl");
204     proxy_token = xstrdup(proxy_name);
205     strcat(proxy_name, "_p.c");
206   }
207
208   wpp_add_cmdline_define("__WIDL__");
209
210   atexit(rm_tempfile);
211   if (!no_preprocess)
212   {
213     chat("Starting preprocess");
214
215     if (!preprocess_only)
216     {
217         ret = wpp_parse_temp( input_name, header_name, &temp_name );
218     }
219     else
220     {
221         ret = wpp_parse( input_name, stdout );
222     }
223
224     if(ret) exit(1);
225     if(preprocess_only) exit(0);
226     if(!(yyin = fopen(temp_name, "r"))) {
227       fprintf(stderr, "Could not open %s for input\n", temp_name);
228       return 1;
229     }
230   }
231   else {
232     if(!(yyin = fopen(input_name, "r"))) {
233       fprintf(stderr, "Could not open %s for input\n", input_name);
234       return 1;
235     }
236   }
237
238   header_token = make_token(header_name);
239
240   header = fopen(header_name, "w");
241   fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
242   fprintf(header, "#include <rpc.h>\n" );
243   fprintf(header, "#include <rpcndr.h>\n\n" );
244   fprintf(header, "#ifndef __WIDL_%s\n", header_token);
245   fprintf(header, "#define __WIDL_%s\n", header_token);
246   fprintf(header, "#ifdef __cplusplus\n");
247   fprintf(header, "extern \"C\" {\n");
248   fprintf(header, "#endif\n");
249
250   ret = yyparse();
251
252   finish_proxy();
253   fprintf(header, "#ifdef __cplusplus\n");
254   fprintf(header, "}\n");
255   fprintf(header, "#endif\n");
256   fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
257   fclose(header);
258   fclose(yyin);
259
260   if(ret) {
261     exit(1);
262   }
263   header_name = NULL;
264   return 0;
265 }
266
267 static void rm_tempfile(void)
268 {
269   abort_import();
270   if(temp_name)
271     unlink(temp_name);
272   if (header_name)
273     unlink( header_name );
274 }
275
276 static void segvhandler(int sig)
277 {
278   fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
279   fflush(stdout);
280   fflush(stderr);
281   abort();
282 }