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