GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
input.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 // Get command input interactively or from files.
24 
25 #if defined (HAVE_CONFIG_H)
26 # include "config.h"
27 #endif
28 
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cstring>
32 #include <cassert>
33 
34 #include <iostream>
35 #include <sstream>
36 #include <string>
37 
38 #include "cmd-edit.h"
39 #include "file-ops.h"
40 #include "quit.h"
41 #include "str-vec.h"
42 
43 #include "bp-table.h"
44 #include "builtin-defun-decls.h"
45 #include "call-stack.h"
46 #include "defun.h"
47 #include "dirfns.h"
48 #include "error.h"
49 #include "errwarn.h"
50 #include "help.h"
51 #include "hook-fcn.h"
52 #include "input.h"
53 #include "interpreter-private.h"
54 #include "interpreter.h"
55 #include "lex.h"
56 #include "load-path.h"
57 #include "octave.h"
58 #include "octave-link.h"
59 #include "oct-map.h"
60 #include "oct-hist.h"
61 #include "interpreter.h"
62 #include "octave-link.h"
63 #include "ovl.h"
64 #include "ov-fcn-handle.h"
65 #include "ov-usr-fcn.h"
66 #include "pager.h"
67 #include "parse.h"
68 #include "pt-eval.h"
69 #include "pt-stmt.h"
70 #include "sighandlers.h"
71 #include "sysdep.h"
72 #include "interpreter.h"
73 #include "unwind-prot.h"
74 #include "utils.h"
75 #include "variables.h"
76 
77 // Primary prompt string.
79 
80 // Secondary prompt string.
82 
83 // The time we last printed a prompt.
85 
86 // Character to append after successful command-line completion attempts.
87 static char Vcompletion_append_char = ' ';
88 
89 // TRUE after a call to completion_matches.
91 
92 // TRUE if the plotting system has requested a call to drawnow at
93 // the next user prompt.
94 bool Vdrawnow_requested = false;
95 
96 // TRUE if we are in debugging mode.
97 bool Vdebugging = false;
98 
99 // TRUE if we are recording line numbers in a source file.
100 // Always true except when debugging and taking input directly from
101 // the terminal.
102 bool Vtrack_line_num = true;
103 
104 // If we are in debugging mode, this is the last command entered, so
105 // that we can repeat the previous command if the user just types RET.
107 
108 // TRUE if we are running in the Emacs GUD mode.
109 static bool Vgud_mode = false;
110 
112 
113 // For octave_quit.
114 void
116 {
118 }
119 
120 void
122 {
123  // Use literal "octave" instead of "\\s" to avoid setting the prompt
124  // to "octave.exe" or "octave-gui", etc.
125 
126  VPS1 = R"(octave:\#> )";
127  VPS2 = "> ";
128  std::string VPS4 = "+ ";
129 
131 }
132 
133 static std::string
134 gnu_readline (const std::string& s, bool& eof)
135 {
136  octave_quit ();
137 
138  eof = false;
139 
141 
142  if (! eof && retval.empty ())
143  retval = "\n";
144 
145  return retval;
146 }
147 
148 static inline std::string
149 interactive_input (const std::string& s, bool& eof)
150 {
152 
153  if (Vdrawnow_requested && octave::application::interactive ())
154  {
155  bool eval_error = false;
156 
157  try
158  {
159  Fdrawnow ();
160  }
161  catch (const octave::execution_exception& e)
162  {
163  eval_error = true;
164 
165  std::string stack_trace = e.info ();
166 
167  if (! stack_trace.empty ())
168  std::cerr << stack_trace;
169 
170  if (octave::application::interactive ())
172  }
173 
175 
176  // We set Vdrawnow_requested to false even if there is an error in
177  // drawnow so that the error doesn't reappear at every prompt.
178 
179  Vdrawnow_requested = false;
180 
181  if (eval_error)
182  return "\n";
183  }
184 
185  return gnu_readline (s, eof);
186 }
187 
188 namespace octave
189 {
191  {
192  octave_quit ();
193 
194  eof = false;
195 
197 
198  // Process pre input event hook function prior to flushing output and
199  // printing the prompt.
200 
201  if (application::interactive ())
202  {
203  if (! Vdebugging)
205 
207 
209  }
210 
211  bool history_skip_auto_repeated_debugging_command = false;
212 
213  std::string ps = (m_pflag > 0) ? VPS1 : VPS2;
214 
216 
218 
219  flush_stdout ();
220 
223 
224  octave_diary << prompt;
225 
226  retval = interactive_input (prompt, eof);
227 
228  // There is no need to update the load_path cache if there is no
229  // user input.
230  if (retval != "\n"
231  && retval.find_first_not_of (" \t\n\r") != std::string::npos)
232  {
233  load_path& lp = __get_load_path__ ("base_reader::octave_gets");
234 
235  lp.update ();
236 
237  if (Vdebugging)
239  else
240  last_debugging_command = "\n";
241  }
242  else if (Vdebugging)
243  {
245  history_skip_auto_repeated_debugging_command = true;
246  }
247 
248  if (retval != "\n")
249  {
250  if (! history_skip_auto_repeated_debugging_command)
251  {
254  }
255 
256  octave_diary << retval;
257 
258  if (retval.back () != '\n')
259  octave_diary << "\n";
260  }
261  else
262  octave_diary << "\n";
263 
264  // Process post input event hook function after the internal history
265  // list has been updated.
266 
267  if (application::interactive ())
269 
270  return retval;
271  }
272 
274  {
275  return m_lexer ? m_lexer->m_reading_fcn_file : false;
276  }
277 
279  {
280  return m_lexer ? m_lexer->m_reading_classdef_file : false;
281  }
282 
284  {
285  return m_lexer ? m_lexer->m_reading_script_file : false;
286  }
287 
288  class
290  {
291  public:
292 
293  terminal_reader (base_lexer *lxr = nullptr)
294  : base_reader (lxr)
295  { }
296 
297  std::string get_input (bool& eof);
298 
299  std::string input_source (void) const { return s_in_src; }
300 
301  bool input_from_terminal (void) const { return true; }
302 
303  private:
304 
305  static const std::string s_in_src;
306  };
307 
308  class
309  file_reader : public base_reader
310  {
311  public:
312 
313  file_reader (FILE *f_arg, base_lexer *lxr = nullptr)
314  : base_reader (lxr), m_file (f_arg) { }
315 
316  std::string get_input (bool& eof);
317 
318  std::string input_source (void) const { return s_in_src; }
319 
320  bool input_from_file (void) const { return true; }
321 
322  private:
323 
324  FILE *m_file;
325 
326  static const std::string s_in_src;
327  };
328 
329  class
331  {
332  public:
333 
334  eval_string_reader (const std::string& str, base_lexer *lxr = nullptr)
335  : base_reader (lxr), m_eval_string (str)
336  { }
337 
338  std::string get_input (bool& eof);
339 
340  std::string input_source (void) const { return s_in_src; }
341 
342  bool input_from_eval_string (void) const { return true; }
343 
344  private:
345 
347 
348  static const std::string s_in_src;
349  };
350 
352  : m_rep (new terminal_reader (lxr))
353  { }
354 
356  : m_rep (new file_reader (file, lxr))
357  { }
358 
360  : m_rep (new eval_string_reader (str, lxr))
361  { }
362 }
363 
364 // Fix things up so that input can come from the standard input. This
365 // may need to become much more complicated, which is why it's in a
366 // separate function.
367 
368 FILE *
370 {
373 }
374 
375 // FIXME: make this generate filenames when appropriate.
376 
377 static string_vector
379  std::string& hint, bool& deemed_struct)
380 {
381  string_vector names;
382 
383  prefix = "";
384 
385  char prev_char = octave::command_editor::get_prev_char (text.length ());
386  deemed_struct = looks_like_struct (text, prev_char);
387 
388  if (deemed_struct)
389  names = generate_struct_completions (text, prefix, hint);
390  else
391  names = octave::make_name_list ();
392 
393  // Sort and remove duplicates.
394 
395  names.sort (true);
396 
397  return names;
398 }
399 
400 static bool
402 {
403  static std::string dirfns_commands[] = {"cd", "ls"};
404  static const size_t dirfns_commands_length = 2;
405 
406  bool retval = false;
407 
409 
410  for (size_t i = 0; i < dirfns_commands_length; i++)
411  {
412  int index = line.find (dirfns_commands[i] + ' ');
413 
414  if (index == 0)
415  {
416  retval = true;
417  break;
418  }
419  }
420 
421  return retval;
422 }
423 
424 static std::string
426 {
428 
429  static std::string prefix;
430  static std::string hint;
431 
432  static size_t hint_len = 0;
433 
434  static int list_index = 0;
435  static int name_list_len = 0;
436  static int name_list_total_len = 0;
437  static string_vector name_list;
438  static string_vector file_name_list;
439 
440  static int matches = 0;
441 
442  if (state == 0)
443  {
444  list_index = 0;
445 
446  prefix = "";
447 
448  hint = text;
449 
450  // No reason to display symbols while completing a
451  // file/directory operation.
452 
453  bool deemed_struct = false;
454 
455  if (is_completing_dirfns ())
456  name_list = string_vector ();
457  else
458  name_list = generate_possible_completions (text, prefix, hint,
459  deemed_struct);
460 
461  name_list_len = name_list.numel ();
462 
463  // If the line was something like "a{1}." then text = "." but
464  // we don't want to expand all the . files.
465  if (! deemed_struct)
466  {
467 
469 
470  name_list.append (file_name_list);
471 
472  }
473 
474  name_list_total_len = name_list.numel ();
475 
476  hint_len = hint.length ();
477 
478  matches = 0;
479 
480  for (int i = 0; i < name_list_len; i++)
481  if (hint == name_list[i].substr (0, hint_len))
482  matches++;
483  }
484 
485  if (name_list_total_len > 0 && matches > 0)
486  {
487  while (list_index < name_list_total_len)
488  {
489  std::string name = name_list[list_index];
490 
491  list_index++;
492 
493  if (hint == name.substr (0, hint_len))
494  {
495  // Special case: array reference forces prefix="."
496  // in generate_struct_completions ()
497  if (list_index <= name_list_len && ! prefix.empty ())
498  retval = (prefix == "." ? "" : prefix) + '.' + name;
499  else
500  retval = name;
501 
503  (text.length ());
504  if (matches == 1 && looks_like_struct (retval, prev_char))
505  {
506  // Don't append anything, since we don't know
507  // whether it should be '(' or '.'.
508 
510  }
511  else
514 
515  break;
516  }
517  }
518  }
519 
520  return retval;
521 }
522 
523 static std::string
524 quoting_filename (const std::string& text, int, char quote)
525 {
526  if (quote)
527  return text;
528  else
529  return ("'" + text);
530 }
531 
532 // Try to parse a partial command line in reverse, excluding trailing TEXT.
533 // If it appears a variable has been indexed by () or {},
534 // return that expression,
535 // to allow autocomplete of field names of arrays of structures.
538 {
540 
541  int pos = line.length () - text.length ();
542  int curly_count = 0;
543  int paren_count = 0;
544 
545  int last = --pos;
546 
547  while (pos >= 0 && (line[pos] == ')' || line[pos] == '}'))
548  {
549  if (line[pos] == ')')
550  paren_count++;
551  else if (line[pos] == '}')
552  curly_count++;
553 
554  while (curly_count + paren_count > 0 && --pos >= 0)
555  {
556  if (line[pos] == ')')
557  paren_count++;
558  else if (line[pos] == '(')
559  paren_count--;
560  else if (line[pos] == '}')
561  curly_count++;
562  else if (line[pos] == '{')
563  curly_count--;
564  }
565 
566  while (--pos >= 0 && line[pos] == ' ')
567  ;
568  }
569 
570  while (pos >= 0 && (isalnum (line[pos]) || line[pos] == '_'))
571  pos--;
572 
573  if (++pos >= 0)
574  return (line.substr (pos, last + 1 - pos));
575  else
576  return std::string ();
577 }
578 
579 void
581 {
582  // If we are using readline, this allows conditional parsing of the
583  // .inputrc file.
584 
586 
587  // FIXME: this needs to include a comma too, but that
588  // causes trouble for the new struct element completion code.
589 
590  static const char *s = "\t\n !\"\'*+-/:;<=>(){}[\\]^`~";
591 
593 
595 
597 
598  octave::command_editor::set_filename_quote_characters (" \t\n\\\"'@<>=;|&()#$`?*[!:{");
600 
602 
604 }
605 
606 static void
607 execute_in_debugger_handler (const std::pair<std::string, int>& arg)
608 {
610 }
611 
612 static void
613 get_debug_input (octave::interpreter& interp, const std::string& prompt)
614 {
616 
619 
620  octave::call_stack& cs = interp.get_call_stack ();
621 
622  octave_user_code *caller = cs.caller_user_code ();
623  std::string nm;
624  int curr_debug_line;
625 
626  if (caller)
627  {
628  nm = caller->fcn_file_name ();
629 
630  if (nm.empty ())
631  nm = caller->name ();
632 
633  curr_debug_line = cs.caller_user_code_line ();
634  }
635  else
636  curr_debug_line = cs.current_line ();
637 
638  std::ostringstream buf;
639 
640  if (! nm.empty ())
641  {
642  if (Vgud_mode)
643  {
644  static char ctrl_z = 'Z' & 0x1f;
645 
646  buf << ctrl_z << ctrl_z << nm << ':' << curr_debug_line;
647  }
648  else
649  {
650  // FIXME: we should come up with a clean way to detect
651  // that we are stopped on the no-op command that marks the
652  // end of a function or script.
653 
654  if (! silent)
655  {
656  buf << "stopped in " << nm;
657 
658  if (curr_debug_line > 0)
659  buf << " at line " << curr_debug_line;
660  }
661 
662  octave_link::enter_debugger_event (nm, curr_debug_line);
663 
665 
666  frame.add_fcn (execute_in_debugger_handler,
667  std::pair<std::string, int> (nm, curr_debug_line));
668 
669  if (! silent)
670  {
671  std::string line_buf;
672 
673  if (caller)
674  line_buf = caller->get_code_line (curr_debug_line);
675 
676  if (! line_buf.empty ())
677  buf << "\n" << curr_debug_line << ": " << line_buf;
678  }
679  }
680  }
681 
682  if (silent)
684 
685  std::string msg = buf.str ();
686 
687  if (! msg.empty ())
688  std::cerr << msg << std::endl;
689 
691  VPS1 = prompt;
692 
693  // FIXME: should debugging be possible in an embedded interpreter?
694 
695  octave::application *app = octave::application::app ();
696 
697  if (! app->interactive ())
698  {
699 
700  frame.add_method (app, &octave::application::interactive,
701  app->interactive ());
702 
703  frame.add_method (app, &octave::application::forced_interactive,
704  app->forced_interactive ());
705 
706  app->interactive (true);
707 
708  app->forced_interactive (true);
709  }
710 
711  octave::parser curr_parser;
712 
713  octave::tree_evaluator& tw = interp.get_evaluator ();
714 
715  while (Vdebugging)
716  {
717  try
718  {
719  Vtrack_line_num = false;
720 
722 
723  curr_parser.reset ();
724 
725  int retval = curr_parser.run ();
726 
728  break;
729  else
730  {
731  if (retval == 0 && curr_parser.m_stmt_list)
732  {
733  curr_parser.m_stmt_list->accept (tw);
734 
737  }
738 
739  octave_quit ();
740  }
741  }
742  catch (const octave::execution_exception& e)
743  {
744  std::string stack_trace = e.info ();
745 
746  if (! stack_trace.empty ())
747  std::cerr << stack_trace;
748 
749  // Ignore errors when in debugging mode;
751  }
752  }
753 }
754 
755 namespace octave
756 {
757  const std::string base_reader::s_in_src ("invalid");
758 
759  const std::string terminal_reader::s_in_src ("terminal");
760 
762  terminal_reader::get_input (bool& eof)
763  {
764  octave_quit ();
765 
766  eof = false;
767 
768  return octave_gets (eof);
769  }
770 
771  const std::string file_reader::s_in_src ("file");
772 
774  file_reader::get_input (bool& eof)
775  {
776  octave_quit ();
777 
778  eof = false;
779 
780  return octave_fgets (m_file, eof);
781  }
782 
783  const std::string eval_string_reader::s_in_src ("eval_string");
784 
787  {
788  octave_quit ();
789 
790  eof = false;
791 
793 
795 
796  // Clear the eval string so that the next call will return
797  // an empty character string with EOF = true.
798  m_eval_string = "";
799 
800  if (retval.empty ())
801  eof = true;
802 
803  return retval;
804  }
805 }
806 
807 // If the user simply hits return, this will produce an empty matrix.
808 
809 static octave_value_list
810 get_user_input (const octave_value_list& args, int nargout)
811 {
813 
814  int read_as_string = 0;
815 
816  if (args.length () == 2)
817  read_as_string++;
818 
819  std::string prompt = args(0).xstring_value ("input: unrecognized argument");
820 
822 
825 
826  octave_diary << prompt;
827 
828  bool eof = false;
829 
830  std::string input_buf = interactive_input (prompt.c_str (), eof);
831 
832  if (input_buf.empty ())
833  error ("input: reading user-input failed!");
834 
835  size_t len = input_buf.length ();
836 
837  octave_diary << input_buf;
838 
839  if (input_buf[len - 1] != '\n')
840  octave_diary << "\n";
841 
842  if (len < 1)
843  return read_as_string ? octave_value ("") : octave_value (Matrix ());
844 
845  if (read_as_string)
846  {
847  // FIXME: fix gnu_readline and octave_gets instead!
848  if (input_buf.length () == 1 && input_buf[0] == '\n')
849  retval(0) = "";
850  else
851  retval(0) = input_buf;
852  }
853  else
854  {
855  int parse_status = 0;
856 
857  retval = octave::eval_string (input_buf, true, parse_status, nargout);
858 
859  if (! Vdebugging && retval.empty ())
860  retval(0) = Matrix ();
861  }
862 
863  return retval;
864 }
865 
866 DEFUN (input, args, nargout,
867  doc: /* -*- texinfo -*-
868 @deftypefn {} {@var{ans} =} input (@var{prompt})
869 @deftypefnx {} {@var{ans} =} input (@var{prompt}, "s")
870 Print @var{prompt} and wait for user input.
871 
872 For example,
873 
874 @example
875 input ("Pick a number, any number! ")
876 @end example
877 
878 @noindent
879 prints the prompt
880 
881 @example
882 Pick a number, any number!
883 @end example
884 
885 @noindent
886 and waits for the user to enter a value. The string entered by the user
887 is evaluated as an expression, so it may be a literal constant, a variable
888 name, or any other valid Octave code.
889 
890 The number of return arguments, their size, and their class depend on the
891 expression entered.
892 
893 If you are only interested in getting a literal string value, you can call
894 @code{input} with the character string @qcode{"s"} as the second argument.
895 This tells Octave to return the string entered by the user directly, without
896 evaluating it first.
897 
898 Because there may be output waiting to be displayed by the pager, it is a
899 good idea to always call @code{fflush (stdout)} before calling @code{input}.
900  This will ensure that all pending output is written to the screen before
901 your prompt.
902 @seealso{yes_or_no, kbhit, pause, menu, listdlg}
903 @end deftypefn */)
904 {
905  int nargin = args.length ();
906 
908  print_usage ();
909 
910  return get_user_input (args, std::max (nargout, 1));
911 }
912 
913 bool
914 octave_yes_or_no (const std::string& prompt)
915 {
916  std::string prompt_string = prompt + "(yes or no) ";
917 
918  while (1)
919  {
920  bool eof = false;
921 
922  std::string input_buf = interactive_input (prompt_string, eof);
923 
924  if (input_buf == "yes")
925  return true;
926  else if (input_buf == "no")
927  return false;
928  else
929  message (nullptr, "Please answer yes or no.");
930  }
931 }
932 
933 DEFUN (yes_or_no, args, ,
934  doc: /* -*- texinfo -*-
935 @deftypefn {} {@var{ans} =} yes_or_no ("@var{prompt}")
936 Ask the user a yes-or-no question.
937 
938 Return logical true if the answer is yes or false if the answer is no.
939 
940 Takes one argument, @var{prompt}, which is the string to display when asking
941 the question. @var{prompt} should end in a space; @code{yes-or-no} adds the
942 string @samp{(yes or no) } to it. The user must confirm the answer with
943 @key{RET} and can edit it until it has been confirmed.
944 @seealso{input}
945 @end deftypefn */)
946 {
947  int nargin = args.length ();
948 
949  if (nargin > 1)
950  print_usage ();
951 
952  std::string prompt;
953 
954  if (nargin == 1)
955  prompt = args(0).xstring_value ("yes_or_no: PROMPT must be a string");
956 
957  return ovl (octave_yes_or_no (prompt));
958 }
959 
962 {
964 
965  int nargin = args.length ();
966 
967  assert (nargin == 0 || nargin == 1);
968 
970 
973 
975 
977 
978  octave::call_stack& cs = interp.get_call_stack ();
979 
981  cs.current_frame ());
982 
983  // FIXME: probably we just want to print one line, not the
984  // entire statement, which might span many lines...
985  //
986  // tree_print_code tpc (octave_stdout);
987  // stmt.accept (tpc);
988 
989  Vdebugging = true;
990  Vtrack_line_num = false;
991 
992  std::string prompt = "debug> ";
993  if (nargin > 0)
994  prompt = args(0).string_value ();
995 
996  get_debug_input (interp, prompt);
997 
998  return retval;
999 }
1000 
1002 do_keyboard (const octave_value_list& args)
1003 {
1004  octave::interpreter& interp = octave::__get_interpreter__ ("do_keyboard");
1005 
1006  return do_keyboard (interp, args);
1007 }
1008 
1009 DEFMETHOD (keyboard, interp, args, ,
1010  doc: /* -*- texinfo -*-
1011 @deftypefn {} {} keyboard ()
1012 @deftypefnx {} {} keyboard ("@var{prompt}")
1013 Stop m-file execution and enter debug mode.
1014 
1015 When the @code{keyboard} function is executed, Octave prints a prompt and
1016 waits for user input. The input strings are then evaluated and the results
1017 are printed. This makes it possible to examine the values of variables
1018 within a function, and to assign new values if necessary. To leave the
1019 prompt and return to normal execution type @samp{return} or @samp{dbcont}.
1020 The @code{keyboard} function does not return an exit status.
1021 
1022 If @code{keyboard} is invoked without arguments, a default prompt of
1023 @samp{debug> } is used.
1024 @seealso{dbstop, dbcont, dbquit}
1025 @end deftypefn */)
1026 {
1027  if (args.length () > 1)
1028  print_usage ();
1029 
1031 
1032  octave::call_stack& cs = interp.get_call_stack ();
1033 
1035  cs.current_frame ());
1036 
1037  // Skip the frame assigned to the keyboard function.
1038  cs.goto_frame_relative (0);
1039 
1042 
1044 
1045  do_keyboard (interp, args);
1046 
1047  return ovl ();
1048 }
1049 
1050 DEFUN (completion_matches, args, nargout,
1051  doc: /* -*- texinfo -*-
1052 @deftypefn {} {} completion_matches (@var{hint})
1053 Generate possible completions given @var{hint}.
1054 
1055 This function is provided for the benefit of programs like Emacs which
1056 might be controlling Octave and handling user input. The current
1057 command number is not incremented when this function is called. This is
1058 a feature, not a bug.
1059 @end deftypefn */)
1060 {
1061  if (args.length () != 1)
1062  print_usage ();
1063 
1065 
1066  std::string hint = args(0).string_value ();
1067 
1068  int n = 32;
1069 
1070  string_vector list (n);
1071 
1072  int k = 0;
1073 
1074  for (;;)
1075  {
1076  std::string cmd = generate_completion (hint, k);
1077 
1078  if (! cmd.empty ())
1079  {
1080  if (k == n)
1081  {
1082  n *= 2;
1083  list.resize (n);
1084  }
1085 
1086  list[k++] = cmd;
1087  }
1088  else
1089  {
1090  list.resize (k);
1091  break;
1092  }
1093  }
1094 
1095  if (nargout > 0)
1096  {
1097  if (! list.empty ())
1098  retval = list;
1099  else
1100  retval = "";
1101  }
1102  else
1103  {
1104  // We don't use string_vector::list_in_columns here
1105  // because it will be easier for Emacs if the names
1106  // appear in a single column.
1107 
1108  int len = list.numel ();
1109 
1110  for (int i = 0; i < len; i++)
1111  octave_stdout << list[i] << "\n";
1112  }
1113 
1115 
1116  return retval;
1117 }
1118 
1119 /*
1120 %!assert (ischar (completion_matches ("")))
1121 %!assert (ischar (completion_matches ("a")))
1122 %!assert (ischar (completion_matches (" ")))
1123 %!assert (isempty (completion_matches (" ")))
1124 %!assert (any (strcmp ("abs", deblank (cellstr (completion_matches (""))))))
1125 %!assert (any (strcmp ("abs", deblank (cellstr (completion_matches ("a"))))))
1126 %!assert (any (strcmp ("abs", deblank (cellstr (completion_matches ("ab"))))))
1127 %!assert (any (strcmp ("abs", deblank (cellstr (completion_matches ("abs"))))))
1128 %!assert (! any (strcmp ("abs", deblank (cellstr (completion_matches ("absa"))))))
1129 
1130 %!error completion_matches ()
1131 %!error completion_matches (1, 2)
1132 */
1133 
1134 DEFUN (readline_read_init_file, args, ,
1135  doc: /* -*- texinfo -*-
1136 @deftypefn {} {} readline_read_init_file (@var{file})
1137 Read the readline library initialization file @var{file}.
1138 
1139 If @var{file} is omitted, read the default initialization file
1140 (normally @file{~/.inputrc}).
1141 
1142 @xref{Readline Init File, , , readline, GNU Readline Library},
1143 for details.
1144 @seealso{readline_re_read_init_file}
1145 @end deftypefn */)
1146 {
1147  int nargin = args.length ();
1148 
1149  if (nargin > 1)
1150  print_usage ();
1151 
1152  if (nargin == 0)
1154  else
1155  {
1156  std::string file = args(0).string_value ();
1157 
1159  }
1160 
1161  return ovl ();
1162 }
1163 
1164 DEFUN (readline_re_read_init_file, args, ,
1165  doc: /* -*- texinfo -*-
1166 @deftypefn {} {} readline_re_read_init_file ()
1167 Re-read the last readline library initialization file that was read.
1168 
1169 @xref{Readline Init File, , , readline, GNU Readline Library},
1170 for details.
1171 @seealso{readline_read_init_file}
1172 @end deftypefn */)
1173 {
1174  if (args.length () != 0)
1175  print_usage ();
1176 
1178 
1179  return ovl ();
1180 }
1181 
1182 static int
1183 internal_input_event_hook_fcn (void)
1184 {
1186 
1188  octave::command_editor::remove_event_hook (internal_input_event_hook_fcn);
1189 
1190  return 0;
1191 }
1192 
1193 DEFUN (add_input_event_hook, args, ,
1194  doc: /* -*- texinfo -*-
1195 @deftypefn {} {@var{id} =} add_input_event_hook (@var{fcn})
1196 @deftypefnx {} {@var{id} =} add_input_event_hook (@var{fcn}, @var{data})
1197 Add the named function or function handle @var{fcn} to the list of functions
1198 to call periodically when Octave is waiting for input.
1199 
1200 The function should have the form
1201 
1202 @example
1203 @var{fcn} (@var{data})
1204 @end example
1205 
1206 If @var{data} is omitted, Octave calls the function without any arguments.
1207 
1208 The returned identifier may be used to remove the function handle from the
1209 list of input hook functions.
1210 @seealso{remove_input_event_hook}
1211 @end deftypefn */)
1212 {
1213  int nargin = args.length ();
1214 
1215  if (nargin < 1 || nargin > 2)
1216  print_usage ();
1217 
1218  octave_value user_data;
1219 
1220  if (nargin == 2)
1221  user_data = args(1);
1222 
1223  hook_function hook_fcn (args(0), user_data);
1224 
1226  octave::command_editor::add_event_hook (internal_input_event_hook_fcn);
1227 
1228  input_event_hook_functions.insert (hook_fcn.id (), hook_fcn);
1229 
1230  return ovl (hook_fcn.id ());
1231 }
1232 
1233 DEFUN (remove_input_event_hook, args, ,
1234  doc: /* -*- texinfo -*-
1235 @deftypefn {} {} remove_input_event_hook (@var{name})
1236 @deftypefnx {} {} remove_input_event_hook (@var{fcn_id})
1237 Remove the named function or function handle with the given identifier
1238 from the list of functions to call periodically when Octave is waiting
1239 for input.
1240 @seealso{add_input_event_hook}
1241 @end deftypefn */)
1242 {
1243  int nargin = args.length ();
1244 
1245  if (nargin < 1 || nargin > 2)
1246  print_usage ();
1247 
1248  std::string hook_fcn_id = args(0).string_value ("remove_input_event_hook: argument not valid as a hook function name or id");
1249 
1250  bool warn = (nargin < 2);
1251 
1253  = input_event_hook_functions.find (hook_fcn_id);
1254 
1255  if (p != input_event_hook_functions.end ())
1257  else if (warn)
1258  warning ("remove_input_event_hook: %s not found in list",
1259  hook_fcn_id.c_str ());
1260 
1262  octave::command_editor::remove_event_hook (internal_input_event_hook_fcn);
1263 
1264  return ovl ();
1265 }
1266 
1267 DEFUN (PS1, args, nargout,
1268  doc: /* -*- texinfo -*-
1269 @deftypefn {} {@var{val} =} PS1 ()
1270 @deftypefnx {} {@var{old_val} =} PS1 (@var{new_val})
1271 @deftypefnx {} {} PS1 (@var{new_val}, "local")
1272 Query or set the primary prompt string.
1273 
1274 When executing interactively, Octave displays the primary prompt when it is
1275 ready to read a command.
1276 
1277 The default value of the primary prompt string is @qcode{'octave:\#> '}.
1278 To change it, use a command like
1279 
1280 @example
1281 PS1 ("\\u@@\\H> ")
1282 @end example
1283 
1284 @noindent
1285 which will result in the prompt @samp{boris@@kremvax> } for the user
1286 @samp{boris} logged in on the host @samp{kremvax.kgb.su}. Note that two
1287 backslashes are required to enter a backslash into a double-quoted
1288 character string. @xref{Strings}.
1289 
1290 You can also use ANSI escape sequences if your terminal supports them.
1291 This can be useful for coloring the prompt. For example,
1292 
1293 @example
1294 PS1 ('\[\033[01;31m\]\s:\#> \[\033[0m\]')
1295 @end example
1296 
1297 @noindent
1298 will give the default Octave prompt a red coloring.
1299 
1300 When called from inside a function with the @qcode{"local"} option, the
1301 variable is changed locally for the function and any subroutines it calls.
1302 The original variable value is restored when exiting the function.
1303 @seealso{PS2, PS4}
1304 @end deftypefn */)
1305 {
1306  return SET_INTERNAL_VARIABLE (PS1);
1307 }
1308 
1309 DEFUN (PS2, args, nargout,
1310  doc: /* -*- texinfo -*-
1311 @deftypefn {} {@var{val} =} PS2 ()
1312 @deftypefnx {} {@var{old_val} =} PS2 (@var{new_val})
1313 @deftypefnx {} {} PS2 (@var{new_val}, "local")
1314 Query or set the secondary prompt string.
1315 
1316 The secondary prompt is printed when Octave is expecting additional input to
1317 complete a command. For example, if you are typing a @code{for} loop that
1318 spans several lines, Octave will print the secondary prompt at the beginning
1319 of each line after the first. The default value of the secondary prompt
1320 string is @qcode{"> "}.
1321 
1322 When called from inside a function with the @qcode{"local"} option, the
1323 variable is changed locally for the function and any subroutines it calls.
1324 The original variable value is restored when exiting the function.
1325 @seealso{PS1, PS4}
1326 @end deftypefn */)
1327 {
1328  return SET_INTERNAL_VARIABLE (PS2);
1329 }
1330 
1331 DEFUN (completion_append_char, args, nargout,
1332  doc: /* -*- texinfo -*-
1333 @deftypefn {} {@var{val} =} completion_append_char ()
1334 @deftypefnx {} {@var{old_val} =} completion_append_char (@var{new_val})
1335 @deftypefnx {} {} completion_append_char (@var{new_val}, "local")
1336 Query or set the internal character variable that is appended to
1337 successful command-line completion attempts.
1338 
1339 The default value is @qcode{" "} (a single space).
1340 
1341 When called from inside a function with the @qcode{"local"} option, the
1342 variable is changed locally for the function and any subroutines it calls.
1343 The original variable value is restored when exiting the function.
1344 @end deftypefn */)
1345 {
1346  return SET_INTERNAL_VARIABLE (completion_append_char);
1347 }
1348 
1349 DEFUN (__request_drawnow__, args, ,
1350  doc: /* -*- texinfo -*-
1351 @deftypefn {} {} __request_drawnow__ ()
1352 @deftypefnx {} {} __request_drawnow__ (@var{flag})
1353 Undocumented internal function.
1354 @end deftypefn */)
1355 {
1356  int nargin = args.length ();
1357 
1358  if (nargin > 1)
1359  print_usage ();
1360 
1361  if (nargin == 0)
1362  Vdrawnow_requested = true;
1363  else
1364  Vdrawnow_requested = args(0).bool_value ();
1365 
1366  return ovl ();
1367 }
1368 
1369 DEFUN (__gud_mode__, args, ,
1370  doc: /* -*- texinfo -*-
1371 @deftypefn {} {} __gud_mode__ ()
1372 Undocumented internal function.
1373 @end deftypefn */)
1374 {
1375  int nargin = args.length ();
1376 
1377  if (nargin > 1)
1378  print_usage ();
1379 
1381 
1382  if (nargin == 0)
1383  retval = ovl (Vgud_mode);
1384  else
1385  Vgud_mode = args(0).bool_value ();
1386 
1387  return retval;
1388 }
virtual bool reading_fcn_file(void) const
Definition: input.cc:273
static bool is_completing_dirfns(void)
Definition: input.cc:401
static hook_function_list input_event_hook_functions
Definition: input.cc:111
static std::string last_debugging_command
Definition: input.cc:106
std::string m_eval_string
Definition: input.cc:346
static void set_completion_function(completion_fcn f)
Definition: cmd-edit.cc:1357
std::string get_input(bool &eof)
static std::string generate_completion(const std::string &text, int state)
Definition: input.cc:425
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
input_reader(base_lexer *lxr=nullptr)
Definition: input.cc:351
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
Definition: defun.h:135
static const std::string s_in_src
Definition: input.h:135
interpreter & __get_interpreter__(const std::string &who)
static std::string decode_prompt_string(const std::string &s)
Definition: cmd-edit.cc:1267
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
Definition: oct-hist.cc:734
static OCTAVE_NORETURN void eval_error(const char *msg, const dim_vector &x, const dim_vector &y)
Definition: pt-tm-const.cc:55
static void recover_from_exception(void)
std::string get_input(bool &eof)
static std::string quoting_filename(const std::string &text, int, char quote)
Definition: input.cc:524
std::string octave_fgets(FILE *f)
Definition: lo-utils.cc:111
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
static void set_filename_quote_characters(const std::string &s)
Definition: cmd-edit.cc:1336
static void set_input_stream(FILE *f)
Definition: cmd-edit.cc:1197
FILE * get_input_from_stdin(void)
Definition: input.cc:369
bool input_from_eval_string(void) const
Definition: input.cc:342
bool empty(void) const
Definition: ovl.h:98
for large enough k
Definition: lu.cc:617
bool looks_like_struct(const std::string &text, char prev_char)
Definition: variables.cc:290
tree_statement_list * m_stmt_list
Definition: parse.h:476
virtual bool reading_script_file(void) const
Definition: input.cc:283
void flush_stdout(void)
Definition: pager.cc:464
void add_fcn(void(*fcn)(void))
std::string get_code_line(size_t line)
Definition: ov-usr-fcn.cc:85
bool goto_frame_relative(int n, bool verbose=false)
Definition: call-stack.cc:412
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
std::string input_source(void) const
Definition: input.cc:340
#define SET_INTERNAL_VARIABLE(NM)
Definition: variables.h:109
std::string input_source(void) const
Definition: input.cc:318
octave_user_code * caller_user_code(size_t nskip=0) const
Definition: call-stack.cc:164
static void ignore_entries(bool=true)
Definition: cmd-hist.cc:592
static bool Vgud_mode
Definition: input.cc:109
int caller_user_code_line(void) const
Definition: call-stack.cc:192
octave::sys::time Vlast_prompt_time
Definition: input.cc:84
static std::string get_line_buffer(void)
Definition: cmd-edit.cc:1434
#define octave_diary
Definition: pager.h:176
static bool add(const std::string &)
Definition: cmd-hist.cc:606
static const std::string s_in_src
Definition: input.cc:305
tree_evaluator & get_evaluator(void)
iterator find(const std::string &id)
Definition: hook-fcn.h:225
s
Definition: file-io.cc:2729
i e
Definition: data.cc:2591
virtual std::string fcn_file_name(void) const
Definition: ov-fcn.h:74
static void reset(void)
Definition: pager.cc:429
static bool quiet_breakpoint_flag
Definition: pt-eval.h:254
octave_value arg
Definition: pr-output.cc:3244
virtual bool reading_classdef_file(void) const
Definition: input.cc:278
static void reset(void)
Definition: pager.cc:354
bool empty(void) const
Definition: hook-fcn.h:216
terminal_reader(base_lexer *lxr=nullptr)
Definition: input.cc:293
octave::call_stack & cs
Definition: ov-class.cc:1752
static void re_read_init_file(void)
Definition: cmd-edit.cc:1591
void insert(const std::string &id, const hook_function &f)
Definition: hook-fcn.h:220
void message(const char *name, const char *fmt,...)
Definition: error.cc:435
nd deftypefn *std::string name
Definition: sysdep.cc:647
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
string_vector make_name_list(void)
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:997
static const std::string s_in_src
Definition: input.cc:348
static string_vector generate_filename_completions(const std::string &text)
Definition: cmd-edit.cc:1427
void erase(iterator p)
Definition: hook-fcn.h:239
bool Vdrawnow_requested
Definition: input.cc:94
int pipe_handler_error_count
Definition: sighandlers.cc:65
static bool erase_empty_line(bool flag)
Definition: cmd-edit.cc:1309
OCTINTERP_API octave_value_list eval_string(const std::string &, bool silent, int &parse_status, int nargout)
static void set_completer_quote_characters(const std::string &s)
Definition: cmd-edit.cc:1343
void initialize_command_input(void)
Definition: input.cc:580
bool Vtrack_line_num
Definition: input.cc:102
bool input_from_file(void) const
Definition: input.cc:320
static char get_prev_char(int)
Definition: cmd-edit.cc:1448
static size_t current_frame
Definition: pt-eval.h:250
static std::string gnu_readline(const std::string &s, bool &eof)
Definition: input.cc:134
static std::string VPS2
Definition: input.cc:81
std::string str
Definition: hash.cc:118
string_vector generate_struct_completions(const std::string &text, std::string &prefix, std::string &hint)
Definition: variables.cc:218
static void remove_event_hook(event_hook_fcn f)
Definition: cmd-edit.cc:1562
string_vector & append(const std::string &s)
Definition: str-vec.cc:107
void accept(tree_walker &tw)
Definition: pt-stmt.h:188
static std::string interactive_input(const std::string &s, bool &eof)
Definition: input.cc:149
octave_value retval
Definition: data.cc:6246
static std::string VPS1
Definition: input.cc:78
void reset_error_handler(void)
Definition: error.cc:125
string_vector & sort(bool make_uniq=false)
Definition: str-vec.cc:74
file_reader(FILE *f_arg, base_lexer *lxr=nullptr)
Definition: input.cc:313
static void set_completer_word_break_characters(const std::string &s)
Definition: cmd-edit.cc:1322
Definition: dMatrix.h:36
static FILE * get_input_stream(void)
Definition: cmd-edit.cc:1204
OCTAVE_EXPORT octave_value_list or cell arrays Arguments are concatenated vertically The returned values are padded with blanks as needed to make each row of the string array have the same length Empty input strings are significant and will concatenated in the output For numerical input
Definition: strfns.cc:81
iterator end(void)
Definition: hook-fcn.h:235
static bool debug_mode
Definition: pt-eval.h:252
static bool interrupt(bool=true)
Definition: cmd-edit.cc:1619
void update(void) const
Definition: load-path.cc:363
load_path & __get_load_path__(const std::string &who)
base_lexer * m_lexer
Definition: input.h:133
static uint32_t state[624]
Definition: randmtzig.cc:183
bool m_reading_script_file
Definition: lex.h:411
static void read_init_file(const std::string &file="")
Definition: cmd-edit.cc:1580
void warning(const char *fmt,...)
Definition: error.cc:801
octave::unwind_protect frame
Definition: graphics.cc:12190
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:227
OCTAVE_EXPORT octave_value_list the first data row corresponds to an index of zero The a spreadsheet style form such as the file is read until end of file is reached The such as text
Definition: dlmread.cc:194
bool octave_yes_or_no(const std::string &prompt)
static string_vector generate_possible_completions(const std::string &text, std::string &prefix, std::string &hint, bool &deemed_struct)
Definition: input.cc:378
#define octave_stdout
Definition: pager.h:174
std::string get_input(bool &eof)
octave_value do_keyboard(const octave_value_list &args=octave_value_list())
static void set_completion_append_character(char c)
Definition: cmd-edit.cc:1350
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
static std::string readline(const std::string &prompt)
Definition: cmd-edit.cc:1173
int current_line(void) const
Definition: call-stack.cc:99
void remove_input_event_hook_functions(void)
Definition: input.cc:115
p
Definition: lu.cc:138
eval_string_reader(const std::string &str, base_lexer *lxr=nullptr)
Definition: input.cc:334
bool m_reading_classdef_file
Definition: lex.h:414
octave_idx_type length(void) const
Definition: ovl.h:96
call_stack & get_call_stack(void)
void clear(void)
Definition: hook-fcn.h:218
static bool ignoring_entries(void)
Definition: cmd-hist.cc:599
static const std::string s_in_src
Definition: input.cc:326
void add_method(T *obj, void(T::*method)(void))
std::string find_indexed_expression(const std::string &text)
Definition: input.cc:537
args.length() nargin
Definition: file-io.cc:589
static void add_event_hook(event_hook_fcn f)
Definition: cmd-edit.cc:1554
void restore_frame(size_t n)
Definition: call-stack.h:224
for i
Definition: data.cc:5264
static void set_basic_word_break_characters(const std::string &s)
Definition: cmd-edit.cc:1315
static void set_basic_quote_characters(const std::string &s)
Definition: cmd-edit.cc:1329
bool input_from_terminal(void) const
Definition: input.cc:301
std::string input_source(void) const
Definition: input.cc:299
int run(void)
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
bool Vdebugging
Definition: input.cc:97
bool octave_completion_matches_called
Definition: input.cc:90
std::string name(void) const
Definition: ov-fcn.h:182
void stamp(void)
Definition: oct-time.cc:106
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
void set_default_prompts(void)
Definition: input.cc:121
void run(const octave_value_list &initial_args=octave_value_list())
Definition: hook-fcn.h:241
static void set_name(const std::string &n)
Definition: cmd-edit.cc:1166
static void set_quoting_function(quoting_fcn f)
Definition: cmd-edit.cc:1364
size_t current_frame(void) const
Definition: call-stack.h:123
static char Vcompletion_append_char
Definition: input.cc:87
map_type::iterator iterator
Definition: hook-fcn.h:197
std::string octave_gets(bool &eof)
Definition: input.cc:190