GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
input.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2024 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 #include <unordered_map>
38 
39 #include "hook-fcn.h"
40 #include "oct-time.h"
41 #include "ovl.h"
42 #include "pager.h"
43 
44 // TRUE after a call to completion_matches.
46 
47 // TRUE if the plotting system has requested a call to drawnow at
48 // the next user prompt.
49 extern OCTINTERP_API bool Vdrawnow_requested;
50 
51 extern OCTINTERP_API octave::sys::time Vlast_prompt_time;
52 
53 class octave_value;
54 
56 
57 class interpreter;
58 
60 {
61 public:
62 
64 
65  void initialize (bool line_editing);
66 
67  octave_value PS1 (const octave_value_list& args, int nargout);
68 
69  std::string PS1 () const { return m_PS1; }
70 
71  std::string PS1 (const std::string& s)
72  {
73  std::string val = m_PS1;
74  m_PS1 = s;
75  return val;
76  }
77 
78  void set_PS1 (const std::string& s) { m_PS1 = s; }
79 
80  octave_value PS2 (const octave_value_list& args, int nargout);
81 
82  std::string PS2 () const { return m_PS2; }
83 
84  std::string PS2 (const std::string& s)
85  {
86  std::string val = m_PS2;
87  m_PS2 = s;
88  return val;
89  }
90 
91  void set_PS2 (const std::string& s) { m_PS2 = s; }
92 
93  std::string last_debugging_command () const
94  {
95  return m_last_debugging_command;
96  }
97 
98  std::string last_debugging_command (const std::string& s)
99  {
100  std::string val = m_last_debugging_command;
101  m_last_debugging_command = s;
102  return val;
103  }
104 
106  completion_append_char (const octave_value_list& args, int nargout);
107 
109  {
110  return m_completion_append_char;
111  }
112 
114  {
115  char val = m_completion_append_char;
116  m_completion_append_char = c;
117  return val;
118  }
119 
120  void set_completion_append_char (char c) { m_completion_append_char = c; }
121 
122  octave_value gud_mode (const octave_value_list& args, int nargout);
123 
124  bool gud_mode () const { return m_gud_mode; }
125 
126  bool gud_mode (bool flag)
127  {
128  bool val = m_gud_mode;
129  m_gud_mode = flag;
130  return val;
131  }
132 
133  void set_gud_mode (bool flag) { m_gud_mode = flag; }
134 
135  octave_value mfile_encoding (const octave_value_list& args, int nargout);
136 
137  std::string mfile_encoding () const { return m_mfile_encoding; }
138 
139  std::string mfile_encoding (const std::string& s)
140  {
141  std::string val = m_mfile_encoding;
142  m_mfile_encoding = s;
143  return val;
144  }
145 
146  void set_mfile_encoding (const std::string& s) { m_mfile_encoding = s; }
147 
148  std::string dir_encoding (const std::string& dir);
149 
150  void set_dir_encoding (const std::string& dir, std::string& enc);
151 
153  auto_repeat_debug_command (const octave_value_list& args, int nargout);
154 
156  {
157  return m_auto_repeat_debug_command;
158  }
159 
161  {
162  bool old_val = m_auto_repeat_debug_command;
163  m_auto_repeat_debug_command = val;
164  return old_val;
165  }
166 
167  bool yes_or_no (const std::string& prompt);
168 
169  std::string interactive_input (const std::string& s, bool& eof);
170 
172  get_user_input (const octave_value_list& args, int nargout);
173 
174  bool have_input_event_hooks () const;
175 
176  void add_input_event_hook (const hook_function& hook_fcn);
177 
178  bool remove_input_event_hook (const std::string& hook_fcn_id);
179 
181 
183 
184 private:
185 
186  interpreter& m_interpreter;
187 
188  // Primary prompt string.
189  std::string m_PS1;
190 
191  // Secondary prompt string.
192  std::string m_PS2;
193 
194  // Character to append after successful command-line completion
195  // attempts.
196  char m_completion_append_char;
197 
198  // TRUE if we are running in the Emacs GUD mode.
199  bool m_gud_mode;
200 
201  // Codepage which is used to read .m files
202  std::string m_mfile_encoding;
203 
204  // map of directories -> used mfile encoding
205  std::unordered_map<std::string, std::string> m_dir_encoding;
206 
207  // TRUE means repeat last debug command if the user just types RET.
208  bool m_auto_repeat_debug_command;
209 
210  // If we are in debugging mode, this is the last command entered,
211  // so that we can repeat the previous command if the user just
212  // types RET.
213  std::string m_last_debugging_command;
214 
215  hook_function_list m_input_event_hook_functions;
216 
217  bool m_initialized;
218 
219  std::string gnu_readline (const std::string& s, bool& eof) const;
220 };
221 
223 {
224 public:
225 
226  friend class input_reader;
227 
229  : m_interpreter (interp)
230  { }
231 
232  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (base_reader)
233 
234  virtual ~base_reader () = default;
235 
236  virtual std::string get_input (const std::string& prompt, bool& eof) = 0;
237 
238  virtual std::string input_source () const { return s_in_src; }
239 
240  std::string octave_gets (const std::string& prompt, bool& eof);
241 
242  virtual bool input_from_terminal () const { return false; }
243 
244  virtual bool input_from_file () const { return false; }
245 
246  virtual bool input_from_eval_string () const { return false; }
247 
248 protected:
249 
251 
252 private:
253 
254  static const std::string s_in_src;
255 };
256 
258 {
259 public:
260 
261  input_reader () = delete;
262 
264 
265  input_reader (interpreter& interp, FILE *file);
266 
267  input_reader (interpreter& interp, FILE *file, const std::string& enc);
268 
269  input_reader (interpreter& interp, const std::string& str);
270 
271  OCTAVE_DEFAULT_COPY_DELETE (input_reader)
272 
273  std::string get_input (const std::string& prompt, bool& eof)
274  {
275  return m_rep->get_input (prompt, eof);
276  }
277 
278  std::string input_source () const
279  {
280  return m_rep->input_source ();
281  }
282 
283  bool input_from_terminal () const
284  {
285  return m_rep->input_from_terminal ();
286  }
287 
288  bool input_from_file () const
289  {
290  return m_rep->input_from_file ();
291  }
292 
294  {
295  return m_rep->input_from_eval_string ();
296  }
297 
298 private:
299 
300  std::shared_ptr<base_reader> m_rep;
301 };
302 
303 OCTAVE_END_NAMESPACE(octave)
304 
305 #endif
virtual std::string input_source() const
Definition: input.h:238
std::string octave_gets(const std::string &prompt, bool &eof)
base_reader(interpreter &interp)
Definition: input.h:228
virtual std::string get_input(const std::string &prompt, bool &eof)=0
virtual bool input_from_terminal() const
Definition: input.h:242
virtual bool input_from_eval_string() const
Definition: input.h:246
interpreter & m_interpreter
Definition: input.h:250
virtual bool input_from_file() const
Definition: input.h:244
bool input_from_file() const
Definition: input.h:288
std::string input_source() const
Definition: input.h:278
input_reader(interpreter &interp, const std::string &str)
std::string get_input(const std::string &prompt, bool &eof)
Definition: input.h:273
bool input_from_eval_string() const
Definition: input.h:293
input_reader(interpreter &interp, FILE *file)
input_reader(interpreter &interp)
bool input_from_terminal() const
Definition: input.h:283
input_reader(interpreter &interp, FILE *file, const std::string &enc)
input_reader()=delete
bool remove_input_event_hook(const std::string &hook_fcn_id)
octave_value auto_repeat_debug_command(const octave_value_list &args, int nargout)
char completion_append_char(char c)
Definition: input.h:113
input_system(interpreter &interp)
void initialize(bool line_editing)
std::string last_debugging_command() const
Definition: input.h:93
std::string PS1(const std::string &s)
Definition: input.h:71
bool yes_or_no(const std::string &prompt)
void clear_input_event_hooks()
void set_gud_mode(bool flag)
Definition: input.h:133
char completion_append_char() const
Definition: input.h:108
octave_value gud_mode(const octave_value_list &args, int nargout)
std::string PS2(const std::string &s)
Definition: input.h:84
void set_completion_append_char(char c)
Definition: input.h:120
std::string mfile_encoding(const std::string &s)
Definition: input.h:139
void set_PS2(const std::string &s)
Definition: input.h:91
void run_input_event_hooks()
void add_input_event_hook(const hook_function &hook_fcn)
void set_dir_encoding(const std::string &dir, std::string &enc)
octave_value completion_append_char(const octave_value_list &args, int nargout)
bool gud_mode(bool flag)
Definition: input.h:126
std::string PS2() const
Definition: input.h:82
bool auto_repeat_debug_command(bool val)
Definition: input.h:160
void set_PS1(const std::string &s)
Definition: input.h:78
octave_value_list get_user_input(const octave_value_list &args, int nargout)
octave_value PS2(const octave_value_list &args, int nargout)
std::string dir_encoding(const std::string &dir)
bool auto_repeat_debug_command() const
Definition: input.h:155
std::string interactive_input(const std::string &s, bool &eof)
std::string mfile_encoding() const
Definition: input.h:137
octave_value PS1(const octave_value_list &args, int nargout)
std::string PS1() const
Definition: input.h:69
bool have_input_event_hooks() const
void set_mfile_encoding(const std::string &s)
Definition: input.h:146
octave_value mfile_encoding(const octave_value_list &args, int nargout)
bool gud_mode() const
Definition: input.h:124
std::string last_debugging_command(const std::string &s)
Definition: input.h:98
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave::sys::time Vlast_prompt_time
Definition: input.cc:83
bool Vdrawnow_requested
Definition: input.cc:90
bool octave_completion_matches_called
Definition: input.cc:86