xref: /reactos/sdk/tools/widl/widl.c (revision 995e4c82)
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 static const char usage[] =
47 "Usage: widl [options...] infile.idl\n"
48 "   or: widl [options...] --dlldata-only name1 [name2...]\n"
49 "   --acf=file         Use ACF file\n"
50 "   -app_config        Ignored, present for midl compatibility\n"
51 "   -b arch            Set the target architecture\n"
52 "   -c                 Generate client stub\n"
53 "   -d n               Set debug level to 'n'\n"
54 "   -D id[=val]        Define preprocessor identifier id=val\n"
55 "   -E                 Preprocess only\n"
56 "   --help             Display this help and exit\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 "   --local-stubs=file Write empty stubs for call_as/local methods to file\n"
61 "   -m32, -m64         Set the target architecture (Win32 or Win64)\n"
62 "   -N                 Do not preprocess input\n"
63 "   --oldnames         Use old naming conventions\n"
64 "   --oldtlb           Use old typelib (SLTG) format\n"
65 "   -o, --output=NAME  Set the output file name\n"
66 "   -Otype             Type of stubs to generate (-Os, -Oi, -Oif)\n"
67 "   -p                 Generate proxy\n"
68 "   --prefix-all=p     Prefix names of client stubs / server functions with 'p'\n"
69 "   --prefix-client=p  Prefix names of client stubs with 'p'\n"
70 "   --prefix-server=p  Prefix names of server functions with 'p'\n"
71 "   -r                 Generate registration script\n"
72 "   -robust            Ignored, present for midl compatibility\n"
73 "   --winrt            Enable Windows Runtime mode\n"
74 "   --ns_prefix        Prefix namespaces with ABI namespace\n"
75 "   -s                 Generate server stub\n"
76 "   -t                 Generate typelib\n"
77 "   -u                 Generate interface identifiers file\n"
78 "   -V                 Print version and exit\n"
79 "   -W                 Enable pedantic warnings\n"
80 "   --win32, --win64   Set the target architecture (Win32 or Win64)\n"
81 "   --win32-align n    Set win32 structure alignment to 'n'\n"
82 "   --win64-align n    Set win64 structure alignment to 'n'\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 #ifdef __i386__
96 enum target_cpu target_cpu = CPU_x86;
97 #elif defined(__x86_64__)
98 enum target_cpu target_cpu = CPU_x86_64;
99 #elif defined(__powerpc__)
100 enum target_cpu target_cpu = CPU_POWERPC;
101 #elif defined(__arm__)
102 enum target_cpu target_cpu = CPU_ARM;
103 #elif defined(__aarch64__)
104 enum target_cpu target_cpu = CPU_ARM64;
105 #else
106 #error Unsupported CPU
107 #endif
108 
109 int debuglevel = DEBUGLEVEL_NONE;
110 int parser_debug, yy_flex_debug;
111 
112 int pedantic = 0;
113 int do_everything = 1;
114 static int preprocess_only = 0;
115 int do_header = 0;
116 int do_typelib = 0;
117 int do_old_typelib = 0;
118 int do_proxies = 0;
119 int do_client = 0;
120 int do_server = 0;
121 int do_regscript = 0;
122 int do_idfile = 0;
123 int do_dlldata = 0;
124 static int no_preprocess = 0;
125 int old_names = 0;
126 int win32_packing = 8;
127 int win64_packing = 8;
128 int winrt_mode = 0;
129 int use_abi_namespace = 0;
130 static enum stub_mode stub_mode = MODE_Os;
131 
132 char *input_name;
133 char *input_idl_name;
134 char *acf_name;
135 char *header_name;
136 char *local_stubs_name;
137 char *header_token;
138 char *typelib_name;
139 char *dlldata_name;
140 char *proxy_name;
141 char *proxy_token;
142 char *client_name;
143 char *client_token;
144 char *server_name;
145 char *server_token;
146 char *regscript_name;
147 char *regscript_token;
148 static char *idfile_name;
149 char *temp_name;
150 const char *prefix_client = "";
151 const char *prefix_server = "";
152 
153 int line_number = 1;
154 
155 static FILE *idfile;
156 
157 unsigned int pointer_size = 0;
158 
159 time_t now;
160 
161 enum {
162     OLDNAMES_OPTION = CHAR_MAX + 1,
163     ACF_OPTION,
164     APP_CONFIG_OPTION,
165     DLLDATA_OPTION,
166     DLLDATA_ONLY_OPTION,
167     LOCAL_STUBS_OPTION,
168     OLD_TYPELIB_OPTION,
169     PREFIX_ALL_OPTION,
170     PREFIX_CLIENT_OPTION,
171     PREFIX_SERVER_OPTION,
172     PRINT_HELP,
173     RT_NS_PREFIX,
174     RT_OPTION,
175     ROBUST_OPTION,
176     WIN32_OPTION,
177     WIN64_OPTION,
178     WIN32_ALIGN_OPTION,
179     WIN64_ALIGN_OPTION
180 };
181 
182 static const char short_options[] =
183     "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
184 static const struct option long_options[] = {
185     { "acf", 1, NULL, ACF_OPTION },
186     { "app_config", 0, NULL, APP_CONFIG_OPTION },
187     { "dlldata", 1, NULL, DLLDATA_OPTION },
188     { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
189     { "help", 0, NULL, PRINT_HELP },
190     { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
191     { "ns_prefix", 0, NULL, RT_NS_PREFIX },
192     { "oldnames", 0, NULL, OLDNAMES_OPTION },
193     { "oldtlb", 0, NULL, OLD_TYPELIB_OPTION },
194     { "output", 0, NULL, 'o' },
195     { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
196     { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
197     { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
198     { "robust", 0, NULL, ROBUST_OPTION },
199     { "target", 0, NULL, 'b' },
200     { "winrt", 0, NULL, RT_OPTION },
201     { "win32", 0, NULL, WIN32_OPTION },
202     { "win64", 0, NULL, WIN64_OPTION },
203     { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
204     { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
205     { NULL, 0, NULL, 0 }
206 };
207 
208 static void rm_tempfile(void);
209 
get_stub_mode(void)210 enum stub_mode get_stub_mode(void)
211 {
212     /* old-style interpreted stubs are not supported on 64-bit */
213     if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
214     return stub_mode;
215 }
216 
make_token(const char * name)217 static char *make_token(const char *name)
218 {
219   char *token;
220   char *slash;
221   int i;
222 
223   slash = strrchr(name, '/');
224   if(!slash)
225     slash = strrchr(name, '\\');
226 
227   if (slash) name = slash + 1;
228 
229   token = xstrdup(name);
230   for (i=0; token[i]; i++) {
231     if (!isalnum(token[i])) token[i] = '_';
232     else token[i] = tolower(token[i]);
233   }
234   return token;
235 }
236 
237 /* duplicate a basename into a valid C token */
dup_basename_token(const char * name,const char * ext)238 static char *dup_basename_token(const char *name, const char *ext)
239 {
240     char *p, *ret = dup_basename( name, ext );
241     /* map invalid characters to '_' */
242     for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
243     return ret;
244 }
245 
add_widl_version_define(void)246 static void add_widl_version_define(void)
247 {
248     unsigned int version;
249     const char *p = PACKAGE_VERSION;
250 
251     /* major */
252     version = atoi(p) * 0x10000;
253     p = strchr(p, '.');
254 
255     /* minor */
256     if (p)
257     {
258         version += atoi(p + 1) * 0x100;
259         p = strchr(p + 1, '.');
260     }
261 
262     /* build */
263     if (p)
264         version += atoi(p + 1);
265 
266     if (version != 0)
267     {
268         char version_str[11];
269         snprintf(version_str, sizeof(version_str), "0x%x", version);
270         wpp_add_define("__WIDL__", version_str);
271     }
272     else
273         wpp_add_define("__WIDL__", NULL);
274 }
275 
276 /* set the target platform */
set_target(const char * target)277 static void set_target( const char *target )
278 {
279     static const struct
280     {
281         const char     *name;
282         enum target_cpu cpu;
283     } cpu_names[] =
284     {
285         { "i386",    CPU_x86 },
286         { "i486",    CPU_x86 },
287         { "i586",    CPU_x86 },
288         { "i686",    CPU_x86 },
289         { "i786",    CPU_x86 },
290         { "amd64",   CPU_x86_64 },
291         { "x86_64",  CPU_x86_64 },
292         { "powerpc", CPU_POWERPC },
293         { "arm",     CPU_ARM },
294         { "armv5",   CPU_ARM },
295         { "armv6",   CPU_ARM },
296         { "armv7",   CPU_ARM },
297         { "arm64",   CPU_ARM64 },
298         { "aarch64", CPU_ARM64 },
299     };
300 
301     unsigned int i;
302     char *p, *spec = xstrdup( target );
303 
304     /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
305 
306     if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
307     *p++ = 0;
308     for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
309     {
310         if (!strcmp( cpu_names[i].name, spec ))
311         {
312             target_cpu = cpu_names[i].cpu;
313             free( spec );
314             return;
315         }
316     }
317     error( "Unrecognized CPU '%s'\n", spec );
318 }
319 
320 /* clean things up when aborting on a signal */
exit_on_signal(int sig)321 static void exit_on_signal( int sig )
322 {
323     exit(1);  /* this will call the atexit functions */
324 }
325 
set_everything(int x)326 static void set_everything(int x)
327 {
328   do_header = x;
329   do_typelib = x;
330   do_old_typelib = x;
331   do_proxies = x;
332   do_client = x;
333   do_server = x;
334   do_regscript = x;
335   do_idfile = x;
336   do_dlldata = x;
337 }
338 
start_cplusplus_guard(FILE * fp)339 void start_cplusplus_guard(FILE *fp)
340 {
341   fprintf(fp, "#ifdef __cplusplus\n");
342   fprintf(fp, "extern \"C\" {\n");
343   fprintf(fp, "#endif\n\n");
344 }
345 
end_cplusplus_guard(FILE * fp)346 void end_cplusplus_guard(FILE *fp)
347 {
348   fprintf(fp, "#ifdef __cplusplus\n");
349   fprintf(fp, "}\n");
350   fprintf(fp, "#endif\n\n");
351 }
352 
353 typedef struct
354 {
355   char *filename;
356   struct list link;
357 } filename_node_t;
358 
add_filename_node(struct list * list,const char * name)359 static void add_filename_node(struct list *list, const char *name)
360 {
361   filename_node_t *node = xmalloc(sizeof *node);
362   node->filename = dup_basename( name, ".idl" );
363   list_add_tail(list, &node->link);
364 }
365 
free_filename_nodes(struct list * list)366 static void free_filename_nodes(struct list *list)
367 {
368   filename_node_t *node, *next;
369   LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
370     list_remove(&node->link);
371     free(node->filename);
372     free(node);
373   }
374 }
375 
write_dlldata_list(struct list * filenames,int define_proxy_delegation)376 static void write_dlldata_list(struct list *filenames, int define_proxy_delegation)
377 {
378   FILE *dlldata;
379   filename_node_t *node;
380 
381   dlldata = fopen(dlldata_name, "w");
382   if (!dlldata)
383     error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
384 
385   fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
386   fprintf(dlldata, "- Do not edit ***/\n\n");
387   if (define_proxy_delegation)
388       fprintf(dlldata, "#define PROXY_DELEGATION\n");
389 
390   fprintf(dlldata, "#ifdef __REACTOS__\n");
391   fprintf(dlldata, "#define WIN32_NO_STATUS\n");
392   fprintf(dlldata, "#define WIN32_LEAN_AND_MEAN\n");
393   fprintf(dlldata, "#endif\n\n");
394 
395   fprintf(dlldata, "#include <objbase.h>\n");
396   fprintf(dlldata, "#include <rpcproxy.h>\n\n");
397   start_cplusplus_guard(dlldata);
398 
399   LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
400     fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
401 
402   fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
403   fprintf(dlldata, "/* Start of list */\n");
404   LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
405     fprintf(dlldata, "  REFERENCE_PROXY_FILE(%s),\n", node->filename);
406   fprintf(dlldata, "/* End of list */\n");
407   fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
408 
409   fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
410   end_cplusplus_guard(dlldata);
411   fclose(dlldata);
412 }
413 
eat_space(char * s)414 static char *eat_space(char *s)
415 {
416   while (isspace((unsigned char) *s))
417     ++s;
418   return s;
419 }
420 
write_dlldata(const statement_list_t * stmts)421 void write_dlldata(const statement_list_t *stmts)
422 {
423   struct list filenames = LIST_INIT(filenames);
424   int define_proxy_delegation = 0;
425   filename_node_t *node;
426   FILE *dlldata;
427 
428   if (!do_dlldata || !need_proxy_file(stmts))
429     return;
430 
431   define_proxy_delegation = need_proxy_delegation(stmts);
432 
433   dlldata = fopen(dlldata_name, "r");
434   if (dlldata) {
435     static const char marker[] = "REFERENCE_PROXY_FILE";
436     static const char delegation_define[] = "#define PROXY_DELEGATION";
437     char *line = NULL;
438     size_t len = 0;
439 
440     while (widl_getline(&line, &len, dlldata)) {
441       char *start, *end;
442       start = eat_space(line);
443       if (strncmp(start, marker, sizeof marker - 1) == 0) {
444         start = eat_space(start + sizeof marker - 1);
445         if (*start != '(')
446           continue;
447         end = start = eat_space(start + 1);
448         while (*end && *end != ')')
449           ++end;
450         if (*end != ')')
451           continue;
452         while (isspace((unsigned char) end[-1]))
453           --end;
454         *end = '\0';
455         if (start < end)
456           add_filename_node(&filenames, start);
457       }else if (!define_proxy_delegation && strncmp(start, delegation_define, sizeof(delegation_define)-1)) {
458           define_proxy_delegation = 1;
459       }
460     }
461 
462     if (ferror(dlldata))
463       error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
464 
465     free(line);
466     fclose(dlldata);
467   }
468 
469   LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
470     if (strcmp(proxy_token, node->filename) == 0) {
471       /* We're already in the list, no need to regenerate this file.  */
472       free_filename_nodes(&filenames);
473       return;
474     }
475 
476   add_filename_node(&filenames, proxy_token);
477   write_dlldata_list(&filenames, define_proxy_delegation);
478   free_filename_nodes(&filenames);
479 }
480 
write_id_guid(FILE * f,const char * type,const char * guid_prefix,const char * name,const UUID * uuid)481 static void write_id_guid(FILE *f, const char *type, const char *guid_prefix, const char *name, const UUID *uuid)
482 {
483   if (!uuid) return;
484   fprintf(f, "MIDL_DEFINE_GUID(%s, %s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
485         "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
486         type, guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
487         uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
488         uuid->Data4[6], uuid->Data4[7]);
489 }
490 
write_id_data_stmts(const statement_list_t * stmts)491 static void write_id_data_stmts(const statement_list_t *stmts)
492 {
493   const statement_t *stmt;
494   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
495   {
496     if (stmt->type == STMT_TYPE)
497     {
498       const type_t *type = stmt->u.type;
499       if (type_get_type(type) == TYPE_INTERFACE)
500       {
501         const UUID *uuid;
502         if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
503           continue;
504         uuid = get_attrp(type->attrs, ATTR_UUID);
505         write_id_guid(idfile, "IID", is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
506                    type->name, uuid);
507         if (type->details.iface->async_iface)
508         {
509           uuid = get_attrp(type->details.iface->async_iface->attrs, ATTR_UUID);
510           write_id_guid(idfile, "IID", "IID", type->details.iface->async_iface->name, uuid);
511         }
512       }
513       else if (type_get_type(type) == TYPE_COCLASS)
514       {
515         const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
516         write_id_guid(idfile, "CLSID", "CLSID", type->name, uuid);
517       }
518     }
519     else if (stmt->type == STMT_LIBRARY)
520     {
521       const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
522       write_id_guid(idfile, "IID", "LIBID", stmt->u.lib->name, uuid);
523       write_id_data_stmts(stmt->u.lib->stmts);
524     }
525   }
526 }
527 
write_id_data(const statement_list_t * stmts)528 void write_id_data(const statement_list_t *stmts)
529 {
530   if (!do_idfile) return;
531 
532   idfile = fopen(idfile_name, "w");
533   if (! idfile) {
534     error("Could not open %s for output\n", idfile_name);
535     return;
536   }
537 
538   fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
539   fprintf(idfile, "from %s - Do not edit ***/\n\n", input_idl_name);
540 
541   fprintf(idfile, "#ifdef __REACTOS__\n");
542   fprintf(idfile, "#define WIN32_NO_STATUS\n");
543   fprintf(idfile, "#define WIN32_LEAN_AND_MEAN\n");
544   fprintf(idfile, "#endif\n\n");
545 
546   fprintf(idfile, "#include <rpc.h>\n");
547   fprintf(idfile, "#include <rpcndr.h>\n\n");
548 
549   fprintf(idfile, "#ifdef _MIDL_USE_GUIDDEF_\n\n");
550 
551   fprintf(idfile, "#ifndef INITGUID\n");
552   fprintf(idfile, "#define INITGUID\n");
553   fprintf(idfile, "#include <guiddef.h>\n");
554   fprintf(idfile, "#undef INITGUID\n");
555   fprintf(idfile, "#else\n");
556   fprintf(idfile, "#include <guiddef.h>\n");
557   fprintf(idfile, "#endif\n\n");
558 
559   fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
560   fprintf(idfile, "    DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)\n\n");
561 
562   fprintf(idfile, "#elif defined(__cplusplus)\n\n");
563 
564   fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
565   fprintf(idfile, "    EXTERN_C const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
566 
567   fprintf(idfile, "#else\n\n");
568 
569   fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
570   fprintf(idfile, "    const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
571 
572   fprintf(idfile, "#endif\n\n");
573   start_cplusplus_guard(idfile);
574 
575   write_id_data_stmts(stmts);
576 
577   fprintf(idfile, "\n");
578   end_cplusplus_guard(idfile);
579   fprintf(idfile, "#undef MIDL_DEFINE_GUID\n" );
580 
581   fclose(idfile);
582 }
583 
main(int argc,char * argv[])584 int main(int argc,char *argv[])
585 {
586   int optc;
587   int ret = 0;
588   int opti = 0;
589   char *output_name = NULL;
590 
591   signal( SIGTERM, exit_on_signal );
592   signal( SIGINT, exit_on_signal );
593 #ifdef SIGHUP
594   signal( SIGHUP, exit_on_signal );
595 #endif
596 
597   now = time(NULL);
598 
599   while((optc = getopt_long_only(argc, argv, short_options, long_options, &opti)) != EOF) {
600     switch(optc) {
601     case DLLDATA_OPTION:
602       dlldata_name = xstrdup(optarg);
603       break;
604     case DLLDATA_ONLY_OPTION:
605       do_everything = 0;
606       do_dlldata = 1;
607       break;
608     case LOCAL_STUBS_OPTION:
609       do_everything = 0;
610       local_stubs_name = xstrdup(optarg);
611       break;
612     case OLDNAMES_OPTION:
613       old_names = 1;
614       break;
615     case PREFIX_ALL_OPTION:
616       prefix_client = xstrdup(optarg);
617       prefix_server = xstrdup(optarg);
618       break;
619     case PREFIX_CLIENT_OPTION:
620       prefix_client = xstrdup(optarg);
621       break;
622     case PREFIX_SERVER_OPTION:
623       prefix_server = xstrdup(optarg);
624       break;
625     case PRINT_HELP:
626       fprintf(stderr, "%s", usage);
627       return 0;
628     case RT_OPTION:
629       winrt_mode = 1;
630       break;
631     case RT_NS_PREFIX:
632       use_abi_namespace = 1;
633       break;
634     case WIN32_OPTION:
635       pointer_size = 4;
636       break;
637     case WIN64_OPTION:
638       pointer_size = 8;
639       break;
640     case WIN32_ALIGN_OPTION:
641       win32_packing = strtol(optarg, NULL, 0);
642       if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
643           error("Packing must be one of 2, 4 or 8\n");
644       break;
645     case WIN64_ALIGN_OPTION:
646       win64_packing = strtol(optarg, NULL, 0);
647       if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
648           error("Packing must be one of 2, 4 or 8\n");
649       break;
650     case ACF_OPTION:
651       acf_name = xstrdup(optarg);
652       break;
653     case APP_CONFIG_OPTION:
654       /* widl does not distinguish between app_mode and default mode,
655          but we ignore this option for midl compatibility */
656       break;
657     case ROBUST_OPTION:
658         /* FIXME: Support robust option */
659         break;
660     case 'b':
661       set_target( optarg );
662       break;
663     case 'c':
664       do_everything = 0;
665       do_client = 1;
666       break;
667     case 'C':
668       client_name = xstrdup(optarg);
669       break;
670     case 'd':
671       debuglevel = strtol(optarg, NULL, 0);
672       break;
673     case 'D':
674       wpp_add_cmdline_define(optarg);
675       break;
676     case 'E':
677       do_everything = 0;
678       preprocess_only = 1;
679       break;
680     case 'h':
681       do_everything = 0;
682       do_header = 1;
683       break;
684     case 'H':
685       header_name = xstrdup(optarg);
686       break;
687     case 'I':
688       wpp_add_include_path(optarg);
689       break;
690     case 'm':
691       if (!strcmp( optarg, "32" )) pointer_size = 4;
692       else if (!strcmp( optarg, "64" )) pointer_size = 8;
693       break;
694     case 'N':
695       no_preprocess = 1;
696       break;
697     case 'o':
698       output_name = xstrdup(optarg);
699       break;
700     case 'O':
701       if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
702       else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
703       else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
704       else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
705       else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
706       else error( "Invalid argument '-O%s'\n", optarg );
707       break;
708     case 'p':
709       do_everything = 0;
710       do_proxies = 1;
711       break;
712     case 'P':
713       proxy_name = xstrdup(optarg);
714       break;
715     case 'r':
716       do_everything = 0;
717       do_regscript = 1;
718       break;
719     case 's':
720       do_everything = 0;
721       do_server = 1;
722       break;
723     case 'S':
724       server_name = xstrdup(optarg);
725       break;
726     case 't':
727       do_everything = 0;
728       do_typelib = 1;
729       break;
730     case OLD_TYPELIB_OPTION:
731       do_old_typelib = 1;
732       break;
733     case 'T':
734       typelib_name = xstrdup(optarg);
735       break;
736     case 'u':
737       do_everything = 0;
738       do_idfile = 1;
739       break;
740     case 'U':
741       idfile_name = xstrdup(optarg);
742       break;
743     case 'V':
744       printf("%s", version_string);
745       return 0;
746     case 'W':
747       pedantic = 1;
748       break;
749     default:
750       fprintf(stderr, "%s", usage);
751       return 1;
752     }
753   }
754 
755 #ifdef DEFAULT_INCLUDE_DIR
756   wpp_add_include_path(DEFAULT_INCLUDE_DIR);
757 #endif
758 
759   switch (target_cpu)
760   {
761   case CPU_x86:
762       if (pointer_size == 8) target_cpu = CPU_x86_64;
763       else pointer_size = 4;
764       break;
765   case CPU_x86_64:
766       if (pointer_size == 4) target_cpu = CPU_x86;
767       else pointer_size = 8;
768       break;
769   case CPU_ARM64:
770       if (pointer_size == 4) error( "Cannot build 32-bit code for this CPU\n" );
771       pointer_size = 8;
772       break;
773   default:
774       if (pointer_size == 8) error( "Cannot build 64-bit code for this CPU\n" );
775       pointer_size = 4;
776       break;
777   }
778 
779   /* if nothing specified, try to guess output type from the output file name */
780   if (output_name && do_everything && !do_header && !do_typelib && !do_proxies &&
781       !do_client && !do_server && !do_regscript && !do_idfile && !do_dlldata)
782   {
783       do_everything = 0;
784       if (strendswith( output_name, ".h" )) do_header = 1;
785       else if (strendswith( output_name, ".tlb" )) do_typelib = 1;
786       else if (strendswith( output_name, "_p.c" )) do_proxies = 1;
787       else if (strendswith( output_name, "_c.c" )) do_client = 1;
788       else if (strendswith( output_name, "_s.c" )) do_server = 1;
789       else if (strendswith( output_name, "_i.c" )) do_idfile = 1;
790       else if (strendswith( output_name, "_r.res" )) do_regscript = 1;
791       else if (strendswith( output_name, "_t.res" )) do_typelib = 1;
792       else if (strendswith( output_name, "dlldata.c" )) do_dlldata = 1;
793       else do_everything = 1;
794   }
795 
796   if(do_everything) {
797     set_everything(TRUE);
798   }
799 
800   if (!output_name) output_name = dup_basename(input_name, ".idl");
801 
802   if (do_header + do_typelib + do_proxies + do_client +
803       do_server + do_regscript + do_idfile + do_dlldata == 1)
804   {
805       if (do_header) header_name = output_name;
806       else if (do_typelib) typelib_name = output_name;
807       else if (do_proxies) proxy_name = output_name;
808       else if (do_client) client_name = output_name;
809       else if (do_server) server_name = output_name;
810       else if (do_regscript) regscript_name = output_name;
811       else if (do_idfile) idfile_name = output_name;
812       else if (do_dlldata) dlldata_name = output_name;
813   }
814 
815   if (!dlldata_name && do_dlldata)
816     dlldata_name = xstrdup("dlldata.c");
817 
818   if(optind < argc) {
819     if (do_dlldata && !do_everything) {
820       struct list filenames = LIST_INIT(filenames);
821       for ( ; optind < argc; ++optind)
822         add_filename_node(&filenames, argv[optind]);
823 
824       write_dlldata_list(&filenames, 0 /* FIXME */ );
825       free_filename_nodes(&filenames);
826       return 0;
827     }
828     else if (optind != argc - 1) {
829       fprintf(stderr, "%s", usage);
830       return 1;
831     }
832     else
833       input_idl_name = input_name = xstrdup(argv[optind]);
834   }
835   else {
836     fprintf(stderr, "%s", usage);
837     return 1;
838   }
839 
840   if(debuglevel)
841   {
842     setbuf(stdout, NULL);
843     setbuf(stderr, NULL);
844   }
845 
846   parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
847   yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
848 
849   wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
850                  (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
851                  (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
852 
853   if (!header_name) {
854     header_name = dup_basename(input_name, ".idl");
855     strcat(header_name, ".h");
856   }
857 
858   if (!typelib_name && do_typelib) {
859     typelib_name = dup_basename(input_name, ".idl");
860     strcat(typelib_name, ".tlb");
861   }
862 
863   if (!proxy_name && do_proxies) {
864     proxy_name = dup_basename(input_name, ".idl");
865     strcat(proxy_name, "_p.c");
866   }
867 
868   if (!client_name && do_client) {
869     client_name = dup_basename(input_name, ".idl");
870     strcat(client_name, "_c.c");
871   }
872 
873   if (!server_name && do_server) {
874     server_name = dup_basename(input_name, ".idl");
875     strcat(server_name, "_s.c");
876   }
877 
878   if (!regscript_name && do_regscript) {
879     regscript_name = dup_basename(input_name, ".idl");
880     strcat(regscript_name, "_r.rgs");
881   }
882 
883   if (!idfile_name && do_idfile) {
884     idfile_name = dup_basename(input_name, ".idl");
885     strcat(idfile_name, "_i.c");
886   }
887 
888   if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
889   if (do_client) client_token = dup_basename_token(client_name,"_c.c");
890   if (do_server) server_token = dup_basename_token(server_name,"_s.c");
891   if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
892 
893   add_widl_version_define();
894   wpp_add_define("_WIN32", NULL);
895 
896   atexit(rm_tempfile);
897   if (!no_preprocess)
898   {
899     chat("Starting preprocess\n");
900 
901     if (!preprocess_only)
902     {
903         FILE *output;
904         int fd;
905         char *name = xmalloc( strlen(header_name) + 8 );
906 
907         strcpy( name, header_name );
908         strcat( name, ".XXXXXX" );
909 
910         if ((fd = mkstemps( name, 0 )) == -1)
911             error("Could not generate a temp name from %s\n", name);
912 
913         temp_name = name;
914         if (!(output = fdopen(fd, "wt")))
915             error("Could not open fd %s for writing\n", name);
916 
917         ret = wpp_parse( input_name, output );
918         fclose( output );
919     }
920     else
921     {
922         ret = wpp_parse( input_name, stdout );
923     }
924 
925     if(ret) exit(1);
926     if(preprocess_only) exit(0);
927     if(!(parser_in = fopen(temp_name, "r"))) {
928       fprintf(stderr, "Could not open %s for input\n", temp_name);
929       return 1;
930     }
931   }
932   else {
933     if(!(parser_in = fopen(input_name, "r"))) {
934       fprintf(stderr, "Could not open %s for input\n", input_name);
935       return 1;
936     }
937   }
938 
939   header_token = make_token(header_name);
940 
941   init_types();
942   ret = parser_parse();
943 
944   fclose(parser_in);
945 
946   if(ret) {
947     exit(1);
948   }
949 
950   /* Everything has been done successfully, don't delete any files.  */
951   set_everything(FALSE);
952   local_stubs_name = NULL;
953 
954   return 0;
955 }
956 
rm_tempfile(void)957 static void rm_tempfile(void)
958 {
959   abort_import();
960   if(temp_name)
961     unlink(temp_name);
962   if (do_header)
963     unlink(header_name);
964   if (local_stubs_name)
965     unlink(local_stubs_name);
966   if (do_client)
967     unlink(client_name);
968   if (do_server)
969     unlink(server_name);
970   if (do_regscript)
971     unlink(regscript_name);
972   if (do_idfile)
973     unlink(idfile_name);
974   if (do_proxies)
975     unlink(proxy_name);
976   if (do_typelib)
977     unlink(typelib_name);
978 }
979