GNU Octave 7.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-2022 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
57OCTAVE_NAMESPACE_BEGIN
58
60 {
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':
116 break;
117
118 case 'p':
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
138 break;
139
143 break;
144
145 case EVAL_OPTION:
147 {
148 if (m_code_to_eval.empty ())
150 else
151 m_code_to_eval += (std::string (" ")
153 }
154 break;
155
156 case EXEC_PATH_OPTION:
159 break;
160
163 break;
164
165 case GUI_OPTION:
166 m_gui = true;
167 break;
168
172 break;
173
174 case INFO_FILE_OPTION:
177 break;
178
179 case INFO_PROG_OPTION:
182 break;
183
186 break;
187
188 case NO_GUI_OPTION:
189 m_gui = false;
190 break;
191
193 m_read_init_files = false;
194 break;
195
197 m_set_initial_path = false;
198 break;
199
201 m_line_editing = false;
202 break;
203
205 m_read_site_files = false;
206 break;
207
208 case PERSIST_OPTION:
209 m_persist = true;
210 break;
211
212 case SERVER_OPTION:
213 m_server = true;
214 break;
215
219 break;
220
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.
232 break;
233 }
234 }
235
237 argc-octave_optind_wrapper ());
238 }
239
241 {
243
244 m.assign ("sys_argc", sys_argc ());
245 m.assign ("sys_argv", string_vector (sys_argv ()));
246 m.assign ("echo_commands", echo_commands ());
247 m.assign ("forced_interactive", forced_interactive ());
248 m.assign ("forced_line_editing", forced_line_editing ());
249 m.assign ("gui", gui ());
250 m.assign ("inhibit_startup_message", inhibit_startup_message ());
251 m.assign ("line_editing", line_editing ());
252 m.assign ("no_window_system", no_window_system ());
253 m.assign ("persist", persist ());
254 m.assign ("read_history_file", read_history_file ());
255 m.assign ("read_init_files", read_init_files ());
256 m.assign ("read_site_files", read_site_files ());
257 m.assign ("server", server ());
258 m.assign ("set_initial_path", set_initial_path ());
259 m.assign ("traditional", traditional ());
260 m.assign ("verbose_flag", verbose_flag ());
261 m.assign ("code_to_eval", code_to_eval ());
262 m.assign ("command_line_path", string_vector (command_line_path ()));
263 m.assign ("docstrings_file", docstrings_file ());
264 m.assign ("doc_cache_file", doc_cache_file ());
265 m.assign ("exec_path", exec_path ());
266 m.assign ("image_path", image_path ());
267 m.assign ("info_file", info_file ());
268 m.assign ("info_program", info_program ());
269 m.assign ("texi_macros_file", texi_macros_file ());
270 m.assign ("all_args", all_args ());
271 m.assign ("remaining_args", remaining_args ());
272
273 return m;
274 }
275
277
278 application::application (int argc, char **argv)
279 : m_options (argc, argv)
280 {
281 init ();
282 }
283
285 : m_options (opts)
286 {
287 init ();
288 }
289
290 // Note: Although the application destructor doesn't explicitly
291 // perform any actions, it can't be declared "default" in the header
292 // file if the interpreter is an incomplete type. Providing
293 // an explicit definition of the destructor here is much simpler than
294 // including the full declaration of interpreter in the
295 // octave.h header file.
297
298 void
299 application::set_program_names (const std::string& pname)
300 {
302
303 std::size_t pos = pname.find_last_of (sys::file_ops::dir_sep_chars ());
304
305 m_program_name = (pos != std::string::npos) ? pname.substr (pos+1) : pname;
306 }
307
308 void
310 {
311 octave_idx_type nargs = args.numel ();
312
313 if (nargs > 0)
314 {
315 // Skip first argument (program name).
316 nargs--;
317
318 m_argv.resize (nargs);
319
320 for (octave_idx_type i = 0; i < nargs; i++)
321 m_argv[i] = args[i+1];
322 }
323 }
324
326 {
328 }
329
330 // Provided for convenience. Will be removed once we eliminate the
331 // old terminal widget.
333 {
334 return (s_instance
336 }
337
339 {
340 return m_interpreter ? m_interpreter->initialized () : false;
341 }
342
344 {
345 if (! m_interpreter)
346 m_interpreter = std::unique_ptr<interpreter> (new interpreter (this));
347
348 return *m_interpreter;
349 }
350
352 {
353 if (m_interpreter)
354 m_interpreter->initialize ();
355 }
356
358 {
359 return m_interpreter ? m_interpreter->execute () : -1;
360 }
361
363 {
364 m_interpreter.reset ();
365 }
366
368 {
369 if (s_instance)
370 throw std::runtime_error
371 ("only one Octave application object may be active");
372
373 s_instance = this;
374
375 string_vector all_args = m_options.all_args ();
376
377 set_program_names (all_args[0]);
378
379 string_vector remaining_args = m_options.remaining_args ();
380
381 std::string code_to_eval = m_options.code_to_eval ();
382
383 m_have_script_file = ! remaining_args.empty ();
384
385 m_have_eval_option_code = ! code_to_eval.empty ();
386
388 {
389 std::cerr << R"(error: --eval "CODE" and script file are mutually exclusive options)" << std::endl;
390
392 }
393
394 if (m_options.gui ())
395 {
397 {
398 std::cerr << "error: --gui and --no-window-system are mutually exclusive options" << std::endl;
400 }
401 if (! m_options.line_editing ())
402 {
403 std::cerr << "error: --gui and --no-line-editing are mutually exclusive options" << std::endl;
405 }
406 if (m_options.server ())
407 {
408 std::cerr << "error: --gui and --server are mutually exclusive options" << std::endl;
410 }
411 }
412
414 && ! m_options.persist ()
415 && ! m_options.traditional ());
416
417 // This should probably happen early.
418 sysdep_init ();
419 }
420
422 {
423 interpreter& interp = create_interpreter ();
424
425 int status = interp.execute ();
426
427 return status;
428 }
429
430DEFUN (isguirunning, args, ,
431 doc: /* -*- texinfo -*-
432@deftypefn {} {} isguirunning ()
433Return true if Octave is running in GUI mode and false otherwise.
434@seealso{have_window_system}
435@end deftypefn */)
436{
437 if (args.length () != 0)
438 print_usage ();
439
440 // FIXME: This isn't quite right, it just says that we intended to
441 // start the GUI, not that it is actually running.
442
444}
445
446/*
447%!assert (islogical (isguirunning ()))
448%!error isguirunning (1)
449*/
450
451DEFUN (argv, args, ,
452 doc: /* -*- texinfo -*-
453@deftypefn {} {} argv ()
454Return the command line arguments passed to Octave.
455
456For example, if you invoked Octave using the command
457
458@example
459octave --no-line-editing --silent
460@end example
461
462@noindent
463@code{argv} would return a cell array of strings with the elements
464@option{--no-line-editing} and @option{--silent}.
465
466If you write an executable Octave script, @code{argv} will return the list
467of arguments passed to the script. @xref{Executable Octave Programs}, for
468an example of how to create an executable Octave script.
469@end deftypefn */)
470{
471 if (args.length () != 0)
472 print_usage ();
473
474 return ovl (Cell (application::argv ()));
475}
476
477/*
478%!assert (iscellstr (argv ()))
479%!error argv (1)
480*/
481
482DEFUN (cmdline_options, args, ,
483 doc: /* -*- texinfo -*-
484@deftypefn {} {} argv ()
485Return a structure containing info about the command line arguments
486passed to Octave.
487@end deftypefn */)
488{
489 if (args.length () != 0)
490 print_usage ();
491
493
494 if (! app)
495 error ("invalid application context!");
496
497 cmdline_options opts = app->options ();
498
499 return ovl (opts.as_octave_value ());
500}
501
502DEFUN (program_invocation_name, args, ,
503 doc: /* -*- texinfo -*-
504@deftypefn {} {} program_invocation_name ()
505Return the name that was typed at the shell prompt to run Octave.
506
507If executing a script from the command line (e.g., @code{octave foo.m})
508or using an executable Octave script, the program name is set to the
509name of the script. @xref{Executable Octave Programs}, for an example of
510how to create an executable Octave script.
511@seealso{program_name}
512@end deftypefn */)
513{
514 if (args.length () != 0)
515 print_usage ();
516
518}
519
520/*
521%!assert (ischar (program_invocation_name ()))
522%!error program_invocation_name (1)
523*/
524
525DEFUN (program_name, args, ,
526 doc: /* -*- texinfo -*-
527@deftypefn {} {} program_name ()
528Return the last component of the value returned by
529@code{program_invocation_name}.
530@seealso{program_invocation_name}
531@end deftypefn */)
532{
533 if (args.length () != 0)
534 print_usage ();
535
536 return ovl (application::program_name ());
537}
538
539/*
540%!assert (ischar (program_name ()))
541%!error program_name (1)
542*/
543
544OCTAVE_NAMESPACE_END
Definition: Cell.h:43
application(const cmdline_options &opts=cmdline_options())
Definition: octave.cc:284
cmdline_options options(void) const
Definition: octave.h:262
static string_vector argv(void)
Definition: octave.h:307
void set_program_names(const std::string &pname)
Definition: octave.cc:299
static bool forced_interactive(void)
Definition: octave.cc:325
virtual ~application(void)
Definition: octave.cc:296
bool experimental_terminal_widget(void) const
Definition: octave.cc:332
static std::string program_invocation_name(void)
Definition: octave.h:297
void intern_argv(const string_vector &args)
Definition: octave.cc:309
bool interpreter_initialized(void)
Definition: octave.cc:338
static bool is_gui_running(void)
Definition: octave.h:312
virtual void delete_interpreter(void)
Definition: octave.cc:362
string_vector m_argv
Definition: octave.h:340
static application * app(void)
Definition: octave.h:295
virtual interpreter & create_interpreter(void)
Definition: octave.cc:343
bool m_have_script_file
Definition: octave.h:349
bool m_is_octave_program
Definition: octave.h:354
void init(void)
Definition: octave.cc:367
static application * s_instance
Definition: octave.h:324
bool m_have_eval_option_code
Definition: octave.h:345
std::string m_program_name
Definition: octave.h:334
virtual int execute_interpreter(void)
Definition: octave.cc:357
std::unique_ptr< interpreter > m_interpreter
Definition: octave.h:356
static std::string program_name(void)
Definition: octave.h:302
cmdline_options m_options
Definition: octave.h:342
std::string m_program_invocation_name
Definition: octave.h:331
virtual void initialize_interpreter(void)
Definition: octave.cc:351
int execute(void)
Definition: octave.cc:421
std::string info_file(void) const
Definition: octave.h:82
std::string docstrings_file(void) const
Definition: octave.h:78
bool m_read_history_file
Definition: octave.h:160
std::list< std::string > m_command_line_path
Definition: octave.h:192
std::string m_doc_cache_file
Definition: octave.h:201
string_vector m_remaining_args
Definition: octave.h:227
std::string m_texi_macros_file
Definition: octave.h:221
bool server(void) const
Definition: octave.h:72
bool m_inhibit_startup_message
Definition: octave.h:144
octave_value as_octave_value(void) const
Definition: octave.cc:240
bool m_forced_interactive
Definition: octave.h:136
bool m_no_window_system
Definition: octave.h:152
char ** sys_argv(void) const
Definition: octave.h:56
bool line_editing(void) const
Definition: octave.h:65
std::list< std::string > command_line_path(void) const
Definition: octave.h:77
std::string doc_cache_file(void) const
Definition: octave.h:79
std::string texi_macros_file(void) const
Definition: octave.h:84
bool m_forced_line_editing
Definition: octave.h:140
string_vector remaining_args(void) const
Definition: octave.h:86
bool persist(void) const
Definition: octave.h:68
std::string m_info_program
Definition: octave.h:217
bool verbose_flag(void) const
Definition: octave.h:75
bool no_window_system(void) const
Definition: octave.h:67
std::string exec_path(void) const
Definition: octave.h:80
bool m_experimental_terminal_widget
Definition: octave.h:128
bool m_server
Definition: octave.h:172
string_vector all_args(void) const
Definition: octave.h:85
bool gui(void) const
Definition: octave.h:63
bool traditional(void) const
Definition: octave.h:74
bool m_set_initial_path
Definition: octave.h:176
bool m_verbose_flag
Definition: octave.h:184
cmdline_options(void)
Definition: octave.cc:59
int sys_argc(void) const
Definition: octave.h:55
bool forced_interactive(void) const
Definition: octave.h:61
bool m_read_site_files
Definition: octave.h:168
bool m_read_init_files
Definition: octave.h:164
bool m_echo_commands
Definition: octave.h:124
bool m_traditional
Definition: octave.h:180
std::string code_to_eval(void) const
Definition: octave.h:76
bool read_site_files(void) const
Definition: octave.h:71
std::string m_image_path
Definition: octave.h:209
bool experimental_terminal_widget(void) const
Definition: octave.h:60
bool forced_line_editing(void) const
Definition: octave.h:62
bool read_history_file(void) const
Definition: octave.h:69
bool set_initial_path(void) const
Definition: octave.h:73
std::string m_exec_path
Definition: octave.h:205
std::string info_program(void) const
Definition: octave.h:83
bool m_persist
Definition: octave.h:156
string_vector m_all_args
Definition: octave.h:224
std::string image_path(void) const
Definition: octave.h:81
std::string m_code_to_eval
Definition: octave.h:188
bool inhibit_startup_message(void) const
Definition: octave.h:64
bool m_line_editing
Definition: octave.h:148
bool echo_commands(void) const
Definition: octave.h:58
std::string m_docstrings_file
Definition: octave.h:197
bool read_init_files(void) const
Definition: octave.h:70
std::string m_info_file
Definition: octave.h:213
int execute(void)
Definition: interpreter.cc:833
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:238
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-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:980
#define panic_impossible()
Definition: error.h:411
int octave_getopt_long_wrapper(int argc, char **argv, const char *shortopts, const struct octave_getopt_options *longopts, int *longind)
char * octave_optarg_wrapper(void)
int octave_optind_wrapper(void)
#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