GNU Octave  3.8.0
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
input.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2013 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 // Get command input interactively or from files.
24 
25 #ifdef 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 <sys/types.h>
39 #include <unistd.h>
40 
41 #include "cmd-edit.h"
42 #include "file-ops.h"
43 #include "quit.h"
44 #include "str-vec.h"
45 
46 #include "debug.h"
47 #include "defun.h"
48 #include "dirfns.h"
49 #include "error.h"
50 #include "gripes.h"
51 #include "help.h"
52 #include "hook-fcn.h"
53 #include "input.h"
54 #include "lex.h"
55 #include "load-path.h"
56 #include "octave-link.h"
57 #include "oct-map.h"
58 #include "oct-hist.h"
59 #include "toplev.h"
60 #include "octave-link.h"
61 #include "oct-obj.h"
62 #include "ov-fcn-handle.h"
63 #include "pager.h"
64 #include "parse.h"
65 #include "pathlen.h"
66 #include "pt.h"
67 #include "pt-const.h"
68 #include "pt-eval.h"
69 #include "pt-stmt.h"
70 #include "sighandlers.h"
71 #include "symtab.h"
72 #include "sysdep.h"
73 #include "toplev.h"
74 #include "unwind-prot.h"
75 #include "utils.h"
76 #include "variables.h"
77 
78 // Primary prompt string.
79 static std::string VPS1;
80 
81 // Secondary prompt string.
82 static std::string VPS2;
83 
84 // String printed before echoed input (enabled by --echo-input).
85 std::string VPS4 = "+ ";
86 
87 // Echo commands as they are executed?
88 //
89 // 1 ==> echo commands read from script files
90 // 2 ==> echo commands from functions
91 // 4 ==> echo commands read from command line
92 //
93 // more than one state can be active at once.
95 
96 // The time we last printed a prompt.
98 
99 // Character to append after successful command-line completion attempts.
100 static char Vcompletion_append_char = ' ';
101 
102 // TRUE means this is an interactive shell.
103 bool interactive = false;
104 
105 // TRUE means the user forced this shell to be interactive (-i).
106 bool forced_interactive = false;
107 
108 // TRUE after a call to completion_matches.
110 
111 // TRUE if the plotting system has requested a call to drawnow at
112 // the next user prompt.
113 bool Vdrawnow_requested = false;
114 
115 // TRUE if we are in debugging mode.
116 bool Vdebugging = false;
117 
118 // If we are in debugging mode, this is the last command entered, so
119 // that we can repeat the previous command if the user just types RET.
120 static std::string last_debugging_command = "\n";
121 
122 // TRUE if we are running in the Emacs GUD mode.
123 static bool Vgud_mode = false;
124 
125 // The filemarker used to separate filenames from subfunction names
126 char Vfilemarker = '>';
127 
129 
130 // For octave_quit.
131 void
133 {
134  input_event_hook_functions.clear ();
135 }
136 
137 void
139 {
140  // Use literal "octave" instead of "\\s" to avoid setting the prompt
141  // to "octave.exe" or "octave-gui", etc.
142 
143  VPS1 = "octave:\\#> ";
144  VPS2 = "> ";
145  VPS4 = "+ ";
146 
148 }
149 
150 void
151 octave_base_reader::do_input_echo (const std::string& input_string) const
152 {
153  int do_echo = reading_script_file ()
154  ? (Vecho_executing_commands & ECHO_SCRIPTS)
155  : (Vecho_executing_commands & ECHO_CMD_LINE) && ! forced_interactive;
156 
157  if (do_echo)
158  {
159  if (forced_interactive)
160  {
161  if (pflag > 0)
163  else
164  octave_stdout << command_editor::decode_prompt_string (VPS2);
165  }
166  else
168 
169  if (! input_string.empty ())
170  {
171  octave_stdout << input_string;
172 
173  if (input_string[input_string.length () - 1] != '\n')
174  octave_stdout << "\n";
175  }
176  }
177 }
178 
179 static std::string
180 gnu_readline (const std::string& s, bool& eof)
181 {
182  octave_quit ();
183 
184  eof = false;
185 
186  std::string retval = command_editor::readline (s, eof);
187 
188  if (! eof && retval.empty ())
189  retval = "\n";
190 
191  return retval;
192 }
193 
194 static inline std::string
195 interactive_input (const std::string& s, bool& eof)
196 {
197  Vlast_prompt_time.stamp ();
198 
200  {
201  feval ("drawnow");
202 
204 
205  // We set Vdrawnow_requested to false even if there is an error
206  // in drawnow so that the error doesn't reappear at every prompt.
207 
208  Vdrawnow_requested = false;
209 
210  if (error_state)
211  return "\n";
212  }
213 
214  return gnu_readline (s, eof);
215 }
216 
217 std::string
219 {
220  octave_quit ();
221 
222  eof = false;
223 
224  std::string retval;
225 
226  // Process pre input event hook function prior to flushing output and
227  // printing the prompt.
228 
230  {
231  if (! Vdebugging)
233 
235 
237  }
238 
239  bool history_skip_auto_repeated_debugging_command = false;
240 
241  std::string ps = (pflag > 0) ? VPS1 : VPS2;
242 
243  std::string prompt = command_editor::decode_prompt_string (ps);
244 
246 
248 
251 
252  octave_diary << prompt;
253 
254  retval = interactive_input (prompt, eof);
255 
256  // There is no need to update the load_path cache if there is no
257  // user input.
258  if (retval != "\n"
259  && retval.find_first_not_of (" \t\n\r") != std::string::npos)
260  {
262 
263  if (Vdebugging)
264  last_debugging_command = retval;
265  else
266  last_debugging_command = "\n";
267  }
268  else if (Vdebugging)
269  {
270  retval = last_debugging_command;
271  history_skip_auto_repeated_debugging_command = true;
272  }
273 
274  if (retval != "\n")
275  {
276  if (! history_skip_auto_repeated_debugging_command)
277  {
278  if (command_history::add (retval))
280  }
281 
282  octave_diary << retval;
283 
284  if (retval[retval.length () - 1] != '\n')
285  octave_diary << "\n";
286 
287  do_input_echo (retval);
288  }
289  else
290  octave_diary << "\n";
291 
292  // Process post input event hook function after the internal history
293  // list has been updated.
294 
297 
298  return retval;
299 }
300 
301 bool
303 {
304  return lexer ? lexer->reading_fcn_file : false;
305 }
306 
307 bool
309 {
310  return lexer ? lexer->reading_classdef_file : false;
311 }
312 
313 bool
315 {
316  return lexer ? lexer->reading_script_file : false;
317 }
318 
319 // Fix things up so that input can come from the standard input. This
320 // may need to become much more complicated, which is why it's in a
321 // separate function.
322 
323 FILE *
325 {
328 }
329 
330 // FIXME: make this generate file names when appropriate.
331 
332 static string_vector
333 generate_possible_completions (const std::string& text, std::string& prefix,
334  std::string& hint)
335 {
337 
338  prefix = "";
339 
340  if (looks_like_struct (text))
341  names = generate_struct_completions (text, prefix, hint);
342  else
343  names = make_name_list ();
344 
345  // Sort and remove duplicates.
346 
347  names.sort (true);
348 
349  return names;
350 }
351 
352 static bool
354 {
355  static std::string dirfns_commands[] = {"cd", "ls"};
356  static const size_t dirfns_commands_length = 2;
357 
358  bool retval = false;
359 
360  std::string line = command_editor::get_line_buffer ();
361 
362  for (size_t i = 0; i < dirfns_commands_length; i++)
363  {
364  int index = line.find (dirfns_commands[i] + " ");
365 
366  if (index == 0)
367  {
368  retval = true;
369  break;
370  }
371  }
372 
373  return retval;
374 }
375 
376 static std::string
377 generate_completion (const std::string& text, int state)
378 {
379  std::string retval;
380 
381  static std::string prefix;
382  static std::string hint;
383 
384  static size_t hint_len = 0;
385 
386  static int list_index = 0;
387  static int name_list_len = 0;
388  static int name_list_total_len = 0;
389  static string_vector name_list;
390  static string_vector file_name_list;
391 
392  static int matches = 0;
393 
394  if (state == 0)
395  {
396  list_index = 0;
397 
398  prefix = "";
399 
400  hint = text;
401 
402  // No reason to display symbols while completing a
403  // file/directory operation.
404 
405  if (is_completing_dirfns ())
406  name_list = string_vector ();
407  else
408  name_list = generate_possible_completions (text, prefix, hint);
409 
410  name_list_len = name_list.length ();
411 
412  file_name_list = command_editor::generate_filename_completions (text);
413 
414  name_list.append (file_name_list);
415 
416  name_list_total_len = name_list.length ();
417 
418  hint_len = hint.length ();
419 
420  matches = 0;
421 
422  for (int i = 0; i < name_list_len; i++)
423  if (hint == name_list[i].substr (0, hint_len))
424  matches++;
425  }
426 
427  if (name_list_total_len > 0 && matches > 0)
428  {
429  while (list_index < name_list_total_len)
430  {
431  std::string name = name_list[list_index];
432 
433  list_index++;
434 
435  if (hint == name.substr (0, hint_len))
436  {
437  if (list_index <= name_list_len && ! prefix.empty ())
438  retval = prefix + "." + name;
439  else
440  retval = name;
441 
442  // FIXME: looks_like_struct is broken for now,
443  // so it always returns false.
444 
445  if (matches == 1 && looks_like_struct (retval))
446  {
447  // Don't append anything, since we don't know
448  // whether it should be '(' or '.'.
449 
451  }
452  else
454  (Vcompletion_append_char);
455 
456  break;
457  }
458  }
459  }
460 
461  return retval;
462 }
463 
464 static std::string
465 quoting_filename (const std::string &text, int, char quote)
466 {
467  if (quote)
468  return text;
469  else
470  return (std::string ("'") + text);
471 }
472 
473 void
475 {
476  // If we are using readline, this allows conditional parsing of the
477  // .inputrc file.
478 
479  command_editor::set_name ("Octave");
480 
481  // FIXME: this needs to include a comma too, but that
482  // causes trouble for the new struct element completion code.
483 
484  static const char *s = "\t\n !\"\'*+-/:;<=>(){}[\\]^`~";
485 
487 
489 
491 
492  command_editor::set_filename_quote_characters (" \t\n\\\"'@<>=;|&()#$`?*[!:{");
494 
496 
498 }
499 
500 static void
501 execute_in_debugger_handler (const std::pair<std::string, int>& arg)
502 {
503  octave_link::execute_in_debugger_event (arg.first, arg.second);
504 }
505 
506 static void
507 get_debug_input (const std::string& prompt)
508 {
509  unwind_protect frame;
510 
512  std::string nm;
513 
514  int curr_debug_line = octave_call_stack::current_line ();
515 
516  bool have_file = false;
517 
518  if (caller)
519  {
520  nm = caller->fcn_file_name ();
521 
522  if (nm.empty ())
523  nm = caller->name ();
524  else
525  have_file = true;
526  }
527  else
528  curr_debug_line = -1;
529 
530  std::ostringstream buf;
531 
532  if (! nm.empty ())
533  {
534  if (Vgud_mode)
535  {
536  static char ctrl_z = 'Z' & 0x1f;
537 
538  buf << ctrl_z << ctrl_z << nm << ":" << curr_debug_line;
539  }
540  else
541  {
542  // FIXME: we should come up with a clean way to detect
543  // that we are stopped on the no-op command that marks the
544  // end of a function or script.
545 
546  buf << "stopped in " << nm;
547 
548  if (curr_debug_line > 0)
549  buf << " at line " << curr_debug_line;
550 
551  if (have_file)
552  {
553  octave_link::enter_debugger_event (nm, curr_debug_line);
554 
556 
558  std::pair<std::string, int> (nm, curr_debug_line));
559 
560  std::string line_buf
561  = get_file_line (nm, curr_debug_line);
562 
563  if (! line_buf.empty ())
564  buf << "\n" << curr_debug_line << ": " << line_buf;
565  }
566  }
567  }
568 
569  std::string msg = buf.str ();
570 
571  if (! msg.empty ())
572  std::cerr << msg << std::endl;
573 
574  frame.protect_var (VPS1);
575  VPS1 = prompt;
576 
577  if (! (interactive || forced_interactive))
578  {
580  forced_interactive = true;
581  }
582 
583  octave_parser curr_parser;
584 
585  while (Vdebugging)
586  {
587  unwind_protect middle_frame;
588 
590 
591  curr_parser.reset ();
592 
593  int retval = curr_parser.run ();
594 
595  if (command_editor::interrupt (false))
596  break;
597  else
598  {
599  if (retval == 0 && curr_parser.stmt_list)
600  {
601  curr_parser.stmt_list->accept (*current_evaluator);
602 
605  }
606 
607  octave_quit ();
608  }
609  }
610 }
611 
612 const std::string octave_base_reader::in_src ("invalid");
613 
614 const std::string octave_terminal_reader::in_src ("terminal");
615 
616 std::string
618 {
619  octave_quit ();
620 
621  eof = false;
622 
623  return octave_gets (eof);
624 }
625 
626 const std::string octave_file_reader::in_src ("file");
627 
628 std::string
630 {
631  octave_quit ();
632 
633  eof = false;
634 
635  return octave_fgets (file, eof);
636 }
637 
638 const std::string octave_eval_string_reader::in_src ("eval_string");
639 
640 std::string
642 {
643  octave_quit ();
644 
645  eof = false;
646 
647  std::string retval;
648 
649  retval = eval_string;
650 
651  // Clear the eval string so that the next call will return
652  // an empty character string with EOF = true.
653  eval_string = "";
654 
655  if (retval.empty ())
656  eof = true;
657 
658  return retval;
659 }
660 
661 // If the user simply hits return, this will produce an empty matrix.
662 
663 static octave_value_list
664 get_user_input (const octave_value_list& args, int nargout)
665 {
666  octave_value_list retval;
667 
668  int nargin = args.length ();
669 
670  int read_as_string = 0;
671 
672  if (nargin == 2)
673  read_as_string++;
674 
675  std::string prompt = args(0).string_value ();
676 
677  if (error_state)
678  {
679  error ("input: unrecognized argument");
680  return retval;
681  }
682 
684 
687 
688  octave_diary << prompt;
689 
690  bool eof = false;
691 
692  std::string input_buf = interactive_input (prompt.c_str (), eof);
693 
694  if (! (error_state || input_buf.empty ()))
695  {
696  size_t len = input_buf.length ();
697 
698  octave_diary << input_buf;
699 
700  if (input_buf[len - 1] != '\n')
701  octave_diary << "\n";
702 
703  if (len < 1)
704  return read_as_string ? octave_value ("") : octave_value (Matrix ());
705 
706  if (read_as_string)
707  {
708  // FIXME: fix gnu_readline and octave_gets instead!
709  if (input_buf.length () == 1 && input_buf[0] == '\n')
710  retval(0) = "";
711  else
712  retval(0) = input_buf;
713  }
714  else
715  {
716  int parse_status = 0;
717 
718  retval = eval_string (input_buf, true, parse_status, nargout);
719 
720  if (! Vdebugging && retval.length () == 0)
721  retval(0) = Matrix ();
722  }
723  }
724  else
725  error ("input: reading user-input failed!");
726 
727  return retval;
728 }
729 
730 DEFUN (input, args, nargout,
731  "-*- texinfo -*-\n\
732 @deftypefn {Built-in Function} {@var{ans} =} input (@var{prompt})\n\
733 @deftypefnx {Built-in Function} {@var{ans} =} input (@var{prompt}, \"s\")\n\
734 Print a prompt and wait for user input. For example,\n\
735 \n\
736 @example\n\
737 input (\"Pick a number, any number! \")\n\
738 @end example\n\
739 \n\
740 @noindent\n\
741 prints the prompt\n\
742 \n\
743 @example\n\
744 Pick a number, any number!\n\
745 @end example\n\
746 \n\
747 @noindent\n\
748 and waits for the user to enter a value. The string entered by the user\n\
749 is evaluated as an expression, so it may be a literal constant, a\n\
750 variable name, or any other valid expression.\n\
751 \n\
752 Currently, @code{input} only returns one value, regardless of the number\n\
753 of values produced by the evaluation of the expression.\n\
754 \n\
755 If you are only interested in getting a literal string value, you can\n\
756 call @code{input} with the character string @qcode{\"s\"} as the second\n\
757 argument. This tells Octave to return the string entered by the user\n\
758 directly, without evaluating it first.\n\
759 \n\
760 Because there may be output waiting to be displayed by the pager, it is\n\
761 a good idea to always call @code{fflush (stdout)} before calling\n\
762 @code{input}. This will ensure that all pending output is written to\n\
763 the screen before your prompt.\n\
764 @seealso{yes_or_no, kbhit}\n\
765 @end deftypefn")
766 {
767  octave_value_list retval;
768 
769  int nargin = args.length ();
770 
771  if (nargin == 1 || nargin == 2)
772  retval = get_user_input (args, nargout);
773  else
774  print_usage ();
775 
776  return retval;
777 }
778 
779 bool
780 octave_yes_or_no (const std::string& prompt)
781 {
782  std::string prompt_string = prompt + "(yes or no) ";
783 
784  while (1)
785  {
786  bool eof = false;
787 
788  std::string input_buf = interactive_input (prompt_string, eof);
789 
790  if (input_buf == "yes")
791  return true;
792  else if (input_buf == "no")
793  return false;
794  else
795  message (0, "Please answer yes or no.");
796  }
797 }
798 
799 DEFUN (yes_or_no, args, ,
800  "-*- texinfo -*-\n\
801 @deftypefn {Built-in Function} {@var{ans} =} yes_or_no (\"@var{prompt}\")\n\
802 Ask the user a yes-or-no question. Return logical true if the answer is yes\n\
803 or false if the answer is no. Takes one argument, @var{prompt}, which is\n\
804 the string to display when asking the question. @var{prompt} should end in\n\
805 a space; @code{yes-or-no} adds the string @samp{(yes or no) } to it. The\n\
806 user must confirm the answer with @key{RET} and can edit it until it has\n\
807 been confirmed.\n\
808 @seealso{input}\n\
809 @end deftypefn")
810 {
811  octave_value retval;
812 
813  int nargin = args.length ();
814 
815  if (nargin == 0 || nargin == 1)
816  {
817  std::string prompt;
818 
819  if (nargin == 1)
820  {
821  prompt = args(0).string_value ();
822 
823  if (error_state)
824  {
825  error ("yes_or_no: PROMPT must be a character string");
826  return retval;
827  }
828  }
829 
830  retval = octave_yes_or_no (prompt);
831  }
832  else
833  print_usage ();
834 
835  return retval;
836 }
837 
840 {
841  octave_value retval;
842 
843  int nargin = args.length ();
844 
845  assert (nargin == 0 || nargin == 1);
846 
847  unwind_protect frame;
848 
851 
853 
854  frame.protect_var (Vdebugging);
855 
858 
859  // FIXME: probably we just want to print one line, not the
860  // entire statement, which might span many lines...
861  //
862  // tree_print_code tpc (octave_stdout);
863  // stmt.accept (tpc);
864 
865  Vdebugging = true;
866 
867  std::string prompt = "debug> ";
868  if (nargin > 0)
869  prompt = args(0).string_value ();
870 
871  if (! error_state)
872  get_debug_input (prompt);
873 
874  return retval;
875 }
876 
877 DEFUN (keyboard, args, ,
878  "-*- texinfo -*-\n\
879 @deftypefn {Built-in Function} {} keyboard ()\n\
880 @deftypefnx {Built-in Function} {} keyboard (\"@var{prompt}\")\n\
881 This function is normally used for simple debugging. When the\n\
882 @code{keyboard} function is executed, Octave prints a prompt and waits\n\
883 for user input. The input strings are then evaluated and the results\n\
884 are printed. This makes it possible to examine the values of variables\n\
885 within a function, and to assign new values if necessary. To leave the\n\
886 prompt and return to normal execution type @samp{return} or @samp{dbcont}.\n\
887 The @code{keyboard} function does not return an exit status.\n\
888 \n\
889 If @code{keyboard} is invoked without arguments, a default prompt of\n\
890 @samp{debug> } is used.\n\
891 @seealso{dbcont, dbquit}\n\
892 @end deftypefn")
893 {
894  octave_value_list retval;
895 
896  int nargin = args.length ();
897 
898  if (nargin == 0 || nargin == 1)
899  {
900  unwind_protect frame;
901 
904 
905  // Skip the frame assigned to the keyboard function.
907 
909 
911 
912  do_keyboard (args);
913  }
914  else
915  print_usage ();
916 
917  return retval;
918 }
919 
920 DEFUN (echo, args, ,
921  "-*- texinfo -*-\n\
922 @deftypefn {Command} {} echo options\n\
923 Control whether commands are displayed as they are executed. Valid\n\
924 options are:\n\
925 \n\
926 @table @code\n\
927 @item on\n\
928 Enable echoing of commands as they are executed in script files.\n\
929 \n\
930 @item off\n\
931 Disable echoing of commands as they are executed in script files.\n\
932 \n\
933 @item on all\n\
934 Enable echoing of commands as they are executed in script files and\n\
935 functions.\n\
936 \n\
937 @item off all\n\
938 Disable echoing of commands as they are executed in script files and\n\
939 functions.\n\
940 @end table\n\
941 \n\
942 @noindent\n\
943 With no arguments, @code{echo} toggles the current echo state.\n\
944 @end deftypefn")
945 {
946  octave_value_list retval;
947 
948  int argc = args.length () + 1;
949 
950  string_vector argv = args.make_argv ("echo");
951 
952  if (error_state)
953  return retval;
954 
955  switch (argc)
956  {
957  case 1:
958  {
959  if ((Vecho_executing_commands & ECHO_SCRIPTS)
960  || (Vecho_executing_commands & ECHO_FUNCTIONS))
961  Vecho_executing_commands = ECHO_OFF;
962  else
963  Vecho_executing_commands = ECHO_SCRIPTS;
964  }
965  break;
966 
967  case 2:
968  {
969  std::string arg = argv[1];
970 
971  if (arg == "on")
972  Vecho_executing_commands = ECHO_SCRIPTS;
973  else if (arg == "off")
974  Vecho_executing_commands = ECHO_OFF;
975  else
976  print_usage ();
977  }
978  break;
979 
980  case 3:
981  {
982  std::string arg = argv[1];
983 
984  if (arg == "on" && argv[2] == "all")
985  {
986  int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS);
987  Vecho_executing_commands = tmp;
988  }
989  else if (arg == "off" && argv[2] == "all")
990  Vecho_executing_commands = ECHO_OFF;
991  else
992  print_usage ();
993  }
994  break;
995 
996  default:
997  print_usage ();
998  break;
999  }
1000 
1001  return retval;
1002 }
1003 
1004 DEFUN (completion_matches, args, nargout,
1005  "-*- texinfo -*-\n\
1006 @deftypefn {Built-in Function} {} completion_matches (@var{hint})\n\
1007 Generate possible completions given @var{hint}.\n\
1008 \n\
1009 This function is provided for the benefit of programs like Emacs which\n\
1010 might be controlling Octave and handling user input. The current\n\
1011 command number is not incremented when this function is called. This is\n\
1012 a feature, not a bug.\n\
1013 @end deftypefn")
1014 {
1015  octave_value retval;
1016 
1017  int nargin = args.length ();
1018 
1019  if (nargin == 1)
1020  {
1021  std::string hint = args(0).string_value ();
1022 
1023  if (! error_state)
1024  {
1025  int n = 32;
1026 
1027  string_vector list (n);
1028 
1029  int k = 0;
1030 
1031  for (;;)
1032  {
1033  std::string cmd = generate_completion (hint, k);
1034 
1035  if (! cmd.empty ())
1036  {
1037  if (k == n)
1038  {
1039  n *= 2;
1040  list.resize (n);
1041  }
1042 
1043  list[k++] = cmd;
1044  }
1045  else
1046  {
1047  list.resize (k);
1048  break;
1049  }
1050  }
1051 
1052  if (nargout > 0)
1053  {
1054  if (! list.empty ())
1055  retval = list;
1056  else
1057  retval = "";
1058  }
1059  else
1060  {
1061  // We don't use string_vector::list_in_columns here
1062  // because it will be easier for Emacs if the names
1063  // appear in a single column.
1064 
1065  int len = list.length ();
1066 
1067  for (int i = 0; i < len; i++)
1068  octave_stdout << list[i] << "\n";
1069  }
1070 
1072  }
1073  }
1074  else
1075  print_usage ();
1076 
1077  return retval;
1078 }
1079 
1080 DEFUN (readline_read_init_file, args, ,
1081  "-*- texinfo -*-\n\
1082 @deftypefn {Built-in Function} {} readline_read_init_file (@var{file})\n\
1083 Read the readline library initialization file @var{file}. If\n\
1084 @var{file} is omitted, read the default initialization file (normally\n\
1085 @file{~/.inputrc}).\n\
1086 \n\
1087 @xref{Readline Init File, , , readline, GNU Readline Library},\n\
1088 for details.\n\
1089 @seealso{readline_re_read_init_file}\n\
1090 @end deftypefn")
1091 {
1092  octave_value_list retval;
1093 
1094  int nargin = args.length ();
1095 
1096  if (nargin == 0)
1098  else if (nargin == 1)
1099  {
1100  std::string file = args(0).string_value ();
1101 
1102  if (! error_state)
1104  }
1105  else
1106  print_usage ();
1107 
1108  return retval;
1109 }
1110 
1111 DEFUN (readline_re_read_init_file, args, ,
1112  "-*- texinfo -*-\n\
1113 @deftypefn {Built-in Function} {} readline_re_read_init_file ()\n\
1114 Re-read the last readline library initialization file that was read.\n\
1115 @xref{Readline Init File, , , readline, GNU Readline Library},\n\
1116 for details.\n\
1117 @seealso{readline_read_init_file}\n\
1118 @end deftypefn")
1119 {
1120  octave_value_list retval;
1121 
1122  if (args.length () == 0)
1124  else
1125  print_usage ();
1126 
1127  return retval;
1128 }
1129 
1130 static int
1132 {
1133  input_event_hook_functions.run ();
1134 
1135  if (input_event_hook_functions.empty ())
1137 
1138  return 0;
1139 }
1140 
1141 DEFUN (add_input_event_hook, args, ,
1142  "-*- texinfo -*-\n\
1143 @deftypefn {Built-in Function} {@var{id} =} add_input_event_hook (@var{fcn})\n\
1144 @deftypefnx {Built-in Function} {@var{id} =} add_input_event_hook (@var{fcn}, @var{data})\n\
1145 Add the named function or function handle @var{fcn} to the list of functions\n\
1146 to call periodically when Octave is waiting for input. The function should\n\
1147 have the form\n\
1148 \n\
1149 @example\n\
1150 @var{fcn} (@var{data})\n\
1151 @end example\n\
1152 \n\
1153 If @var{data} is omitted, Octave calls the function without any\n\
1154 arguments.\n\
1155 \n\
1156 The returned identifier may be used to remove the function handle from\n\
1157 the list of input hook functions.\n\
1158 @seealso{remove_input_event_hook}\n\
1159 @end deftypefn")
1160 {
1161  octave_value retval;
1162 
1163  int nargin = args.length ();
1164 
1165  if (nargin == 1 || nargin == 2)
1166  {
1167  octave_value user_data;
1168 
1169  if (nargin == 2)
1170  user_data = args(1);
1171 
1172  hook_function hook_fcn (args(0), user_data);
1173 
1174  if (! error_state)
1175  {
1176  if (input_event_hook_functions.empty ())
1178 
1179  input_event_hook_functions.insert (hook_fcn.id (), hook_fcn);
1180 
1181  retval = hook_fcn.id ();
1182  }
1183  else
1184  error ("add_input_event_hook: expecting function handle or character string as first argument");
1185  }
1186  else
1187  print_usage ();
1188 
1189  return retval;
1190 }
1191 
1192 DEFUN (remove_input_event_hook, args, ,
1193  "-*- texinfo -*-\n\
1194 @deftypefn {Built-in Function} {} remove_input_event_hook (@var{name})\n\
1195 @deftypefnx {Built-in Function} {} remove_input_event_hook (@var{fcn_id})\n\
1196 Remove the named function or function handle with the given identifier\n\
1197 from the list of functions to call periodically when Octave is waiting\n\
1198 for input.\n\
1199 @seealso{add_input_event_hook}\n\
1200 @end deftypefn")
1201 {
1202  octave_value_list retval;
1203 
1204  int nargin = args.length ();
1205 
1206  if (nargin == 1 || nargin == 2)
1207  {
1208  std::string hook_fcn_id = args(0).string_value ();
1209 
1210  bool warn = (nargin < 2);
1211 
1212  if (! error_state)
1213  {
1215  = input_event_hook_functions.find (hook_fcn_id);
1216 
1217  if (p != input_event_hook_functions.end ())
1218  input_event_hook_functions.erase (p);
1219  else if (warn)
1220  warning ("remove_input_event_hook: %s not found in list",
1221  hook_fcn_id.c_str ());
1222 
1223  if (input_event_hook_functions.empty ())
1225  }
1226  else
1227  error ("remove_input_event_hook: argument not valid as a hook function name or id");
1228  }
1229  else
1230  print_usage ();
1231 
1232  return retval;
1233 }
1234 
1235 DEFUN (PS1, args, nargout,
1236  "-*- texinfo -*-\n\
1237 @deftypefn {Built-in Function} {@var{val} =} PS1 ()\n\
1238 @deftypefnx {Built-in Function} {@var{old_val} =} PS1 (@var{new_val})\n\
1239 @deftypefnx {Built-in Function} {} PS1 (@var{new_val}, \"local\")\n\
1240 Query or set the primary prompt string. When executing interactively,\n\
1241 Octave displays the primary prompt when it is ready to read a command.\n\
1242 \n\
1243 The default value of the primary prompt string is @qcode{\"octave:\\#> \"}.\n\
1244 To change it, use a command like\n\
1245 \n\
1246 @example\n\
1247 PS1 (\"\\\\u@@\\\\H> \")\n\
1248 @end example\n\
1249 \n\
1250 @noindent\n\
1251 which will result in the prompt @samp{boris@@kremvax> } for the user\n\
1252 @samp{boris} logged in on the host @samp{kremvax.kgb.su}. Note that two\n\
1253 backslashes are required to enter a backslash into a double-quoted\n\
1254 character string. @xref{Strings}.\n\
1255 \n\
1256 You can also use ANSI escape sequences if your terminal supports them.\n\
1257 This can be useful for coloring the prompt. For example,\n\
1258 \n\
1259 @example\n\
1260 PS1 (\"\\\\[\\\\033[01;31m\\\\]\\\\s:\\\\#> \\\\[\\\\033[0m\\\\]\")\n\
1261 @end example\n\
1262 \n\
1263 @noindent\n\
1264 will give the default Octave prompt a red coloring.\n\
1265 \n\
1266 When called from inside a function with the @qcode{\"local\"} option, the\n\
1267 variable is changed locally for the function and any subroutines it calls. \n\
1268 The original variable value is restored when exiting the function.\n\
1269 @seealso{PS2, PS4}\n\
1270 @end deftypefn")
1271 {
1272  return SET_INTERNAL_VARIABLE (PS1);
1273 }
1274 
1275 DEFUN (PS2, args, nargout,
1276  "-*- texinfo -*-\n\
1277 @deftypefn {Built-in Function} {@var{val} =} PS2 ()\n\
1278 @deftypefnx {Built-in Function} {@var{old_val} =} PS2 (@var{new_val})\n\
1279 @deftypefnx {Built-in Function} {} PS2 (@var{new_val}, \"local\")\n\
1280 Query or set the secondary prompt string. The secondary prompt is\n\
1281 printed when Octave is expecting additional input to complete a\n\
1282 command. For example, if you are typing a @code{for} loop that spans several\n\
1283 lines, Octave will print the secondary prompt at the beginning of\n\
1284 each line after the first. The default value of the secondary prompt\n\
1285 string is @qcode{\"> \"}.\n\
1286 \n\
1287 When called from inside a function with the @qcode{\"local\"} option, the\n\
1288 variable is changed locally for the function and any subroutines it calls. \n\
1289 The original variable value is restored when exiting the function.\n\
1290 @seealso{PS1, PS4}\n\
1291 @end deftypefn")
1292 {
1293  return SET_INTERNAL_VARIABLE (PS2);
1294 }
1295 
1296 DEFUN (PS4, args, nargout,
1297  "-*- texinfo -*-\n\
1298 @deftypefn {Built-in Function} {@var{val} =} PS4 ()\n\
1299 @deftypefnx {Built-in Function} {@var{old_val} =} PS4 (@var{new_val})\n\
1300 @deftypefnx {Built-in Function} {} PS4 (@var{new_val}, \"local\")\n\
1301 Query or set the character string used to prefix output produced\n\
1302 when echoing commands is enabled.\n\
1303 The default value is @qcode{\"+ \"}.\n\
1304 @xref{Diary and Echo Commands}, for a description of echoing commands.\n\
1305 \n\
1306 When called from inside a function with the @qcode{\"local\"} option, the\n\
1307 variable is changed locally for the function and any subroutines it calls. \n\
1308 The original variable value is restored when exiting the function.\n\
1309 @seealso{echo, echo_executing_commands, PS1, PS2}\n\
1310 @end deftypefn")
1311 {
1312  return SET_INTERNAL_VARIABLE (PS4);
1313 }
1314 
1315 DEFUN (completion_append_char, args, nargout,
1316  "-*- texinfo -*-\n\
1317 @deftypefn {Built-in Function} {@var{val} =} completion_append_char ()\n\
1318 @deftypefnx {Built-in Function} {@var{old_val} =} completion_append_char (@var{new_val})\n\
1319 @deftypefnx {Built-in Function} {} completion_append_char (@var{new_val}, \"local\")\n\
1320 Query or set the internal character variable that is appended to\n\
1321 successful command-line completion attempts. The default\n\
1322 value is @qcode{\" \"} (a single space).\n\
1323 \n\
1324 When called from inside a function with the @qcode{\"local\"} option, the\n\
1325 variable is changed locally for the function and any subroutines it calls. \n\
1326 The original variable value is restored when exiting the function.\n\
1327 @end deftypefn")
1328 {
1329  return SET_INTERNAL_VARIABLE (completion_append_char);
1330 }
1331 
1332 DEFUN (echo_executing_commands, args, nargout,
1333  "-*- texinfo -*-\n\
1334 @deftypefn {Built-in Function} {@var{val} =} echo_executing_commands ()\n\
1335 @deftypefnx {Built-in Function} {@var{old_val} =} echo_executing_commands (@var{new_val})\n\
1336 @deftypefnx {Built-in Function} {} echo_executing_commands (@var{new_val}, \"local\")\n\
1337 Query or set the internal variable that controls the echo state.\n\
1338 It may be the sum of the following values:\n\
1339 \n\
1340 @table @asis\n\
1341 @item 1\n\
1342 Echo commands read from script files.\n\
1343 \n\
1344 @item 2\n\
1345 Echo commands from functions.\n\
1346 \n\
1347 @item 4\n\
1348 Echo commands read from command line.\n\
1349 @end table\n\
1350 \n\
1351 More than one state can be active at once. For example, a value of 3 is\n\
1352 equivalent to the command @kbd{echo on all}.\n\
1353 \n\
1354 The value of @code{echo_executing_commands} may be set by the @kbd{echo}\n\
1355 command or the command line option @option{--echo-commands}.\n\
1356 \n\
1357 When called from inside a function with the @qcode{\"local\"} option, the\n\
1358 variable is changed locally for the function and any subroutines it calls. \n\
1359 The original variable value is restored when exiting the function.\n\
1360 @end deftypefn")
1361 {
1362  return SET_INTERNAL_VARIABLE (echo_executing_commands);
1363 }
1364 
1365 DEFUN (__request_drawnow__, args, ,
1366  "-*- texinfo -*-\n\
1367 @deftypefn {Built-in Function} {} __request_drawnow__ ()\n\
1368 @deftypefnx {Built-in Function} {} __request_drawnow__ (@var{flag})\n\
1369 Undocumented internal function.\n\
1370 @end deftypefn")
1371 {
1372  octave_value retval;
1373 
1374  int nargin = args.length ();
1375 
1376  if (nargin == 0)
1377  Vdrawnow_requested = true;
1378  else if (nargin == 1)
1379  Vdrawnow_requested = args(0).bool_value ();
1380  else
1381  print_usage ();
1382 
1383  return retval;
1384 }
1385 
1386 DEFUN (__gud_mode__, args, ,
1387  "-*- texinfo -*-\n\
1388 @deftypefn {Built-in Function} {} __gud_mode__ ()\n\
1389 Undocumented internal function.\n\
1390 @end deftypefn")
1391 {
1392  octave_value retval;
1393 
1394  int nargin = args.length ();
1395 
1396  if (nargin == 0)
1397  retval = Vgud_mode;
1398  else if (nargin == 1)
1399  Vgud_mode = args(0).bool_value ();
1400  else
1401  print_usage ();
1402 
1403  return retval;
1404 }
1405 
1406 DEFUN (filemarker, args, nargout,
1407  "-*- texinfo -*-\n\
1408 @deftypefn {Built-in Function} {@var{val} =} filemarker ()\n\
1409 @deftypefnx {Built-in Function} {} filemarker (@var{new_val})\n\
1410 @deftypefnx {Built-in Function} {} filemarker (@var{new_val}, \"local\")\n\
1411 Query or set the character used to separate filename from the\n\
1412 the subfunction names contained within the file. This can be used in\n\
1413 a generic manner to interact with subfunctions. For example,\n\
1414 \n\
1415 @example\n\
1416 help ([\"myfunc\", filemarker, \"mysubfunc\"])\n\
1417 @end example\n\
1418 \n\
1419 @noindent\n\
1420 returns the help string associated with the subfunction @code{mysubfunc}\n\
1421 of the function @code{myfunc}. Another use of @code{filemarker} is when\n\
1422 debugging it allows easier placement of breakpoints within subfunctions.\n\
1423 For example,\n\
1424 \n\
1425 @example\n\
1426 dbstop ([\"myfunc\", filemarker, \"mysubfunc\"])\n\
1427 @end example\n\
1428 \n\
1429 @noindent\n\
1430 will set a breakpoint at the first line of the subfunction @code{mysubfunc}.\n\
1431 \n\
1432 When called from inside a function with the @qcode{\"local\"} option, the\n\
1433 variable is changed locally for the function and any subroutines it calls. \n\
1434 The original variable value is restored when exiting the function.\n\
1435 @end deftypefn")
1436 {
1437  char tmp = Vfilemarker;
1438  octave_value retval = SET_INTERNAL_VARIABLE (filemarker);
1439 
1440  // The character passed must not be a legal character for a function name
1441  if (! error_state && (::isalnum (Vfilemarker) || Vfilemarker == '_'))
1442  {
1443  Vfilemarker = tmp;
1444  error ("filemarker: character can not be a valid character for a function name");
1445  }
1446 
1447  return retval;
1448 }