Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / TAO_IDL / driver / drv_args.cpp
bloba2ad4de81e8a9ddcd6ce711884e218d059665f60
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 (void)
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 = 0;
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;
235 else
237 s = av[i] + 2;
240 ACE_OS::strcat (idl_global->local_escapes (), s);
241 ACE_OS::strcat (idl_global->local_escapes (), " ");
242 break;
243 case 'a':
244 if (av[i][2] == 'e')
246 idl_global->anon_type_diagnostic (
247 IDL_GlobalData::ANON_TYPE_ERROR);
249 else if (av[i][2] == 'w')
251 idl_global->anon_type_diagnostic (
252 IDL_GlobalData::ANON_TYPE_WARNING);
254 else if (av[i][2] == 's')
256 idl_global->anon_type_diagnostic (
257 IDL_GlobalData::ANON_TYPE_SILENT);
259 else
261 UNKNOWN_OPTION;
264 break;
265 // Temp directory for the IDL compiler to keep its files.
266 case 't':
267 if ((av[i][2] == '\0') && (i < ac - 1))
269 idl_global->append_idl_flag (av[i + 1]);
270 idl_global->temp_dir (av[i + 1]);
271 ++i;
273 else
275 UNKNOWN_OPTION;
278 break;
279 case 'D':
280 case 'U':
281 case 'I':
282 if (av[i][2] == '\0')
284 if (i < ac - 1)
286 idl_global->append_idl_flag (av[i + 1]);
288 ACE_CString arg = av[i];
289 arg += av[i + 1];
291 DRV_cpp_putarg (arg.c_str ());
292 idl_global->add_include_path (arg.substr (2).c_str (), false);
293 ++i;
295 else
297 UNKNOWN_OPTION;
300 else
302 ACE_CString arg (av[i], 2);
303 arg += av[i] + 2;
305 idl_global->add_include_path (arg.substr (2).c_str (), false);
306 DRV_cpp_putarg (arg.c_str ());
309 break;
310 case 'E':
311 if (av[i][2] != '\0')
313 UNKNOWN_OPTION;
315 idl_global->set_compile_flags (idl_global->compile_flags () |
316 IDL_CF_ONLY_PREPROC);
317 break;
318 case 'V':
319 if (av[i][2] != '\0')
321 UNKNOWN_OPTION;
323 idl_global->print_version ();
324 break;
325 case 'W':
326 if (av[i][2] == '\0')
328 if (i < ac - 1)
330 s = av[i + 1];
331 ++i;
333 else
335 UNKNOWN_OPTION;
338 else
340 s = av[i] + 2;
343 switch (*s)
345 default:
346 ACE_ERROR ((
347 LM_ERROR,
348 ACE_TEXT ("IDL: Incorrect use of -W option\n")
350 idl_global->parse_args_exit (1);
351 return;
353 case 'p':
354 if (*(s + 1) == ',')
356 DRV_prep_cpp_arg (s + 2);
359 break;
360 case 'b':
361 if (*(s + 1) == ',')
363 be_util::prep_be_arg (s + 2);
366 break;
369 break;
370 case 'Y':
371 if (av[i][2] == '\0')
373 if (i < ac - 1)
375 s = av[i + 1];
376 ++i;
378 else
380 UNKNOWN_OPTION;
383 else
385 s = av[i] + 2;
388 switch (*s)
390 case 'p':
391 if (*(s + 1) == ',')
393 idl_global->set_cpp_location (s + 2);
394 DRV_cpp_new_location (s + 2);
396 else
398 ACE_ERROR ((
399 LM_ERROR,
400 ACE_TEXT ("IDL: I don't understand")
401 ACE_TEXT (" the '-Y' option\n")
403 idl_global->parse_args_exit (1);
404 return;
407 break;
408 default:
409 ACE_ERROR ((
410 LM_ERROR,
411 ACE_TEXT ("IDL: I dont' understand the use of")
412 ACE_TEXT (" %s with the '-Y' option\n"),
413 ACE_TEXT_CHAR_TO_TCHAR (s)
415 idl_global->parse_args_exit (1);
416 return;
418 break;
419 case 'd':
420 if (av[i][2] != '\0')
422 UNKNOWN_OPTION;
424 idl_global->dump_ast ();
425 break;
426 case 'v':
427 if (av[i][2] != '\0')
429 UNKNOWN_OPTION;
431 idl_global->set_compile_flags (idl_global->compile_flags ()
432 | IDL_CF_INFORMATIVE);
433 break;
434 case 'w':
435 if (av[i][2] != '\0')
437 UNKNOWN_OPTION;
439 idl_global->set_compile_flags (idl_global->compile_flags ()
440 | IDL_CF_NOWARNINGS);
441 break;
442 case 'C':
443 // If identifiers in the same scope differ only by case...
444 if (av[i][2] == 'e')
446 // ...report an error.
447 idl_global->case_diff_error (true);
449 else if (av[i][2] == 'w')
451 // ...report a warning (default for now)
452 idl_global->case_diff_error (false);
454 else
456 UNKNOWN_OPTION;
459 break;
461 case 'u': // Old Help Option
462 if (av[i][2] != '\0')
464 UNKNOWN_OPTION;
466 idl_global->print_help ();
467 break;
469 case 'h': // Short Help Option, else let be_global process it
470 if (av[i][2] == '\0') {
471 idl_global->print_help ();
472 } else {
473 be_global->parse_args (i, av);
475 break;
477 case '-': // -- or Long Options
478 if (av[i][2] == '\0')
480 just_files = true; // Treat the following arguments as files
482 else
484 process_long_option(ac, av, i);
486 break;
488 default:
489 be_global->parse_args (i, av);
490 } // End of switch (av[i][1])
491 } // End of IF (av[i][0] == '-')
492 else
494 DRV_push_file (av[i]);
497 if (idl_global->parse_args_exit_) return; // Catch Exits
498 } // End of FOR (i = 1; i < ac; i++)
500 be_util::arg_post_proc ();
502 // Make sure the output directory is valid.
503 if (idl_global->temp_dir () == 0)
505 ACE_TCHAR tmpdir[MAXPATHLEN + 1];
507 if (ACE::get_temp_dir (tmpdir, MAXPATHLEN) == -1)
509 ACE_ERROR ((LM_ERROR,
510 ACE_TEXT ("Temporary path too long, ")
511 ACE_TEXT ("defaulting to current directory\n")));
513 ACE_OS::strcpy (tmpdir, ACE_TEXT ("."));
516 #if defined(ACE_MVS)
517 if (ACE_OS::access (tmpdir, F_OK) == -1
518 || ACE_OS::access (tmpdir, R_OK) == -1
519 || ACE_OS::access (tmpdir, W_OK) == -1)
520 #else
521 if (ACE_OS::access (tmpdir, F_OK | R_OK | W_OK) == -1)
522 #endif /* ACE_MVS */
524 ACE_ERROR ((
525 LM_ERROR,
526 ACE_TEXT ("Can't access temporary directory (%s),")
527 ACE_TEXT (" using current directory for temp files.\n"),
528 tmpdir
531 ACE_OS::strcpy (tmpdir, ACE_TEXT ("."));
532 #if defined(ACE_MVS)
533 if (ACE_OS::access (tmpdir, F_OK) == -1
534 || ACE_OS::access (tmpdir, R_OK) == -1
535 || ACE_OS::access (tmpdir, W_OK) == -1)
536 #else
537 if (ACE_OS::access (tmpdir, F_OK | R_OK | W_OK) == -1)
538 #endif /* ACE_MVS */
540 ACE_ERROR ((LM_ERROR,
541 "Error: Can't access temporary directory %s\n",
542 tmpdir));
544 throw Bailout ();
548 idl_global->temp_dir (ACE_TEXT_ALWAYS_CHAR (tmpdir));
551 DRV_cpp_post_init ();
554 void
555 print_idl_versions()
557 ACE_DEBUG ((LM_INFO,
558 ACE_TEXT ("These are the valid IDL versions this compiler will accept:\n")
560 for (int v = 1; v < IDL_VERSION_COUNT; v++)
562 ACE_DEBUG ((LM_INFO, ACE_TEXT ("%C\n"),
563 IdlVersion (static_cast<SpecificIdlVersion>(v)).to_string ()
568 void
569 process_long_option(long ac, char **av, long &i)
571 const char *long_option = av[i] + 2;
572 bool no_more_args = i + 1 >= ac;
573 if (!ACE_OS::strcmp (long_option, "idl-version"))
575 bool invalid_version = no_more_args;
576 if (no_more_args)
578 ACE_ERROR ((LM_ERROR,
579 ACE_TEXT ("--idl-version is missing a required argument, ")
580 ACE_TEXT ("the IDL version to use.\n")
583 else
585 idl_global->idl_version_.from_string (av[++i]);
586 invalid_version = !idl_global->idl_version_.is_valid ();
587 if (invalid_version)
589 ACE_ERROR ((LM_ERROR,
590 ACE_TEXT ("\"%C\" is not a valid IDL version supported\n"),
591 av[i]
595 if (invalid_version)
597 print_idl_versions ();
598 idl_global->parse_args_exit (1);
601 else if (!ACE_OS::strcmp (long_option, "syntax-only"))
603 idl_global->syntax_only_ = true;
605 else if (!ACE_OS::strcmp (long_option, "default-idl-version"))
607 ACE_DEBUG ((LM_INFO, ACE_TEXT ("%C\n"),
608 idl_global->default_idl_version_.to_string ()));
609 idl_global->parse_args_exit (0);
611 else if (!ACE_OS::strcmp (long_option, "list-idl-versions"))
613 print_idl_versions ();
614 idl_global->parse_args_exit (0);
616 else if (!ACE_OS::strcmp (long_option, "help"))
618 idl_global->print_help ();
620 else if (!ACE_OS::strcmp (long_option, "bison-trace"))
622 FE_yydebug (true);
624 else if (!ACE_OS::strcmp (long_option, "version"))
626 idl_global->print_version ();
628 else if (!ACE_OS::strcmp (long_option, "dump-builtins"))
630 idl_global->dump_builtins_ = true;
631 idl_global->dump_ast ();
633 else if (!ACE_OS::strcmp (long_option, "just-dump-builtins"))
635 // Don't use dump_ast because we need to dump before parsing any files.
636 idl_global->dump_builtins_ = true;
637 idl_global->just_dump_builtins_ = true;
639 else if (!ACE_OS::strcmp (long_option, "dump"))
641 idl_global->dump_ast ();
643 else if (!ACE_OS::strcmp (long_option, "unknown-annotations"))
645 bool invalid_argument = no_more_args;
646 if (no_more_args)
648 ACE_ERROR ((LM_ERROR,
649 ACE_TEXT ("--unknown-annotations is missing its required argument.")
652 else
654 i++;
655 if (!ACE_OS::strcmp (av[i], "warn-once"))
657 idl_global->unknown_annotations_ =
658 IDL_GlobalData::UNKNOWN_ANNOTATIONS_WARN_ONCE;
660 else if (!ACE_OS::strcmp (av[i], "warn-all"))
662 idl_global->unknown_annotations_ =
663 IDL_GlobalData::UNKNOWN_ANNOTATIONS_WARN_ALL;
665 else if (!ACE_OS::strcmp (av[i], "error"))
667 idl_global->unknown_annotations_ =
668 IDL_GlobalData::UNKNOWN_ANNOTATIONS_ERROR;
670 else if (!ACE_OS::strcmp (av[i], "ignore"))
672 idl_global->unknown_annotations_ =
673 IDL_GlobalData::UNKNOWN_ANNOTATIONS_IGNORE;
675 else
677 invalid_argument = true;
678 ACE_ERROR ((LM_ERROR,
679 ACE_TEXT ("\"%C\" is not a valid argument.\n"),
680 av[i]
684 if (invalid_argument)
686 ACE_ERROR ((LM_ERROR,
687 ACE_TEXT ("Use either \"warn-once\", \"warn-all\", ")
688 ACE_TEXT ("\"error\" or \"ignore\".\n"),
689 av[i]
691 idl_global->parse_args_exit (1);
694 else
696 be_global->parse_args (i, av);