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