comparison libinterp/octave.cc @ 19559:18377fc879d0

Make cmd-line options for --eval, script file, '< script' mutually exclusive (bug #35318). * octave.cc (octave_process_command_line): Check for invalid pairs of mutually exclusive command line options. Re-format various comments to use up to 80 columns.
author Rik <rik@octave.org>
date Fri, 02 Jan 2015 18:05:24 -0800
parents b39cbe9f3bb0
children 65f4d9e1206c
comparison
equal deleted inserted replaced
19558:ffc339cea115 19559:18377fc879d0
94 94
95 // The command-line options. 95 // The command-line options.
96 static string_vector octave_argv; 96 static string_vector octave_argv;
97 97
98 // The name used to invoke Octave. 98 // The name used to invoke Octave.
99 static std::string 99 static std::string octave_program_invocation_name;
100 octave_program_invocation_name;
101 100
102 // The last component of octave_program_invocation_name. 101 // The last component of octave_program_invocation_name.
103 static std::string octave_program_name; 102 static std::string octave_program_name;
104 103
105 // TRUE means we are using readline. 104 // TRUE means we are using readline.
156 155
157 // If TRUE, ignore the window system even if it is available. 156 // If TRUE, ignore the window system even if it is available.
158 // (--no-window-system, -W) 157 // (--no-window-system, -W)
159 static bool no_window_system = false; 158 static bool no_window_system = false;
160 159
161 // The code to evaluate at startup (--eval CODE) 160 // The code to evaluate at startup
161 // (--eval CODE)
162 static std::string code_to_eval; 162 static std::string code_to_eval;
163 163
164 // If TRUE, don't exit after evaluating code given by --eval option. 164 // If TRUE, don't exit after evaluating code given by --eval option.
165 // (--persist)
165 static bool persist = false; 166 static bool persist = false;
166 167
167 // If TRUE, the GUI should be started. 168 // If TRUE, the GUI should be started.
168 static bool start_gui = false; 169 static bool start_gui = false;
169 170
170 // If TRUE use traditional settings (--traditional) 171 // If TRUE use traditional (maximally MATLAB compatible) settings
172 // (--traditional)
171 static bool traditional = false; 173 static bool traditional = false;
172 174
173 // Store the command-line options for later use. 175 // Store the command-line options for later use.
174 176
175 static void 177 static void
255 << "error: execution of " << file << " failed\n" 257 << "error: execution of " << file << " failed\n"
256 << "error: trying to make my way to a command prompt" 258 << "error: trying to make my way to a command prompt"
257 << std::endl; 259 << std::endl;
258 } 260 }
259 261
260 // Execute commands from a file and catch potential exceptions in a 262 // Execute commands from a file and catch potential exceptions in a consistent
261 // consistent way. This function should be called anywhere we might 263 // way. This function should be called anywhere we might parse and execute
262 // parse and execute commands from a file before before we have entered 264 // commands from a file before before we have entered the main loop in
263 // the main loop in toplev.cc. 265 // toplev.cc.
264 266
265 static void 267 static void
266 safe_source_file (const std::string& file_name, 268 safe_source_file (const std::string& file_name,
267 const std::string& context = std::string (), 269 const std::string& context = std::string (),
268 bool verbose = false, bool require_file = true, 270 bool verbose = false, bool require_file = true,
313 } 315 }
314 316
315 if (read_init_files) 317 if (read_init_files)
316 { 318 {
317 // Try to execute commands from $HOME/$OCTAVE_INITFILE and 319 // Try to execute commands from $HOME/$OCTAVE_INITFILE and
318 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set, .octaverc 320 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set,
319 // is assumed. 321 // .octaverc is assumed.
320 322
321 bool home_rc_already_executed = false; 323 bool home_rc_already_executed = false;
322 324
323 std::string initfile = octave_env::getenv ("OCTAVE_INITFILE"); 325 std::string initfile = octave_env::getenv ("OCTAVE_INITFILE");
324 326
471 set_liboctave_error_with_id_handler (lo_error_with_id_handler); 473 set_liboctave_error_with_id_handler (lo_error_with_id_handler);
472 set_liboctave_warning_handler (warning); 474 set_liboctave_warning_handler (warning);
473 set_liboctave_warning_with_id_handler (warning_with_id); 475 set_liboctave_warning_with_id_handler (warning_with_id);
474 } 476 }
475 477
476 // What happens on --traditional. 478 // What internal options get configured by --traditional.
477 479
478 static void 480 static void
479 maximum_braindamage (void) 481 maximum_braindamage (void)
480 { 482 {
481 persist = true; 483 persist = true;
535 break; 537 break;
536 538
537 switch (optc) 539 switch (optc)
538 { 540 {
539 case '?': 541 case '?':
540 // Unrecognized option. getopt_long already printed a 542 // Unrecognized option. getopt_long already printed a message about
541 // message about that, so we will just print the usage string 543 // it, so we will just print the usage string and exit.
542 // and exit.
543 octave_print_terse_usage_and_exit (); 544 octave_print_terse_usage_and_exit ();
544 break; 545 break;
545 546
546 case 'H': 547 case 'H':
547 Fhistory_save (octave_value (false)); 548 Fhistory_save (octave_value (false));
682 case TRADITIONAL_OPTION: 683 case TRADITIONAL_OPTION:
683 traditional = true; 684 traditional = true;
684 break; 685 break;
685 686
686 default: 687 default:
687 // getopt_long should print a message about unrecognized 688 // getopt_long should print a message about unrecognized options and
688 // options and return '?', which is handled above. So if we 689 // return '?', which is handled above. If we end up here, it is
689 // end up here, it is because there was an option but we 690 // because there was an option but we forgot to handle it.
690 // forgot to handle it. That should be fatal. 691 // That should be fatal.
691 panic_impossible (); 692 panic_impossible ();
692 break; 693 break;
693 } 694 }
694 } 695 }
695 696
697 // Check for various incompatible argument pairs
696 if (force_gui_option && no_gui_option) 698 if (force_gui_option && no_gui_option)
697 { 699 {
698 error ("error: only one of --force-gui and --no-gui may be used"); 700 error ("only one of --force-gui and --no-gui may be used");
699 701
700 octave_print_terse_usage_and_exit (); 702 octave_print_terse_usage_and_exit ();
701 } 703 }
704
705 bool script_file = (argc - optind) > 0;
706
707 if (! code_to_eval.empty () && script_file)
708 {
709 error ("--eval \"CODE\" and script file are mutually exclusive options");
710
711 octave_print_terse_usage_and_exit ();
712 }
713
714 bool redir_input = ! gnulib::isatty (fileno (stdin));
715
716 if (! code_to_eval.empty () && redir_input)
717 {
718 error ("--eval \"CODE\" and '< script' are mutually exclusive options");
719
720 octave_print_terse_usage_and_exit ();
721 }
722
723 if (script_file && redir_input)
724 {
725 error ("script file and '< script' are mutually exclusive options");
726
727 octave_print_terse_usage_and_exit ();
728 }
729
702 } 730 }
703 731
704 // EMBEDDED is declared int instead of bool because this function is 732 // EMBEDDED is declared int instead of bool because this function is
705 // declared extern "C". 733 // declared extern "C".
706 734
720 748
721 octave_thread::init (); 749 octave_thread::init ();
722 750
723 set_default_prompts (); 751 set_default_prompts ();
724 752
725 // Initialize default warning state before --traditional option may 753 // Initialize default warning state before --traditional option
726 // reset them. 754 // that may reset them.
727 755
728 initialize_default_warning_state (); 756 initialize_default_warning_state ();
729 757
730 if (traditional) 758 if (traditional)
731 maximum_braindamage (); 759 maximum_braindamage ();
732 760
733 init_signals (); 761 init_signals ();
734 762
735 octave_ieee_init (); 763 octave_ieee_init ();
736 764
737 // The idea here is to force xerbla to be referenced so that we will 765 // The idea here is to force xerbla to be referenced so that we will link to
738 // link to our own version instead of the one provided by the BLAS 766 // our own version instead of the one provided by the BLAS library. But
739 // library. But octave_NaN should never be -1, so we should never 767 // octave_NaN should never be -1, so we should never actually call xerbla.
740 // actually call xerbla.
741 768
742 if (octave_NaN == -1) 769 if (octave_NaN == -1)
743 F77_FUNC (xerbla, XERBLA) ("octave", 13 F77_CHAR_ARG_LEN (6)); 770 F77_FUNC (xerbla, XERBLA) ("octave", 13 F77_CHAR_ARG_LEN (6));
744 771
745 initialize_error_handlers (); 772 initialize_error_handlers ();
770 set_image_path (image_path); 797 set_image_path (image_path);
771 798
772 if (no_window_system) 799 if (no_window_system)
773 display_info::no_window_system (); 800 display_info::no_window_system ();
774 801
775 // Is input coming from a terminal? If so, we are probably 802 // Is input coming from a terminal? If so, we are probably interactive.
776 // interactive.
777 803
778 // If stdin is not a tty, then we are reading commands from a pipe or 804 // If stdin is not a tty, then we are reading commands from a pipe or
779 // a redirected file. 805 // a redirected file.
780 bool stdin_is_tty = gnulib::isatty (fileno (stdin)); 806 bool stdin_is_tty = gnulib::isatty (fileno (stdin));
781 807
820 execute_startup_files (); 846 execute_startup_files ();
821 847
822 if (! inhibit_startup_message && reading_startup_message_printed) 848 if (! inhibit_startup_message && reading_startup_message_printed)
823 std::cout << std::endl; 849 std::cout << std::endl;
824 850
825 // If there is an extra argument, see if it names a file to read. 851 // Execute any code specified with --eval 'CODE'
826 // Additional arguments are taken as command line options for the
827 // script.
828
829 int last_arg_idx = optind;
830
831 int remaining_args = octave_cmdline_argc - last_arg_idx;
832
833 if (! code_to_eval.empty ()) 852 if (! code_to_eval.empty ())
834 { 853 {
835 int parse_status = execute_eval_option_code (code_to_eval); 854 int parse_status = execute_eval_option_code (code_to_eval);
836
837 if (! (persist || remaining_args > 0))
838 {
839 quitting_gracefully = true;
840
841 clean_up_and_exit (parse_status || error_state ? 1 : 0);
842 }
843 }
844
845 if (remaining_args > 0)
846 {
847 // If we are running an executable script (#! /bin/octave) then
848 // we should only see the args passed to the script.
849
850 intern_argv (remaining_args, octave_cmdline_argv+last_arg_idx);
851
852 execute_command_line_file (octave_cmdline_argv[last_arg_idx]);
853 855
854 if (! persist) 856 if (! persist)
855 { 857 {
856 quitting_gracefully = true; 858 quitting_gracefully = true;
857 859
860 clean_up_and_exit (parse_status || error_state ? 1 : 0);
861 }
862 }
863
864 // If there is an extra argument, see if it names a file to read.
865 // Additional arguments are taken as command line options for the script.
866
867 int last_arg_idx = optind;
868 int remaining_args = octave_cmdline_argc - last_arg_idx;
869
870 if (remaining_args > 0)
871 {
872 // If we are running an executable script (#! /bin/octave) then
873 // we should only see the args passed to the script.
874
875 intern_argv (remaining_args, octave_cmdline_argv+last_arg_idx);
876
877 execute_command_line_file (octave_cmdline_argv[last_arg_idx]);
878
879 if (! persist)
880 {
881 quitting_gracefully = true;
882
858 clean_up_and_exit (error_state ? 1 : 0); 883 clean_up_and_exit (error_state ? 1 : 0);
859 } 884 }
860 } 885 }
861 886
862 // Avoid counting commands executed from startup files. 887 // Avoid counting commands executed from startup files.
864 command_editor::reset_current_command_number (1); 889 command_editor::reset_current_command_number (1);
865 890
866 // Now argv should have the full set of args. 891 // Now argv should have the full set of args.
867 intern_argv (octave_cmdline_argc, octave_cmdline_argv); 892 intern_argv (octave_cmdline_argc, octave_cmdline_argv);
868 893
869 // Force input to be echoed if not really interactive, but the user 894 // Force input to be echoed if not really interactive,
870 // has forced interactive behavior. 895 // but the user has forced interactive behavior.
871 896
872 if (! interactive && forced_interactive) 897 if (! interactive && forced_interactive)
873 { 898 {
874 command_editor::blink_matching_paren (false); 899 command_editor::blink_matching_paren (false);
875 900
876 // FIXME: is this the right thing to do? 901 // FIXME: is this the right thing to do?
877
878 Fecho_executing_commands (octave_value (ECHO_CMD_LINE)); 902 Fecho_executing_commands (octave_value (ECHO_CMD_LINE));
879 } 903 }
880 904
881 if (octave_embedded) 905 if (octave_embedded)
882 { 906 {
883 // FIXME: do we need to do any cleanup here before 907 // FIXME: Do we need to do any cleanup here before returning?
884 // returning? If we don't, what will happen to Octave functions 908 // If we don't, what will happen to Octave functions that have been
885 // that have been registered to execute with atexit, for example? 909 // registered to execute with atexit, for example?
886 910
887 return 1; 911 return 1;
888 } 912 }
889 913
890 int retval = main_loop (); 914 int retval = main_loop ();
918 return false; 942 return false;
919 943
920 if (persist) 944 if (persist)
921 return true; 945 return true;
922 946
923 // If stdin is not a tty, then assume we are reading commands from a 947 // If stdin is not a tty, then assume we are reading commands from a pipe or
924 // pipe or a redirected file and the GUI should not start. If this is 948 // a redirected file and the GUI should not start. If this is not the case
925 // not the case (for example, starting from a desktop "launcher" with 949 // (for example, starting from a desktop "launcher" with no terminal) and you
926 // no terminal) and you want to start the GUI, you may use the 950 // want to start the GUI, you may use the --force-gui option to start the GUI.
927 // --force-gui option to start the GUI.
928 951
929 if (! gnulib::isatty (fileno (stdin))) 952 if (! gnulib::isatty (fileno (stdin)))
930 return false; 953 return false;
931 954
932 // If we have code to eval or execute from a file, and we are going to 955 // If we have code to eval or execute from a file, and we are going to exit
933 // exit immediately after executing it, don't start the gui. 956 // immediately after executing it, don't start the gui.
934 957
935 int last_arg_idx = optind; 958 int last_arg_idx = optind;
936 int remaining_args = octave_cmdline_argc - last_arg_idx; 959 int remaining_args = octave_cmdline_argc - last_arg_idx;
937 960
938 if (! code_to_eval.empty () || remaining_args > 0) 961 if (! code_to_eval.empty () || remaining_args > 0)
939 return false; 962 return false;
940 963
941 return true; 964 return true;
942 } 965 }
943 966
944 // Return int instead of bool because this function is declared 967 // Return int instead of bool because this function is declared extern "C".
945 // extern "C".
946 968
947 int 969 int
948 octave_starting_gui (void) 970 octave_starting_gui (void)
949 { 971 {
950 start_gui = check_starting_gui (); 972 start_gui = check_starting_gui ();
973 */ 995 */
974 996
975 DEFUN (argv, args, , 997 DEFUN (argv, args, ,
976 "-*- texinfo -*-\n\ 998 "-*- texinfo -*-\n\
977 @deftypefn {Built-in Function} {} argv ()\n\ 999 @deftypefn {Built-in Function} {} argv ()\n\
978 Return the command line arguments passed to Octave. For example,\n\ 1000 Return the command line arguments passed to Octave.\n\
979 if you invoked Octave using the command\n\ 1001 \n\
1002 For example, if you invoked Octave using the command\n\
980 \n\ 1003 \n\
981 @example\n\ 1004 @example\n\
982 octave --no-line-editing --silent\n\ 1005 octave --no-line-editing --silent\n\
983 @end example\n\ 1006 @end example\n\
984 \n\ 1007 \n\
985 @noindent\n\ 1008 @noindent\n\
986 @code{argv} would return a cell array of strings with the elements\n\ 1009 @code{argv} would return a cell array of strings with the elements\n\
987 @option{--no-line-editing} and @option{--silent}.\n\ 1010 @option{--no-line-editing} and @option{--silent}.\n\
988 \n\ 1011 \n\
989 If you write an executable Octave script, @code{argv} will return the\n\ 1012 If you write an executable Octave script, @code{argv} will return the list\n\
990 list of arguments passed to the script. @xref{Executable Octave Programs},\n\ 1013 of arguments passed to the script. @xref{Executable Octave Programs}, for\n\
991 for an example of how to create an executable Octave script.\n\ 1014 an example of how to create an executable Octave script.\n\
992 @end deftypefn") 1015 @end deftypefn")
993 { 1016 {
994 octave_value retval; 1017 octave_value retval;
995 1018
996 if (args.length () == 0) 1019 if (args.length () == 0)