GNU Octave  4.4.1
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-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_octave_h)
24 #define octave_octave_h 1
25 
26 #include "octave-config.h"
27 
28 #if defined (__cplusplus)
29 
30 #include <list>
31 #include <string>
32 
33 #include "str-vec.h"
34 
35 namespace octave
36 {
37  // Command line arguments. See also options-usage.h.
38 
39  class OCTINTERP_API cmdline_options
40  {
41  public:
42 
43  cmdline_options (void);
44 
45  cmdline_options (int argc, char **argv);
46 
47  cmdline_options (const cmdline_options&) = default;
48 
49  cmdline_options& operator = (const cmdline_options&) = default;
50 
51  bool debug_jit (void) const { return m_debug_jit; }
52  bool echo_commands (void) const { return m_echo_commands; }
53 
54  OCTAVE_DEPRECATED (4.4, "use 'gui' instead")
55  bool force_gui (void) const { return m_gui; }
56 
57  bool forced_interactive (void) const { return m_forced_interactive; }
58  bool forced_line_editing (void) const { return m_forced_line_editing; }
59  bool gui (void) const { return m_gui; }
60  bool inhibit_startup_message (void) const { return m_inhibit_startup_message; }
61  bool jit_compiler (void) const { return m_jit_compiler; }
62  bool line_editing (void) const { return m_line_editing; }
63 
64  OCTAVE_DEPRECATED (4.4, "use '! gui' instead")
65  bool no_gui (void) const { return ! gui (); }
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 set_initial_path (void) const { return m_set_initial_path; }
73  bool traditional (void) const { return m_traditional; }
74  bool verbose_flag (void) const { return m_verbose_flag; }
75  std::string code_to_eval (void) const { return m_code_to_eval; }
76  std::list<std::string> command_line_path (void) const { return m_command_line_path; }
77  std::string docstrings_file (void) const { return m_docstrings_file; }
78  std::string doc_cache_file (void) const { return m_doc_cache_file; }
79  std::string exec_path (void) const { return m_exec_path; }
80  std::string image_path (void) const { return m_image_path; }
81  std::string info_file (void) const { return m_info_file; }
82  std::string info_program (void) const { return m_info_program; }
83  std::string texi_macros_file (void) const {return m_texi_macros_file; }
84  string_vector all_args (void) const { return m_all_args; }
85  string_vector remaining_args (void) const { return m_remaining_args; }
86 
87  void debug_jit (bool arg) { m_debug_jit = arg; }
88  void echo_commands (bool arg) { m_echo_commands = arg; }
89 
90  OCTAVE_DEPRECATED (4.4, "use 'gui' instead")
91  void force_gui (bool arg) { m_gui = arg; }
92 
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 jit_compiler (bool arg) { m_jit_compiler = arg; }
98  void line_editing (bool arg) { m_line_editing = arg; }
99 
100  OCTAVE_DEPRECATED (4.4, "this has been removed and is the default now")
101  void no_gui (bool) { return; }
102 
103  void no_window_system (bool arg) { m_no_window_system = arg; }
104  void persist (bool arg) { m_persist = arg; }
105  void read_history_file (bool arg) { m_read_history_file = arg; }
106  void read_init_files (bool arg) { m_read_init_files = arg; }
107  void read_site_files (bool arg) { m_read_site_files = arg; }
108  void set_initial_path (bool arg) { m_set_initial_path = arg; }
109  void traditional (bool arg) { m_traditional = arg; }
110  void verbose_flag (bool arg) { m_verbose_flag = arg; }
111  void code_to_eval (const std::string& arg) { m_code_to_eval = arg; }
112  void command_line_path (const std::list<std::string>& arg) { 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  private:
124 
125  // TRUE means enable debug tracing for the JIT compiler.
126  // (--debug-jit)
127  bool m_debug_jit = false;
128 
129  // If TRUE, echo commands as they are read and executed.
130  // (--echo-commands, -x)
131  bool m_echo_commands = 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 enable the JIT compiler.
150  // (--jit-compiler)
151  bool m_jit_compiler = false;
152 
153  // TRUE means we are using readline.
154  // (--no-line-editing)
155  bool m_line_editing = true;
156 
157  // If TRUE, ignore the window system even if it is available.
158  // (--no-window-system, -W)
159  bool m_no_window_system = false;
160 
161  // If TRUE, don't exit after evaluating code given by --eval option.
162  // (--persist)
163  bool m_persist = false;
164 
165  // If TRUE, initialize history list from saved history file.
166  // (--no-history; -H)
167  bool m_read_history_file = true;
168 
169  // TRUE means we read ~/.octaverc and ./.octaverc.
170  // (--norc; --no-init-file; -f)
171  bool m_read_init_files = true;
172 
173  // TRUE means we read the site-wide octaverc files.
174  // (--norc; --no-site-file; -f)
175  bool m_read_site_files = true;
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  // No copying, at least not yet...
251 
252  application (const application&) = delete;
253 
254  application& operator = (const application&) = delete;
255 
256  virtual ~application (void);
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  void interactive (bool arg);
292 
293  static application * app (void) { return instance; }
294 
295  static std::string program_invocation_name (void)
296  {
297  return instance ? instance->m_program_invocation_name : "";
298  }
299 
300  static std::string program_name (void)
301  {
302  return instance ? instance->m_program_name : "";
303  }
304 
305  static string_vector argv (void)
306  {
307  return instance ? instance->m_argv : string_vector ();
308  }
309 
310  static bool is_gui_running (void)
311  {
312  return instance ? instance->gui_running () : false;
313  }
314 
315  // Convenience functions.
316 
317  static bool forced_interactive (void);
318  static bool interactive (void);
319 
320  private:
321 
322  // The application instance; There should be only one.
323  static application *instance;
324 
325  void init (void);
326 
327  protected:
328 
329  // The name used to invoke Octave.
330  std::string m_program_invocation_name;
331 
332  // The last component of octave_program_invocation_name.
333  std::string m_program_name;
334 
335  // The current argument vector (may change if we are running a
336  // script with --persist because after the script is done, the
337  // arguments revert to the full list given to the octave
338  // interpreter at startup.
339  string_vector m_argv;
340 
341  cmdline_options m_options;
342 
343  // TRUE means we have --eval CODE
344  bool m_have_eval_option_code = false;
345 
346  // TRUE if there is a command line argument that looks like the
347  // name of a file to execute.
348  bool m_have_script_file = false;
349 
350  // TRUE if this is a program and no interpreter and interaction is
351  // needed. For example, an octave program with shebang line, or code
352  // from eval without persist.
353  bool m_is_octave_program = false;
354 
355  // If TRUE, the GUI should be started.
356  bool m_gui_running = false;
357 
358  interpreter *m_interpreter = nullptr;
359  };
360 
361  class OCTINTERP_API cli_application : public application
362  {
363  public:
364 
365  cli_application (const cmdline_options& opts = cmdline_options ())
366  : application (opts)
367  { }
368 
369  cli_application (int argc, char **argv)
370  : application (argc, argv)
371  { }
372 
373  // No copying, at least not yet...
374 
375  cli_application (const cli_application&) = delete;
376 
377  cli_application& operator = (const cli_application&) = delete;
378 
379  ~cli_application (void) = default;
380 
381  int execute (void);
382  };
383 }
384 
385 #endif
386 
387 #if defined (__cplusplus)
388 extern "C" {
389 #endif
390 
391 OCTAVE_DEPRECATED (4.4, "see the Octave documentation for other options")
392 extern OCTINTERP_API int octave_main (int argc, char **argv, int embedded);
393 
394 #if defined (__cplusplus)
395 }
396 #endif
397 
398 #endif
int argc
Definition: load-save.cc:646
OCTINTERP_API int octave_main(int argc, char **argv, int embedded)
Definition: octave.cc:402
octave_value arg
Definition: pr-output.cc:3244
string_vector argv
Definition: load-save.cc:648
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
std::string pname
Definition: graphics.cc:11810
OCTAVE_EXPORT octave_value_list only variables visible in the local scope are displayed The following are valid options
Definition: variables.cc:1862
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888