Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / TAO_IDL / driver / drv_args.cpp
blob03c82030f93de69028059e9e1bcd12a267042616
1 /*
3 COPYRIGHT
5 Copyright 1992, 1993, 1994 Sun Microsystems, Inc. Printed in the United
6 States of America. All Rights Reserved.
8 This product is protected by copyright and distributed under the following
9 license restricting its use.
11 The Interface Definition Language Compiler Front End (CFE) is made
12 available for your use provided that you include this license and copyright
13 notice on all media and documentation and the software program in which
14 this product is incorporated in whole or part. You may copy and extend
15 functionality (but may not remove functionality) of the Interface
16 Definition Language CFE without charge, but you are not authorized to
17 license or distribute it to anyone else except as part of a product or
18 program developed by you or with the express written consent of Sun
19 Microsystems, Inc. ("Sun").
21 The names of Sun Microsystems, Inc. and any of its subsidiaries or
22 affiliates may not be used in advertising or publicity pertaining to
23 distribution of Interface Definition Language CFE as permitted herein.
25 This license is effective until terminated by Sun for failure to comply
26 with this license. Upon termination, you shall destroy or return all code
27 and documentation for the Interface Definition Language CFE.
29 INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED AS IS WITH NO WARRANTIES OF
30 ANY KIND INCLUDING THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS
31 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR ARISING FROM A COURSE OF
32 DEALING, USAGE OR TRADE PRACTICE.
34 INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED WITH NO SUPPORT AND WITHOUT
35 ANY OBLIGATION ON THE PART OF Sun OR ANY OF ITS SUBSIDIARIES OR AFFILIATES
36 TO ASSIST IN ITS USE, CORRECTION, MODIFICATION OR ENHANCEMENT.
38 SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES SHALL HAVE NO LIABILITY WITH
39 RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY
40 INTERFACE DEFINITION LANGUAGE CFE OR ANY PART THEREOF.
42 IN NO EVENT WILL SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES BE LIABLE FOR
43 ANY LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
44 DAMAGES, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46 Use, duplication, or disclosure by the government is subject to
47 restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
48 Technical Data and Computer Software clause at DFARS 252.227-7013 and FAR
49 52.227-19.
51 Sun, Sun Microsystems and the Sun logo are trademarks or registered
52 trademarks of Sun Microsystems, Inc.
54 SunSoft, Inc.
55 2550 Garcia Avenue
56 Mountain View, California 94043
58 NOTE:
60 SunOS, SunSoft, Sun, Solaris, Sun Microsystems or the Sun logo are
61 trademarks or registered trademarks of Sun Microsystems, Inc.
65 // drv_args.cpp - Argument parsing for IDL compiler main driver
67 #include <string>
69 #include "idl_defines.h"
70 #include "global_extern.h"
71 #include "drv_extern.h"
72 #include "fe_extern.h"
74 #include "be_global.h"
75 #include "be_extern.h"
76 #include "be_util.h"
78 #include "ace/OS_NS_stdio.h"
79 #include "ace/OS_NS_stdlib.h"
80 #include "ace/OS_NS_unistd.h"
81 #include "ace/os_include/os_ctype.h"
82 #include "ace/Lib_Find.h"
84 extern long DRV_nfiles;
85 extern char *DRV_files[];
87 void process_long_option(long ac, char **av, long &i);
89 // Push a file into the list of files to be processed
90 void
91 DRV_push_file (const char *s)
93 // If filenames come from the command line, there is no
94 // need to duplicate the string, but some backends have
95 // an option to recurse over a directory and add all
96 // IDL files found. In this latter case we have to
97 // duplicate the file name string.
98 DRV_files[DRV_nfiles++] = ACE::strnew (s);
101 // Prepare a CPP argument
102 static void
103 DRV_prep_cpp_arg (char *s)
105 std::string instr (s);
106 std::string newarg;
107 newarg.reserve (512);
109 for (std::string::size_type pos = instr.find_first_of (',');
110 pos != std::string::npos;
111 instr = instr.substr (pos + 1), pos = instr.find_first_of (','))
113 newarg.append (instr.substr (0, pos));
116 DRV_cpp_putarg (newarg.c_str ());
119 // Print a usage message and exit.
120 void
121 DRV_usage ()
123 ACE_DEBUG ((LM_DEBUG,
124 ACE_TEXT ("%C: usage: %C [[flag|file] ...] [-- file ...]\n"),
125 idl_global->prog_name (),
126 idl_global->prog_name ()));
128 ACE_DEBUG ((LM_DEBUG,
129 ACE_TEXT ("Legal flags:\n")
130 ACE_TEXT (" -h | --help | -u\tPrint this list and exit successfully\n")
131 ACE_TEXT (" -A...\t\t\tlocal implementation-specific escape\n")
132 ACE_TEXT (" -Cw\t\t\tWarning if identifier spellings differ ")
133 ACE_TEXT ("only in case (default is error)\n")
134 ACE_TEXT (" -Ce\t\t\tError if identifier spellings differ ")
135 ACE_TEXT ("only in case (default)\n")
136 ACE_TEXT (" -ae\t\t\tError if anonymous type is seen ")
137 ACE_TEXT ("(default)\n")
138 ACE_TEXT (" -aw\t\t\tWarning if anonymous type is seen ")
139 ACE_TEXT ("(default is error)\n")
140 ACE_TEXT (" -as\t\t\tSilences the anonymous type diagnostic ")
141 ACE_TEXT ("(default is error)\n")
142 ACE_TEXT (" -d | --dump\t\tPrints a dump of the AST and exits\n")
143 ACE_TEXT (" -Dname[=value]\t\tdefines name for preprocessor\n")
144 ACE_TEXT (" -E\t\t\truns preprocessor only, prints on stdout\n")
145 ACE_TEXT (" -Idir\t\t\tincludes dir in search path for preprocessor\n")
146 ACE_TEXT (" -t\t\t\tTemporary directory to be used")
147 ACE_TEXT (" by the IDL compiler.\n")
148 ACE_TEXT (" -Uname\t\t\tundefines name for preprocessor\n")
149 ACE_TEXT (" -v\t\t\ttraces compilation stages\n")
150 ACE_TEXT (" -V | --version\t\tprints version info then exits\n")
151 ACE_TEXT (" -w\t\t\tsuppresses IDL compiler warning messages\n")
152 ACE_TEXT (" -Wp,<arg1,...,argn>\tpasses args to preprocessor\n")
153 ACE_TEXT (" -Yp,path\t\tdefines location of preprocessor\n")
154 ACE_TEXT (" --idl-version VERSION\tSet the version of IDL to use\n")
155 ACE_TEXT (" --default-idl-version\tPrint the default IDL version and exit\n")
156 ACE_TEXT (" --list-idl-versions\tPrint IDL versions supported and exit\n")
157 ACE_TEXT (" --syntax-only\t\tJust check the syntax, do not create files\n")
158 ACE_TEXT (" --bison-trace\t\tEnable Bison Tracing (sets yydebug to 1)\n")
159 ACE_TEXT (" --dump-builtins\tDump the compiler and user defined IDL.\n")
160 ACE_TEXT (" --just-dump-builtins\tJust dump the compiler defined IDL and exit.\n")
161 ACE_TEXT (" --unknown-annotations ARG\t")
162 ACE_TEXT ("Set reaction to unknown annotations. ARG must be one of the following:\n")
163 ACE_TEXT ("\t\t\t\twarn-once\tThe default, warn once per unique local name\n")
164 ACE_TEXT ("\t\t\t\twarn-all\tWarn for all unknown annotations\n")
165 ACE_TEXT ("\t\t\t\terror\t\tCauses the process to exit with a failed return status\n")
166 ACE_TEXT ("\t\t\t\tignore\t\tDon't report unknown annotations\n")
169 be_util::usage ();
172 #define UNKNOWN_OPTION \
173 ACE_ERROR (( \
174 LM_ERROR, \
175 ACE_TEXT ("IDL: I don't understand") \
176 ACE_TEXT (" the '%s' option\n"), \
177 ACE_TEXT_CHAR_TO_TCHAR (av[i]) \
178 )); \
179 idl_global->parse_args_exit (1); \
180 return;
182 // Parse arguments on command line
183 void
184 DRV_parse_args (long ac, char **av)
186 ACE_CString buffer;
187 char *s = nullptr;
188 long i;
190 // After -- process all arguments as files
191 bool just_files = false;
193 FE_store_env_include_paths ();
194 DRV_cpp_init ();
195 idl_global->set_prog_name (av[0]);
197 for (i = 1; i < ac; i++)
199 if (!just_files && av[i][0] == '-')
201 idl_global->append_idl_flag (av[i]);
203 switch (av[i][1])
205 case 0:
206 // One or more letters expected after the dash.
207 ACE_ERROR ((
208 LM_ERROR,
209 ACE_TEXT ("IDL: Space between dash and option ")
210 ACE_TEXT ("letters not allowed\n")
212 idl_global->parse_args_exit (1);
213 return;
215 case 'A':
216 if (av[i][2] == '\0')
218 if (i < ac - 1)
220 s = av[i + 1];
221 ++i;
223 else
225 ACE_ERROR ((
226 LM_ERROR,
227 ACE_TEXT ("IDL: incorrect use of ")
228 ACE_TEXT ("the -A option\n")
230 idl_global->parse_args_exit (1);
231 return;
234 else
236 s = av[i] + 2;
239 ACE_OS::strcat (idl_global->local_escapes (), s);
240 ACE_OS::strcat (idl_global->local_escapes (), " ");
241 break;
242 case 'a':
243 if (av[i][2] == 'e')
245 idl_global->anon_type_diagnostic (
246 IDL_GlobalData::ANON_TYPE_ERROR);
248 else if (av[i][2] == 'w')
250 idl_global->anon_type_diagnostic (
251 IDL_GlobalData::ANON_TYPE_WARNING);
253 else if (av[i][2] == 's')
255 idl_global->anon_type_diagnostic (
256 IDL_GlobalData::ANON_TYPE_SILENT);
258 else
260 UNKNOWN_OPTION;
263 break;
264 // Temp directory for the IDL compiler to keep its files.
265 case 't':
266 if ((av[i][2] == '\0') && (i < ac - 1))
268 idl_global->append_idl_flag (av[i + 1]);
269 idl_global->temp_dir (av[i + 1]);
270 ++i;
272 else
274 UNKNOWN_OPTION;
277 break;
278 case 'D':
279 case 'U':
280 case 'I':
281 if (av[i][2] == '\0')
283 if (i < ac - 1)
285 idl_global->append_idl_flag (av[i + 1]);
287 ACE_CString arg = av[i];
288 arg += av[i + 1];
290 DRV_cpp_putarg (arg.c_str ());
291 idl_global->add_include_path (arg.substr (2).c_str (), false);
292 ++i;
294 else
296 UNKNOWN_OPTION;
299 else
301 ACE_CString arg (av[i], 2);
302 arg += av[i] + 2;
304 idl_global->add_include_path (arg.substr (2).c_str (), false);
305 DRV_cpp_putarg (arg.c_str ());
308 break;
309 case 'E':
310 if (av[i][2] != '\0')
312 UNKNOWN_OPTION;
314 idl_global->set_compile_flags (idl_global->compile_flags () |
315 IDL_CF_ONLY_PREPROC);
316 break;
317 case 'V':
318 if (av[i][2] != '\0')
320 UNKNOWN_OPTION;
322 idl_global->print_version ();
323 break;
324 case 'W':
325 if (av[i][2] == '\0')
327 if (i < ac - 1)
329 s = av[i + 1];
330 ++i;
332 else
334 UNKNOWN_OPTION;
337 else
339 s = av[i] + 2;
342 switch (*s)
344 default:
345 ACE_ERROR ((
346 LM_ERROR,
347 ACE_TEXT ("IDL: Incorrect use of -W option\n")
349 idl_global->parse_args_exit (1);
350 return;
352 case 'p':
353 if (*(s + 1) == ',')
355 DRV_prep_cpp_arg (s + 2);
358 break;
359 case 'b':
360 if (*(s + 1) == ',')
362 be_util::prep_be_arg (s + 2);
365 break;
368 break;
369 case 'Y':
370 if (av[i][2] == '\0')
372 if (i < ac - 1)
374 s = av[i + 1];
375 ++i;
377 else
379 UNKNOWN_OPTION;
382 else
384 s = av[i] + 2;
387 switch (*s)
389 case 'p':
390 if (*(s + 1) == ',')
392 idl_global->set_cpp_location (s + 2);
393 DRV_cpp_new_location (s + 2);
395 else
397 ACE_ERROR ((
398 LM_ERROR,
399 ACE_TEXT ("IDL: I don't understand")
400 ACE_TEXT (" the '-Y' option\n")
402 idl_global->parse_args_exit (1);
403 return;
406 break;
407 default:
408 ACE_ERROR ((
409 LM_ERROR,
410 ACE_TEXT ("IDL: I dont' understand the use of")
411 ACE_TEXT (" %s with the '-Y' option\n"),
412 ACE_TEXT_CHAR_TO_TCHAR (s)
414 idl_global->parse_args_exit (1);
415 return;
417 break;
418 case 'd':
419 if (av[i][2] != '\0')
421 UNKNOWN_OPTION;
423 idl_global->dump_ast ();
424 break;
425 case 'v':
426 if (av[i][2] != '\0')
428 UNKNOWN_OPTION;
430 idl_global->set_compile_flags (idl_global->compile_flags ()
431 | IDL_CF_INFORMATIVE);
432 break;
433 case 'w':
434 if (av[i][2] != '\0')
436 UNKNOWN_OPTION;
438 idl_global->set_compile_flags (idl_global->compile_flags ()
439 | IDL_CF_NOWARNINGS);
440 break;
441 case 'C':
442 // If identifiers in the same scope differ only by case...
443 if (av[i][2] == 'e')
445 // ...report an error.
446 idl_global->case_diff_error (true);
448 else if (av[i][2] == 'w')
450 // ...report a warning (default for now)
451 idl_global->case_diff_error (false);
453 else
455 UNKNOWN_OPTION;
458 break;
460 case 'u': // Old Help Option
461 if (av[i][2] != '\0')
463 UNKNOWN_OPTION;
465 idl_global->print_help ();
466 break;
468 case 'h': // Short Help Option, else let be_global process it
469 if (av[i][2] == '\0') {
470 idl_global->print_help ();
471 } else {
472 be_global->parse_args (i, av);
474 break;
476 case '-': // -- or Long Options
477 if (av[i][2] == '\0')
479 just_files = true; // Treat the following arguments as files
481 else
483 process_long_option(ac, av, i);
485 break;
487 default:
488 be_global->parse_args (i, av);
489 } // End of switch (av[i][1])
490 } // End of IF (av[i][0] == '-')
491 else
493 DRV_push_file (av[i]);
496 if (idl_global->parse_args_exit_) return; // Catch Exits
497 } // End of FOR (i = 1; i < ac; i++)
499 be_util::arg_post_proc ();
501 // Make sure the output directory is valid.
502 if (idl_global->temp_dir () == nullptr)
504 ACE_TCHAR tmpdir[MAXPATHLEN + 1];
506 if (ACE::get_temp_dir (tmpdir, MAXPATHLEN) == -1)
508 ACE_ERROR ((LM_ERROR,
509 ACE_TEXT ("Temporary path too long, ")
510 ACE_TEXT ("defaulting to current directory\n")));
512 ACE_OS::strcpy (tmpdir, ACE_TEXT ("."));
515 #if defined(ACE_MVS)
516 if (ACE_OS::access (tmpdir, F_OK) == -1
517 || ACE_OS::access (tmpdir, R_OK) == -1
518 || ACE_OS::access (tmpdir, W_OK) == -1)
519 #else
520 if (ACE_OS::access (tmpdir, F_OK | R_OK | W_OK) == -1)
521 #endif /* ACE_MVS */
523 ACE_ERROR ((
524 LM_ERROR,
525 ACE_TEXT ("Can't access temporary directory (%s),")
526 ACE_TEXT (" using current directory for temp files.\n"),
527 tmpdir
530 ACE_OS::strcpy (tmpdir, ACE_TEXT ("."));
531 #if defined(ACE_MVS)
532 if (ACE_OS::access (tmpdir, F_OK) == -1
533 || ACE_OS::access (tmpdir, R_OK) == -1
534 || ACE_OS::access (tmpdir, W_OK) == -1)
535 #else
536 if (ACE_OS::access (tmpdir, F_OK | R_OK | W_OK) == -1)
537 #endif /* ACE_MVS */
539 ACE_ERROR ((LM_ERROR,
540 "Error: Can't access temporary directory %s\n",
541 tmpdir));
543 throw Bailout ();
547 idl_global->temp_dir (ACE_TEXT_ALWAYS_CHAR (tmpdir));
550 DRV_cpp_post_init ();
553 void
554 print_idl_versions()
556 ACE_DEBUG ((LM_INFO,
557 ACE_TEXT ("These are the valid IDL versions this compiler will accept:\n")
559 for (int v = 1; v < IDL_VERSION_COUNT; v++)
561 ACE_DEBUG ((LM_INFO, ACE_TEXT ("%C\n"),
562 IdlVersion (static_cast<SpecificIdlVersion>(v)).to_string ()
567 void
568 process_long_option(long ac, char **av, long &i)
570 const char *long_option = av[i] + 2;
571 bool no_more_args = i + 1 >= ac;
572 if (!ACE_OS::strcmp (long_option, "idl-version"))
574 bool invalid_version = no_more_args;
575 if (no_more_args)
577 ACE_ERROR ((LM_ERROR,
578 ACE_TEXT ("--idl-version is missing a required argument, ")
579 ACE_TEXT ("the IDL version to use.\n")
582 else
584 idl_global->idl_version_.from_string (av[++i]);
585 invalid_version = !idl_global->idl_version_.is_valid ();
586 if (invalid_version)
588 ACE_ERROR ((LM_ERROR,
589 ACE_TEXT ("\"%C\" is not a valid IDL version supported\n"),
590 av[i]
594 if (invalid_version)
596 print_idl_versions ();
597 idl_global->parse_args_exit (1);
600 else if (!ACE_OS::strcmp (long_option, "syntax-only"))
602 idl_global->syntax_only_ = true;
604 else if (!ACE_OS::strcmp (long_option, "default-idl-version"))
606 ACE_DEBUG ((LM_INFO, ACE_TEXT ("%C\n"),
607 idl_global->default_idl_version_.to_string ()));
608 idl_global->parse_args_exit (0);
610 else if (!ACE_OS::strcmp (long_option, "list-idl-versions"))
612 print_idl_versions ();
613 idl_global->parse_args_exit (0);
615 else if (!ACE_OS::strcmp (long_option, "help"))
617 idl_global->print_help ();
619 else if (!ACE_OS::strcmp (long_option, "bison-trace"))
621 FE_yydebug (true);
623 else if (!ACE_OS::strcmp (long_option, "version"))
625 idl_global->print_version ();
627 else if (!ACE_OS::strcmp (long_option, "dump-builtins"))
629 idl_global->dump_builtins_ = true;
630 idl_global->dump_ast ();
632 else if (!ACE_OS::strcmp (long_option, "just-dump-builtins"))
634 // Don't use dump_ast because we need to dump before parsing any files.
635 idl_global->dump_builtins_ = true;
636 idl_global->just_dump_builtins_ = true;
638 else if (!ACE_OS::strcmp (long_option, "dump"))
640 idl_global->dump_ast ();
642 else if (!ACE_OS::strcmp (long_option, "unknown-annotations"))
644 bool invalid_argument = no_more_args;
645 if (no_more_args)
647 ACE_ERROR ((LM_ERROR,
648 ACE_TEXT ("--unknown-annotations is missing its required argument.")
651 else
653 i++;
654 if (!ACE_OS::strcmp (av[i], "warn-once"))
656 idl_global->unknown_annotations_ =
657 IDL_GlobalData::UNKNOWN_ANNOTATIONS_WARN_ONCE;
659 else if (!ACE_OS::strcmp (av[i], "warn-all"))
661 idl_global->unknown_annotations_ =
662 IDL_GlobalData::UNKNOWN_ANNOTATIONS_WARN_ALL;
664 else if (!ACE_OS::strcmp (av[i], "error"))
666 idl_global->unknown_annotations_ =
667 IDL_GlobalData::UNKNOWN_ANNOTATIONS_ERROR;
669 else if (!ACE_OS::strcmp (av[i], "ignore"))
671 idl_global->unknown_annotations_ =
672 IDL_GlobalData::UNKNOWN_ANNOTATIONS_IGNORE;
674 else
676 invalid_argument = true;
677 ACE_ERROR ((LM_ERROR,
678 ACE_TEXT ("\"%C\" is not a valid argument.\n"),
679 av[i]
683 if (invalid_argument)
685 ACE_ERROR ((LM_ERROR,
686 ACE_TEXT ("Use either \"warn-once\", \"warn-all\", ")
687 ACE_TEXT ("\"error\" or \"ignore\".\n"),
688 av[i]
690 idl_global->parse_args_exit (1);
693 else
695 be_global->parse_args (i, av);