GNU Octave 10.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-2025 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 <iostream>
33#include <string>
34
35#include "file-ops.h"
36#include "getopt-wrapper.h"
37#include "lo-error.h"
38#include "oct-env.h"
39#include "str-vec.h"
40
41#include "Cell.h"
42#include "defun.h"
43#include "display.h"
44#include "error.h"
45#include "input.h"
46#include "interpreter.h"
47#include "octave.h"
48#include "oct-hist.h"
49#include "oct-map.h"
50#include "ovl.h"
51#include "options.h"
52#include "ov.h"
53#include "parse.h"
54#include "sysdep.h"
55#include "usage.h"
56
58
60{
61 m_all_args.resize (1);
62 m_all_args[0] = "";
63}
64
65cmdline_options::cmdline_options (int argc, char **argv)
66{
67 // Save raw program arguments.
68 m_all_args = string_vector (argv, argc);
69
70 while (true)
71 {
72 int long_idx;
73
74 int optc = octave_getopt_long_wrapper (argc, argv, short_opts,
75 long_opts, &long_idx);
76
77 if (optc < 0)
78 break;
79
80 switch (optc)
81 {
82 case '?':
83 // Unrecognized option. getopt_long already printed a message about
84 // it, so we will just print the usage string and exit.
85 octave_print_terse_usage_and_exit ();
86 break;
87
88 case 'G':
89 m_gui = false;
90 break;
91
92 case 'H':
93 m_read_history_file = false;
94 break;
95
96 case 'W':
97 m_no_window_system = true;
98 break;
99
100 case 'V':
101 m_init_trace = true;
102 break;
103
104 // FIXME: Disabled debug option for parser 2023-12-29.
105 // However, uncomment and restore option if Octave adds a debug option
106 // to immediately enter debug mode for a script.
107 // case 'd':
108 // break;
109
110 case 'e':
112 {
113 if (m_code_to_eval.empty ())
114 m_code_to_eval = octave_optarg_wrapper ();
115 else
116 m_code_to_eval += (std::string (" ")
118 }
119 break;
120
121 case 'f':
122 m_read_user_files = false;
123 m_read_site_files = false;
124 break;
125
126 case 'g':
127 m_gui = true;
128 break;
129
130 case 'h':
131 octave_print_verbose_usage_and_exit ();
132 break;
133
134 case 'i':
135 m_forced_interactive = true;
136 break;
137
138 case 'p':
140 m_command_line_path.push_back (octave_optarg_wrapper ());
141 break;
142
143 case 'q':
144 m_inhibit_startup_message = true;
145 break;
146
147 case 'x':
148 m_echo_commands = true;
149 break;
150
151 case 'v':
152 octave_print_version_and_exit ();
153 break;
154
157 m_docstrings_file = octave_optarg_wrapper ();
158 break;
159
162 m_doc_cache_file = octave_optarg_wrapper ();
163 break;
164
165 case EXEC_PATH_OPTION:
167 m_exec_path = octave_optarg_wrapper ();
168 break;
169
171#if defined (HAVE_QSCINTILLA)
172 m_experimental_terminal_widget = true;
173#endif
174 break;
175
178 m_image_path = octave_optarg_wrapper ();
179 break;
180
181 case INFO_FILE_OPTION:
183 m_info_file = octave_optarg_wrapper ();
184 break;
185
186 case INFO_PROG_OPTION:
188 m_info_program = octave_optarg_wrapper ();
189 break;
190
192 m_forced_line_editing = m_line_editing = true;
193 break;
194
196 m_read_user_files = false;
197 break;
198
200 m_set_initial_path = false;
201 break;
202
204 m_line_editing = false;
205 break;
206
208 m_read_site_files = false;
209 break;
210
211 case PERSIST_OPTION:
212 m_persist = true;
213 break;
214
215 case SERVER_OPTION:
216 m_server = true;
217 break;
218
221 m_texi_macros_file = octave_optarg_wrapper ();
222 break;
223
225 m_traditional = true;
226 m_persist = true;
227 break;
228
229 default:
230 // getopt_long should print a message about unrecognized options and
231 // return '?', which is handled above. If we end up here, it is
232 // because there was an option but we forgot to handle it.
233 // That should be fatal.
234
235 error ("unexpected option (= %d) - please reportt this bug", optc);
236 break;
237 }
238 }
239
240 m_remaining_args = string_vector (argv+octave_optind_wrapper (),
241 argc-octave_optind_wrapper ());
242}
243
246{
248
249 m.assign ("sys_argc", sys_argc ());
250 m.assign ("sys_argv", Cell (all_args ()));
251 m.assign ("echo_commands", echo_commands ());
252 m.assign ("forced_interactive", forced_interactive ());
253 m.assign ("forced_line_editing", forced_line_editing ());
254 m.assign ("gui", gui ());
255 m.assign ("inhibit_startup_message", inhibit_startup_message ());
256 m.assign ("line_editing", line_editing ());
257 m.assign ("no_window_system", no_window_system ());
258 m.assign ("persist", persist ());
259 m.assign ("read_history_file", read_history_file ());
260 // FIXME: read_init_files deprecated in Octave 10 in favor of read_user_files
261 m.assign ("read_init_files", read_user_files ());
262 m.assign ("read_user_files", read_user_files ());
263 m.assign ("read_site_files", read_site_files ());
264 m.assign ("server", server ());
265 m.assign ("set_initial_path", set_initial_path ());
266 m.assign ("traditional", traditional ());
267 m.assign ("init_trace", init_trace ());
268 // FIXME: --verbose deprecated in Octave 10. Remove in Octave 12.
269 m.assign ("verbose", init_trace ());
270 m.assign ("code_to_eval", code_to_eval ());
271 m.assign ("command_line_path", string_vector (command_line_path ()));
272 m.assign ("docstrings_file", docstrings_file ());
273 m.assign ("doc_cache_file", doc_cache_file ());
274 m.assign ("exec_path", exec_path ());
275 m.assign ("image_path", image_path ());
276 m.assign ("info_file", info_file ());
277 m.assign ("info_program", info_program ());
278 m.assign ("texi_macros_file", texi_macros_file ());
279 m.assign ("all_args", Cell (all_args ()));
280 m.assign ("remaining_args", Cell (remaining_args ()));
281
282 return m;
283}
284
285application *application::s_instance = nullptr;
286
287application::application (int argc, char **argv)
288 : m_options (argc, argv)
289{
290 init ();
291}
292
294 : m_options (opts)
295{
296 init ();
297}
298
299// Note: Although the application destructor doesn't explicitly
300// perform any actions, it can't be declared "default" in the header
301// file if the interpreter is an incomplete type. Providing
302// an explicit definition of the destructor here is much simpler than
303// including the full declaration of interpreter in the
304// octave.h header file.
306
307void
308application::set_program_names (const std::string& pname)
309{
311
312 std::size_t pos = pname.find_last_of (sys::file_ops::dir_sep_chars ());
313
314 m_program_name = (pos != std::string::npos) ? pname.substr (pos+1) : pname;
315}
316
317void
319{
320 octave_idx_type nargs = args.numel ();
321
322 if (nargs > 0)
323 {
324 // Skip first argument (program name).
325 nargs--;
326
327 m_argv.resize (nargs);
328
329 for (octave_idx_type i = 0; i < nargs; i++)
330 m_argv[i] = args[i+1];
331 }
332}
333
334bool
336{
337 return s_instance ? s_instance->m_options.forced_interactive () : false;
338}
339
340// Provided for convenience. Will be removed once we eliminate the
341// old terminal widget.
342bool
344{
345 return (s_instance
346 ? s_instance->m_options.experimental_terminal_widget () : false);
347}
348
349bool
351{
352 return m_interpreter ? m_interpreter->is_initialized () : false;
353}
354
357{
358 if (! m_interpreter)
359 m_interpreter = std::unique_ptr<interpreter> (new interpreter (this));
360
361 return *m_interpreter;
362}
363
364void
366{
367 if (m_interpreter)
368 m_interpreter->initialize ();
369}
370
371int
373{
374 return m_interpreter ? m_interpreter->execute () : -1;
375}
376
377void
382
383void
384application::init ()
385{
386 if (s_instance)
387 throw std::runtime_error
388 ("only one Octave application object may be active");
389
390 s_instance = this;
391
392 string_vector all_args = m_options.all_args ();
393
394 set_program_names (all_args[0]);
395
396 string_vector remaining_args = m_options.remaining_args ();
397
398 std::string code_to_eval = m_options.code_to_eval ();
399
400 m_have_script_file = ! remaining_args.empty ();
401
402 m_have_eval_option_code = ! code_to_eval.empty ();
403
405 {
406 std::cerr << R"(error: --eval "CODE" and script file are mutually exclusive options)" << std::endl;
407
408 octave_print_terse_usage_and_exit ();
409 }
410
411 if (m_options.gui ())
412 {
414 {
415 std::cerr << "error: --gui and --no-window-system are mutually exclusive options" << std::endl;
416 octave_print_terse_usage_and_exit ();
417 }
418 if (! m_options.line_editing ())
419 {
420 std::cerr << "error: --gui and --no-line-editing are mutually exclusive options" << std::endl;
421 octave_print_terse_usage_and_exit ();
422 }
423 if (m_options.server ())
424 {
425 std::cerr << "error: --gui and --server are mutually exclusive options" << std::endl;
426 octave_print_terse_usage_and_exit ();
427 }
428 }
429
431 && ! m_options.persist ()
432 && ! m_options.traditional ());
433
434 // This should probably happen early.
435 sysdep_init ();
436}
437
438int
440{
441 interpreter& interp = create_interpreter ();
442
443 int status = interp.execute ();
444
445 return status;
446}
447
448DEFUN (isguirunning, args, ,
449 doc: /* -*- texinfo -*-
450@deftypefn {} {@var{tf} =} isguirunning ()
451Return true if Octave is running in GUI mode and false otherwise.
452@seealso{have_window_system}
453@end deftypefn */)
454{
455 if (args.length () != 0)
456 print_usage ();
457
458 // FIXME: This isn't quite right, it just says that we intended to
459 // start the GUI, not that it is actually running.
460
462}
463
464/*
465%!assert (islogical (isguirunning ()))
466%!error <Invalid call> isguirunning (1)
467*/
468
469DEFUN (__is_multi_threaded__, args, ,
470 doc: /* -*- texinfo -*-
471@deftypefn {} {@var{tf} =} __is_multi_threaded__ ()
472Return true if Octave is running with multiple threads.
473@seealso{isguirunning}
474@end deftypefn */)
475{
476 if (args.length () != 0)
477 print_usage ();
478
479 // FIXME: This isn't quite right, it just says that we intended to
480 // start the GUI, not that it is actually running.
481
483}
484
485/*
486%!assert (islogical (__is_multi_threaded__ ()))
487%!error <Invalid call> __is_multi_threaded__ (1)
488*/
489
490DEFUN (argv, args, ,
491 doc: /* -*- texinfo -*-
492@deftypefn {} {@var{args} =} argv ()
493Return the command line arguments passed to Octave.
494
495For example, if you invoked Octave using the command
496
497@example
498octave --no-line-editing --quiet
499@end example
500
501@noindent
502@code{argv} would return a cell array of strings with the elements
503@option{--no-line-editing} and @option{--quiet}.
504
505If you write an executable Octave script, @code{argv} will return the list
506of arguments passed to the script. @xref{Executable Octave Programs}, for
507an example of how to create an executable Octave script.
508@seealso{program_name, cmdline_options}
509@end deftypefn */)
510{
511 if (args.length () != 0)
512 print_usage ();
513
514 return ovl (Cell (application::argv ()));
515}
516
517/*
518%!assert (iscellstr (argv ()))
519%!error <Invalid call> argv (1)
520*/
521
522DEFUN (cmdline_options, args, ,
523 doc: /* -*- texinfo -*-
524@deftypefn {} {@var{opt_struct} =} cmdline_options ()
525Return a structure containing detailed information about the command line
526arguments passed to Octave.
527
528Programming Note: This function provides copious amounts of information about
529Octave's parsing of command line options and may be more useful for debugging
530Octave rather than for general use.
531@seealso{argv, program_name}
532@end deftypefn */)
533{
534 if (args.length () != 0)
535 print_usage ();
536
538
539 if (! app)
540 error ("invalid application context!");
541
542 cmdline_options opts = app->options ();
543
544 return ovl (opts.as_octave_value ());
545}
546
547/*
548%!assert (isstruct (cmdline_options ()))
549%!error <Invalid call> cmdline_options (1)
550*/
551
552DEFUN (program_invocation_name, args, ,
553 doc: /* -*- texinfo -*-
554@deftypefn {} {@var{name} =} program_invocation_name ()
555Return the string that was typed at the shell prompt to run Octave.
556
557The string may include path components as well as the program filename.
558
559If executing a script from the command line (e.g., @code{octave foo.m}) or
560using an executable Octave script, the program name is set to the name of the
561script. @xref{Executable Octave Programs}, for an example of how to create an
562executable Octave script.
563@seealso{program_name, argv}
564@end deftypefn */)
565{
566 if (args.length () != 0)
567 print_usage ();
568
570}
571
572/*
573%!assert (ischar (program_invocation_name ()))
574%!error <Invalid call> program_invocation_name (1)
575*/
576
577DEFUN (program_name, args, ,
578 doc: /* -*- texinfo -*-
579@deftypefn {} {@var{name} =} program_name ()
580Return the filename component of the value returned by
581@code{program_invocation_name}.
582
583@seealso{program_invocation_name, argv}
584@end deftypefn */)
585{
586 if (args.length () != 0)
587 print_usage ();
588
589 return ovl (application::program_name ());
590}
591
592/*
593%!assert (ischar (program_name ()))
594%!error <Invalid call> program_name (1)
595*/
596
597OCTAVE_END_NAMESPACE(octave)
Definition Cell.h:41
application(const cmdline_options &opts=cmdline_options())
Definition octave.cc:293
virtual int execute_interpreter()
Definition octave.cc:372
void set_program_names(const std::string &pname)
Definition octave.cc:308
static application * app()
Definition octave.h:316
static bool forced_interactive()
Definition octave.cc:335
void intern_argv(const string_vector &args)
Definition octave.cc:318
virtual ~application()
Definition octave.cc:305
string_vector m_argv
Definition octave.h:366
cmdline_options options() const
Definition octave.h:273
bool m_have_script_file
Definition octave.h:375
static std::string program_name()
Definition octave.h:323
bool m_is_octave_program
Definition octave.h:380
static string_vector argv()
Definition octave.h:328
bool interpreter_is_initialized() const
Definition octave.cc:350
bool m_have_eval_option_code
Definition octave.h:371
std::string m_program_name
Definition octave.h:360
static bool is_gui_running()
Definition octave.h:333
virtual void initialize_interpreter()
Definition octave.cc:365
virtual interpreter & create_interpreter()
Definition octave.cc:356
bool experimental_terminal_widget() const
Definition octave.cc:343
std::unique_ptr< interpreter > m_interpreter
Definition octave.h:382
static std::string program_invocation_name()
Definition octave.h:318
cmdline_options m_options
Definition octave.h:368
std::string m_program_invocation_name
Definition octave.h:357
static bool is_multi_threaded()
Definition octave.h:338
virtual void delete_interpreter()
Definition octave.cc:378
bool line_editing() const
Definition octave.h:65
bool forced_interactive() const
Definition octave.h:60
bool experimental_terminal_widget() const
Definition octave.h:58
int sys_argc() const
Definition octave.h:53
std::string texi_macros_file() const
Definition octave.h:90
bool inhibit_startup_message() const
Definition octave.h:63
std::string doc_cache_file() const
Definition octave.h:85
bool init_trace() const
Definition octave.h:80
std::string image_path() const
Definition octave.h:87
bool read_site_files() const
Definition octave.h:70
string_vector remaining_args() const
Definition octave.h:92
bool persist() const
Definition octave.h:68
bool read_user_files() const
Definition octave.h:71
std::string info_program() const
Definition octave.h:89
bool server() const
Definition octave.h:74
bool read_history_file() const
Definition octave.h:69
std::string exec_path() const
Definition octave.h:86
bool no_window_system() const
Definition octave.h:67
std::string code_to_eval() const
Definition octave.h:81
string_vector all_args() const
Definition octave.h:91
std::string info_file() const
Definition octave.h:88
std::string docstrings_file() const
Definition octave.h:84
bool echo_commands() const
Definition octave.h:56
bool gui() const
Definition octave.h:62
bool traditional() const
Definition octave.h:76
bool set_initial_path() const
Definition octave.h:75
bool forced_line_editing() const
Definition octave.h:61
std::list< std::string > command_line_path() const
Definition octave.h:82
octave_value as_octave_value() const
Definition octave.cc:245
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:1003
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:52
#define LINE_EDITING_OPTION
Definition options.h:47
#define TRADITIONAL_OPTION
Definition options.h:56
#define INFO_PROG_OPTION
Definition options.h:46
#define BUILT_IN_DOCSTRINGS_FILE_OPTION
Definition options.h:40
#define SERVER_OPTION
Definition options.h:54
#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:51
#define TEXI_MACROS_FILE_OPTION
Definition options.h:55
#define NO_INIT_USER_OPTION
Definition options.h:49
struct octave_getopt_options long_opts[]
Definition options.h:57
#define EXEC_PATH_OPTION
Definition options.h:42
#define IMAGE_PATH_OPTION
Definition options.h:44
#define PERSIST_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:437