user32/tests: Fix some window test failures on various Windows platforms.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <signal.h>
36 #ifdef HAVE_GETOPT_H
37 # include <getopt.h>
38 #endif
39
40 #include "widl.h"
41 #include "utils.h"
42 #include "parser.h"
43 #include "wine/wpp.h"
44 #include "header.h"
45
46 /* future options to reserve characters for: */
47 /* a = alignment of structures */
48 /* A = ACF input filename */
49 /* J = do not search standard include path */
50 /* O = generate interpreted stubs */
51 /* w = select win16/win32 output (?) */
52
53 static const char usage[] =
54 "Usage: widl [options...] infile.idl\n"
55 "   or: widl [options...] --dlldata-only name1 [name2...]\n"
56 "   -c          Generate client stub\n"
57 "   -C file     Name of client stub file (default is infile_c.c)\n"
58 "   -d n        Set debug level to 'n'\n"
59 "   -D id[=val] Define preprocessor identifier id=val\n"
60 "   --dlldata=file  Name of the dlldata file (default is dlldata.c)\n"
61 "   -E          Preprocess only\n"
62 "   -h          Generate headers\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 "   --local-stubs=file  Write empty stubs for call_as/local methods to file\n"
66 "   -N          Do not preprocess input\n"
67 "   --oldnames  Use old naming conventions\n"
68 "   -p          Generate proxy\n"
69 "   -P file     Name of proxy file (default is infile_p.c)\n"
70 "   --prefix-all=p  Prefix names of client stubs / server functions with 'p'\n"
71 "   --prefix-client=p  Prefix names of client stubs with 'p'\n"
72 "   --prefix-server=p  Prefix names of server functions with 'p'\n"
73 "   -s          Generate server stub\n"
74 "   -S file     Name of server stub file (default is infile_s.c)\n"
75 "   -t          Generate typelib\n"
76 "   -T file     Name of typelib file (default is infile.tlb)\n"
77 "   -u          Generate interface identifiers file\n"
78 "   -U file     Name of interface identifiers file (default is infile_i.c)\n"
79 "   -V          Print version and exit\n"
80 "   -W          Enable pedantic warnings\n"
81 "   --win32     Only generate 32-bit code\n"
82 "   --win64     Only generate 64-bit code\n"
83 "Debug level 'n' is a bitmask with following meaning:\n"
84 "    * 0x01 Tell which resource is parsed (verbose mode)\n"
85 "    * 0x02 Dump internal structures\n"
86 "    * 0x04 Create a parser trace (yydebug=1)\n"
87 "    * 0x08 Preprocessor messages\n"
88 "    * 0x10 Preprocessor lex messages\n"
89 "    * 0x20 Preprocessor yacc trace\n"
90 ;
91
92 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
93                         "Copyright 2002 Ove Kaaven\n";
94
95 int win32 = 1;
96 int debuglevel = DEBUGLEVEL_NONE;
97 int parser_debug, yy_flex_debug;
98
99 int pedantic = 0;
100 int do_everything = 1;
101 int preprocess_only = 0;
102 int do_header = 0;
103 int do_typelib = 0;
104 int do_proxies = 0;
105 int do_client = 0;
106 int do_server = 0;
107 int do_idfile = 0;
108 int do_dlldata = 0;
109 int no_preprocess = 0;
110 int old_names = 0;
111 int do_win32 = 1;
112 int do_win64 = 1;
113
114 char *input_name;
115 char *header_name;
116 char *local_stubs_name;
117 char *header_token;
118 char *typelib_name;
119 char *dlldata_name;
120 char *proxy_name;
121 char *proxy_token;
122 char *client_name;
123 char *client_token;
124 char *server_name;
125 char *server_token;
126 char *idfile_name;
127 char *idfile_token;
128 char *temp_name;
129 const char *prefix_client = "";
130 const char *prefix_server = "";
131
132 int line_number = 1;
133
134 FILE *header;
135 FILE *idfile;
136
137 size_t pointer_size = 0;
138
139 time_t now;
140
141 enum {
142     OLDNAMES_OPTION = CHAR_MAX + 1,
143     DLLDATA_OPTION,
144     DLLDATA_ONLY_OPTION,
145     LOCAL_STUBS_OPTION,
146     PREFIX_ALL_OPTION,
147     PREFIX_CLIENT_OPTION,
148     PREFIX_SERVER_OPTION,
149     WIN32_OPTION,
150     WIN64_OPTION
151 };
152
153 static const char short_options[] =
154     "cC:d:D:EhH:I:NpP:sS:tT:uU:VW";
155 static const struct option long_options[] = {
156     { "dlldata", 1, 0, DLLDATA_OPTION },
157     { "dlldata-only", 0, 0, DLLDATA_ONLY_OPTION },
158     { "local-stubs", 1, 0, LOCAL_STUBS_OPTION },
159     { "oldnames", 0, 0, OLDNAMES_OPTION },
160     { "prefix-all", 1, 0, PREFIX_ALL_OPTION },
161     { "prefix-client", 1, 0, PREFIX_CLIENT_OPTION },
162     { "prefix-server", 1, 0, PREFIX_SERVER_OPTION },
163     { "win32", 0, 0, WIN32_OPTION },
164     { "win64", 0, 0, WIN64_OPTION },
165     { 0, 0, 0, 0 }
166 };
167
168 static void rm_tempfile(void);
169
170 static char *make_token(const char *name)
171 {
172   char *token;
173   char *slash;
174   int i;
175
176   slash = strrchr(name, '/');
177   if(!slash)
178     slash = strrchr(name, '\\');
179
180   if (slash) name = slash + 1;
181
182   token = xstrdup(name);
183   for (i=0; token[i]; i++) {
184     if (!isalnum(token[i])) token[i] = '_';
185     else token[i] = toupper(token[i]);
186   }
187   return token;
188 }
189
190 /* duplicate a basename into a valid C token */
191 static char *dup_basename_token(const char *name, const char *ext)
192 {
193     char *p, *ret = dup_basename( name, ext );
194     /* map invalid characters to '_' */
195     for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
196     return ret;
197 }
198
199 /* clean things up when aborting on a signal */
200 static void exit_on_signal( int sig )
201 {
202     exit(1);  /* this will call the atexit functions */
203 }
204
205 static void set_everything(int x)
206 {
207   do_header = x;
208   do_typelib = x;
209   do_proxies = x;
210   do_client = x;
211   do_server = x;
212   do_idfile = x;
213   do_dlldata = x;
214 }
215
216 void start_cplusplus_guard(FILE *fp)
217 {
218   fprintf(fp, "#ifdef __cplusplus\n");
219   fprintf(fp, "extern \"C\" {\n");
220   fprintf(fp, "#endif\n\n");
221 }
222
223 void end_cplusplus_guard(FILE *fp)
224 {
225   fprintf(fp, "#ifdef __cplusplus\n");
226   fprintf(fp, "}\n");
227   fprintf(fp, "#endif\n\n");
228 }
229
230 typedef struct
231 {
232   char *filename;
233   struct list link;
234 } filename_node_t;
235
236 static void add_filename_node(struct list *list, const char *name)
237 {
238   filename_node_t *node = xmalloc(sizeof *node);
239   node->filename = dup_basename( name, ".idl" );
240   list_add_tail(list, &node->link);
241 }
242
243 static void free_filename_nodes(struct list *list)
244 {
245   filename_node_t *node, *next;
246   LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
247     list_remove(&node->link);
248     free(node->filename);
249     free(node);
250   }
251 }
252
253 static void write_dlldata_list(struct list *filenames)
254 {
255   FILE *dlldata;
256   filename_node_t *node;
257
258   dlldata = fopen(dlldata_name, "w");
259   if (!dlldata)
260     error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
261
262   fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
263   fprintf(dlldata, "- Do not edit ***/\n\n");
264   fprintf(dlldata, "#include <objbase.h>\n");
265   fprintf(dlldata, "#include <rpcproxy.h>\n\n");
266   start_cplusplus_guard(dlldata);
267
268   LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
269     fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
270
271   fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
272   fprintf(dlldata, "/* Start of list */\n");
273   LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
274     fprintf(dlldata, "  REFERENCE_PROXY_FILE(%s),\n", node->filename);
275   fprintf(dlldata, "/* End of list */\n");
276   fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
277
278   fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
279   end_cplusplus_guard(dlldata);
280   fclose(dlldata);
281 }
282
283 static char *eat_space(char *s)
284 {
285   while (isspace((unsigned char) *s))
286     ++s;
287   return s;
288 }
289
290 void write_dlldata(const statement_list_t *stmts)
291 {
292   struct list filenames = LIST_INIT(filenames);
293   filename_node_t *node;
294   FILE *dlldata;
295
296   if (!do_dlldata || !need_proxy_file(stmts))
297     return;
298
299   dlldata = fopen(dlldata_name, "r");
300   if (dlldata) {
301     static char marker[] = "REFERENCE_PROXY_FILE";
302     char *line = NULL;
303     size_t len = 0;
304
305     while (widl_getline(&line, &len, dlldata)) {
306       char *start, *end;
307       start = eat_space(line);
308       if (strncmp(start, marker, sizeof marker - 1) == 0) {
309         start = eat_space(start + sizeof marker - 1);
310         if (*start != '(')
311           continue;
312         end = start = eat_space(start + 1);
313         while (*end && *end != ')')
314           ++end;
315         if (*end != ')')
316           continue;
317         while (isspace((unsigned char) end[-1]))
318           --end;
319         *end = '\0';
320         if (start < end)
321           add_filename_node(&filenames, start);
322       }
323     }
324
325     if (ferror(dlldata))
326       error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
327
328     free(line);
329     fclose(dlldata);
330   }
331
332   LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
333     if (strcmp(proxy_token, node->filename) == 0) {
334       /* We're already in the list, no need to regenerate this file.  */
335       free_filename_nodes(&filenames);
336       return;
337     }
338
339   add_filename_node(&filenames, proxy_token);
340   write_dlldata_list(&filenames);
341   free_filename_nodes(&filenames);
342 }
343
344 static void write_id_data_stmts(const statement_list_t *stmts)
345 {
346   const statement_t *stmt;
347   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
348   {
349     if (stmt->type == STMT_TYPE)
350     {
351       const type_t *type = stmt->u.type;
352       if (type->type == RPC_FC_IP)
353       {
354         const UUID *uuid;
355         if (!is_object(type->attrs) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
356           continue;
357         uuid = get_attrp(type->attrs, ATTR_UUID);
358         write_guid(idfile, is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
359                    type->name, uuid);
360       }
361       else if (type->type == RPC_FC_COCLASS)
362       {
363         const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
364         write_guid(idfile, "CLSID", type->name, uuid);
365       }
366     }
367     else if (stmt->type == STMT_LIBRARY)
368     {
369       const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
370       write_guid(idfile, "LIBID", stmt->u.lib->name, uuid);
371       write_id_data_stmts(stmt->u.lib->stmts);
372     }
373   }
374 }
375
376 void write_id_data(const statement_list_t *stmts)
377 {
378   if (!do_idfile) return;
379
380   idfile_token = make_token(idfile_name);
381
382   idfile = fopen(idfile_name, "w");
383   if (! idfile) {
384     error("Could not open %s for output\n", idfile_name);
385     return;
386   }
387
388   fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
389   fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
390   fprintf(idfile, "#include <rpc.h>\n");
391   fprintf(idfile, "#include <rpcndr.h>\n\n");
392   fprintf(idfile, "#include <initguid.h>\n\n");
393   start_cplusplus_guard(idfile);
394
395   write_id_data_stmts(stmts);
396
397   fprintf(idfile, "\n");
398   end_cplusplus_guard(idfile);
399
400   fclose(idfile);
401 }
402
403 int main(int argc,char *argv[])
404 {
405   extern char* optarg;
406   extern int   optind;
407   int optc;
408   int ret = 0;
409   int opti = 0;
410
411   signal( SIGTERM, exit_on_signal );
412   signal( SIGINT, exit_on_signal );
413 #ifdef SIGHUP
414   signal( SIGHUP, exit_on_signal );
415 #endif
416
417   now = time(NULL);
418
419   while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
420     switch(optc) {
421     case DLLDATA_OPTION:
422       dlldata_name = xstrdup(optarg);
423       break;
424     case DLLDATA_ONLY_OPTION:
425       do_everything = 0;
426       do_dlldata = 1;
427       break;
428     case LOCAL_STUBS_OPTION:
429       do_everything = 0;
430       local_stubs_name = xstrdup(optarg);
431       break;
432     case OLDNAMES_OPTION:
433       old_names = 1;
434       break;
435     case PREFIX_ALL_OPTION:
436       prefix_client = xstrdup(optarg);
437       prefix_server = xstrdup(optarg);
438       break;
439     case PREFIX_CLIENT_OPTION:
440       prefix_client = xstrdup(optarg);
441       break;
442     case PREFIX_SERVER_OPTION:
443       prefix_server = xstrdup(optarg);
444       break;
445     case WIN32_OPTION:
446       do_win32 = 1;
447       do_win64 = 0;
448       break;
449     case WIN64_OPTION:
450       do_win32 = 0;
451       do_win64 = 1;
452       break;
453     case 'c':
454       do_everything = 0;
455       do_client = 1;
456       break;
457     case 'C':
458       client_name = xstrdup(optarg);
459       break;
460     case 'd':
461       debuglevel = strtol(optarg, NULL, 0);
462       break;
463     case 'D':
464       wpp_add_cmdline_define(optarg);
465       break;
466     case 'E':
467       do_everything = 0;
468       preprocess_only = 1;
469       break;
470     case 'h':
471       do_everything = 0;
472       do_header = 1;
473       break;
474     case 'H':
475       header_name = xstrdup(optarg);
476       break;
477     case 'I':
478       wpp_add_include_path(optarg);
479       break;
480     case 'N':
481       no_preprocess = 1;
482       break;
483     case 'p':
484       do_everything = 0;
485       do_proxies = 1;
486       break;
487     case 'P':
488       proxy_name = xstrdup(optarg);
489       break;
490     case 's':
491       do_everything = 0;
492       do_server = 1;
493       break;
494     case 'S':
495       server_name = xstrdup(optarg);
496       break;
497     case 't':
498       do_everything = 0;
499       do_typelib = 1;
500       break;
501     case 'T':
502       typelib_name = xstrdup(optarg);
503       break;
504     case 'u':
505       do_everything = 0;
506       do_idfile = 1;
507       break;
508     case 'U':
509       idfile_name = xstrdup(optarg);
510       break;
511     case 'V':
512       printf("%s", version_string);
513       return 0;
514     case 'W':
515       pedantic = 1;
516       break;
517     default:
518       fprintf(stderr, "%s", usage);
519       return 1;
520     }
521   }
522
523   if(do_everything) {
524     set_everything(TRUE);
525   }
526
527   if (!dlldata_name && do_dlldata)
528     dlldata_name = xstrdup("dlldata.c");
529
530   if(optind < argc) {
531     if (do_dlldata && !do_everything) {
532       struct list filenames = LIST_INIT(filenames);
533       for ( ; optind < argc; ++optind)
534         add_filename_node(&filenames, argv[optind]);
535
536       write_dlldata_list(&filenames);
537       free_filename_nodes(&filenames);
538       return 0;
539     }
540     else if (optind != argc - 1) {
541       fprintf(stderr, "%s", usage);
542       return 1;
543     }
544     else
545       input_name = xstrdup(argv[optind]);
546   }
547   else {
548     fprintf(stderr, "%s", usage);
549     return 1;
550   }
551
552   if(debuglevel)
553   {
554     setbuf(stdout,0);
555     setbuf(stderr,0);
556   }
557
558   parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
559   yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
560
561   wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
562                  (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
563                  (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
564
565   if (!header_name) {
566     header_name = dup_basename(input_name, ".idl");
567     strcat(header_name, ".h");
568   }
569
570   if (!typelib_name && do_typelib) {
571     typelib_name = dup_basename(input_name, ".idl");
572     strcat(typelib_name, ".tlb");
573   }
574
575   if (!proxy_name && do_proxies) {
576     proxy_name = dup_basename(input_name, ".idl");
577     strcat(proxy_name, "_p.c");
578   }
579
580   if (!client_name && do_client) {
581     client_name = dup_basename(input_name, ".idl");
582     strcat(client_name, "_c.c");
583   }
584
585   if (!server_name && do_server) {
586     server_name = dup_basename(input_name, ".idl");
587     strcat(server_name, "_s.c");
588   }
589
590   if (!idfile_name && do_idfile) {
591     idfile_name = dup_basename(input_name, ".idl");
592     strcat(idfile_name, "_i.c");
593   }
594
595   if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
596   if (do_client) client_token = dup_basename_token(client_name,"_c.c");
597   if (do_server) server_token = dup_basename_token(server_name,"_s.c");
598
599   wpp_add_cmdline_define("__WIDL__");
600
601   atexit(rm_tempfile);
602   if (!no_preprocess)
603   {
604     chat("Starting preprocess\n");
605
606     if (!preprocess_only)
607     {
608         ret = wpp_parse_temp( input_name, header_name, &temp_name );
609     }
610     else
611     {
612         ret = wpp_parse( input_name, stdout );
613     }
614
615     if(ret) exit(1);
616     if(preprocess_only) exit(0);
617     if(!(parser_in = fopen(temp_name, "r"))) {
618       fprintf(stderr, "Could not open %s for input\n", temp_name);
619       return 1;
620     }
621   }
622   else {
623     if(!(parser_in = fopen(input_name, "r"))) {
624       fprintf(stderr, "Could not open %s for input\n", input_name);
625       return 1;
626     }
627   }
628
629   header_token = make_token(header_name);
630
631   init_types();
632   ret = parser_parse();
633
634   fclose(parser_in);
635
636   if(ret) {
637     exit(1);
638   }
639
640   /* Everything has been done successfully, don't delete any files.  */
641   set_everything(FALSE);
642   local_stubs_name = NULL;
643
644   return 0;
645 }
646
647 static void rm_tempfile(void)
648 {
649   abort_import();
650   if(temp_name)
651     unlink(temp_name);
652   if (do_header)
653     unlink(header_name);
654   if (local_stubs_name)
655     unlink(local_stubs_name);
656   if (do_client)
657     unlink(client_name);
658   if (do_server)
659     unlink(server_name);
660   if (do_idfile)
661     unlink(idfile_name);
662   if (do_proxies)
663     unlink(proxy_name);
664   if (do_typelib)
665     unlink(typelib_name);
666 }