1 | // **************************************************************************
|
---|
2 | /** @class Readline
|
---|
3 |
|
---|
4 | @brief C++ wrapper for GNU's readline library
|
---|
5 |
|
---|
6 | This class is meant as a C++ wrapper around GNU's readline library.
|
---|
7 | Note that because readline uses a global namespace only one instance
|
---|
8 | of this class can exist at a time. Instantiating a second object after
|
---|
9 | a first one was deleted might show unexpected results.
|
---|
10 |
|
---|
11 | When the object is instantiated readline's history is read from a file.
|
---|
12 | At destruction the history in memory is copied back to that file.
|
---|
13 | The history file will be truncated to fMaxLines.
|
---|
14 |
|
---|
15 | By overloading the Readline class the function used for auto-completion
|
---|
16 | can be overwritten.
|
---|
17 |
|
---|
18 | Simple example:
|
---|
19 |
|
---|
20 | \code
|
---|
21 |
|
---|
22 | Readline rl("MyProg"); // will read the history from "MyProg.his"
|
---|
23 | while (1)
|
---|
24 | {
|
---|
25 | string txt = rl.Prompt("prompt> ");
|
---|
26 | if (txt=="quit)
|
---|
27 | break;
|
---|
28 |
|
---|
29 | // ... do something ...
|
---|
30 |
|
---|
31 | rl.AddHistory(txt);
|
---|
32 | }
|
---|
33 |
|
---|
34 | // On destruction the history will be written to the file
|
---|
35 |
|
---|
36 | \endcode
|
---|
37 |
|
---|
38 | Simpler example (you need to implement the Process() function)
|
---|
39 |
|
---|
40 | \code
|
---|
41 |
|
---|
42 | Readline rl("MyProg"); // will read the history from "MyProg.his"
|
---|
43 | rl.Run("prompt> ");
|
---|
44 |
|
---|
45 | // On destruction the history will be written to the file
|
---|
46 |
|
---|
47 | \endcode
|
---|
48 |
|
---|
49 | @section References
|
---|
50 |
|
---|
51 | - <A HREF="http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html">GNU Readline</A>
|
---|
52 |
|
---|
53 | */
|
---|
54 | // **************************************************************************
|
---|
55 | #include "Readline.h"
|
---|
56 |
|
---|
57 | #include <fstream>
|
---|
58 | #include <iostream>
|
---|
59 |
|
---|
60 | #include <readline/readline.h>
|
---|
61 | #include <readline/history.h>
|
---|
62 |
|
---|
63 | #include "tools.h"
|
---|
64 |
|
---|
65 | using namespace std;
|
---|
66 |
|
---|
67 | Readline *Readline::This = 0;
|
---|
68 |
|
---|
69 | // --------------------------------------------------------------------------
|
---|
70 | //
|
---|
71 | //! Construct a Readline object. The constructor reads the history from a
|
---|
72 | //! history file. The filename is compiled by adding ".his" to the
|
---|
73 | //! supplied argument. The name oif the history file is stored in fName.
|
---|
74 | //!
|
---|
75 | //! Since readline has a global namespace, the creation of only one
|
---|
76 | //! Readline instance is allowed.
|
---|
77 | //!
|
---|
78 | //! The provided program name is supplied to readline by means of
|
---|
79 | //! rl_readline_name.
|
---|
80 | //!
|
---|
81 | //! Readlines default callback frunction for completions is redirected
|
---|
82 | //! to CompletionImp which in turn will call Completion, which can be
|
---|
83 | //! overwritten by the user.
|
---|
84 | //!
|
---|
85 | //! Bind some default key sequences like Page-up/-down for searching forward
|
---|
86 | //! and backward in history.
|
---|
87 | //!
|
---|
88 | //! @param prgname
|
---|
89 | //! The prefix of the history filename. Usually the program name, which
|
---|
90 | //! can be initialized by argv[0].
|
---|
91 | //
|
---|
92 | Readline::Readline(const char *prgname) :
|
---|
93 | fMaxLines(500), fLine(0), fCompletion(0)
|
---|
94 | {
|
---|
95 | if (This)
|
---|
96 | {
|
---|
97 | cout << "ERROR - Readline can only be instatiated once!" << endl;
|
---|
98 | exit(-1);
|
---|
99 | }
|
---|
100 |
|
---|
101 | This = this;
|
---|
102 |
|
---|
103 | // Alternative completion function
|
---|
104 | rl_attempted_completion_function = rl_ncurses_completion_function;
|
---|
105 |
|
---|
106 | // Program name
|
---|
107 | rl_readline_name = prgname;
|
---|
108 |
|
---|
109 | // Compile filename for history file
|
---|
110 | fName = string(prgname)+".his";
|
---|
111 |
|
---|
112 | // Read history file
|
---|
113 | if (read_history(fName.c_str()))
|
---|
114 | cout << "WARNING - Reading " << fName << ": " << strerror(errno) << endl;
|
---|
115 |
|
---|
116 | // Setup the readline callback which are needed to redirect
|
---|
117 | // the otuput properly to our ncurses panel
|
---|
118 | rl_getc_function = rl_ncurses_getc;
|
---|
119 | rl_startup_hook = rl_ncurses_startup;
|
---|
120 | rl_redisplay_function = rl_ncurses_redisplay;
|
---|
121 | rl_event_hook = rl_ncurses_event_hook;
|
---|
122 | rl_completion_display_matches_hook = rl_ncurses_completion_display;
|
---|
123 |
|
---|
124 | // Bind delete, page up, page down
|
---|
125 | rl_bind_keyseq("\e[3~", rl_named_function("delete-char"));
|
---|
126 | rl_bind_keyseq("\e[5~", rl_named_function("history-search-backward"));
|
---|
127 | rl_bind_keyseq("\e[6~", rl_named_function("history-search-forward"));
|
---|
128 | rl_bind_keyseq("\033[1;3F", rl_named_function("kill-line"));
|
---|
129 | rl_bind_keyseq("\033[1;5D", rl_named_function("backward-word"));
|
---|
130 | rl_bind_keyseq("\033[1;5C", rl_named_function("forward-word"));
|
---|
131 | rl_bind_key(25, rl_named_function("kill-whole-line"));
|
---|
132 |
|
---|
133 | //for (int i=0; i<10; i++) cout << (int)getchar() << endl;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // --------------------------------------------------------------------------
|
---|
137 | //
|
---|
138 | //! Writes the current history to the file with the name stored in fName.
|
---|
139 | //! In addition the written file is truncated to fMaxLines to keep the
|
---|
140 | //! file of a reasonable size. The number of lines fMaxLines can be set
|
---|
141 | //! by SetMaxLines before the destructor is called. Setting fMaxLines
|
---|
142 | //! to 0 or a negative value switches automatic truncation off.
|
---|
143 | //
|
---|
144 | Readline::~Readline()
|
---|
145 | {
|
---|
146 | // Write current history to file
|
---|
147 | if (write_history(fName.c_str()))
|
---|
148 | cout << "WARNING - Write " << fName.c_str() << ": " << strerror(errno) << endl;
|
---|
149 |
|
---|
150 | // Truncate file
|
---|
151 | if (fMaxLines>0 && history_truncate_file(fName.c_str(), fMaxLines))
|
---|
152 | cout << "WARNING - Truncate " << fName.c_str() << ": " << strerror(errno) << endl;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // --------------------------------------------------------------------------
|
---|
156 | //
|
---|
157 | //! This is a static helper to remove leading and trailing whitespaces.
|
---|
158 | //!
|
---|
159 | //! @param buf
|
---|
160 | //! a pointer to the char array from which the whitespaces should be
|
---|
161 | //! removed
|
---|
162 | //!
|
---|
163 | //! @returns
|
---|
164 | //! a std::string with the whitespaces removed from buf
|
---|
165 | //
|
---|
166 | string Readline::TrimSpaces(const char *buf)
|
---|
167 | {
|
---|
168 | const string str(buf);
|
---|
169 |
|
---|
170 | // Trim Both leading and trailing spaces
|
---|
171 | const size_t start = str.find_first_not_of(" "); // Find the first character position after excluding leading blank spaces
|
---|
172 | const size_t end = str.find_last_not_of(" "); // Find the first character position from reverse af
|
---|
173 |
|
---|
174 | // if all spaces or empty return an empty string
|
---|
175 | if (string::npos==start || string::npos==end)
|
---|
176 | return "";
|
---|
177 |
|
---|
178 | return str.substr(start, end-start+1);
|
---|
179 | }
|
---|
180 |
|
---|
181 | // --------------------------------------------------------------------------
|
---|
182 | //
|
---|
183 | //! Redirected from rl_getc_function, calls Getc
|
---|
184 | //
|
---|
185 | int Readline::rl_ncurses_getc(FILE *f)
|
---|
186 | {
|
---|
187 | return This->Getc(f);
|
---|
188 | }
|
---|
189 |
|
---|
190 | // --------------------------------------------------------------------------
|
---|
191 | //
|
---|
192 | //! Redirected from rl_startup_hook, calls Startup.
|
---|
193 | //! A function called just before readline prints the first prompt.
|
---|
194 | //
|
---|
195 | int Readline::rl_ncurses_startup()
|
---|
196 | {
|
---|
197 | This->Startup();
|
---|
198 | return 0; // What is this for?
|
---|
199 | }
|
---|
200 |
|
---|
201 | // --------------------------------------------------------------------------
|
---|
202 | //
|
---|
203 | //! Redirected from rl_redisplay_function, calls Redisplay.
|
---|
204 | //! Readline will call indirectly to update the display with the current
|
---|
205 | //! contents of the editing buffer.
|
---|
206 | //
|
---|
207 | void Readline::rl_ncurses_redisplay()
|
---|
208 | {
|
---|
209 | This->Redisplay();
|
---|
210 | }
|
---|
211 |
|
---|
212 | // --------------------------------------------------------------------------
|
---|
213 | //
|
---|
214 | //! Redirected from rl_event_hook, calls Update().
|
---|
215 | //! A function called periodically when readline is waiting for
|
---|
216 | //! terminal input.
|
---|
217 | //!
|
---|
218 | int Readline::rl_ncurses_event_hook()
|
---|
219 | {
|
---|
220 | This->EventHook();
|
---|
221 | return 0;
|
---|
222 | }
|
---|
223 |
|
---|
224 | // --------------------------------------------------------------------------
|
---|
225 | //
|
---|
226 | //! Redirected from rl_completion_display_matches_hook,
|
---|
227 | //! calls CompletionDisplayImp
|
---|
228 | //!
|
---|
229 | //! A function to be called when completing a word would normally display
|
---|
230 | //! the list of possible matches. This function is called in lieu of
|
---|
231 | //! Readline displaying the list. It takes three arguments:
|
---|
232 | //! (char **matches, int num_matches, int max_length) where matches is
|
---|
233 | //! the array of matching strings, num_matches is the number of strings
|
---|
234 | //! in that array, and max_length is the length of the longest string in
|
---|
235 | //! that array. Readline provides a convenience function,
|
---|
236 | //! rl_display_match_list, that takes care of doing the display to
|
---|
237 | //! Readline's output stream.
|
---|
238 | //
|
---|
239 | void Readline::rl_ncurses_completion_display(char **matches, int num, int max)
|
---|
240 | {
|
---|
241 | This->CompletionDisplay(matches, num, max);
|
---|
242 | }
|
---|
243 |
|
---|
244 | char **Readline::rl_ncurses_completion_function(const char *text, int start, int end)
|
---|
245 | {
|
---|
246 | return This->Completion(text, start, end);
|
---|
247 | }
|
---|
248 |
|
---|
249 | // --------------------------------------------------------------------------
|
---|
250 | //
|
---|
251 | //! Calls the default rl_getc function.
|
---|
252 | //
|
---|
253 | int Readline::Getc(FILE *f)
|
---|
254 | {
|
---|
255 | return rl_getc(f);
|
---|
256 | }
|
---|
257 |
|
---|
258 | // --------------------------------------------------------------------------
|
---|
259 | //
|
---|
260 | //! Default: Do nothing.
|
---|
261 | //
|
---|
262 | void Readline::Startup()
|
---|
263 | {
|
---|
264 | }
|
---|
265 |
|
---|
266 | // --------------------------------------------------------------------------
|
---|
267 | //
|
---|
268 | //! The default is to redisplay the prompt which is gotten from
|
---|
269 | //! GetUpdatePrompt(). If GetUpdatePrompt() returns an empty string the
|
---|
270 | //! prompt is kept untouched. This can be used to keep a prompt updated
|
---|
271 | //! with some information (e.g. time) just by overwriting GetUpdatePrompt()
|
---|
272 | //!
|
---|
273 | void Readline::EventHook()
|
---|
274 | {
|
---|
275 | const string p = GetUpdatePrompt();
|
---|
276 | if (p.empty())
|
---|
277 | return;
|
---|
278 |
|
---|
279 | UpdatePrompt("");
|
---|
280 | Redisplay();
|
---|
281 | UpdatePrompt(p);
|
---|
282 | Redisplay();
|
---|
283 | }
|
---|
284 |
|
---|
285 | // --------------------------------------------------------------------------
|
---|
286 | //
|
---|
287 | //! Called from Prompt and PromptEOF after readline has returned. It is
|
---|
288 | //! meant as the opposite of Startup (called after readline finsihes)
|
---|
289 | //! The default is to do nothing.
|
---|
290 | //!
|
---|
291 | //! @param buf
|
---|
292 | //! A pointer to the buffer returned by readline
|
---|
293 | //
|
---|
294 | void Readline::Shutdown(const char *)
|
---|
295 | {
|
---|
296 | }
|
---|
297 |
|
---|
298 | // --------------------------------------------------------------------------
|
---|
299 | //
|
---|
300 | //! Default: call rl_redisplay()
|
---|
301 | //
|
---|
302 | void Readline::Redisplay()
|
---|
303 | {
|
---|
304 | rl_redisplay();
|
---|
305 | }
|
---|
306 |
|
---|
307 | // --------------------------------------------------------------------------
|
---|
308 | //
|
---|
309 | //! Default: call rl_completion_display_matches()
|
---|
310 | //
|
---|
311 | void Readline::CompletionDisplay(char **matches, int num, int max)
|
---|
312 | {
|
---|
313 | rl_display_match_list(matches, num, max);
|
---|
314 | }
|
---|
315 |
|
---|
316 | // --------------------------------------------------------------------------
|
---|
317 | //
|
---|
318 | //! This is a static helper for the compilation of a completion-list.
|
---|
319 | //! It compares the two inputs (str and txt) to a maximum of the size of
|
---|
320 | //! txt. If they match, memory is allocated with malloc and a pointer to
|
---|
321 | //! the null-terminated version of str is returned.
|
---|
322 | //!
|
---|
323 | //! @param str
|
---|
324 | //! A reference to the string which is checked (e.g. "Makefile.am")
|
---|
325 | //!
|
---|
326 | //! @param txt
|
---|
327 | //! A reference to the part of the string the user has already typed,
|
---|
328 | //! e.g. "Makef"
|
---|
329 | //!
|
---|
330 | //! @returns
|
---|
331 | //! A pointer to memory allocated with malloc containing the string str
|
---|
332 | //
|
---|
333 | char *Readline::Compare(const string &str, const string &txt)
|
---|
334 | {
|
---|
335 | return str.substr(0, txt.length())==txt ? strndup(str.c_str(), str.length()) : 0;
|
---|
336 | }
|
---|
337 |
|
---|
338 | char **Readline::CompletionMatches(const char *text, char *(*func)(const char*, int))
|
---|
339 | {
|
---|
340 | return rl_completion_matches(text, func);
|
---|
341 | }
|
---|
342 |
|
---|
343 | // --------------------------------------------------------------------------
|
---|
344 | //
|
---|
345 | //! The given vector should be a reference to a vector of strings
|
---|
346 | //! containing all possible matches. The actual match-making is then
|
---|
347 | //! done in Complete(const char *, int)
|
---|
348 | //!
|
---|
349 | //! The pointer fCompletion is redirected to the vector for the run time
|
---|
350 | //! of the function, but restored afterwards. So by this you can set a
|
---|
351 | //! default completion list in case Complete is not called or Completion
|
---|
352 | //! not overloaded.
|
---|
353 | //!
|
---|
354 | //! @param v
|
---|
355 | //! reference to a vector of strings with all possible matches
|
---|
356 | //!
|
---|
357 | //! @param text
|
---|
358 | //! the text which should be matched (it is just propagated to
|
---|
359 | //! Readline::Completion)
|
---|
360 | //!
|
---|
361 | char **Readline::Complete(const vector<string> &v, const char *text)
|
---|
362 | {
|
---|
363 | const vector<string> *save = fCompletion;
|
---|
364 |
|
---|
365 | fCompletion = &v;
|
---|
366 | char **rc = rl_completion_matches(const_cast<char*>(text), CompleteImp);
|
---|
367 | fCompletion = save;
|
---|
368 |
|
---|
369 | return rc;
|
---|
370 | }
|
---|
371 |
|
---|
372 | // --------------------------------------------------------------------------
|
---|
373 | //
|
---|
374 | //! If fCompletion==0 the default is to call readline's
|
---|
375 | //! rl_filename_completion_function. Otherwise the contents of fCompletion
|
---|
376 | //! are returned. To change fCompletion either initialize it via
|
---|
377 | //! SetCompletion() (in this case you must ensure the life time of the
|
---|
378 | //! object) or call
|
---|
379 | //! Complete(const vector<string>&, const char*)
|
---|
380 | //! from
|
---|
381 | //! Completion(const char * int, int)
|
---|
382 | //!
|
---|
383 | //! This is the so called generator function, the readline manual says
|
---|
384 | //! about this function:
|
---|
385 | //!
|
---|
386 | //! The generator function is called repeatedly from
|
---|
387 | //! rl_completion_matches(), returning a string each time. The arguments
|
---|
388 | //! to the generator function are text and state. text is the partial word
|
---|
389 | //! to be completed. state is zero the first time the function is called,
|
---|
390 | //! allowing the generator to perform any necessary initialization, and a
|
---|
391 | //! positive non-zero integer for each subsequent call. The generator
|
---|
392 | //! function returns (char *)NULL to inform rl_completion_matches() that
|
---|
393 | //! there are no more possibilities left. Usually the generator function
|
---|
394 | //! computes the list of possible completions when state is zero, and
|
---|
395 | //! returns them one at a time on subsequent calls. Each string the
|
---|
396 | //! generator function returns as a match must be allocated with malloc();
|
---|
397 | //! Readline frees the strings when it has finished with them.
|
---|
398 | //
|
---|
399 | char *Readline::Complete(const char* text, int state)
|
---|
400 | {
|
---|
401 | if (fCompletion==0)
|
---|
402 | return rl_filename_completion_function(text, state);
|
---|
403 |
|
---|
404 | static vector<string>::const_iterator pos;
|
---|
405 | if (state==0)
|
---|
406 | pos = fCompletion->begin();
|
---|
407 |
|
---|
408 | while (pos!=fCompletion->end())
|
---|
409 | {
|
---|
410 | char *rc = Compare(*pos++, text);
|
---|
411 | if (rc)
|
---|
412 | return rc;
|
---|
413 | }
|
---|
414 |
|
---|
415 | return 0;
|
---|
416 | }
|
---|
417 |
|
---|
418 | // --------------------------------------------------------------------------
|
---|
419 | //
|
---|
420 | //! Calls Complete()
|
---|
421 | //
|
---|
422 | char *Readline::CompleteImp(const char* text, int state)
|
---|
423 | {
|
---|
424 | return This->Complete(text, state);
|
---|
425 | }
|
---|
426 |
|
---|
427 | // --------------------------------------------------------------------------
|
---|
428 | //
|
---|
429 | //! The readline manual says about this function:
|
---|
430 | //!
|
---|
431 | //! A pointer to an alternative function to create matches. The
|
---|
432 | //! function is called with text, start, and end. start and end are
|
---|
433 | //! indices in rl_line_buffer saying what the boundaries of text are.
|
---|
434 | //! If this function exists and returns NULL, or if this variable is
|
---|
435 | //! set to NULL, then rl_complete() will call the value of
|
---|
436 | //! rl_completion_entry_function to generate matches, otherwise the
|
---|
437 | //! array of strings returned will be used.
|
---|
438 | //!
|
---|
439 | //! This function is virtual and can be overwritten. It defaults to
|
---|
440 | //! a call to rl_completion_matches with CompleteImp as an argument
|
---|
441 | //! which defaults to filename completion, but can also be overwritten.
|
---|
442 | //!
|
---|
443 | //! It is suggested that you call
|
---|
444 | //! Complete(const vector<string>&, const char*)
|
---|
445 | //! from here.
|
---|
446 | //!
|
---|
447 | //! @param text
|
---|
448 | //! A pointer to a char array conatining the text which should be
|
---|
449 | //! completed. The text is null-terminated.
|
---|
450 | //!
|
---|
451 | //! @param start
|
---|
452 | //! The start index within readline's line buffer rl_line_buffer,
|
---|
453 | //! at which the text starts which presumably should be completed.
|
---|
454 | //!
|
---|
455 | //! @param end
|
---|
456 | //! The end index within readline's line buffer rl_line_buffer,
|
---|
457 | //! at which the text ends which presumably should be completed.
|
---|
458 | //!
|
---|
459 | //! @returns
|
---|
460 | //! An array of strings which were allocated with malloc and which
|
---|
461 | //! will be freed by readline with the possible matches.
|
---|
462 | //
|
---|
463 | char **Readline::Completion(const char *text, int /*start*/, int /*end*/)
|
---|
464 | {
|
---|
465 | // To do filename completion call
|
---|
466 | return rl_completion_matches((char*)text, CompleteImp);
|
---|
467 | }
|
---|
468 |
|
---|
469 | // --------------------------------------------------------------------------
|
---|
470 | //
|
---|
471 | //! Adds the given string to the history buffer of readline's history by
|
---|
472 | //! calling add_history. If skip is set to true str will be compared
|
---|
473 | //! with the last line added to the history and only be added if it difers.
|
---|
474 | //!
|
---|
475 | //! @param str
|
---|
476 | //! A reference to a string which should be added to readline's
|
---|
477 | //! history.
|
---|
478 | //!
|
---|
479 | //! @param skip
|
---|
480 | //! If skip is true do not add a new entry if it is identical to
|
---|
481 | //! the last one.
|
---|
482 | //
|
---|
483 | void Readline::AddToHistory(const string &str, bool skip)
|
---|
484 | {
|
---|
485 | if (skip && fLastLine==str)
|
---|
486 | return;
|
---|
487 |
|
---|
488 | if (str.empty())
|
---|
489 | return;
|
---|
490 |
|
---|
491 | add_history(str.c_str());
|
---|
492 | fLastLine = str;
|
---|
493 | }
|
---|
494 |
|
---|
495 | string Readline::GetLinePrompt() const
|
---|
496 | {
|
---|
497 | return Form("[%d]", fLine);
|
---|
498 | }
|
---|
499 |
|
---|
500 | // --------------------------------------------------------------------------
|
---|
501 | //
|
---|
502 | //! Calls rl_set_prompt. This can be used from readline's callback function
|
---|
503 | //! to change the prompt while a call to the readline function is in
|
---|
504 | //! progress.
|
---|
505 | //!
|
---|
506 | //! @param prompt
|
---|
507 | //! The new prompt to be shown
|
---|
508 | //
|
---|
509 | void Readline::UpdatePrompt(const string &prompt) const
|
---|
510 | {
|
---|
511 | rl_set_prompt(prompt.c_str());
|
---|
512 | }
|
---|
513 |
|
---|
514 | // --------------------------------------------------------------------------
|
---|
515 | //
|
---|
516 | //! This function is used to bind a key sequence via a call to
|
---|
517 | //! rl_bind_keyseq.
|
---|
518 | //!
|
---|
519 | //! Readline's manual says about this function:
|
---|
520 | //!
|
---|
521 | //! Bind the key sequence represented by the string keyseq to the
|
---|
522 | //! function function, beginning in the current keymap. This makes
|
---|
523 | //! new keymaps as necessary. The return value is non-zero if keyseq
|
---|
524 | //! is invalid.
|
---|
525 | //!
|
---|
526 | //! Key sequences are escaped sequences of characters read from an input
|
---|
527 | //! stream when a special key is pressed. This is necessary because
|
---|
528 | //! there are usually more keys and possible combinations than ascii codes.
|
---|
529 | //!
|
---|
530 | //! Possible key sequences are for example:
|
---|
531 | //! "\033OP" F1
|
---|
532 | //! "\033[1;5A" Ctrl+up
|
---|
533 | //! "\033[1;5B" Ctrl+down
|
---|
534 | //! "\033[1;3A" Alt+up
|
---|
535 | //! "\033[1;3B" Alt+down
|
---|
536 | //! "\033[5;3~" Alt+page up
|
---|
537 | //! "\033[6;3~" Alt+page down
|
---|
538 | //! "\033+" Alt++
|
---|
539 | //! "\033-" Alt+-
|
---|
540 | //! "\033\t" Alt+tab
|
---|
541 | //! "\033[1~" Alt+tab
|
---|
542 | //!
|
---|
543 | //! @param seq
|
---|
544 | //! The key sequence to be bound
|
---|
545 | //!
|
---|
546 | //! @param func
|
---|
547 | //! A function of type "int func(int, int)
|
---|
548 | //
|
---|
549 | void Readline::BindKeySequence(const char *seq, int (*func)(int, int))
|
---|
550 | {
|
---|
551 | rl_bind_keyseq(seq, func);
|
---|
552 | }
|
---|
553 |
|
---|
554 | // --------------------------------------------------------------------------
|
---|
555 | //
|
---|
556 | //! Calls rl_variable_dumper(1)
|
---|
557 | //!
|
---|
558 | //! Print the readline variable names and their current values
|
---|
559 | //! to rl_outstream. If readable is non-zero, the list is formatted
|
---|
560 | //! in such a way that it can be made part of an inputrc file and
|
---|
561 | //! re-read.
|
---|
562 | //!
|
---|
563 | //! rl_outstream can be redirected using SetStreamOut()
|
---|
564 | //!
|
---|
565 | //! @returns
|
---|
566 | //! always true
|
---|
567 | //
|
---|
568 | bool Readline::DumpVariables()
|
---|
569 | {
|
---|
570 | rl_variable_dumper(1);
|
---|
571 | return true;
|
---|
572 | }
|
---|
573 |
|
---|
574 | // --------------------------------------------------------------------------
|
---|
575 | //
|
---|
576 | //! Calls rl_function_dumper(1)
|
---|
577 | //!
|
---|
578 | //! Print the readline function names and the key sequences currently
|
---|
579 | //! bound to them to rl_outstream. If readable is non-zero, the list
|
---|
580 | //! is formatted in such a way that it can be made part of an inputrc
|
---|
581 | //! file and re-read.
|
---|
582 | //!
|
---|
583 | //! rl_outstream can be redirected using SetStreamOut()
|
---|
584 | //!
|
---|
585 | //! @returns
|
---|
586 | //! always true
|
---|
587 | //
|
---|
588 | bool Readline::DumpFunctions()
|
---|
589 | {
|
---|
590 | rl_function_dumper(1);
|
---|
591 | return true;
|
---|
592 | }
|
---|
593 |
|
---|
594 | // --------------------------------------------------------------------------
|
---|
595 | //
|
---|
596 | //! Calls rl_list_funmap_names()
|
---|
597 | //!
|
---|
598 | //! Print the names of all bindable Readline functions to rl_outstream.
|
---|
599 | //!
|
---|
600 | //! rl_outstream can be redirected using SetStreamOut()
|
---|
601 | //!
|
---|
602 | //! @returns
|
---|
603 | //! always true
|
---|
604 | //
|
---|
605 | bool Readline::DumpFunmap()
|
---|
606 | {
|
---|
607 | rl_list_funmap_names();
|
---|
608 | return true;
|
---|
609 | }
|
---|
610 |
|
---|
611 | // --------------------------------------------------------------------------
|
---|
612 | //
|
---|
613 | //! Sets rl_outstream (the stdio stream to which Readline performs output)
|
---|
614 | //! to the new stream.
|
---|
615 | //!
|
---|
616 | //! @param f
|
---|
617 | //! The new stdio stream to which readline should perform its output
|
---|
618 | //!
|
---|
619 | //! @return
|
---|
620 | //! The old stream to which readline was performing output
|
---|
621 | //
|
---|
622 | FILE *Readline::SetStreamOut(FILE *f)
|
---|
623 | {
|
---|
624 | FILE *rc = rl_outstream;
|
---|
625 | rl_outstream = f;
|
---|
626 | return rc;
|
---|
627 | }
|
---|
628 |
|
---|
629 | // --------------------------------------------------------------------------
|
---|
630 | //
|
---|
631 | //! Sets rl_instream (the stdio stream from which Readline reads input)
|
---|
632 | //! to the new stream.
|
---|
633 | //!
|
---|
634 | //! @param f
|
---|
635 | //! The new stdio stream from which readline should read its input
|
---|
636 | //!
|
---|
637 | //! @return
|
---|
638 | //! The old stream from which readline was reading it input
|
---|
639 | //
|
---|
640 | FILE *Readline::SetStreamIn(FILE *f)
|
---|
641 | {
|
---|
642 | FILE *rc = rl_instream;
|
---|
643 | rl_instream = f;
|
---|
644 | return rc;
|
---|
645 | }
|
---|
646 |
|
---|
647 | // --------------------------------------------------------------------------
|
---|
648 | //
|
---|
649 | //! return rl_display_prompt (the prompt which should currently be
|
---|
650 | //! displayed on the screen) while a readline command is in progress
|
---|
651 | //
|
---|
652 | string Readline::GetPrompt()
|
---|
653 | {
|
---|
654 | return rl_display_prompt;
|
---|
655 | }
|
---|
656 |
|
---|
657 | // --------------------------------------------------------------------------
|
---|
658 | //
|
---|
659 | //! return rl_line_buffer (the current input line which should currently be
|
---|
660 | //! displayed on the screen) while a readline command is in progress
|
---|
661 | //!
|
---|
662 | //! The length of the current line buffer (rl_end) is available as
|
---|
663 | //! GetLineBuffer().size()
|
---|
664 | //!
|
---|
665 | //! Note that after readline has returned the contents of rl_end might
|
---|
666 | //! not reflect the correct buffer length anymore, hence, the returned buffer
|
---|
667 | //! might be truncated.
|
---|
668 | //
|
---|
669 | string Readline::GetBuffer()
|
---|
670 | {
|
---|
671 | return string(rl_line_buffer, rl_end);
|
---|
672 | }
|
---|
673 |
|
---|
674 | // --------------------------------------------------------------------------
|
---|
675 | //
|
---|
676 | //! return rl_point (the current cursor position within the line buffer)
|
---|
677 | //
|
---|
678 | int Readline::GetCursor()
|
---|
679 | {
|
---|
680 | return rl_point;
|
---|
681 | }
|
---|
682 |
|
---|
683 | // --------------------------------------------------------------------------
|
---|
684 | //
|
---|
685 | //! return strlen(rl_display_prompt) + rl_point
|
---|
686 | //
|
---|
687 | int Readline::GetAbsCursor()
|
---|
688 | {
|
---|
689 | return strlen(rl_display_prompt) + rl_point;
|
---|
690 | }
|
---|
691 |
|
---|
692 | // --------------------------------------------------------------------------
|
---|
693 | //
|
---|
694 | //! return rl_end (the current total length of the line buffer)
|
---|
695 | //! Note that after readline has returned the contents of rl_end might
|
---|
696 | //! not reflect the correct buffer length anymore.
|
---|
697 | //
|
---|
698 | int Readline::GetBufferLength()
|
---|
699 | {
|
---|
700 | return rl_end;
|
---|
701 | }
|
---|
702 |
|
---|
703 | // --------------------------------------------------------------------------
|
---|
704 | //
|
---|
705 | //! return the length of the prompt plus the length of the line buffer
|
---|
706 | //
|
---|
707 | int Readline::GetLineLength()
|
---|
708 | {
|
---|
709 | return strlen(rl_display_prompt) + rl_end;
|
---|
710 | }
|
---|
711 |
|
---|
712 | // --------------------------------------------------------------------------
|
---|
713 | //
|
---|
714 | //! Calls: Function: void rl_resize_terminal()
|
---|
715 | //! Update Readline's internal screen size by reading values from the kernel.
|
---|
716 | //
|
---|
717 | void Readline::Resize()
|
---|
718 | {
|
---|
719 | rl_resize_terminal();
|
---|
720 | }
|
---|
721 |
|
---|
722 | // --------------------------------------------------------------------------
|
---|
723 | //
|
---|
724 | //! Calls: Function: void rl_set_screen_size (int rows, int cols)
|
---|
725 | //! Set Readline's idea of the terminal size to rows rows and cols columns.
|
---|
726 | //!
|
---|
727 | //! @param width
|
---|
728 | //! Number of columns
|
---|
729 | //!
|
---|
730 | //! @param height
|
---|
731 | //! Number of rows
|
---|
732 | //
|
---|
733 | void Readline::Resize(int width, int height)
|
---|
734 | {
|
---|
735 | rl_set_screen_size(height, width);
|
---|
736 | }
|
---|
737 |
|
---|
738 | // --------------------------------------------------------------------------
|
---|
739 | //
|
---|
740 | //! Get the number of cols readline assumes the screen size to be
|
---|
741 | //
|
---|
742 | int Readline::GetCols() const
|
---|
743 | {
|
---|
744 | int rows, cols;
|
---|
745 | rl_get_screen_size(&rows, &cols);
|
---|
746 | return cols;
|
---|
747 | }
|
---|
748 |
|
---|
749 | // --------------------------------------------------------------------------
|
---|
750 | //
|
---|
751 | //! Get the number of rows readline assumes the screen size to be
|
---|
752 | //
|
---|
753 | int Readline::GetRows() const
|
---|
754 | {
|
---|
755 | int rows, cols;
|
---|
756 | rl_get_screen_size(&rows, &cols);
|
---|
757 | return rows;
|
---|
758 | }
|
---|
759 |
|
---|
760 | // --------------------------------------------------------------------------
|
---|
761 | //
|
---|
762 | //! Return a list of pointer to the history contents
|
---|
763 | //
|
---|
764 | vector<const char*> Readline::GetHistory() const
|
---|
765 | {
|
---|
766 | HIST_ENTRY **next = history_list();
|
---|
767 |
|
---|
768 | vector<const char*> v;
|
---|
769 |
|
---|
770 | for (; *next; next++)
|
---|
771 | v.push_back((*next)->line);
|
---|
772 |
|
---|
773 | return v;
|
---|
774 | }
|
---|
775 |
|
---|
776 | // --------------------------------------------------------------------------
|
---|
777 | //
|
---|
778 | //! Clear readline history (calls clear_history())
|
---|
779 | //!
|
---|
780 | //! @returns
|
---|
781 | //! always true
|
---|
782 | //
|
---|
783 | bool Readline::ClearHistory()
|
---|
784 | {
|
---|
785 | clear_history();
|
---|
786 | return true;
|
---|
787 | }
|
---|
788 |
|
---|
789 | // --------------------------------------------------------------------------
|
---|
790 | //
|
---|
791 | //! Displays the current history on rl_outstream
|
---|
792 | //!
|
---|
793 | //! rl_outstream can be redirected using SetStreamOut()
|
---|
794 | //!
|
---|
795 | //! @returns
|
---|
796 | //! always true
|
---|
797 | //
|
---|
798 | bool Readline::DumpHistory()
|
---|
799 | {
|
---|
800 | HIST_ENTRY **next = history_list();
|
---|
801 |
|
---|
802 | for (; *next; next++)
|
---|
803 | fprintf(rl_outstream, "%s\n", (*next)->line);
|
---|
804 |
|
---|
805 | return true;
|
---|
806 | }
|
---|
807 |
|
---|
808 | // --------------------------------------------------------------------------
|
---|
809 | //
|
---|
810 | //! Print the available commands. This is intended for being overwritten
|
---|
811 | //! by deriving classes.
|
---|
812 | //!
|
---|
813 | //! rl_outstream can be redirected using SetStreamOut()
|
---|
814 | //!
|
---|
815 | //! @returns
|
---|
816 | //! always true
|
---|
817 | //
|
---|
818 | //
|
---|
819 | bool Readline::PrintCommands()
|
---|
820 | {
|
---|
821 | fprintf(rl_outstream, "\n");
|
---|
822 | fprintf(rl_outstream, " Commands:\n");
|
---|
823 | fprintf(rl_outstream, " No application specific commands defined.\n");
|
---|
824 | fprintf(rl_outstream, "\n");
|
---|
825 | return true;
|
---|
826 | }
|
---|
827 |
|
---|
828 | // --------------------------------------------------------------------------
|
---|
829 | //
|
---|
830 | //! Print a general help message. This is intended for being overwritten
|
---|
831 | //! by deriving classes.
|
---|
832 | //!
|
---|
833 | //!
|
---|
834 | //! rl_outstream can be redirected using SetStreamOut()
|
---|
835 | //!
|
---|
836 | //! @returns
|
---|
837 | //! always true
|
---|
838 | //
|
---|
839 | //
|
---|
840 | bool Readline::PrintGeneralHelp()
|
---|
841 | {
|
---|
842 | fprintf(rl_outstream, "\n");
|
---|
843 | fprintf(rl_outstream, " General help:\n");
|
---|
844 | fprintf(rl_outstream, " h,help Print this help message\n");
|
---|
845 | fprintf(rl_outstream, " clear Clear history buffer\n");
|
---|
846 | fprintf(rl_outstream, " lh,history Dump the history buffer to the screen\n");
|
---|
847 | fprintf(rl_outstream, " v,variables Dump readline variables\n");
|
---|
848 | fprintf(rl_outstream, " f,functions Dump readline functions\n");
|
---|
849 | fprintf(rl_outstream, " m,funmap Dump readline funmap\n");
|
---|
850 | fprintf(rl_outstream, " c,commands Dump available commands\n");
|
---|
851 | fprintf(rl_outstream, " k,keylist Dump key bindings\n");
|
---|
852 | fprintf(rl_outstream, " .q,quit Quit\n");
|
---|
853 | fprintf(rl_outstream, "\n");
|
---|
854 | fprintf(rl_outstream, " The command history is automatically loaded and saves to\n");
|
---|
855 | fprintf(rl_outstream, " and from %s.\n", GetName().c_str());
|
---|
856 | fprintf(rl_outstream, "\n");
|
---|
857 | return true;
|
---|
858 | }
|
---|
859 |
|
---|
860 | // --------------------------------------------------------------------------
|
---|
861 | //
|
---|
862 | //! Print a help text about key bindings. This is intended for being
|
---|
863 | //! overwritten by deriving classes.
|
---|
864 | //!
|
---|
865 | //!
|
---|
866 | //! rl_outstream can be redirected using SetStreamOut()
|
---|
867 | //!
|
---|
868 | //! @returns
|
---|
869 | //! always true
|
---|
870 | //
|
---|
871 | //
|
---|
872 | bool Readline::PrintKeyBindings()
|
---|
873 | {
|
---|
874 | fprintf(rl_outstream, "\n");
|
---|
875 | fprintf(rl_outstream, " Key bindings:\n");
|
---|
876 | fprintf(rl_outstream, " Page-up Search backward in history\n");
|
---|
877 | fprintf(rl_outstream, " Page-dn Search forward in history\n");
|
---|
878 | fprintf(rl_outstream, " Ctrl-left One word backward\n");
|
---|
879 | fprintf(rl_outstream, " Ctrl-right One word forward\n");
|
---|
880 | fprintf(rl_outstream, " Ctrl-y Delete line\n");
|
---|
881 | fprintf(rl_outstream, " Alt-end/Ctrl-k Delete until the end of the line\n");
|
---|
882 | fprintf(rl_outstream, " F1 Toggle visibility of upper panel\n");
|
---|
883 | fprintf(rl_outstream, "\n");
|
---|
884 | fprintf(rl_outstream, " Default key-bindings are identical with your bash.\n");
|
---|
885 | fprintf(rl_outstream, "\n");
|
---|
886 | return true;
|
---|
887 | }
|
---|
888 |
|
---|
889 | // --------------------------------------------------------------------------
|
---|
890 | //
|
---|
891 | //!
|
---|
892 | //
|
---|
893 | bool Readline::Process(const string &str)
|
---|
894 | {
|
---|
895 | // ----------- Readline static -------------
|
---|
896 |
|
---|
897 | if (str=="clear")
|
---|
898 | return ClearHistory();
|
---|
899 |
|
---|
900 | if (str=="lh" || str=="history")
|
---|
901 | return DumpHistory();
|
---|
902 |
|
---|
903 | if (str=="v" || str=="variables")
|
---|
904 | return DumpVariables();
|
---|
905 |
|
---|
906 | if (str=="f" || str=="functions")
|
---|
907 | return DumpFunctions();
|
---|
908 |
|
---|
909 | if (str=="m" || str=="funmap")
|
---|
910 | return DumpFunmap();
|
---|
911 |
|
---|
912 | // ---------- Readline virtual -------------
|
---|
913 |
|
---|
914 | if (str=="h" || str=="help")
|
---|
915 | return PrintGeneralHelp();
|
---|
916 |
|
---|
917 | if (str=="c" || str=="commands")
|
---|
918 | return PrintCommands();
|
---|
919 |
|
---|
920 | if (str=="k" || str=="keylist")
|
---|
921 | return PrintKeyBindings();
|
---|
922 |
|
---|
923 | return false;
|
---|
924 | }
|
---|
925 |
|
---|
926 | // --------------------------------------------------------------------------
|
---|
927 | //
|
---|
928 | //! This function is a wrapper around the call to readline. It encapsultes
|
---|
929 | //! the return buffer into a std::string and deletes the memory allocated
|
---|
930 | //! by readline. Furthermore, it removes leading and trailing whitespaces
|
---|
931 | //! before return the result. The result is returned in the given
|
---|
932 | //! argument containing the prompt. Before the function returns Shutdown()
|
---|
933 | //! is called (as opposed to Startup when readline starts)
|
---|
934 | //!
|
---|
935 | //! @param str
|
---|
936 | //! The prompt which is to be shown by the readline libarary. it is
|
---|
937 | //! directly given to the call to readline. The result of the
|
---|
938 | //! readline call is returned in this string.
|
---|
939 | //!
|
---|
940 | //! @returns
|
---|
941 | //! true if the call succeeded as usual, false if EOF was detected
|
---|
942 | //! by the readline call.
|
---|
943 | //
|
---|
944 | bool Readline::PromptEOF(string &str)
|
---|
945 | {
|
---|
946 | char *buf = readline(str.c_str());
|
---|
947 | Shutdown(buf);
|
---|
948 |
|
---|
949 | // Happens when EOF is encountered
|
---|
950 | if (!buf)
|
---|
951 | return false;
|
---|
952 |
|
---|
953 | str = TrimSpaces(buf);
|
---|
954 |
|
---|
955 | free(buf);
|
---|
956 |
|
---|
957 | return true;
|
---|
958 | }
|
---|
959 |
|
---|
960 | // --------------------------------------------------------------------------
|
---|
961 | //
|
---|
962 | //! This function is a wrapper around the call to readline. It encapsultes
|
---|
963 | //! the return buffer into a std::string and deletes the memory allocated
|
---|
964 | //! by readline. Furthermore, it removes leading and trailing whitespaces
|
---|
965 | //! before return the result. Before the function returns Shutdown() is
|
---|
966 | //! called (as opposed to Startup when readline starts)
|
---|
967 | //!
|
---|
968 | //! @param prompt
|
---|
969 | //! The prompt which is to be shown by the readline libarary. it is
|
---|
970 | //! directly given to the call to readline.
|
---|
971 | //!
|
---|
972 | //! @returns
|
---|
973 | //! The result of the readline call
|
---|
974 | //
|
---|
975 | string Readline::Prompt(const string &prompt)
|
---|
976 | {
|
---|
977 | char *buf = readline(prompt.c_str());
|
---|
978 | Shutdown(buf ? buf : "");
|
---|
979 |
|
---|
980 | const string str = buf ? TrimSpaces(buf) : ".q";
|
---|
981 |
|
---|
982 | free(buf);
|
---|
983 |
|
---|
984 | return str;
|
---|
985 | }
|
---|
986 |
|
---|
987 | // --------------------------------------------------------------------------
|
---|
988 | //
|
---|
989 | //! This implements a loop over readline calls. A prompt to issue can be
|
---|
990 | //! given. If it is NULL, the prompt is retrieved from GetUpdatePrompt().
|
---|
991 | //! It is updated regularly by means of calls to GetUpdatePrompt() from
|
---|
992 | //! EventHook(). If Prompt() returns with "quit" or ".q" the loop is
|
---|
993 | //! exited. If ".qqq" is entered exit(-1) is called. In case of ".qqqqqq"
|
---|
994 | //! abort(). Both ways to exit the program are not recommended. Empty
|
---|
995 | //! inputs are ignored. After that Process() with the returned string
|
---|
996 | //! is called. If Process returns true the input is not counted and not
|
---|
997 | //! added to the history, otherwise the line counter is increased
|
---|
998 | //! and the input is added to the history.
|
---|
999 | //!
|
---|
1000 | //! @param prompt
|
---|
1001 | //! The prompt to be issued or NULL if GetUPdatePrompt should be used
|
---|
1002 | //! instead.
|
---|
1003 | //!
|
---|
1004 | void Readline::Run(const char *prompt)
|
---|
1005 | {
|
---|
1006 | fLine = 0;
|
---|
1007 | while (1)
|
---|
1008 | {
|
---|
1009 | // Before we start we have to make sure that the
|
---|
1010 | // screen looks like and is ordered like expected.
|
---|
1011 | const string str = Prompt(prompt?prompt:GetUpdatePrompt());
|
---|
1012 | if (str.empty())
|
---|
1013 | continue;
|
---|
1014 |
|
---|
1015 | if (str=="quit" || str==".q")
|
---|
1016 | break;
|
---|
1017 |
|
---|
1018 | if (str==".qqq")
|
---|
1019 | exit(-1);
|
---|
1020 |
|
---|
1021 | if (str==".qqqqqq")
|
---|
1022 | abort();
|
---|
1023 |
|
---|
1024 | if (Process(str))
|
---|
1025 | continue;
|
---|
1026 |
|
---|
1027 | fLine++;
|
---|
1028 |
|
---|
1029 | AddToHistory(str);
|
---|
1030 | }
|
---|
1031 | }
|
---|