GNU Octave  8.1.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-2023 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 
63  input_system (interpreter& interp);
64 
65  void initialize (bool line_editing);
66 
67  octave_value PS1 (const octave_value_list& args, int nargout);
68 
69  std::string PS1 (void) 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 (void) 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 (void) const
94  {
96  }
97 
98  std::string last_debugging_command (const std::string& s)
99  {
100  std::string val = m_last_debugging_command;
102  return val;
103  }
104 
106  completion_append_char (const octave_value_list& args, int nargout);
107 
108  char completion_append_char (void) const
109  {
111  }
112 
114  {
115  char val = m_completion_append_char;
117  return val;
118  }
119 
121 
122  octave_value gud_mode (const octave_value_list& args, int nargout);
123 
124  bool gud_mode (void) 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 (void) 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 
155  bool auto_repeat_debug_command (void) const
156  {
158  }
159 
161  {
162  bool old_val = m_auto_repeat_debug_command;
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 (void) 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 
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.
197 
198  // TRUE if we are running in the Emacs 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.
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.
214 
216 
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 
234  { }
235 
236  virtual ~base_reader (void) = default;
237 
238  virtual std::string get_input (const std::string& prompt, bool& eof) = 0;
239 
240  virtual std::string input_source (void) const { return s_in_src; }
241 
242  std::string octave_gets (const std::string& prompt, bool& eof);
243 
244  virtual bool input_from_terminal (void) const { return false; }
245 
246  virtual bool input_from_file (void) const { return false; }
247 
248  virtual bool input_from_eval_string (void) const { return false; }
249 
250 protected:
251 
253 
254 private:
255 
256  static const std::string s_in_src;
257 };
258 
260 {
261 public:
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  input_reader (const input_reader& ir) = default;
272 
273  input_reader& operator = (const input_reader& ir) = default;
274 
275  ~input_reader (void) = default;
276 
277  std::string get_input (const std::string& prompt, bool& eof)
278  {
279  return m_rep->get_input (prompt, eof);
280  }
281 
282  std::string input_source (void) const
283  {
284  return m_rep->input_source ();
285  }
286 
287  bool input_from_terminal (void) const
288  {
289  return m_rep->input_from_terminal ();
290  }
291 
292  bool input_from_file (void) const
293  {
294  return m_rep->input_from_file ();
295  }
296 
297  bool input_from_eval_string (void) const
298  {
299  return m_rep->input_from_eval_string ();
300  }
301 
302 private:
303 
304  std::shared_ptr<base_reader> m_rep;
305 };
306 
308 
309 #endif
OCTAVE_END_NAMESPACE(octave)
base_reader(const base_reader &x)
Definition: input.h:232
virtual std::string input_source(void) const
Definition: input.h:240
virtual bool input_from_eval_string(void) const
Definition: input.h:248
virtual bool input_from_terminal(void) const
Definition: input.h:244
std::string octave_gets(const std::string &prompt, bool &eof)
virtual ~base_reader(void)=default
base_reader(interpreter &interp)
Definition: input.h:228
virtual bool input_from_file(void) const
Definition: input.h:246
virtual std::string get_input(const std::string &prompt, bool &eof)=0
static const std::string s_in_src
Definition: input.h:256
interpreter & m_interpreter
Definition: input.h:252
bool input_from_terminal(void) const
Definition: input.h:287
input_reader(interpreter &interp, const std::string &str)
std::string get_input(const std::string &prompt, bool &eof)
Definition: input.h:277
input_reader(interpreter &interp, FILE *file)
std::string input_source(void) const
Definition: input.h:282
input_reader & operator=(const input_reader &ir)=default
input_reader(const input_reader &ir)=default
bool input_from_eval_string(void) const
Definition: input.h:297
input_reader(interpreter &interp)
bool input_from_file(void) const
Definition: input.h:292
input_reader(interpreter &interp, FILE *file, const std::string &enc)
std::shared_ptr< base_reader > m_rep
Definition: input.h:304
~input_reader(void)=default
bool remove_input_event_hook(const std::string &hook_fcn_id)
octave_value auto_repeat_debug_command(const octave_value_list &args, int nargout)
interpreter & m_interpreter
Definition: input.h:186
bool m_auto_repeat_debug_command
Definition: input.h:208
char completion_append_char(char c)
Definition: input.h:113
input_system(interpreter &interp)
Definition: input.cc:404
void initialize(bool line_editing)
Definition: input.cc:412
std::string PS1(const std::string &s)
Definition: input.h:71
std::string m_mfile_encoding
Definition: input.h:202
bool yes_or_no(const std::string &prompt)
std::string PS2(void) const
Definition: input.h:82
std::string m_PS1
Definition: input.h:189
std::string last_debugging_command(void) const
Definition: input.h:93
void set_gud_mode(bool flag)
Definition: input.h:133
bool have_input_event_hooks(void) const
std::string m_last_debugging_command
Definition: input.h:213
octave_value gud_mode(const octave_value_list &args, int nargout)
std::string PS2(const std::string &s)
Definition: input.h:84
void clear_input_event_hooks(void)
bool m_gud_mode
Definition: input.h:199
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 add_input_event_hook(const hook_function &hook_fcn)
void run_input_event_hooks(void)
std::string PS1(void) const
Definition: input.h:69
void set_dir_encoding(const std::string &dir, std::string &enc)
bool m_initialized
Definition: input.h:217
octave_value completion_append_char(const octave_value_list &args, int nargout)
bool gud_mode(bool flag)
Definition: input.h:126
bool gud_mode(void) const
Definition: input.h:124
bool auto_repeat_debug_command(bool val)
Definition: input.h:160
std::string m_PS2
Definition: input.h:192
void set_PS1(const std::string &s)
Definition: input.h:78
octave_value_list get_user_input(const octave_value_list &args, int nargout)
std::string gnu_readline(const std::string &s, bool &eof) const
octave_value PS2(const octave_value_list &args, int nargout)
std::string dir_encoding(const std::string &dir)
hook_function_list m_input_event_hook_functions
Definition: input.h:215
std::string interactive_input(const std::string &s, bool &eof)
octave_value PS1(const octave_value_list &args, int nargout)
std::unordered_map< std::string, std::string > m_dir_encoding
Definition: input.h:205
void set_mfile_encoding(const std::string &s)
Definition: input.h:146
char m_completion_append_char
Definition: input.h:196
char completion_append_char(void) const
Definition: input.h:108
octave_value mfile_encoding(const octave_value_list &args, int nargout)
bool auto_repeat_debug_command(void) const
Definition: input.h:155
std::string last_debugging_command(const std::string &s)
Definition: input.h:98
std::string mfile_encoding(void) const
Definition: input.h:137
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTINTERP_API octave::sys::time Vlast_prompt_time
Definition: input.cc:84
OCTINTERP_API bool Vdrawnow_requested
Definition: input.cc:91
bool octave_completion_matches_called
Definition: input.cc:87
F77_RET_T const F77_DBLE * x