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