comparison src/octave.cc @ 10195:b49d47b637b7

use getopt directly instead of the (mostly useless) prog_args wrapper class
author John W. Eaton <jwe@octave.org>
date Sat, 23 Jan 2010 12:42:48 -0500
parents a552859b009a
children b0485f5a921e
comparison
equal deleted inserted replaced
10194:d4f813c3f5ed 10195:b49d47b637b7
31 #include <cstdlib> 31 #include <cstdlib>
32 #include <cstring> 32 #include <cstring>
33 #include <ctime> 33 #include <ctime>
34 34
35 #include <iostream> 35 #include <iostream>
36
37 #include <getopt.h>
36 38
37 #include "cmd-edit.h" 39 #include "cmd-edit.h"
38 #include "f77-fcn.h" 40 #include "f77-fcn.h"
39 #include "file-ops.h" 41 #include "file-ops.h"
40 #include "file-stat.h" 42 #include "file-stat.h"
58 #include "oct-obj.h" 60 #include "oct-obj.h"
59 #include "ops.h" 61 #include "ops.h"
60 #include "toplev.h" 62 #include "toplev.h"
61 #include "parse.h" 63 #include "parse.h"
62 #include "procstream.h" 64 #include "procstream.h"
63 #include "prog-args.h"
64 #include "sighandlers.h" 65 #include "sighandlers.h"
65 #include "sysdep.h" 66 #include "sysdep.h"
66 #include "ov.h" 67 #include "ov.h"
67 #include "unwind-prot.h" 68 #include "unwind-prot.h"
68 #include "utils.h" 69 #include "utils.h"
149 #define NO_LINE_EDITING_OPTION 10 150 #define NO_LINE_EDITING_OPTION 10
150 #define NO_SITE_FILE_OPTION 11 151 #define NO_SITE_FILE_OPTION 11
151 #define NO_WINDOW_SYSTEM_OPTION 12 152 #define NO_WINDOW_SYSTEM_OPTION 12
152 #define PERSIST_OPTION 13 153 #define PERSIST_OPTION 13
153 #define TRADITIONAL_OPTION 14 154 #define TRADITIONAL_OPTION 14
154 long_options long_opts[] = 155 struct option long_opts[] =
155 { 156 {
156 { "braindead", prog_args::no_arg, 0, TRADITIONAL_OPTION }, 157 { "braindead", no_argument, 0, TRADITIONAL_OPTION },
157 { "debug", prog_args::no_arg, 0, 'd' }, 158 { "debug", no_argument, 0, 'd' },
158 { "doc-cache-file", prog_args::required_arg, 0, DOC_CACHE_FILE_OPTION }, 159 { "doc-cache-file", required_argument, 0, DOC_CACHE_FILE_OPTION },
159 { "echo-commands", prog_args::no_arg, 0, 'x' }, 160 { "echo-commands", no_argument, 0, 'x' },
160 { "eval", prog_args::required_arg, 0, EVAL_OPTION }, 161 { "eval", required_argument, 0, EVAL_OPTION },
161 { "exec-path", prog_args::required_arg, 0, EXEC_PATH_OPTION }, 162 { "exec-path", required_argument, 0, EXEC_PATH_OPTION },
162 { "help", prog_args::no_arg, 0, 'h' }, 163 { "help", no_argument, 0, 'h' },
163 { "image-path", prog_args::required_arg, 0, IMAGE_PATH_OPTION }, 164 { "image-path", required_argument, 0, IMAGE_PATH_OPTION },
164 { "info-file", prog_args::required_arg, 0, INFO_FILE_OPTION }, 165 { "info-file", required_argument, 0, INFO_FILE_OPTION },
165 { "info-program", prog_args::required_arg, 0, INFO_PROG_OPTION }, 166 { "info-program", required_argument, 0, INFO_PROG_OPTION },
166 { "interactive", prog_args::no_arg, 0, 'i' }, 167 { "interactive", no_argument, 0, 'i' },
167 { "line-editing", prog_args::no_arg, 0, LINE_EDITING_OPTION }, 168 { "line-editing", no_argument, 0, LINE_EDITING_OPTION },
168 { "no-history", prog_args::no_arg, 0, 'H' }, 169 { "no-history", no_argument, 0, 'H' },
169 { "no-init-file", prog_args::no_arg, 0, NO_INIT_FILE_OPTION }, 170 { "no-init-file", no_argument, 0, NO_INIT_FILE_OPTION },
170 { "no-init-path", prog_args::no_arg, 0, NO_INIT_PATH_OPTION }, 171 { "no-init-path", no_argument, 0, NO_INIT_PATH_OPTION },
171 { "no-line-editing", prog_args::no_arg, 0, NO_LINE_EDITING_OPTION }, 172 { "no-line-editing", no_argument, 0, NO_LINE_EDITING_OPTION },
172 { "no-site-file", prog_args::no_arg, 0, NO_SITE_FILE_OPTION }, 173 { "no-site-file", no_argument, 0, NO_SITE_FILE_OPTION },
173 { "no-window-system", prog_args::no_arg, 0, NO_WINDOW_SYSTEM_OPTION }, 174 { "no-window-system", no_argument, 0, NO_WINDOW_SYSTEM_OPTION },
174 { "norc", prog_args::no_arg, 0, 'f' }, 175 { "norc", no_argument, 0, 'f' },
175 { "path", prog_args::required_arg, 0, 'p' }, 176 { "path", required_argument, 0, 'p' },
176 { "persist", prog_args::no_arg, 0, PERSIST_OPTION }, 177 { "persist", no_argument, 0, PERSIST_OPTION },
177 { "quiet", prog_args::no_arg, 0, 'q' }, 178 { "quiet", no_argument, 0, 'q' },
178 { "silent", prog_args::no_arg, 0, 'q' }, 179 { "silent", no_argument, 0, 'q' },
179 { "traditional", prog_args::no_arg, 0, TRADITIONAL_OPTION }, 180 { "traditional", no_argument, 0, TRADITIONAL_OPTION },
180 { "verbose", prog_args::no_arg, 0, 'V' }, 181 { "verbose", no_argument, 0, 'V' },
181 { "version", prog_args::no_arg, 0, 'v' }, 182 { "version", no_argument, 0, 'v' },
182 { 0, 0, 0, 0 } 183 { 0, 0, 0, 0 }
183 }; 184 };
184 185
185 // Store the command-line options for later use. 186 // Store the command-line options for later use.
186 187
187 static void 188 static void
625 626
626 install_ops (); 627 install_ops ();
627 628
628 install_builtins (); 629 install_builtins ();
629 630
630 prog_args args (argc, argv, short_opts, long_opts);
631
632 bool forced_line_editing = false; 631 bool forced_line_editing = false;
633 632
634 bool read_history_file = true; 633 bool read_history_file = true;
635 634
636 int optc; 635 while (true)
637 while ((optc = args.get_option ()) != EOF) 636 {
638 { 637 int long_idx;
638
639 int optc = getopt_long (argc, argv, short_opts, long_opts, &long_idx);
640
641 if (optc < 0)
642 break;
643
639 switch (optc) 644 switch (optc)
640 { 645 {
641 case 'H': 646 case 'H':
642 read_history_file = false; 647 read_history_file = false;
643 bind_internal_variable ("saving_history", false); 648 bind_internal_variable ("saving_history", false);
665 case 'i': 670 case 'i':
666 forced_interactive = true; 671 forced_interactive = true;
667 break; 672 break;
668 673
669 case 'p': 674 case 'p':
670 if (args.option_argument ()) 675 if (optarg)
671 load_path::set_command_line_path (args.option_argument ()); 676 load_path::set_command_line_path (optarg);
672 break; 677 break;
673 678
674 case 'q': 679 case 'q':
675 inhibit_startup_message = true; 680 inhibit_startup_message = true;
676 break; 681 break;
685 case 'v': 690 case 'v':
686 print_version_and_exit (); 691 print_version_and_exit ();
687 break; 692 break;
688 693
689 case DOC_CACHE_FILE_OPTION: 694 case DOC_CACHE_FILE_OPTION:
690 if (args.option_argument ()) 695 if (optarg)
691 bind_internal_variable ("doc_cache_file", args.option_argument ()); 696 bind_internal_variable ("doc_cache_file", optarg);
692 break; 697 break;
693 698
694 case EVAL_OPTION: 699 case EVAL_OPTION:
695 if (args.option_argument ()) 700 if (optarg)
696 { 701 {
697 if (code_to_eval.empty ()) 702 if (code_to_eval.empty ())
698 code_to_eval = args.option_argument (); 703 code_to_eval = optarg;
699 else 704 else
700 code_to_eval += std::string (" ") + args.option_argument (); 705 code_to_eval += std::string (" ") + optarg;
701 } 706 }
702 break; 707 break;
703 708
704 case EXEC_PATH_OPTION: 709 case EXEC_PATH_OPTION:
705 if (args.option_argument ()) 710 if (optarg)
706 set_exec_path (args.option_argument ()); 711 set_exec_path (optarg);
707 break; 712 break;
708 713
709 case IMAGE_PATH_OPTION: 714 case IMAGE_PATH_OPTION:
710 if (args.option_argument ()) 715 if (optarg)
711 set_image_path (args.option_argument ()); 716 set_image_path (optarg);
712 break; 717 break;
713 718
714 case INFO_FILE_OPTION: 719 case INFO_FILE_OPTION:
715 if (args.option_argument ()) 720 if (optarg)
716 bind_internal_variable ("info_file", args.option_argument ()); 721 bind_internal_variable ("info_file", optarg);
717 break; 722 break;
718 723
719 case INFO_PROG_OPTION: 724 case INFO_PROG_OPTION:
720 if (args.option_argument ()) 725 if (optarg)
721 bind_internal_variable ("info_program", args.option_argument ()); 726 bind_internal_variable ("info_program", optarg);
722 break; 727 break;
723 728
724 case LINE_EDITING_OPTION: 729 case LINE_EDITING_OPTION:
725 forced_line_editing = true; 730 forced_line_editing = true;
726 break; 731 break;
815 820
816 // If there is an extra argument, see if it names a file to read. 821 // If there is an extra argument, see if it names a file to read.
817 // Additional arguments are taken as command line options for the 822 // Additional arguments are taken as command line options for the
818 // script. 823 // script.
819 824
820 int last_arg_idx = args.option_index (); 825 int last_arg_idx = optind;
821 826
822 int remaining_args = argc - last_arg_idx; 827 int remaining_args = argc - last_arg_idx;
823 828
824 if (! code_to_eval.empty ()) 829 if (! code_to_eval.empty ())
825 { 830 {