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