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