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