GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
octave.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26// Born February 20, 1992.
27
28#if defined (HAVE_CONFIG_H)
29# include "config.h"
30#endif
31
32#include <cstdlib>
33
34#include <iostream>
35#include <string>
36
37#include "file-ops.h"
38#include "getopt-wrapper.h"
39#include "oct-env.h"
40#include "oct-error.h"
41#include "str-vec.h"
42
43#include "Cell.h"
44#include "defun.h"
45#include "display.h"
46#include "error.h"
47#include "input.h"
48#include "interpreter.h"
49#include "octave.h"
50#include "oct-hist.h"
51#include "oct-map.h"
52#include "ovl.h"
53#include "options.h"
54#include "ov.h"
55#include "parse.h"
56#include "sysdep.h"
57#include "usage.h"
58
60
62{
63 m_all_args.resize (1);
64 m_all_args[0] = "";
65}
66
67cmdline_options::cmdline_options (int argc, char **argv)
68{
69 // Save raw program arguments.
70 m_all_args = string_vector (argv, argc);
71
72 while (true)
73 {
74 int long_idx;
75
76 int optc = octave_getopt_long_wrapper (argc, argv, short_opts,
77 long_opts, &long_idx);
78
79 if (optc < 0)
80 break;
81
82 switch (optc)
83 {
84 case '?':
85 // Unrecognized option. getopt_long already printed a message about
86 // it, so we will just print the usage string and exit.
87 octave_print_terse_usage_and_exit ();
88 break;
89
90 case 'G':
91 m_gui = false;
92 break;
93
94 case 'H':
95 m_read_history_file = false;
96 break;
97
98 case 'W':
99 m_no_window_system = true;
100 break;
101
102 case 'V':
103 m_init_trace = true;
104 break;
105
106 // FIXME: Disabled debug option for parser 2023-12-29.
107 // However, uncomment and restore option if Octave adds a debug option
108 // to immediately enter debug mode for a script.
109 // case 'd':
110 // break;
111
112 case 'e':
114 {
115 if (m_code_to_eval.empty ())
116 m_code_to_eval = octave_optarg_wrapper ();
117 else
118 m_code_to_eval += (std::string (" ")
120 }
121 break;
122
123 case 'f':
124 m_read_user_files = false;
125 m_read_site_files = false;
126 break;
127
128 case 'g':
129 m_gui = true;
130 break;
131
132 case 'h':
133 octave_print_verbose_usage_and_exit ();
134 break;
135
136 case 'i':
137 m_forced_interactive = true;
138 break;
139
140 case 'p':
142 m_command_line_path.push_back (octave_optarg_wrapper ());
143 break;
144
145 case 'q':
146 m_inhibit_startup_message = true;
147 break;
148
149 case 'x':
150 m_echo_commands = true;
151 break;
152
153 case 'v':
154 octave_print_version_and_exit ();
155 break;
156
159 m_docstrings_file = octave_optarg_wrapper ();
160 break;
161
164 m_doc_cache_file = octave_optarg_wrapper ();
165 break;
166
167 case EXEC_PATH_OPTION:
169 m_exec_path = octave_optarg_wrapper ();
170 break;
171
173#if defined (HAVE_QSCINTILLA)
174 m_experimental_terminal_widget = true;
175#endif
176 break;
177
180 m_image_path = octave_optarg_wrapper ();
181 break;
182
183 case INFO_FILE_OPTION:
185 m_info_file = octave_optarg_wrapper ();
186 break;
187
188 case INFO_PROG_OPTION:
190 m_info_program = octave_optarg_wrapper ();
191 break;
192
194 m_forced_line_editing = m_line_editing = true;
195 break;
196
198 m_read_user_files = false;
199 break;
200
202 m_set_initial_path = false;
203 break;
204
206 m_line_editing = false;
207 break;
208
210 m_read_site_files = false;
211 break;
212
214 m_inhibit_startup_tests = true;
215 break;
216
217 case PERSIST_OPTION:
218 m_persist = true;
219 break;
220
221 case SERVER_OPTION:
222 m_server = true;
223 break;
224
227 m_texi_macros_file = octave_optarg_wrapper ();
228 break;
229
231 m_traditional = true;
232 m_persist = true;
233 break;
234
235 default:
236 // getopt_long should print a message about unrecognized options and
237 // return '?', which is handled above. If we end up here, it is
238 // because there was an option but we forgot to handle it.
239 // This is fatal.
240 std::cerr << "unexpected option (= " << optc << ") - please report this bug\n";
241 std::exit (EXIT_FAILURE);
242 break;
243 }
244 }
245
246 m_remaining_args = string_vector (argv+octave_optind_wrapper (),
247 argc-octave_optind_wrapper ());
248}
249
252{
254
255 m.assign ("sys_argc", sys_argc ());
256 m.assign ("sys_argv", Cell (all_args ()));
257 m.assign ("echo_commands", echo_commands ());
258 m.assign ("forced_interactive", forced_interactive ());
259 m.assign ("forced_line_editing", forced_line_editing ());
260 m.assign ("gui", gui ());
261 m.assign ("inhibit_startup_message", inhibit_startup_message ());
262 m.assign ("inhibit_startup_tests", inhibit_startup_tests ());
263 m.assign ("line_editing", line_editing ());
264 m.assign ("no_window_system", no_window_system ());
265 m.assign ("persist", persist ());
266 m.assign ("read_history_file", read_history_file ());
267 // FIXME: read_init_files deprecated in Octave 10 in favor of read_user_files
268 m.assign ("read_init_files", read_user_files ());
269 m.assign ("read_user_files", read_user_files ());
270 m.assign ("read_site_files", read_site_files ());
271 m.assign ("server", server ());
272 m.assign ("set_initial_path", set_initial_path ());
273 m.assign ("traditional", traditional ());
274 m.assign ("init_trace", init_trace ());
275 // FIXME: --verbose deprecated in Octave 10. Remove in Octave 12.
276 m.assign ("verbose", init_trace ());
277 m.assign ("code_to_eval", code_to_eval ());
278 m.assign ("command_line_path", string_vector (command_line_path ()));
279 m.assign ("docstrings_file", docstrings_file ());
280 m.assign ("doc_cache_file", doc_cache_file ());
281 m.assign ("exec_path", exec_path ());
282 m.assign ("image_path", image_path ());
283 m.assign ("info_file", info_file ());
284 m.assign ("info_program", info_program ());
285 m.assign ("texi_macros_file", texi_macros_file ());
286 m.assign ("all_args", Cell (all_args ()));
287 m.assign ("remaining_args", Cell (remaining_args ()));
288
289 return m;
290}
291
292application *application::s_instance = nullptr;
293
294application::application (int argc, char **argv)
295 : m_options (argc, argv)
296{
297 init ();
298}
299
301 : m_options (opts)
302{
303 init ();
304}
305
306// Note: Although the application destructor doesn't explicitly
307// perform any actions, it can't be declared "default" in the header
308// file if the interpreter is an incomplete type. Providing
309// an explicit definition of the destructor here is much simpler than
310// including the full declaration of interpreter in the
311// octave.h header file.
313
314void
315application::set_program_names (const std::string& pname)
316{
318
319 std::size_t pos = pname.find_last_of (sys::file_ops::dir_sep_chars ());
320
321 m_program_name = (pos != std::string::npos) ? pname.substr (pos+1) : pname;
322}
323
324void
326{
327 octave_idx_type nargs = args.numel ();
328
329 if (nargs > 0)
330 {
331 // Skip first argument (program name).
332 nargs--;
333
334 m_argv.resize (nargs);
335
336 for (octave_idx_type i = 0; i < nargs; i++)
337 m_argv[i] = args[i+1];
338 }
339}
340
341bool
343{
344 return s_instance ? s_instance->m_options.forced_interactive () : false;
345}
346
347// Provided for convenience. Will be removed once we eliminate the
348// old terminal widget.
349bool
351{
352 return (s_instance
353 ? s_instance->m_options.experimental_terminal_widget () : false);
354}
355
356bool
358{
359 return m_interpreter ? m_interpreter->is_initialized () : false;
360}
361
364{
365 if (! m_interpreter)
366 m_interpreter = std::unique_ptr<interpreter> (new interpreter (this));
367
368 return *m_interpreter;
369}
370
371void
373{
374 if (m_interpreter)
375 m_interpreter->initialize ();
376}
377
378int
380{
381 return m_interpreter ? m_interpreter->execute () : -1;
382}
383
384void
389
390void
391application::init ()
392{
393 if (s_instance)
394 throw std::runtime_error
395 ("only one Octave application object may be active");
396
397 s_instance = this;
398
399 string_vector all_args = m_options.all_args ();
400
401 set_program_names (all_args[0]);
402
403 string_vector remaining_args = m_options.remaining_args ();
404
405 std::string code_to_eval = m_options.code_to_eval ();
406
407 m_have_script_file = ! remaining_args.empty ();
408
409 m_have_eval_option_code = ! code_to_eval.empty ();
410
412 {
413 std::cerr << R"(error: --eval "CODE" and script file are mutually exclusive options)" << std::endl;
414
415 octave_print_terse_usage_and_exit ();
416 }
417
418 if (m_options.gui ())
419 {
421 {
422 std::cerr << "error: --gui and --no-window-system are mutually exclusive options" << std::endl;
423 octave_print_terse_usage_and_exit ();
424 }
425 if (! m_options.line_editing ())
426 {
427 std::cerr << "error: --gui and --no-line-editing are mutually exclusive options" << std::endl;
428 octave_print_terse_usage_and_exit ();
429 }
430 if (m_options.server ())
431 {
432 std::cerr << "error: --gui and --server are mutually exclusive options" << std::endl;
433 octave_print_terse_usage_and_exit ();
434 }
435 }
436
438 && ! m_options.persist ()
439 && ! m_options.traditional ());
440
441 // Perform system-dependent initialization here in case it is needed
442 // prior to constructing the interpreter object.
443 sysdep_init ();
444}
445
446int
448{
449 interpreter& interp = create_interpreter ();
450
451 int status = interp.execute ();
452
453 return status;
454}
455
456DEFUN (isguirunning, args, ,
457 doc: /* -*- texinfo -*-
458@deftypefn {} {@var{tf} =} isguirunning ()
459Return true if Octave is running in GUI mode and false otherwise.
460@seealso{have_window_system}
461@end deftypefn */)
462{
463 if (args.length () != 0)
464 print_usage ();
465
466 // FIXME: This isn't quite right, it just says that we intended to
467 // start the GUI, not that it is actually running.
468
470}
471
472/*
473%!assert (islogical (isguirunning ()))
474%!error <Invalid call> isguirunning (1)
475*/
476
477DEFUN (__is_multi_threaded__, args, ,
478 doc: /* -*- texinfo -*-
479@deftypefn {} {@var{tf} =} __is_multi_threaded__ ()
480Return true if Octave is running with multiple threads.
481@seealso{isguirunning}
482@end deftypefn */)
483{
484 if (args.length () != 0)
485 print_usage ();
486
487 // FIXME: This isn't quite right, it just says that we intended to
488 // start the GUI, not that it is actually running.
489
491}
492
493/*
494%!assert (islogical (__is_multi_threaded__ ()))
495%!error <Invalid call> __is_multi_threaded__ (1)
496*/
497
498DEFUN (argv, args, ,
499 doc: /* -*- texinfo -*-
500@deftypefn {} {@var{args} =} argv ()
501Return the command line arguments passed to Octave.
502
503For example, if you invoked Octave using the command
504
505@example
506octave --no-line-editing --quiet
507@end example
508
509@noindent
510@code{argv} would return a cell array of strings with the elements
511@option{--no-line-editing} and @option{--quiet}.
512
513If you write an executable Octave script, @code{argv} will return the list
514of arguments passed to the script. @xref{Executable Octave Programs}, for
515an example of how to create an executable Octave script.
516@seealso{program_name, cmdline_options}
517@end deftypefn */)
518{
519 if (args.length () != 0)
520 print_usage ();
521
522 return ovl (Cell (application::argv ()));
523}
524
525/*
526%!assert (iscellstr (argv ()))
527%!error <Invalid call> argv (1)
528*/
529
530DEFUN (cmdline_options, args, ,
531 doc: /* -*- texinfo -*-
532@deftypefn {} {@var{opt_struct} =} cmdline_options ()
533Return a structure containing detailed information about the command line
534arguments passed to Octave.
535
536Programming Note: This function provides copious amounts of information about
537Octave's parsing of command line options and may be more useful for debugging
538Octave rather than for general use.
539@seealso{argv, program_name}
540@end deftypefn */)
541{
542 if (args.length () != 0)
543 print_usage ();
544
546
547 if (! app)
548 error ("invalid application context!");
549
550 cmdline_options opts = app->options ();
551
552 return ovl (opts.as_octave_value ());
553}
554
555/*
556%!assert (isstruct (cmdline_options ()))
557%!error <Invalid call> cmdline_options (1)
558*/
559
560DEFUN (program_invocation_name, args, ,
561 doc: /* -*- texinfo -*-
562@deftypefn {} {@var{name} =} program_invocation_name ()
563Return the string that was typed at the shell prompt to run Octave.
564
565The string may include path components as well as the program filename.
566
567If executing a script from the command line (e.g., @code{octave foo.m}) or
568using an executable Octave script, the program name is set to the name of the
569script. @xref{Executable Octave Programs}, for an example of how to create an
570executable Octave script.
571@seealso{program_name, argv}
572@end deftypefn */)
573{
574 if (args.length () != 0)
575 print_usage ();
576
578}
579
580/*
581%!assert (ischar (program_invocation_name ()))
582%!error <Invalid call> program_invocation_name (1)
583*/
584
585DEFUN (program_name, args, ,
586 doc: /* -*- texinfo -*-
587@deftypefn {} {@var{name} =} program_name ()
588Return the filename component of the value returned by
589@code{program_invocation_name}.
590
591@seealso{program_invocation_name, argv}
592@end deftypefn */)
593{
594 if (args.length () != 0)
595 print_usage ();
596
597 return ovl (application::program_name ());
598}
599
600/*
601%!assert (ischar (program_name ()))
602%!error <Invalid call> program_name (1)
603*/
604
605OCTAVE_END_NAMESPACE(octave)
Definition Cell.h:41
application(const cmdline_options &opts=cmdline_options())
Definition octave.cc:300
virtual int execute_interpreter()
Definition octave.cc:379
void set_program_names(const std::string &pname)
Definition octave.cc:315
static application * app()
Definition octave.h:318
static bool forced_interactive()
Definition octave.cc:342
void intern_argv(const string_vector &args)
Definition octave.cc:325
virtual ~application()
Definition octave.cc:312
string_vector m_argv
Definition octave.h:368
cmdline_options options() const
Definition octave.h:281
bool m_have_script_file
Definition octave.h:377
static std::string program_name()
Definition octave.h:325
bool m_is_octave_program
Definition octave.h:382
static string_vector argv()
Definition octave.h:330
bool interpreter_is_initialized() const
Definition octave.cc:357
bool m_have_eval_option_code
Definition octave.h:373
std::string m_program_name
Definition octave.h:362
static bool is_gui_running()
Definition octave.h:335
virtual void initialize_interpreter()
Definition octave.cc:372
virtual interpreter & create_interpreter()
Definition octave.cc:363
bool experimental_terminal_widget() const
Definition octave.cc:350
std::unique_ptr< interpreter > m_interpreter
Definition octave.h:384
static std::string program_invocation_name()
Definition octave.h:320
cmdline_options m_options
Definition octave.h:370
std::string m_program_invocation_name
Definition octave.h:359
static bool is_multi_threaded()
Definition octave.h:340
virtual void delete_interpreter()
Definition octave.cc:385
bool line_editing() const
Definition octave.h:68
bool forced_interactive() const
Definition octave.h:61
bool experimental_terminal_widget() const
Definition octave.h:59
int sys_argc() const
Definition octave.h:54
std::string texi_macros_file() const
Definition octave.h:93
bool inhibit_startup_message() const
Definition octave.h:66
std::string doc_cache_file() const
Definition octave.h:88
bool init_trace() const
Definition octave.h:83
std::string image_path() const
Definition octave.h:90
bool read_site_files() const
Definition octave.h:73
string_vector remaining_args() const
Definition octave.h:95
bool persist() const
Definition octave.h:71
bool read_user_files() const
Definition octave.h:74
std::string info_program() const
Definition octave.h:92
bool server() const
Definition octave.h:77
bool read_history_file() const
Definition octave.h:72
std::string exec_path() const
Definition octave.h:89
bool no_window_system() const
Definition octave.h:70
std::string code_to_eval() const
Definition octave.h:84
string_vector all_args() const
Definition octave.h:94
std::string info_file() const
Definition octave.h:91
std::string docstrings_file() const
Definition octave.h:87
bool echo_commands() const
Definition octave.h:57
bool gui() const
Definition octave.h:63
bool traditional() const
Definition octave.h:79
bool set_initial_path() const
Definition octave.h:78
bool forced_line_editing() const
Definition octave.h:62
bool inhibit_startup_tests() const
Definition octave.h:64
std::list< std::string > command_line_path() const
Definition octave.h:85
octave_value as_octave_value() const
Definition octave.cc:251
void assign(const std::string &k, const octave_value &val)
Definition oct-map.h:230
void resize(octave_idx_type n, const std::string &rfv="")
Definition str-vec.h:93
bool empty() const
Definition str-vec.h:75
octave_idx_type numel() const
Definition str-vec.h:98
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage()
Definition defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition defun.h:56
void error(const char *fmt,...)
Definition error.cc:1008
int octave_getopt_long_wrapper(int argc, char **argv, const char *shortopts, const struct octave_getopt_options *longopts, int *longind)
char * octave_optarg_wrapper(void)
int octave_optind_wrapper(void)
#define INFO_FILE_OPTION
Definition options.h:45
#define NO_INIT_PATH_OPTION
Definition options.h:50
#define NO_INIT_SITE_OPTION
Definition options.h:51
#define LINE_EDITING_OPTION
Definition options.h:47
#define TRADITIONAL_OPTION
Definition options.h:57
#define INFO_PROG_OPTION
Definition options.h:46
#define BUILT_IN_DOCSTRINGS_FILE_OPTION
Definition options.h:40
#define SERVER_OPTION
Definition options.h:55
#define DOC_CACHE_FILE_OPTION
Definition options.h:41
#define EXPERIMENTAL_TERMINAL_WIDGET_OPTION
Definition options.h:43
#define NO_LINE_EDITING_OPTION
Definition options.h:52
#define TEXI_MACROS_FILE_OPTION
Definition options.h:56
#define NO_INIT_USER_OPTION
Definition options.h:49
struct octave_getopt_options long_opts[]
Definition options.h:58
#define EXEC_PATH_OPTION
Definition options.h:42
#define IMAGE_PATH_OPTION
Definition options.h:44
#define PERSIST_OPTION
Definition options.h:54
#define NO_STARTUP_TESTS_OPTION
Definition options.h:53
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217
void sysdep_init()
Definition sysdep.cc:439