GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
octave.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 "builtin-defun-decls.h"
38 #include "Cell.h"
39 #include "defaults.h"
40 #include "defun.h"
41 #include "display.h"
42 #include "error.h"
43 #include "input.h"
44 #include "interpreter.h"
45 #include "octave.h"
46 #include "oct-hist.h"
47 #include "oct-map.h"
48 #include "ovl.h"
49 #include "options-usage.h"
50 #include "ov.h"
51 #include "parse.h"
52 #include "sysdep.h"
53 
54 namespace octave
55 {
56  cmdline_options::cmdline_options (void)
57  {
58  m_all_args.resize (1);
59  m_all_args[0] = "";
60  }
61 
62  cmdline_options::cmdline_options (int argc, char **argv)
63  {
64  // Save raw program arguments.
65  m_all_args = string_vector (argv, argc);
66 
67  while (true)
68  {
69  int long_idx;
70 
71  int optc = octave_getopt_long_wrapper (argc, argv, short_opts,
72  long_opts, &long_idx);
73 
74  if (optc < 0)
75  break;
76 
77  switch (optc)
78  {
79  case '?':
80  // Unrecognized option. getopt_long already printed a message about
81  // it, so we will just print the usage string and exit.
83  break;
84 
85  case 'H':
86  Fhistory_save (octave_value (false));
87  m_read_history_file = false;
88  break;
89 
90  case 'W':
91  m_no_window_system = true;
92  break;
93 
94  case 'V':
95  m_verbose_flag = true;
96  break;
97 
98  case 'd':
99  // This is the same as yydebug in parse.y.
100  octave_debug++;
101  break;
102 
103  case 'f':
104  m_read_init_files = false;
105  m_read_site_files = false;
106  break;
107 
108  case 'h':
110  break;
111 
112  case 'i':
113  m_forced_interactive = true;
114  break;
115 
116  case 'p':
117  if (octave_optarg_wrapper ())
118  m_command_line_path.push_back (octave_optarg_wrapper ());
119  break;
120 
121  case 'q':
122  m_inhibit_startup_message = true;
123  break;
124 
125  case 'x':
126  {
129  }
130  break;
131 
132  case 'v':
134  break;
135 
137  if (octave_optarg_wrapper ())
139  break;
140 
142  if (octave_optarg_wrapper ())
144  break;
145 
146  case EVAL_OPTION:
147  if (octave_optarg_wrapper ())
148  {
149  if (m_code_to_eval.empty ())
150  m_code_to_eval = octave_optarg_wrapper ();
151  else
152  m_code_to_eval += std::string (" ") + octave_optarg_wrapper ();
153  }
154  break;
155 
156  case EXEC_PATH_OPTION:
157  if (octave_optarg_wrapper ())
158  m_exec_path = octave_optarg_wrapper ();
159  break;
160 
161  case FORCE_GUI_OPTION:
162  m_force_gui = true;
163  break;
164 
165  case IMAGE_PATH_OPTION:
166  if (octave_optarg_wrapper ())
167  m_image_path = octave_optarg_wrapper ();
168  break;
169 
170  case INFO_FILE_OPTION:
171  if (octave_optarg_wrapper ())
173  break;
174 
175  case INFO_PROG_OPTION:
176  if (octave_optarg_wrapper ())
178  break;
179 
180  case DEBUG_JIT_OPTION:
181  Fdebug_jit (octave_value (true));
182  break;
183 
184  case JIT_COMPILER_OPTION:
185  Fjit_enable (octave_value (true));
186  break;
187 
188  case LINE_EDITING_OPTION:
189  m_forced_line_editing = m_line_editing = true;
190  break;
191 
192  case NO_GUI_OPTION:
193  m_no_gui = true;
194  break;
195 
196  case NO_INIT_FILE_OPTION:
197  m_read_init_files = false;
198  break;
199 
200  case NO_INIT_PATH_OPTION:
201  m_set_initial_path = false;
202  break;
203 
205  m_line_editing = false;
206  break;
207 
208  case NO_SITE_FILE_OPTION:
209  m_read_site_files = 0;
210  break;
211 
212  case PERSIST_OPTION:
213  m_persist = true;
214  break;
215 
217  if (octave_optarg_wrapper ())
219  break;
220 
221  case TRADITIONAL_OPTION:
222  m_traditional = true;
223  m_persist = true;
224  break;
225 
226  default:
227  // getopt_long should print a message about unrecognized options and
228  // return '?', which is handled above. If we end up here, it is
229  // because there was an option but we forgot to handle it.
230  // That should be fatal.
231  panic_impossible ();
232  break;
233  }
234  }
235 
236  // Check for various incompatible argument pairs
237  if (m_force_gui && m_no_gui)
238  {
239  warning ("only one of --force-gui and --no-gui may be used");
240 
242  }
243 
244  m_remaining_args = string_vector (argv+octave_optind_wrapper (),
245  argc-octave_optind_wrapper ());
246  }
247 
248  cmdline_options::cmdline_options (const cmdline_options& opts)
249  : m_force_gui (opts.m_force_gui),
250  m_forced_interactive (opts.m_forced_interactive),
251  m_forced_line_editing (opts.m_forced_line_editing),
252  m_inhibit_startup_message (opts.m_inhibit_startup_message),
253  m_line_editing (opts.m_line_editing),
254  m_no_gui (opts.m_no_gui),
255  m_no_window_system (opts.m_no_window_system),
256  m_persist (opts.m_persist),
257  m_read_history_file (opts.m_read_history_file),
258  m_read_init_files (opts.m_read_init_files),
259  m_read_site_files (opts.m_read_site_files),
260  m_set_initial_path (opts.m_set_initial_path),
261  m_traditional (opts.m_traditional),
262  m_verbose_flag (opts.m_verbose_flag),
263  m_code_to_eval (opts.m_code_to_eval),
264  m_command_line_path (opts.m_command_line_path),
265  m_exec_path (opts.m_exec_path),
266  m_image_path (opts.m_image_path),
267  m_all_args (opts.m_all_args),
268  m_remaining_args (opts.m_remaining_args)
269  { }
270 
271  cmdline_options&
272  cmdline_options::operator = (const cmdline_options& opts)
273  {
274  if (this != &opts)
275  {
276  m_force_gui = opts.m_force_gui;
277  m_forced_interactive = opts.m_forced_interactive;
278  m_forced_line_editing = opts.m_forced_line_editing;
279  m_inhibit_startup_message = opts.m_inhibit_startup_message;
280  m_line_editing = opts.m_line_editing;
281  m_no_gui = opts.m_no_gui;
282  m_no_window_system = opts.m_no_window_system;
283  m_persist = opts.m_persist;
284  m_read_history_file = opts.m_read_history_file;
285  m_read_init_files = opts.m_read_init_files;
286  m_read_site_files = opts.m_read_site_files;
287  m_set_initial_path = opts.m_set_initial_path;
288  m_traditional = opts.m_traditional;
289  m_verbose_flag = opts.m_verbose_flag;
290  m_code_to_eval = opts.m_code_to_eval;
291  m_command_line_path = opts.m_command_line_path;
292  m_exec_path = opts.m_exec_path;
293  m_image_path = opts.m_image_path;
294  m_all_args = opts.m_all_args;
295  m_remaining_args = opts.m_remaining_args;
296  }
297 
298  return *this;
299  }
300 
301  application *application::instance = 0;
302 
303  application::application (int argc, char **argv)
304  : m_options (argc, argv)
305  {
306  init ();
307  }
308 
309  application::application (const cmdline_options& opts)
310  : m_options (opts)
311  {
312  init ();
313  }
314 
315  void
316  application::set_program_names (const std::string& pname)
317  {
318  m_program_invocation_name = pname;
319 
320  size_t pos = pname.find_last_of (octave::sys::file_ops::dir_sep_chars ());
321 
322  m_program_name = (pos != std::string::npos) ? pname.substr (pos+1) : pname;
323  }
324 
325  void
326  application::intern_argv (const string_vector& args)
327  {
328  assert (symbol_table::at_top_level ());
329 
330  octave_idx_type nargs = args.numel ();
331 
332  if (nargs > 0)
333  {
334  // Skip first argument (program name).
335  nargs--;
336 
337  m_argv.resize (nargs);
338 
339  for (octave_idx_type i = 0; i < nargs; i++)
340  m_argv[i] = args[i+1];
341  }
342 
343  symbol_table::assign (".nargin.", nargs);
344  symbol_table::mark_hidden (".nargin.");
345  }
346 
347  void application::interactive (bool arg)
348  {
349  interpreter *interp = instance->m_interpreter;
350 
351  if (interp)
352  interp->interactive (arg);
353  }
354 
355  bool application::forced_interactive (void)
356  {
357  return instance->m_options.forced_interactive ();
358  }
359 
360  bool application::interactive (void)
361  {
362  interpreter *interp = instance->m_interpreter;
363 
364  return interp ? interp->interactive () : false;
365  }
366 
367  application::~application (void)
368  {
369  instance = 0;
370 
371  delete m_interpreter;
372  }
373 
374  void application::create_interpreter (void)
375  {
376  if (! m_interpreter)
377  m_interpreter = new interpreter (this);
378  }
379 
380  int application::execute_interpreter (void)
381  {
382  return m_interpreter ? m_interpreter->execute () : -1;
383  }
384 
385  void application::init (void)
386  {
387  if (instance)
388  {
389  // FIXME: Should this be an error?
390  }
391  else
392  instance = this;
393 
394  string_vector all_args = m_options.all_args ();
395 
396  set_program_names (all_args[0]);
397 
398  string_vector remaining_args = m_options.remaining_args ();
399 
400  std::string code_to_eval = m_options.code_to_eval ();
401 
402  m_have_script_file = ! remaining_args.empty ();
403 
404  if (! code_to_eval.empty () && m_have_script_file)
405  {
406  warning ("--eval \"CODE\" and script file are mutually exclusive options");
407 
409  }
410 
411  m_is_octave_program = ((m_have_script_file || ! code_to_eval.empty ())
412  && ! m_options.persist ()
413  && ! m_options.traditional ());
414 
415  // This should probably happen early.
416  sysdep_init ();
417 
418  // Need to have global Vfoo variables defined early.
419  install_defaults ();
420  }
421 
422  int cli_application::execute (void)
423  {
424  create_interpreter ();
425 
426  return execute_interpreter ();
427  }
428 
429  void embedded_application::create_interpreter (void)
430  {
431  if (! m_interpreter)
432  m_interpreter = new interpreter (this, true);
433  }
434 
435  int embedded_application::execute (void)
436  {
437  create_interpreter ();
438 
439  return execute_interpreter ();
440  }
441 }
442 
443 // embedded is int here because octave_main is extern "C".
444 
445 int
446 octave_main (int argc, char **argv, int embedded)
447 {
449 
450  if (embedded)
451  {
452  octave::embedded_application app (argc, argv);
453  return app.execute ();
454  }
455  else
456  {
457  octave::cli_application app (argc, argv);
458  return app.execute ();
459  }
460 }
461 
462 DEFUN (isguirunning, args, ,
463  doc: /* -*- texinfo -*-
464 @deftypefn {} {} isguirunning ()
465 Return true if Octave is running in GUI mode and false otherwise.
466 @seealso{have_window_system}
467 @end deftypefn */)
468 {
469  if (args.length () != 0)
470  print_usage ();
471 
472  // FIXME: This isn't quite right, it just says that we intended to
473  // start the GUI, not that it is actually running.
474 
475  return ovl (octave::application::is_gui_running ());
476 }
477 
478 /*
479 %!assert (islogical (isguirunning ()))
480 %!error isguirunning (1)
481 */
482 
483 DEFUN (argv, args, ,
484  doc: /* -*- texinfo -*-
485 @deftypefn {} {} argv ()
486 Return the command line arguments passed to Octave.
487 
488 For example, if you invoked Octave using the command
489 
490 @example
491 octave --no-line-editing --silent
492 @end example
493 
494 @noindent
495 @code{argv} would return a cell array of strings with the elements
496 @option{--no-line-editing} and @option{--silent}.
497 
498 If you write an executable Octave script, @code{argv} will return the list
499 of arguments passed to the script. @xref{Executable Octave Programs}, for
500 an example of how to create an executable Octave script.
501 @end deftypefn */)
502 {
503  if (args.length () != 0)
504  print_usage ();
505 
506  return ovl (Cell (octave::application::argv ()));
507 }
508 
509 /*
510 %!assert (iscellstr (argv ()))
511 %!error argv (1)
512 */
513 
514 DEFUN (program_invocation_name, args, ,
515  doc: /* -*- texinfo -*-
516 @deftypefn {} {} program_invocation_name ()
517 Return the name that was typed at the shell prompt to run Octave.
518 
519 If executing a script from the command line (e.g., @code{octave foo.m})
520 or using an executable Octave script, the program name is set to the
521 name of the script. @xref{Executable Octave Programs}, for an example of
522 how to create an executable Octave script.
523 @seealso{program_name}
524 @end deftypefn */)
525 {
526  if (args.length () != 0)
527  print_usage ();
528 
529  return ovl (octave::application::program_invocation_name ());
530 }
531 
532 /*
533 %!assert (ischar (program_invocation_name ()))
534 %!error program_invocation_name (1)
535 */
536 
537 DEFUN (program_name, args, ,
538  doc: /* -*- texinfo -*-
539 @deftypefn {} {} program_name ()
540 Return the last component of the value returned by
541 @code{program_invocation_name}.
542 @seealso{program_invocation_name}
543 @end deftypefn */)
544 {
545  if (args.length () != 0)
546  print_usage ();
547 
548  return ovl (octave::application::program_name ());
549 }
550 
551 /*
552 %!assert (ischar (program_name ()))
553 %!error program_name (1)
554 */
#define LINE_EDITING_OPTION
Definition: options-usage.h:61
#define NO_GUI_OPTION
Definition: options-usage.h:62
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
Definition: Cell.h:37
static bool at_top_level(void)
Definition: symtab.h:1300
OCTINTERP_API octave_value_list Fhistory_save(const octave_value_list &=octave_value_list(), int=0)
static void octave_print_version_and_exit(void)
OCTINTERP_API octave_value_list Finfo_program(const octave_value_list &=octave_value_list(), int=0)
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:52
OCTINTERP_API octave_value_list Fjit_enable(const octave_value_list &=octave_value_list(), int=0)
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
static std::string dir_sep_chars(void)
Definition: file-ops.h:85
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
int argc
Definition: load-save.cc:633
bool empty(void) const
Definition: str-vec.h:79
#define INFO_FILE_OPTION
Definition: options-usage.h:57
#define DEBUG_JIT_OPTION
Definition: options-usage.h:59
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
#define PERSIST_OPTION
Definition: options-usage.h:67
#define IMAGE_PATH_OPTION
Definition: options-usage.h:56
OCTINTERP_API octave_value_list Fdebug_jit(const octave_value_list &=octave_value_list(), int=0)
#define NO_SITE_FILE_OPTION
Definition: options-usage.h:66
#define EXEC_PATH_OPTION
Definition: options-usage.h:54
int octave_main(int argc, char **argv, int embedded)
Definition: octave.cc:446
#define TRADITIONAL_OPTION
Definition: options-usage.h:69
#define NO_LINE_EDITING_OPTION
Definition: options-usage.h:65
OCTINTERP_API octave_value_list Ftexi_macros_file(const octave_value_list &=octave_value_list(), int=0)
octave_value arg
Definition: pr-output.cc:3440
string_vector argv
Definition: load-save.cc:635
#define TEXI_MACROS_FILE_OPTION
Definition: options-usage.h:68
JNIEnv void * args
Definition: ov-java.cc:67
static void octave_print_terse_usage_and_exit(void)
#define EVAL_OPTION
Definition: options-usage.h:53
OCTINTERP_API octave_value_list Fecho_executing_commands(const octave_value_list &=octave_value_list(), int=0)
#define panic_impossible()
Definition: error.h:40
static void assign(const std::string &name, const octave_value &value=octave_value(), scope_id scope=xcurrent_scope, context_id context=xdefault_context, bool force_add=false)
Definition: symtab.h:1330
std::string pname
Definition: graphics.cc:11207
char * octave_optarg_wrapper(void)
void warning(const char *fmt,...)
Definition: error.cc:788
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:354
int octave_getopt_long_wrapper(int argc, char **argv, const char *shortopts, const struct octave_getopt_options *longopts, int *longind)
static void mark_hidden(const std::string &name, scope_id scope=xcurrent_scope)
Definition: symtab.h:1966
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
static void set_program_name(const std::string &s)
Definition: oct-env.cc:171
static const char * short_opts
Definition: options-usage.h:47
struct octave_getopt_options long_opts[]
Definition: options-usage.h:70
#define DOC_CACHE_FILE_OPTION
Definition: options-usage.h:52
#define BUILT_IN_DOCSTRINGS_FILE_OPTION
Definition: options-usage.h:51
static void octave_print_verbose_usage_and_exit(void)
int octave_optind_wrapper(void)
OCTINTERP_API octave_value_list Finfo_file(const octave_value_list &=octave_value_list(), int=0)
int octave_debug
#define NO_INIT_PATH_OPTION
Definition: options-usage.h:64
void install_defaults(void)
Definition: defaults.cc:416
#define NO_INIT_FILE_OPTION
Definition: options-usage.h:63
#define INFO_PROG_OPTION
Definition: options-usage.h:58
OCTINTERP_API octave_value_list Fbuilt_in_docstrings_file(const octave_value_list &=octave_value_list(), int=0)
Definition: help.cc:521
OCTINTERP_API octave_value_list Fdoc_cache_file(const octave_value_list &=octave_value_list(), int=0)
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:854
#define FORCE_GUI_OPTION
Definition: options-usage.h:55
#define JIT_COMPILER_OPTION
Definition: options-usage.h:60
void sysdep_init(void)
Definition: sysdep.cc:327