GNU Octave  8.1.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-2023 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.h"
52 #include "ov.h"
53 #include "parse.h"
54 #include "sysdep.h"
55 #include "usage.h"
56 
58 
60 {
61  m_all_args.resize (1);
62  m_all_args[0] = "";
63 }
64 
65 cmdline_options::cmdline_options (int argc, char **argv)
66 {
67  // Save raw program arguments.
68  m_all_args = string_vector (argv, argc);
69 
70  while (true)
71  {
72  int long_idx;
73 
74  int optc = octave_getopt_long_wrapper (argc, argv, short_opts,
75  long_opts, &long_idx);
76 
77  if (optc < 0)
78  break;
79 
80  switch (optc)
81  {
82  case '?':
83  // Unrecognized option. getopt_long already printed a message about
84  // it, so we will just print the usage string and exit.
86  break;
87 
88  case 'H':
89  m_read_history_file = false;
90  break;
91 
92  case 'W':
93  m_no_window_system = true;
94  break;
95 
96  case 'V':
97  m_verbose_flag = true;
98  break;
99 
100  case 'd':
101  // This is the same as yydebug in parse.y.
102  octave_debug++;
103  break;
104 
105  case 'f':
106  m_read_init_files = false;
107  m_read_site_files = false;
108  break;
109 
110  case 'h':
112  break;
113 
114  case 'i':
115  m_forced_interactive = true;
116  break;
117 
118  case 'p':
119  if (octave_optarg_wrapper ())
121  break;
122 
123  case 'q':
125  break;
126 
127  case 'x':
128  m_echo_commands = true;
129  break;
130 
131  case 'v':
133  break;
134 
136  if (octave_optarg_wrapper ())
138  break;
139 
141  if (octave_optarg_wrapper ())
143  break;
144 
145  case EVAL_OPTION:
146  if (octave_optarg_wrapper ())
147  {
148  if (m_code_to_eval.empty ())
150  else
151  m_code_to_eval += (std::string (" ")
152  + octave_optarg_wrapper ());
153  }
154  break;
155 
156  case EXEC_PATH_OPTION:
157  if (octave_optarg_wrapper ())
159  break;
160 
162 #if defined (HAVE_QSCINTILLA)
164 #endif
165  break;
166 
167  case GUI_OPTION:
168  m_gui = true;
169  break;
170 
171  case IMAGE_PATH_OPTION:
172  if (octave_optarg_wrapper ())
174  break;
175 
176  case INFO_FILE_OPTION:
177  if (octave_optarg_wrapper ())
179  break;
180 
181  case INFO_PROG_OPTION:
182  if (octave_optarg_wrapper ())
184  break;
185 
186  case LINE_EDITING_OPTION:
188  break;
189 
190  case NO_GUI_OPTION:
191  m_gui = false;
192  break;
193 
194  case NO_INIT_FILE_OPTION:
195  m_read_init_files = false;
196  break;
197 
198  case NO_INIT_PATH_OPTION:
199  m_set_initial_path = false;
200  break;
201 
203  m_line_editing = false;
204  break;
205 
206  case NO_SITE_FILE_OPTION:
207  m_read_site_files = false;
208  break;
209 
210  case PERSIST_OPTION:
211  m_persist = true;
212  break;
213 
214  case SERVER_OPTION:
215  m_server = true;
216  break;
217 
219  if (octave_optarg_wrapper ())
221  break;
222 
223  case TRADITIONAL_OPTION:
224  m_traditional = true;
225  m_persist = true;
226  break;
227 
228  default:
229  // getopt_long should print a message about unrecognized options and
230  // return '?', which is handled above. If we end up here, it is
231  // because there was an option but we forgot to handle it.
232  // That should be fatal.
233  panic_impossible ();
234  break;
235  }
236  }
237 
239  argc-octave_optind_wrapper ());
240 }
241 
243 {
245 
246  m.assign ("sys_argc", sys_argc ());
247  m.assign ("sys_argv", Cell (string_vector (sys_argv ())));
248  m.assign ("echo_commands", echo_commands ());
249  m.assign ("forced_interactive", forced_interactive ());
250  m.assign ("forced_line_editing", forced_line_editing ());
251  m.assign ("gui", gui ());
252  m.assign ("inhibit_startup_message", inhibit_startup_message ());
253  m.assign ("line_editing", line_editing ());
254  m.assign ("no_window_system", no_window_system ());
255  m.assign ("persist", persist ());
256  m.assign ("read_history_file", read_history_file ());
257  m.assign ("read_init_files", read_init_files ());
258  m.assign ("read_site_files", read_site_files ());
259  m.assign ("server", server ());
260  m.assign ("set_initial_path", set_initial_path ());
261  m.assign ("traditional", traditional ());
262  m.assign ("verbose_flag", verbose_flag ());
263  m.assign ("code_to_eval", code_to_eval ());
264  m.assign ("command_line_path", string_vector (command_line_path ()));
265  m.assign ("docstrings_file", docstrings_file ());
266  m.assign ("doc_cache_file", doc_cache_file ());
267  m.assign ("exec_path", exec_path ());
268  m.assign ("image_path", image_path ());
269  m.assign ("info_file", info_file ());
270  m.assign ("info_program", info_program ());
271  m.assign ("texi_macros_file", texi_macros_file ());
272  m.assign ("all_args", Cell (all_args ()));
273  m.assign ("remaining_args", Cell (remaining_args ()));
274 
275  return m;
276 }
277 
279 
280 application::application (int argc, char **argv)
281  : m_options (argc, argv)
282 {
283  init ();
284 }
285 
287  : m_options (opts)
288 {
289  init ();
290 }
291 
292 // Note: Although the application destructor doesn't explicitly
293 // perform any actions, it can't be declared "default" in the header
294 // file if the interpreter is an incomplete type. Providing
295 // an explicit definition of the destructor here is much simpler than
296 // including the full declaration of interpreter in the
297 // octave.h header file.
299 
300 void
301 application::set_program_names (const std::string& pname)
302 {
304 
305  std::size_t pos = pname.find_last_of (sys::file_ops::dir_sep_chars ());
306 
307  m_program_name = (pos != std::string::npos) ? pname.substr (pos+1) : pname;
308 }
309 
310 void
312 {
313  octave_idx_type nargs = args.numel ();
314 
315  if (nargs > 0)
316  {
317  // Skip first argument (program name).
318  nargs--;
319 
320  m_argv.resize (nargs);
321 
322  for (octave_idx_type i = 0; i < nargs; i++)
323  m_argv[i] = args[i+1];
324  }
325 }
326 
328 {
329  return s_instance ? s_instance->m_options.forced_interactive () : false;
330 }
331 
332 // Provided for convenience. Will be removed once we eliminate the
333 // old terminal widget.
335 {
336  return (s_instance
338 }
339 
341 {
342  return m_interpreter ? m_interpreter->initialized () : false;
343 }
344 
346 {
347  if (! m_interpreter)
348  m_interpreter = std::unique_ptr<interpreter> (new interpreter (this));
349 
350  return *m_interpreter;
351 }
352 
354 {
355  if (m_interpreter)
356  m_interpreter->initialize ();
357 }
358 
360 {
361  return m_interpreter ? m_interpreter->execute () : -1;
362 }
363 
365 {
366  m_interpreter.reset ();
367 }
368 
369 void application::init (void)
370 {
371  if (s_instance)
372  throw std::runtime_error
373  ("only one Octave application object may be active");
374 
375  s_instance = this;
376 
377  string_vector all_args = m_options.all_args ();
378 
379  set_program_names (all_args[0]);
380 
381  string_vector remaining_args = m_options.remaining_args ();
382 
383  std::string code_to_eval = m_options.code_to_eval ();
384 
385  m_have_script_file = ! remaining_args.empty ();
386 
387  m_have_eval_option_code = ! code_to_eval.empty ();
388 
390  {
391  std::cerr << R"(error: --eval "CODE" and script file are mutually exclusive options)" << std::endl;
392 
394  }
395 
396  if (m_options.gui ())
397  {
399  {
400  std::cerr << "error: --gui and --no-window-system are mutually exclusive options" << std::endl;
402  }
403  if (! m_options.line_editing ())
404  {
405  std::cerr << "error: --gui and --no-line-editing are mutually exclusive options" << std::endl;
407  }
408  if (m_options.server ())
409  {
410  std::cerr << "error: --gui and --server are mutually exclusive options" << std::endl;
412  }
413  }
414 
416  && ! m_options.persist ()
417  && ! m_options.traditional ());
418 
419  // This should probably happen early.
420  sysdep_init ();
421 }
422 
424 {
425  interpreter& interp = create_interpreter ();
426 
427  int status = interp.execute ();
428 
429  return status;
430 }
431 
432 DEFUN (isguirunning, args, ,
433  doc: /* -*- texinfo -*-
434 @deftypefn {} {@var{tf} =} isguirunning ()
435 Return true if Octave is running in GUI mode and false otherwise.
436 @seealso{have_window_system}
437 @end deftypefn */)
438 {
439  if (args.length () != 0)
440  print_usage ();
441 
442  // FIXME: This isn't quite right, it just says that we intended to
443  // start the GUI, not that it is actually running.
444 
445  return ovl (application::is_gui_running ());
446 }
447 
448 /*
449 %!assert (islogical (isguirunning ()))
450 %!error <Invalid call> isguirunning (1)
451 */
452 
453 DEFUN (argv, args, ,
454  doc: /* -*- texinfo -*-
455 @deftypefn {} {@var{args} =} argv ()
456 Return the command line arguments passed to Octave.
457 
458 For example, if you invoked Octave using the command
459 
460 @example
461 octave --no-line-editing --silent
462 @end example
463 
464 @noindent
465 @code{argv} would return a cell array of strings with the elements
466 @option{--no-line-editing} and @option{--silent}.
467 
468 If you write an executable Octave script, @code{argv} will return the list
469 of arguments passed to the script. @xref{Executable Octave Programs}, for
470 an example of how to create an executable Octave script.
471 @seealso{program_name, cmdline_options}
472 @end deftypefn */)
473 {
474  if (args.length () != 0)
475  print_usage ();
476 
477  return ovl (Cell (application::argv ()));
478 }
479 
480 /*
481 %!assert (iscellstr (argv ()))
482 %!error <Invalid call> argv (1)
483 */
484 
485 DEFUN (cmdline_options, args, ,
486  doc: /* -*- texinfo -*-
487 @deftypefn {} {@var{opt_struct} =} cmdline_options ()
488 Return a structure containing detailed information about the command line
489 arguments passed to Octave.
490 
491 Programming Note: This function provides copious amounts of information about
492 Octave's parsing of command line options and may be more useful for debugging
493 Octave rather than for general use.
494 @seealso{argv, program_name}
495 @end deftypefn */)
496 {
497  if (args.length () != 0)
498  print_usage ();
499 
500  application *app = application::app ();
501 
502  if (! app)
503  error ("invalid application context!");
504 
505  cmdline_options opts = app->options ();
506 
507  return ovl (opts.as_octave_value ());
508 }
509 
510 /*
511 %!assert (isstruct (cmdline_options ()))
512 %!error <Invalid call> cmdline_options (1)
513 */
514 
515 DEFUN (program_invocation_name, args, ,
516  doc: /* -*- texinfo -*-
517 @deftypefn {} {@var{name} =} program_invocation_name ()
518 Return the string that was typed at the shell prompt to run Octave.
519 
520 The string may include path components as well as the program filename.
521 
522 If executing a script from the command line (e.g., @code{octave foo.m}) or
523 using an executable Octave script, the program name is set to the name of the
524 script. @xref{Executable Octave Programs}, for an example of how to create an
525 executable Octave script.
526 @seealso{program_name, argv}
527 @end deftypefn */)
528 {
529  if (args.length () != 0)
530  print_usage ();
531 
533 }
534 
535 /*
536 %!assert (ischar (program_invocation_name ()))
537 %!error <Invalid call> program_invocation_name (1)
538 */
539 
540 DEFUN (program_name, args, ,
541  doc: /* -*- texinfo -*-
542 @deftypefn {} {@var{name} =} program_name ()
543 Return the filename component of the value returned by
544 @code{program_invocation_name}.
545 
546 @seealso{program_invocation_name, argv}
547 @end deftypefn */)
548 {
549  if (args.length () != 0)
550  print_usage ();
551 
552  return ovl (application::program_name ());
553 }
554 
555 /*
556 %!assert (ischar (program_name ()))
557 %!error <Invalid call> program_name (1)
558 */
559 
OCTAVE_END_NAMESPACE(octave)
Definition: Cell.h:43
application(const cmdline_options &opts=cmdline_options())
Definition: octave.cc:286
static application * app(void)
Definition: octave.h:301
cmdline_options options(void) const
Definition: octave.h:267
static string_vector argv(void)
Definition: octave.h:313
void set_program_names(const std::string &pname)
Definition: octave.cc:301
static bool forced_interactive(void)
Definition: octave.cc:327
virtual ~application(void)
Definition: octave.cc:298
bool experimental_terminal_widget(void) const
Definition: octave.cc:334
static std::string program_invocation_name(void)
Definition: octave.h:303
void intern_argv(const string_vector &args)
Definition: octave.cc:311
bool interpreter_initialized(void)
Definition: octave.cc:340
static bool is_gui_running(void)
Definition: octave.h:318
virtual void delete_interpreter(void)
Definition: octave.cc:364
string_vector m_argv
Definition: octave.h:346
virtual interpreter & create_interpreter(void)
Definition: octave.cc:345
bool m_have_script_file
Definition: octave.h:355
bool m_is_octave_program
Definition: octave.h:360
void init(void)
Definition: octave.cc:369
static application * s_instance
Definition: octave.h:330
bool m_have_eval_option_code
Definition: octave.h:351
std::string m_program_name
Definition: octave.h:340
virtual int execute_interpreter(void)
Definition: octave.cc:359
std::unique_ptr< interpreter > m_interpreter
Definition: octave.h:362
static std::string program_name(void)
Definition: octave.h:308
cmdline_options m_options
Definition: octave.h:348
std::string m_program_invocation_name
Definition: octave.h:337
virtual void initialize_interpreter(void)
Definition: octave.cc:353
int execute(void)
Definition: octave.cc:423
std::string info_file(void) const
Definition: octave.h:85
std::string docstrings_file(void) const
Definition: octave.h:81
bool m_read_history_file
Definition: octave.h:165
std::list< std::string > m_command_line_path
Definition: octave.h:197
std::string m_doc_cache_file
Definition: octave.h:206
string_vector m_remaining_args
Definition: octave.h:232
std::string m_texi_macros_file
Definition: octave.h:226
bool server(void) const
Definition: octave.h:74
bool m_inhibit_startup_message
Definition: octave.h:149
octave_value as_octave_value(void) const
Definition: octave.cc:242
bool m_forced_interactive
Definition: octave.h:141
bool m_no_window_system
Definition: octave.h:157
bool line_editing(void) const
Definition: octave.h:67
std::string doc_cache_file(void) const
Definition: octave.h:82
std::string texi_macros_file(void) const
Definition: octave.h:87
bool m_forced_line_editing
Definition: octave.h:145
string_vector remaining_args(void) const
Definition: octave.h:89
bool persist(void) const
Definition: octave.h:70
std::string m_info_program
Definition: octave.h:222
bool verbose_flag(void) const
Definition: octave.h:77
bool no_window_system(void) const
Definition: octave.h:69
std::string exec_path(void) const
Definition: octave.h:83
bool m_experimental_terminal_widget
Definition: octave.h:133
bool m_server
Definition: octave.h:177
char ** sys_argv(void) const
Definition: octave.h:56
string_vector all_args(void) const
Definition: octave.h:88
bool gui(void) const
Definition: octave.h:64
bool traditional(void) const
Definition: octave.h:76
bool m_set_initial_path
Definition: octave.h:181
bool m_verbose_flag
Definition: octave.h:189
int sys_argc(void) const
Definition: octave.h:55
bool forced_interactive(void) const
Definition: octave.h:62
cmdline_options(void)
Definition: octave.cc:59
bool m_read_site_files
Definition: octave.h:173
bool m_read_init_files
Definition: octave.h:169
bool m_echo_commands
Definition: octave.h:129
bool m_traditional
Definition: octave.h:185
std::string code_to_eval(void) const
Definition: octave.h:78
bool read_site_files(void) const
Definition: octave.h:73
std::string m_image_path
Definition: octave.h:214
bool experimental_terminal_widget(void) const
Definition: octave.h:60
bool forced_line_editing(void) const
Definition: octave.h:63
bool read_history_file(void) const
Definition: octave.h:71
bool set_initial_path(void) const
Definition: octave.h:75
std::list< std::string > command_line_path(void) const
Definition: octave.h:79
std::string m_exec_path
Definition: octave.h:210
std::string info_program(void) const
Definition: octave.h:86
bool m_persist
Definition: octave.h:161
string_vector m_all_args
Definition: octave.h:229
std::string image_path(void) const
Definition: octave.h:84
std::string m_code_to_eval
Definition: octave.h:193
bool inhibit_startup_message(void) const
Definition: octave.h:65
bool m_line_editing
Definition: octave.h:153
bool echo_commands(void) const
Definition: octave.h:58
std::string m_docstrings_file
Definition: octave.h:202
bool read_init_files(void) const
Definition: octave.h:72
std::string m_info_file
Definition: octave.h:218
int execute(void)
Definition: interpreter.cc:841
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
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTINTERP_API void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:979
#define panic_impossible()
Definition: error.h:508
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)
T octave_idx_type m
Definition: mx-inlines.cc:773
#define INFO_FILE_OPTION
Definition: options.h:47
#define NO_INIT_PATH_OPTION
Definition: options.h:53
#define NO_GUI_OPTION
Definition: options.h:50
#define LINE_EDITING_OPTION
Definition: options.h:49
#define TRADITIONAL_OPTION
Definition: options.h:59
#define INFO_PROG_OPTION
Definition: options.h:48
static const char * short_opts
Definition: options.h:36
#define BUILT_IN_DOCSTRINGS_FILE_OPTION
Definition: options.h:40
#define SERVER_OPTION
Definition: options.h:57
#define DOC_CACHE_FILE_OPTION
Definition: options.h:41
#define GUI_OPTION
Definition: options.h:45
#define EXPERIMENTAL_TERMINAL_WIDGET_OPTION
Definition: options.h:44
#define NO_SITE_FILE_OPTION
Definition: options.h:55
#define NO_LINE_EDITING_OPTION
Definition: options.h:54
#define TEXI_MACROS_FILE_OPTION
Definition: options.h:58
struct octave_getopt_options long_opts[]
Definition: options.h:60
#define EXEC_PATH_OPTION
Definition: options.h:43
#define IMAGE_PATH_OPTION
Definition: options.h:46
#define PERSIST_OPTION
Definition: options.h:56
#define EVAL_OPTION
Definition: options.h:42
#define NO_INIT_FILE_OPTION
Definition: options.h:52
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
int octave_debug
static std::string dir_sep_chars
Definition: shared-fcns.h:96
void sysdep_init(void)
Definition: sysdep.cc:509
static void octave_print_verbose_usage_and_exit(void)
Definition: usage.h:49
static void octave_print_version_and_exit(void)
Definition: usage.h:114
static void octave_print_terse_usage_and_exit(void)
Definition: usage.h:106