GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
octave.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-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 // Born February 20, 1992.
27 
28 #if defined (HAVE_CONFIG_H)
29 # include "config.h"
30 #endif
31 
32 #include <iostream>
33 #include <string>
34 
35 #include "file-ops.h"
36 #include "getopt-wrapper.h"
37 #include "lo-error.h"
38 #include "oct-env.h"
39 #include "str-vec.h"
40 
41 #include "Cell.h"
42 #include "defun.h"
43 #include "display.h"
44 #include "error.h"
45 #include "input.h"
46 #include "interpreter.h"
47 #include "octave.h"
48 #include "oct-hist.h"
49 #include "oct-map.h"
50 #include "ovl.h"
51 #include "options-usage.h"
52 #include "ov.h"
53 #include "parse.h"
54 #include "sysdep.h"
55 
56 namespace octave
57 {
59  {
60  m_all_args.resize (1);
61  m_all_args[0] = "";
62  }
63 
64  cmdline_options::cmdline_options (int argc, char **argv)
65  {
66  // Save raw program arguments.
67  m_all_args = string_vector (argv, argc);
68 
69  while (true)
70  {
71  int long_idx;
72 
73  int optc = octave_getopt_long_wrapper (argc, argv, short_opts,
74  long_opts, &long_idx);
75 
76  if (optc < 0)
77  break;
78 
79  switch (optc)
80  {
81  case '?':
82  // Unrecognized option. getopt_long already printed a message about
83  // it, so we will just print the usage string and exit.
85  break;
86 
87  case 'H':
88  m_read_history_file = false;
89  break;
90 
91  case 'W':
92  m_no_window_system = true;
93  break;
94 
95  case 'V':
96  m_verbose_flag = true;
97  break;
98 
99  case 'd':
100  // This is the same as yydebug in parse.y.
101  octave_debug++;
102  break;
103 
104  case 'f':
105  m_read_init_files = false;
106  m_read_site_files = false;
107  break;
108 
109  case 'h':
111  break;
112 
113  case 'i':
114  m_forced_interactive = true;
115  break;
116 
117  case 'p':
118  if (octave_optarg_wrapper ())
120  break;
121 
122  case 'q':
124  break;
125 
126  case 'x':
127  m_echo_commands = true;
128  break;
129 
130  case 'v':
132  break;
133 
135  if (octave_optarg_wrapper ())
137  break;
138 
140  if (octave_optarg_wrapper ())
142  break;
143 
144  case EVAL_OPTION:
145  if (octave_optarg_wrapper ())
146  {
147  if (m_code_to_eval.empty ())
149  else
150  m_code_to_eval += (std::string (" ")
151  + octave_optarg_wrapper ());
152  }
153  break;
154 
155  case EXEC_PATH_OPTION:
156  if (octave_optarg_wrapper ())
158  break;
159 
160  case GUI_OPTION:
161  m_gui = true;
162  break;
163 
164  case IMAGE_PATH_OPTION:
165  if (octave_optarg_wrapper ())
167  break;
168 
169  case INFO_FILE_OPTION:
170  if (octave_optarg_wrapper ())
172  break;
173 
174  case INFO_PROG_OPTION:
175  if (octave_optarg_wrapper ())
177  break;
178 
179  case DEBUG_JIT_OPTION:
180  m_debug_jit = true;
181  break;
182 
183  case JIT_COMPILER_OPTION:
184  m_jit_compiler = true;
185  break;
186 
187  case LINE_EDITING_OPTION:
189  break;
190 
191  case NO_GUI_OPTION:
192  m_gui = false;
193  break;
194 
195  case NO_INIT_FILE_OPTION:
196  m_read_init_files = false;
197  break;
198 
199  case NO_INIT_PATH_OPTION:
200  m_set_initial_path = false;
201  break;
202 
204  m_line_editing = false;
205  break;
206 
207  case NO_SITE_FILE_OPTION:
208  m_read_site_files = false;
209  break;
210 
211  case PERSIST_OPTION:
212  m_persist = true;
213  break;
214 
216  if (octave_optarg_wrapper ())
218  break;
219 
220  case TRADITIONAL_OPTION:
221  m_traditional = true;
222  m_persist = true;
223  break;
224 
225  default:
226  // getopt_long should print a message about unrecognized options and
227  // return '?', which is handled above. If we end up here, it is
228  // because there was an option but we forgot to handle it.
229  // That should be fatal.
230  panic_impossible ();
231  break;
232  }
233  }
234 
236  argc-octave_optind_wrapper ());
237  }
238 
240 
241  application::application (int argc, char **argv)
242  : m_options (argc, argv)
243  {
244  init ();
245  }
246 
248  : m_options (opts)
249  {
250  init ();
251  }
252 
253  void
254  application::set_program_names (const std::string& pname)
255  {
257 
258  size_t pos = pname.find_last_of (sys::file_ops::dir_sep_chars ());
259 
260  m_program_name = (pos != std::string::npos) ? pname.substr (pos+1) : pname;
261  }
262 
263  void
265  {
266  octave_idx_type nargs = args.numel ();
267 
268  if (nargs > 0)
269  {
270  // Skip first argument (program name).
271  nargs--;
272 
273  m_argv.resize (nargs);
274 
275  for (octave_idx_type i = 0; i < nargs; i++)
276  m_argv[i] = args[i+1];
277  }
278  }
279 
281  {
282  return instance ? instance->m_options.forced_interactive () : false;
283  }
284 
286  {
287  // Delete interpreter if it still exists.
288 
289  delete m_interpreter;
290 
291  instance = nullptr;
292  }
293 
295  {
296  return m_interpreter ? m_interpreter->initialized () : false;
297  }
298 
300  {
301  if (! m_interpreter)
302  m_interpreter = new interpreter (this);
303 
304  return *m_interpreter;
305  }
306 
308  {
309  if (m_interpreter)
311  }
312 
314  {
315  return m_interpreter ? m_interpreter->execute () : -1;
316  }
317 
319  {
320  delete m_interpreter;
321 
322  m_interpreter = nullptr;
323  }
324 
325  void application::init (void)
326  {
327  if (instance)
328  throw std::runtime_error
329  ("only one Octave application object may be active");
330 
331  instance = this;
332 
333  string_vector all_args = m_options.all_args ();
334 
335  set_program_names (all_args[0]);
336 
337  string_vector remaining_args = m_options.remaining_args ();
338 
339  std::string code_to_eval = m_options.code_to_eval ();
340 
341  m_have_script_file = ! remaining_args.empty ();
342 
343  m_have_eval_option_code = ! code_to_eval.empty ();
344 
346  {
347  std::cerr << R"(error: --eval "CODE" and script file are mutually exclusive options)" << std::endl;
348 
350  }
351 
352  if (m_options.gui ())
353  {
355  {
356  std::cerr << "error: --gui and --no-window-system are mutually exclusive options" << std::endl;
358  }
359  if (! m_options.line_editing ())
360  {
361  std::cerr << "error: --gui and --no-line-editing are mutually exclusive options" << std::endl;
363  }
364  }
365 
366 
368  && ! m_options.persist ()
369  && ! m_options.traditional ());
370 
371  // This should probably happen early.
372  sysdep_init ();
373  }
374 
376  {
377  interpreter& interp = create_interpreter ();
378 
379  int status = interp.execute ();
380 
381  interp.shutdown ();
382 
383  return status;
384  }
385 }
386 
387 DEFUN (isguirunning, args, ,
388  doc: /* -*- texinfo -*-
389 @deftypefn {} {} isguirunning ()
390 Return true if Octave is running in GUI mode and false otherwise.
391 @seealso{have_window_system}
392 @end deftypefn */)
393 {
394  if (args.length () != 0)
395  print_usage ();
396 
397  // FIXME: This isn't quite right, it just says that we intended to
398  // start the GUI, not that it is actually running.
399 
401 }
402 
403 /*
404 %!assert (islogical (isguirunning ()))
405 %!error isguirunning (1)
406 */
407 
408 DEFUN (argv, args, ,
409  doc: /* -*- texinfo -*-
410 @deftypefn {} {} argv ()
411 Return the command line arguments passed to Octave.
412 
413 For example, if you invoked Octave using the command
414 
415 @example
416 octave --no-line-editing --silent
417 @end example
418 
419 @noindent
420 @code{argv} would return a cell array of strings with the elements
421 @option{--no-line-editing} and @option{--silent}.
422 
423 If you write an executable Octave script, @code{argv} will return the list
424 of arguments passed to the script. @xref{Executable Octave Programs}, for
425 an example of how to create an executable Octave script.
426 @end deftypefn */)
427 {
428  if (args.length () != 0)
429  print_usage ();
430 
431  return ovl (Cell (octave::application::argv ()));
432 }
433 
434 /*
435 %!assert (iscellstr (argv ()))
436 %!error argv (1)
437 */
438 
439 DEFUN (program_invocation_name, args, ,
440  doc: /* -*- texinfo -*-
441 @deftypefn {} {} program_invocation_name ()
442 Return the name that was typed at the shell prompt to run Octave.
443 
444 If executing a script from the command line (e.g., @code{octave foo.m})
445 or using an executable Octave script, the program name is set to the
446 name of the script. @xref{Executable Octave Programs}, for an example of
447 how to create an executable Octave script.
448 @seealso{program_name}
449 @end deftypefn */)
450 {
451  if (args.length () != 0)
452  print_usage ();
453 
455 }
456 
457 /*
458 %!assert (ischar (program_invocation_name ()))
459 %!error program_invocation_name (1)
460 */
461 
462 DEFUN (program_name, args, ,
463  doc: /* -*- texinfo -*-
464 @deftypefn {} {} program_name ()
465 Return the last component of the value returned by
466 @code{program_invocation_name}.
467 @seealso{program_invocation_name}
468 @end deftypefn */)
469 {
470  if (args.length () != 0)
471  print_usage ();
472 
474 }
475 
476 /*
477 %!assert (ischar (program_name ()))
478 %!error program_name (1)
479 */
Definition: Cell.h:43
void intern_argv(const string_vector &args)
Definition: octave.cc:264
virtual int execute_interpreter(void)
Definition: octave.cc:313
static bool forced_interactive(void)
Definition: octave.cc:280
static bool is_gui_running(void)
Definition: octave.h:303
void set_program_names(const std::string &pname)
Definition: octave.cc:254
static std::string program_name(void)
Definition: octave.h:293
static std::string program_invocation_name(void)
Definition: octave.h:288
application(const cmdline_options &opts=cmdline_options())
Definition: octave.cc:247
interpreter * m_interpreter
Definition: octave.h:347
std::string m_program_name
Definition: octave.h:325
bool m_is_octave_program
Definition: octave.h:345
virtual void delete_interpreter(void)
Definition: octave.cc:318
static application * instance
Definition: octave.h:315
bool m_have_script_file
Definition: octave.h:340
bool interpreter_initialized(void)
Definition: octave.cc:294
virtual interpreter & create_interpreter(void)
Definition: octave.cc:299
cmdline_options m_options
Definition: octave.h:333
virtual ~application(void)
Definition: octave.cc:285
void init(void)
Definition: octave.cc:325
string_vector m_argv
Definition: octave.h:331
virtual void initialize_interpreter(void)
Definition: octave.cc:307
bool m_have_eval_option_code
Definition: octave.h:336
static string_vector argv(void)
Definition: octave.h:298
std::string m_program_invocation_name
Definition: octave.h:322
std::string m_doc_cache_file
Definition: octave.h:196
std::string m_exec_path
Definition: octave.h:200
std::string m_image_path
Definition: octave.h:204
bool line_editing(void) const
Definition: octave.h:63
bool traditional(void) const
Definition: octave.h:71
string_vector m_all_args
Definition: octave.h:219
bool gui(void) const
Definition: octave.h:60
std::list< std::string > m_command_line_path
Definition: octave.h:187
bool persist(void) const
Definition: octave.h:66
std::string m_texi_macros_file
Definition: octave.h:216
std::string m_info_program
Definition: octave.h:212
bool forced_interactive(void) const
Definition: octave.h:58
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
bool m_inhibit_startup_message
Definition: octave.h:139
bool no_window_system(void) const
Definition: octave.h:65
std::string code_to_eval(void) const
Definition: octave.h:73
std::string m_info_file
Definition: octave.h:208
string_vector all_args(void) const
Definition: octave.h:82
bool initialized(void) const
Definition: interpreter.h:193
void initialize(void)
Definition: interpreter.cc:714
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:95
octave_idx_type numel(void) const
Definition: str-vec.h:100
bool empty(void) const
Definition: str-vec.h:77
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
#define panic_impossible()
Definition: error.h:380
char * octave_optarg_wrapper(void)
int octave_getopt_long_wrapper(int argc, char **argv, const char *shortopts, const struct octave_getopt_options *longopts, int *longind)
int octave_optind_wrapper(void)
std::string dir_sep_chars(void)
Definition: file-ops.cc:252
void sysdep_init(void)
Definition: sysdep.cc:433
#define INFO_FILE_OPTION
Definition: options-usage.h:67
#define NO_INIT_PATH_OPTION
Definition: options-usage.h:74
#define NO_GUI_OPTION
Definition: options-usage.h:72
#define LINE_EDITING_OPTION
Definition: options-usage.h:71
#define TRADITIONAL_OPTION
Definition: options-usage.h:79
#define INFO_PROG_OPTION
Definition: options-usage.h:68
static const char * short_opts
Definition: options-usage.h:50
#define BUILT_IN_DOCSTRINGS_FILE_OPTION
Definition: options-usage.h:61
#define DOC_CACHE_FILE_OPTION
Definition: options-usage.h:62
static void octave_print_verbose_usage_and_exit(void)
#define GUI_OPTION
Definition: options-usage.h:65
#define DEBUG_JIT_OPTION
Definition: options-usage.h:69
#define JIT_COMPILER_OPTION
Definition: options-usage.h:70
#define NO_SITE_FILE_OPTION
Definition: options-usage.h:76
#define NO_LINE_EDITING_OPTION
Definition: options-usage.h:75
#define TEXI_MACROS_FILE_OPTION
Definition: options-usage.h:78
struct octave_getopt_options long_opts[]
Definition: options-usage.h:80
#define EXEC_PATH_OPTION
Definition: options-usage.h:64
#define IMAGE_PATH_OPTION
Definition: options-usage.h:66
#define PERSIST_OPTION
Definition: options-usage.h:77
#define EVAL_OPTION
Definition: options-usage.h:63
static void octave_print_version_and_exit(void)
#define NO_INIT_FILE_OPTION
Definition: options-usage.h:73
static void octave_print_terse_usage_and_exit(void)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
int octave_debug