GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
octave.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2002-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#if ! defined (octave_octave_h)
27#define octave_octave_h 1
28
29#include "octave-config.h"
30
31#include <list>
32#include <memory>
33#include <string>
34
35#include "Array.h"
36#include "str-vec.h"
37
38class octave_value;
39
41
42// Command line arguments. See also options.h.
43
44class OCTINTERP_API cmdline_options
45{
46public:
47
49
50 cmdline_options (int argc, char **argv);
51
52 OCTAVE_DEFAULT_COPY_MOVE_DELETE (cmdline_options)
53
54 int sys_argc () const { return m_all_args.numel (); }
55 char ** sys_argv () const { return m_all_args.c_str_vec (); }
56
57 bool echo_commands () const { return m_echo_commands; }
58
60 { return m_experimental_terminal_widget; }
61 bool forced_interactive () const { return m_forced_interactive; }
62 bool forced_line_editing () const { return m_forced_line_editing; }
63 bool gui () const { return m_gui; }
65 { return m_inhibit_startup_tests; }
67 { return m_inhibit_startup_message; }
68 bool line_editing () const { return m_line_editing; }
69
70 bool no_window_system () const { return m_no_window_system; }
71 bool persist () const { return m_persist; }
72 bool read_history_file () const { return m_read_history_file; }
73 bool read_site_files () const { return m_read_site_files; }
74 bool read_user_files () const { return m_read_user_files; }
75 OCTAVE_DEPRECATED (10, "cmdline_options::read_init_files is deprecated, use read_user_files")
76 bool read_init_files () const { return m_read_user_files; }
77 bool server () const { return m_server; }
78 bool set_initial_path () const { return m_set_initial_path; }
79 bool traditional () const { return m_traditional; }
80
81 OCTAVE_DEPRECATED (10, "cmdline_options::verbose is deprecated, use init_trace")
82 bool verbose () const { return m_init_trace; }
83 bool init_trace () const { return m_init_trace; }
84 std::string code_to_eval () const { return m_code_to_eval; }
85 std::list<std::string> command_line_path () const
86 { return m_command_line_path; }
87 std::string docstrings_file () const { return m_docstrings_file; }
88 std::string doc_cache_file () const { return m_doc_cache_file; }
89 std::string exec_path () const { return m_exec_path; }
90 std::string image_path () const { return m_image_path; }
91 std::string info_file () const { return m_info_file; }
92 std::string info_program () const { return m_info_program; }
93 std::string texi_macros_file () const {return m_texi_macros_file; }
94 string_vector all_args () const { return m_all_args; }
95 string_vector remaining_args () const { return m_remaining_args; }
96
97 void echo_commands (bool arg) { m_echo_commands = arg; }
98
100 { m_experimental_terminal_widget = arg; }
101 void forced_line_editing (bool arg) { m_forced_line_editing = arg; }
102 void forced_interactive (bool arg) { m_forced_interactive = arg; }
103 void gui (bool arg) { m_gui = arg; }
104 void inhibit_startup_tests (bool arg) { m_inhibit_startup_tests = arg; }
105 void inhibit_startup_message (bool arg) { m_inhibit_startup_message = arg; }
106 void line_editing (bool arg) { m_line_editing = arg; }
107
108 void no_window_system (bool arg) { m_no_window_system = arg; }
109 void persist (bool arg) { m_persist = arg; }
110 void read_history_file (bool arg) { m_read_history_file = arg; }
111 // FIXME: Alias for read_user_files() introduced in Octave 10.
112 // Remove at some point in the future.
113 void read_site_files (bool arg) { m_read_site_files = arg; }
114 void read_user_files (bool arg) { m_read_user_files = arg; }
115 OCTAVE_DEPRECATED (10, "cmdline_options::read_init_files is deprecated, use read_user_files")
116 void read_init_files (bool arg) { read_user_files (arg); }
117 void server (bool arg) { m_server = arg; }
118 void set_initial_path (bool arg) { m_set_initial_path = arg; }
119 void traditional (bool arg) { m_traditional = arg; }
120 OCTAVE_DEPRECATED (10, "cmdline_options::verbose is deprecated, use init_trace")
121 void verbose (bool arg) { m_init_trace = arg; }
122 void init_trace (bool arg) { m_init_trace = arg; }
123 void code_to_eval (const std::string& arg) { m_code_to_eval = arg; }
124 void command_line_path (const std::list<std::string>& arg)
125 { m_command_line_path = arg; }
126 void docstrings_file (const std::string& arg) { m_docstrings_file = arg; }
127 void doc_cache_file (const std::string& arg) { m_doc_cache_file = arg; }
128 void exec_path (const std::string& arg) { m_exec_path = arg; }
129 void image_path (const std::string& arg) { m_image_path = arg; }
130 void info_file (const std::string& arg) { m_info_file = arg; }
131 void info_program (const std::string& arg) { m_info_program = arg; }
132 void texi_macros_file (const std::string& arg) { m_texi_macros_file = arg; }
133 void all_args (const string_vector& arg) { m_all_args = arg; }
134 void remaining_args (const string_vector& arg) { m_remaining_args = arg; }
135
136 octave_value as_octave_value () const;
137
138private:
139
140 // If TRUE, echo commands as they are read and executed.
141 // (--echo-commands, -x)
142 bool m_echo_commands = false;
143
144 // If TRUE, use new experimental terminal widget in the GUI.
145 // (--experimental-terminal-widget)
146 bool m_experimental_terminal_widget = false;
147
148 // If TRUE, start the GUI.
149 // (--gui) and (--force-gui) for backwards compatibility
150 bool m_gui = false;
151
152 // TRUE means the user forced this shell to be interactive.
153 // (--interactive, -i)
154 bool m_forced_interactive = false;
155
156 // If TRUE, force readline command line editing.
157 // (--line-editing)
158 bool m_forced_line_editing = false;
159
160 // TRUE means we don't check for incompatibilites on startup.
161 // (--no-startup-tests)
162 bool m_inhibit_startup_tests = false;
163
164 // TRUE means we don't print the usual startup message.
165 // (--quiet; --silent; -q)
166 bool m_inhibit_startup_message = false;
167
168 // TRUE means we are using readline.
169 // (--no-line-editing)
170 bool m_line_editing = true;
171
172 // If TRUE, ignore the window system even if it is available.
173 // (--no-window-system, -W)
174 bool m_no_window_system = false;
175
176 // If TRUE, don't exit after evaluating code given by --eval option.
177 // (--persist)
178 bool m_persist = false;
179
180 // If TRUE, initialize history list from saved history file.
181 // (--no-history; -H)
182 bool m_read_history_file = true;
183
184 // TRUE means we read ~/.octaverc and ./.octaverc.
185 // (--no-init-user; --no-init-file; --norc; -f)
186 bool m_read_user_files = true;
187
188 // TRUE means we read the site-wide octaverc files.
189 // (--no-init-site; --no-site-file; --norc; -f)
190 bool m_read_site_files = true;
191
192 // If TRUE, start the command server.
193 // (--server)
194 bool m_server = false;
195
196 // TRUE means we set the initial path to configured defaults.
197 // (--no-init-path)
198 bool m_set_initial_path = true;
199
200 // If TRUE use traditional (maximally MATLAB compatible) settings
201 // (--traditional)
202 bool m_traditional = false;
203
204 // If TRUE, print trace of files read during initialization
205 // (--init-trace, --verbose; -V)
206 // FIXME: --verbose and -V will eventually be deprecated and removed.
207 bool m_init_trace = false;
208
209 // The code to evaluate at startup
210 // (--eval CODE)
211 std::string m_code_to_eval;
212
213 // The value of "path" specified on the command line.
214 // (--path; -p)
215 std::list<std::string> m_command_line_path;
216
217 // The value for "built_in_docstrings_file" specified on the
218 // command line.
219 // (--built-in-docstrings-file)
220 std::string m_docstrings_file;
221
222 // The value for "doc_cache_file" specified on the command line.
223 // (--doc-cache-file)
224 std::string m_doc_cache_file;
225
226 // The value for "EXEC_PATH" specified on the command line.
227 // (--exec-path)
228 std::string m_exec_path;
229
230 // The value for "IMAGE_PATH" specified on the command line.
231 // (--image-path)
232 std::string m_image_path;
233
234 // The value for "info_file" specified on the command line.
235 // (--info-file)
236 std::string m_info_file;
237
238 // The value for "info_program" specified on the command line.
239 // (--info-program)
240 std::string m_info_program;
241
242 // The value for "texi_macos_file" specified on the command line.
243 // (--texi-macros-file)
244 std::string m_texi_macros_file;
245
246 // All arguments passed to the argc, argv constructor.
247 string_vector m_all_args;
248
249 // Arguments remaining after parsing.
250 string_vector m_remaining_args;
251};
252
253// The application object contains a pointer to the current
254// interpreter and the interpreter contains a pointer back to the
255// application context so we need a forward declaration for one (or
256// both) of them...
257
258class interpreter;
259
260// Base class for an Octave application.
261
262class OCTINTERP_API application
263{
264public:
265
267
268 application (int argc, char **argv);
269
270 OCTAVE_DISABLE_COPY_MOVE (application)
271
272 virtual ~application ();
273
274 int sys_argc () const { return m_options.sys_argc (); }
275 char ** sys_argv () const { return m_options.sys_argv (); }
276
277 void set_program_names (const std::string& pname);
278
279 void intern_argv (const string_vector& args);
280
281 cmdline_options options () const { return m_options; }
282
283 bool have_eval_option_code () const { return m_have_eval_option_code; }
284
285 bool have_script_file () const { return m_have_script_file; }
286
287 bool is_octave_program () const { return m_is_octave_program; }
288
289 bool interpreter_is_initialized () const;
290
291 virtual interpreter& create_interpreter ();
292
293 virtual void initialize_interpreter ();
294
295 virtual int execute_interpreter ();
296
297 virtual void delete_interpreter ();
298
299 virtual int execute () = 0;
300
301 virtual bool gui_running () const { return false; }
302 virtual void gui_running (bool) { }
303
304 // TRUE if Octave uses multiple threads (e.g., for interpreter and GUI).
305 virtual bool multi_threaded () const { return false; }
306
307 void program_invocation_name (const std::string& nm)
308 { m_program_invocation_name = nm; }
309
310 void program_name (const std::string& nm) { m_program_name = nm; }
311
312 void forced_interactive (bool arg) { m_options.forced_interactive (arg); }
313
314 // Provided for convenience. Will be removed once we eliminate the
315 // old terminal widget.
316 bool experimental_terminal_widget () const;
317
318 static application * app () { return s_instance; }
319
320 static std::string program_invocation_name ()
321 {
322 return s_instance ? s_instance->m_program_invocation_name : "";
323 }
324
325 static std::string program_name ()
326 {
327 return s_instance ? s_instance->m_program_name : "";
328 }
329
331 {
332 return s_instance ? s_instance->m_argv : string_vector ();
333 }
334
335 static bool is_gui_running ()
336 {
337 return s_instance ? s_instance->gui_running () : false;
338 }
339
340 static bool is_multi_threaded ()
341 {
342 return s_instance ? s_instance->multi_threaded () : false;
343 }
344
345 // Convenience functions.
346
347 static bool forced_interactive ();
348
349private:
350
351 // The application instance; There should be only one.
352 static application *s_instance;
353
354 void init ();
355
356protected:
357
358 // The name used to invoke Octave.
360
361 // The last component of octave_program_invocation_name.
362 std::string m_program_name;
363
364 // The current argument vector (may change if we are running a
365 // script with --persist because after the script is done, the
366 // arguments revert to the full list given to the octave
367 // interpreter at startup.
369
371
372 // TRUE means we have --eval CODE
373 bool m_have_eval_option_code = false;
374
375 // TRUE if there is a command line argument that looks like the
376 // name of a file to execute.
377 bool m_have_script_file = false;
378
379 // TRUE if this is a program and no interpreter and interaction is
380 // needed. For example, an octave program with shebang line, or code
381 // from eval without persist.
382 bool m_is_octave_program = false;
383
384 std::unique_ptr<interpreter> m_interpreter;
385};
386
387class OCTINTERP_API cli_application : public application
388{
389public:
390
392 : application (opts)
393 { }
394
395 cli_application (int argc, char **argv)
396 : application (argc, argv)
397 { }
398
399 OCTAVE_DISABLE_COPY_MOVE (cli_application)
400
401 ~cli_application () = default;
402
403 int execute ();
404};
405
406OCTAVE_END_NAMESPACE(octave)
407
408#endif
int sys_argc() const
Definition octave.h:274
void forced_interactive(bool arg)
Definition octave.h:312
void program_name(const std::string &nm)
Definition octave.h:310
static application * app()
Definition octave.h:318
virtual bool gui_running() const
Definition octave.h:301
virtual void gui_running(bool)
Definition octave.h:302
bool have_eval_option_code() const
Definition octave.h:283
string_vector m_argv
Definition octave.h:368
void program_invocation_name(const std::string &nm)
Definition octave.h:307
cmdline_options options() const
Definition octave.h:281
virtual bool multi_threaded() const
Definition octave.h:305
static std::string program_name()
Definition octave.h:325
static string_vector argv()
Definition octave.h:330
std::string m_program_name
Definition octave.h:362
char ** sys_argv() const
Definition octave.h:275
static bool is_gui_running()
Definition octave.h:335
bool is_octave_program() const
Definition octave.h:287
bool have_script_file() const
Definition octave.h:285
std::unique_ptr< interpreter > m_interpreter
Definition octave.h:384
virtual int execute()=0
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
cli_application(const cmdline_options &opts=cmdline_options())
Definition octave.h:391
cli_application(int argc, char **argv)
Definition octave.h:395
~cli_application()=default
void inhibit_startup_tests(bool arg)
Definition octave.h:104
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
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
void forced_interactive(bool arg)
Definition octave.h:102
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
void info_program(const std::string &arg)
Definition octave.h:131
void image_path(const std::string &arg)
Definition octave.h:129
bool read_user_files() const
Definition octave.h:74
std::string info_program() const
Definition octave.h:92
void traditional(bool arg)
Definition octave.h:119
void persist(bool arg)
Definition octave.h:109
void doc_cache_file(const std::string &arg)
Definition octave.h:127
bool server() const
Definition octave.h:77
bool read_history_file() const
Definition octave.h:72
char ** sys_argv() const
Definition octave.h:55
std::string exec_path() const
Definition octave.h:89
bool no_window_system() const
Definition octave.h:70
void docstrings_file(const std::string &arg)
Definition octave.h:126
std::string code_to_eval() const
Definition octave.h:84
string_vector all_args() const
Definition octave.h:94
void server(bool arg)
Definition octave.h:117
void all_args(const string_vector &arg)
Definition octave.h:133
void inhibit_startup_message(bool arg)
Definition octave.h:105
void echo_commands(bool arg)
Definition octave.h:97
std::string info_file() const
Definition octave.h:91
void set_initial_path(bool arg)
Definition octave.h:118
void remaining_args(const string_vector &arg)
Definition octave.h:134
void experimental_terminal_widget(bool arg)
Definition octave.h:99
std::string docstrings_file() const
Definition octave.h:87
bool echo_commands() const
Definition octave.h:57
void read_site_files(bool arg)
Definition octave.h:113
void read_user_files(bool arg)
Definition octave.h:114
void code_to_eval(const std::string &arg)
Definition octave.h:123
bool gui() const
Definition octave.h:63
bool traditional() const
Definition octave.h:79
void init_trace(bool arg)
Definition octave.h:122
void read_history_file(bool arg)
Definition octave.h:110
void forced_line_editing(bool arg)
Definition octave.h:101
void command_line_path(const std::list< std::string > &arg)
Definition octave.h:124
bool set_initial_path() const
Definition octave.h:78
void gui(bool arg)
Definition octave.h:103
bool forced_line_editing() const
Definition octave.h:62
void line_editing(bool arg)
Definition octave.h:106
bool inhibit_startup_tests() const
Definition octave.h:64
void texi_macros_file(const std::string &arg)
Definition octave.h:132
void info_file(const std::string &arg)
Definition octave.h:130
std::list< std::string > command_line_path() const
Definition octave.h:85
void exec_path(const std::string &arg)
Definition octave.h:128
void no_window_system(bool arg)
Definition octave.h:108
octave_value arg() const
Definition ov.h:1464
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTAVE_DEPRECATED(11, "") typedef bool(*b_d_Mapper)(double)