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