GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
input.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2021 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 // Use the GNU readline library for command line editing and history.
27 
28 #if ! defined (octave_input_h)
29 #define octave_input_h 1
30 
31 #include "octave-config.h"
32 
33 #include <cstdio>
34 
35 #include <memory>
36 #include <string>
37 
38 #include "hook-fcn.h"
39 #include "oct-time.h"
40 #include "ovl.h"
41 #include "pager.h"
42 
43 // TRUE after a call to completion_matches.
45 
46 // TRUE if the plotting system has requested a call to drawnow at
47 // the next user prompt.
48 extern OCTINTERP_API bool Vdrawnow_requested;
49 
50 OCTAVE_DEPRECATED (6, "'Vtrack_line_num' is an obsolete internal variable; any uses should be removed")
51 extern OCTINTERP_API bool Vtrack_line_num;
52 
53 extern octave::sys::time Vlast_prompt_time;
54 
55 class octave_value;
56 
57 namespace octave
58 {
59  class interpreter;
60 
62  {
63  public:
64 
66 
67  void initialize (bool line_editing);
68 
69  octave_value PS1 (const octave_value_list& args, int nargout);
70 
71  std::string PS1 (void) const { return m_PS1; }
72 
73  std::string PS1 (const std::string& s)
74  {
75  std::string val = m_PS1;
76  m_PS1 = s;
77  return val;
78  }
79 
80  void set_PS1 (const std::string& s) { m_PS1 = s; }
81 
82  octave_value PS2 (const octave_value_list& args, int nargout);
83 
84  std::string PS2 (void) const { return m_PS2; }
85 
86  std::string PS2 (const std::string& s)
87  {
88  std::string val = m_PS2;
89  m_PS2 = s;
90  return val;
91  }
92 
93  void set_PS2 (const std::string& s) { m_PS2 = s; }
94 
95  std::string last_debugging_command (void) const
96  {
97  return m_last_debugging_command;
98  }
99 
100  std::string last_debugging_command (const std::string& s)
101  {
102  std::string val = m_last_debugging_command;
103  m_last_debugging_command = s;
104  return val;
105  }
106 
108  completion_append_char (const octave_value_list& args, int nargout);
109 
110  char completion_append_char (void) const
111  {
112  return m_completion_append_char;
113  }
114 
116  {
117  char val = m_completion_append_char;
118  m_completion_append_char = c;
119  return val;
120  }
121 
122  void set_completion_append_char (char c) { m_completion_append_char = c; }
123 
124  octave_value gud_mode (const octave_value_list& args, int nargout);
125 
126  bool gud_mode (void) const { return m_gud_mode; }
127 
128  bool gud_mode (bool flag)
129  {
130  bool val = m_gud_mode;
131  m_gud_mode = flag;
132  return val;
133  }
134 
135  void set_gud_mode (bool flag) { m_gud_mode = flag; }
136 
137  octave_value mfile_encoding (const octave_value_list& args, int nargout);
138 
139  std::string mfile_encoding (void) const { return m_mfile_encoding; }
140 
141  std::string mfile_encoding (const std::string& s)
142  {
143  std::string val = m_mfile_encoding;
144  m_mfile_encoding = s;
145  return val;
146  }
147 
148  void set_mfile_encoding (const std::string& s) { m_mfile_encoding = s; }
149 
151  auto_repeat_debug_command (const octave_value_list& args, int nargout);
152 
153  bool auto_repeat_debug_command (void) const
154  {
155  return m_auto_repeat_debug_command;
156  }
157 
159  {
160  bool old_val = m_auto_repeat_debug_command;
161  m_auto_repeat_debug_command = val;
162  return old_val;
163  }
164 
165  bool yes_or_no (const std::string& prompt);
166 
167  std::string interactive_input (const std::string& s, bool& eof);
168 
170  get_user_input (const octave_value_list& args, int nargout);
171 
172  bool have_input_event_hooks (void) const;
173 
174  void add_input_event_hook (const hook_function& hook_fcn);
175 
176  bool remove_input_event_hook (const std::string& hook_fcn_id);
177 
179 
181 
182  private:
183 
185 
186  // Primary prompt string.
187  std::string m_PS1;
188 
189  // Secondary prompt string.
190  std::string m_PS2;
191 
192  // Character to append after successful command-line completion
193  // attempts.
195 
196  // TRUE if we are running in the Emacs GUD mode.
198 
199  // Codepage which is used to read .m files
200  std::string m_mfile_encoding;
201 
202  // TRUE means repeat last debug command if the user just types RET.
204 
205  // If we are in debugging mode, this is the last command entered,
206  // so that we can repeat the previous command if the user just
207  // types RET.
209 
211 
213 
214  std::string gnu_readline (const std::string& s, bool& eof) const;
215  };
216 
218  {
219  public:
220 
221  friend class input_reader;
222 
224  : m_interpreter (interp)
225  { }
226 
228  : m_interpreter (x.m_interpreter)
229  { }
230 
231  virtual ~base_reader (void) = default;
232 
233  virtual std::string get_input (const std::string& prompt, bool& eof) = 0;
234 
235  virtual std::string input_source (void) const { return s_in_src; }
236 
237  std::string octave_gets (const std::string& prompt, bool& eof);
238 
239  virtual bool input_from_terminal (void) const { return false; }
240 
241  virtual bool input_from_file (void) const { return false; }
242 
243  virtual bool input_from_eval_string (void) const { return false; }
244 
245  protected:
246 
248 
249  private:
250 
251  static const std::string s_in_src;
252  };
253 
255  {
256  public:
257 
259 
260  input_reader (interpreter& interp, FILE *file);
261 
262  input_reader (interpreter& interp, const std::string& str);
263 
264  input_reader (const input_reader& ir) = default;
265 
266  input_reader& operator = (const input_reader& ir) = default;
267 
268  ~input_reader (void) = default;
269 
270  std::string get_input (const std::string& prompt, bool& eof)
271  {
272  return m_rep->get_input (prompt, eof);
273  }
274 
275  std::string input_source (void) const
276  {
277  return m_rep->input_source ();
278  }
279 
280  bool input_from_terminal (void) const
281  {
282  return m_rep->input_from_terminal ();
283  }
284 
285  bool input_from_file (void) const
286  {
287  return m_rep->input_from_file ();
288  }
289 
290  bool input_from_eval_string (void) const
291  {
292  return m_rep->input_from_eval_string ();
293  }
294 
295  private:
296 
297  std::shared_ptr<base_reader> m_rep;
298  };
299 }
300 
301 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
302 
303 OCTAVE_DEPRECATED (5, "use 'octave::input_system::yes_or_no' instead")
304 extern bool octave_yes_or_no (const std::string& prompt);
305 
306 OCTAVE_DEPRECATED (5, "use 'octave::input_system::clear_input_event_hooks' instead")
307 extern void remove_input_event_hook_functions (void);
308 
309 OCTAVE_DEPRECATED (5, "this function will be removed in a future version of Octave")
310 extern OCTINTERP_API FILE * get_input_from_stdin (void);
311 
312 #endif
313 
314 #endif
base_reader(const base_reader &x)
Definition: input.h:227
virtual std::string get_input(const std::string &prompt, bool &eof)=0
virtual bool input_from_file(void) const
Definition: input.h:241
interpreter & m_interpreter
Definition: input.h:247
virtual ~base_reader(void)=default
virtual std::string input_source(void) const
Definition: input.h:235
base_reader(interpreter &interp)
Definition: input.h:223
virtual bool input_from_terminal(void) const
Definition: input.h:239
std::string octave_gets(const std::string &prompt, bool &eof)
virtual bool input_from_eval_string(void) const
Definition: input.h:243
static const std::string s_in_src
Definition: input.h:251
input_reader(const input_reader &ir)=default
input_reader(interpreter &interp)
bool input_from_eval_string(void) const
Definition: input.h:290
std::shared_ptr< base_reader > m_rep
Definition: input.h:297
std::string get_input(const std::string &prompt, bool &eof)
Definition: input.h:270
bool input_from_file(void) const
Definition: input.h:285
input_reader(interpreter &interp, const std::string &str)
input_reader(interpreter &interp, FILE *file)
bool input_from_terminal(void) const
Definition: input.h:280
std::string input_source(void) const
Definition: input.h:275
~input_reader(void)=default
octave_value PS1(const octave_value_list &args, int nargout)
octave_value completion_append_char(const octave_value_list &args, int nargout)
hook_function_list m_input_event_hook_functions
Definition: input.h:210
std::string PS1(const std::string &s)
Definition: input.h:73
void set_PS1(const std::string &s)
Definition: input.h:80
std::string m_last_debugging_command
Definition: input.h:208
std::string gnu_readline(const std::string &s, bool &eof) const
bool gud_mode(bool flag)
Definition: input.h:128
void set_gud_mode(bool flag)
Definition: input.h:135
std::string interactive_input(const std::string &s, bool &eof)
void set_PS2(const std::string &s)
Definition: input.h:93
octave_value mfile_encoding(const octave_value_list &args, int nargout)
std::string mfile_encoding(const std::string &s)
Definition: input.h:141
bool remove_input_event_hook(const std::string &hook_fcn_id)
bool m_auto_repeat_debug_command
Definition: input.h:203
std::string PS1(void) const
Definition: input.h:71
char completion_append_char(char c)
Definition: input.h:115
interpreter & m_interpreter
Definition: input.h:184
octave_value_list get_user_input(const octave_value_list &args, int nargout)
std::string m_PS1
Definition: input.h:187
bool auto_repeat_debug_command(bool val)
Definition: input.h:158
std::string last_debugging_command(const std::string &s)
Definition: input.h:100
char m_completion_append_char
Definition: input.h:194
std::string mfile_encoding(void) const
Definition: input.h:139
octave_value auto_repeat_debug_command(const octave_value_list &args, int nargout)
std::string PS2(const std::string &s)
Definition: input.h:86
void initialize(bool line_editing)
std::string m_PS2
Definition: input.h:190
void add_input_event_hook(const hook_function &hook_fcn)
void run_input_event_hooks(void)
void set_completion_append_char(char c)
Definition: input.h:122
bool auto_repeat_debug_command(void) const
Definition: input.h:153
octave_value PS2(const octave_value_list &args, int nargout)
bool yes_or_no(const std::string &prompt)
input_system(interpreter &interp)
bool have_input_event_hooks(void) const
char completion_append_char(void) const
Definition: input.h:110
bool gud_mode(void) const
Definition: input.h:126
std::string m_mfile_encoding
Definition: input.h:200
std::string PS2(void) const
Definition: input.h:84
octave_value gud_mode(const octave_value_list &args, int nargout)
std::string last_debugging_command(void) const
Definition: input.h:95
void set_mfile_encoding(const std::string &s)
Definition: input.h:148
void clear_input_event_hooks(void)
octave::sys::time Vlast_prompt_time
Definition: input.cc:83
OCTINTERP_API bool Vtrack_line_num
Definition: input.cc:95
OCTINTERP_API bool Vdrawnow_requested
Definition: input.cc:90
bool octave_completion_matches_called
Definition: input.cc:86
F77_RET_T const F77_DBLE * x