| 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 wraps the given readline function such that the output can be
 | 
|---|
| 158 | //! redirected from thr rl_outstream to the given C++ ostream.
 | 
|---|
| 159 | //!
 | 
|---|
| 160 | //! @param out
 | 
|---|
| 161 | //!    The stream to which the output should be redirected.
 | 
|---|
| 162 | //!
 | 
|---|
| 163 | //! @param function
 | 
|---|
| 164 | //!    Takes a function of type bool(*)() as argument
 | 
|---|
| 165 | //!
 | 
|---|
| 166 | //! @returns
 | 
|---|
| 167 | //!    The return value of the function
 | 
|---|
| 168 | //
 | 
|---|
| 169 | bool Readline::RedirectionWrapper(ostream &out, bool (*function)())
 | 
|---|
| 170 | {
 | 
|---|
| 171 |     FILE *save = SetStreamOut(tmpfile());
 | 
|---|
| 172 |     const bool rc = function();
 | 
|---|
| 173 |     FILE *file = SetStreamOut(save);
 | 
|---|
| 174 | 
 | 
|---|
| 175 |     const bool empty = ftell(file)==0;
 | 
|---|
| 176 | 
 | 
|---|
| 177 |     rewind(file);
 | 
|---|
| 178 | 
 | 
|---|
| 179 |     if (empty)
 | 
|---|
| 180 |     {
 | 
|---|
| 181 |         out << " <empty>" << endl;
 | 
|---|
| 182 |         fclose(file);
 | 
|---|
| 183 |         return rc;
 | 
|---|
| 184 |     }
 | 
|---|
| 185 | 
 | 
|---|
| 186 |     while (1)
 | 
|---|
| 187 |     {
 | 
|---|
| 188 |         const int c = getc(file);
 | 
|---|
| 189 |         if (feof(file))
 | 
|---|
| 190 |             break;
 | 
|---|
| 191 |         out << (char)c;
 | 
|---|
| 192 |     }
 | 
|---|
| 193 |     out << endl;
 | 
|---|
| 194 | 
 | 
|---|
| 195 |     fclose(file);
 | 
|---|
| 196 | 
 | 
|---|
| 197 |     return rc;
 | 
|---|
| 198 | }
 | 
|---|
| 199 | 
 | 
|---|
| 200 | // --------------------------------------------------------------------------
 | 
|---|
| 201 | //
 | 
|---|
| 202 | //! Redirected from rl_getc_function, calls Getc
 | 
|---|
| 203 | //
 | 
|---|
| 204 | int Readline::rl_ncurses_getc(FILE *f)
 | 
|---|
| 205 | {
 | 
|---|
| 206 |     return This->Getc(f);
 | 
|---|
| 207 | }
 | 
|---|
| 208 | 
 | 
|---|
| 209 | // --------------------------------------------------------------------------
 | 
|---|
| 210 | //
 | 
|---|
| 211 | //! Redirected from rl_startup_hook, calls Startup.
 | 
|---|
| 212 | //! A function called just before readline prints the first prompt.
 | 
|---|
| 213 | //
 | 
|---|
| 214 | int Readline::rl_ncurses_startup()
 | 
|---|
| 215 | {
 | 
|---|
| 216 |     This->Startup();
 | 
|---|
| 217 |     return 0; // What is this for?
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|
| 220 | // --------------------------------------------------------------------------
 | 
|---|
| 221 | //
 | 
|---|
| 222 | //! Redirected from rl_redisplay_function, calls Redisplay.
 | 
|---|
| 223 | //! Readline will call indirectly to update the display with the current
 | 
|---|
| 224 | //! contents of the editing buffer. 
 | 
|---|
| 225 | //
 | 
|---|
| 226 | void Readline::rl_ncurses_redisplay()
 | 
|---|
| 227 | {
 | 
|---|
| 228 |     This->Redisplay();
 | 
|---|
| 229 | }
 | 
|---|
| 230 | 
 | 
|---|
| 231 | // --------------------------------------------------------------------------
 | 
|---|
| 232 | //
 | 
|---|
| 233 | //! Redirected from rl_event_hook, calls Update().
 | 
|---|
| 234 | //! A function called periodically when readline is waiting for
 | 
|---|
| 235 | //! terminal input.
 | 
|---|
| 236 | //!
 | 
|---|
| 237 | int Readline::rl_ncurses_event_hook()
 | 
|---|
| 238 | {
 | 
|---|
| 239 |     This->EventHook();
 | 
|---|
| 240 |     return 0;
 | 
|---|
| 241 | }
 | 
|---|
| 242 | 
 | 
|---|
| 243 | // --------------------------------------------------------------------------
 | 
|---|
| 244 | //
 | 
|---|
| 245 | //! Redirected from rl_completion_display_matches_hook,
 | 
|---|
| 246 | //! calls CompletionDisplayImp
 | 
|---|
| 247 | //!
 | 
|---|
| 248 | //! A function to be called when completing a word would normally display
 | 
|---|
| 249 | //! the list of possible matches. This function is called in lieu of
 | 
|---|
| 250 | //! Readline displaying the list. It takes three arguments:
 | 
|---|
| 251 | //! (char **matches, int num_matches, int max_length) where matches is
 | 
|---|
| 252 | //! the array of matching strings, num_matches is the number of strings
 | 
|---|
| 253 | //! in that array, and max_length is the length of the longest string in
 | 
|---|
| 254 | //! that array. Readline provides a convenience function,
 | 
|---|
| 255 | //! rl_display_match_list, that takes care of doing the display to
 | 
|---|
| 256 | //! Readline's output stream. 
 | 
|---|
| 257 | //
 | 
|---|
| 258 | void Readline::rl_ncurses_completion_display(char **matches, int num, int max)
 | 
|---|
| 259 | {
 | 
|---|
| 260 |     This->CompletionDisplay(matches, num, max);
 | 
|---|
| 261 | }
 | 
|---|
| 262 | 
 | 
|---|
| 263 | char **Readline::rl_ncurses_completion_function(const char *text, int start, int end)
 | 
|---|
| 264 | {
 | 
|---|
| 265 |     return This->Completion(text, start, end);
 | 
|---|
| 266 | }
 | 
|---|
| 267 | 
 | 
|---|
| 268 | // --------------------------------------------------------------------------
 | 
|---|
| 269 | //
 | 
|---|
| 270 | //! Calls the default rl_getc function.
 | 
|---|
| 271 | //
 | 
|---|
| 272 | int  Readline::Getc(FILE *f)
 | 
|---|
| 273 | {
 | 
|---|
| 274 |     return rl_getc(f);
 | 
|---|
| 275 | }
 | 
|---|
| 276 | 
 | 
|---|
| 277 | // --------------------------------------------------------------------------
 | 
|---|
| 278 | //
 | 
|---|
| 279 | //! Default: Do nothing.
 | 
|---|
| 280 | //
 | 
|---|
| 281 | void Readline::Startup()
 | 
|---|
| 282 | {
 | 
|---|
| 283 | }
 | 
|---|
| 284 | 
 | 
|---|
| 285 | // --------------------------------------------------------------------------
 | 
|---|
| 286 | //
 | 
|---|
| 287 | //! The default is to redisplay the prompt which is gotten from
 | 
|---|
| 288 | //! GetUpdatePrompt(). If GetUpdatePrompt() returns an empty string the
 | 
|---|
| 289 | //! prompt is kept untouched. This can be used to keep a prompt updated
 | 
|---|
| 290 | //! with some information (e.g. time) just by overwriting GetUpdatePrompt()
 | 
|---|
| 291 | //!
 | 
|---|
| 292 | void Readline::EventHook()
 | 
|---|
| 293 | {
 | 
|---|
| 294 |     const string p = GetUpdatePrompt();
 | 
|---|
| 295 |     if (p.empty())
 | 
|---|
| 296 |         return;
 | 
|---|
| 297 | 
 | 
|---|
| 298 |     UpdatePrompt("");
 | 
|---|
| 299 |     Redisplay();
 | 
|---|
| 300 |     UpdatePrompt(p);
 | 
|---|
| 301 |     Redisplay();
 | 
|---|
| 302 | }
 | 
|---|
| 303 | 
 | 
|---|
| 304 | // --------------------------------------------------------------------------
 | 
|---|
| 305 | //
 | 
|---|
| 306 | //! Called from Prompt and PromptEOF after readline has returned. It is
 | 
|---|
| 307 | //! meant as the opposite of Startup (called after readline finsihes)
 | 
|---|
| 308 | //! The default is to do nothing.
 | 
|---|
| 309 | //!
 | 
|---|
| 310 | //! @param buf
 | 
|---|
| 311 | //!    A pointer to the buffer returned by readline
 | 
|---|
| 312 | //
 | 
|---|
| 313 | void Readline::Shutdown(const char *)
 | 
|---|
| 314 | {
 | 
|---|
| 315 | }
 | 
|---|
| 316 | 
 | 
|---|
| 317 | // --------------------------------------------------------------------------
 | 
|---|
| 318 | //
 | 
|---|
| 319 | //! Default: call rl_redisplay()
 | 
|---|
| 320 | //
 | 
|---|
| 321 | void Readline::Redisplay()
 | 
|---|
| 322 | {
 | 
|---|
| 323 |     rl_redisplay();
 | 
|---|
| 324 | }
 | 
|---|
| 325 | 
 | 
|---|
| 326 | // --------------------------------------------------------------------------
 | 
|---|
| 327 | //
 | 
|---|
| 328 | //! Default: call rl_completion_display_matches()
 | 
|---|
| 329 | //
 | 
|---|
| 330 | void Readline::CompletionDisplay(char **matches, int num, int max)
 | 
|---|
| 331 | {
 | 
|---|
| 332 |     rl_display_match_list(matches, num, max);
 | 
|---|
| 333 | }
 | 
|---|
| 334 | 
 | 
|---|
| 335 | // --------------------------------------------------------------------------
 | 
|---|
| 336 | //
 | 
|---|
| 337 | //! This is a static helper for the compilation of a completion-list.
 | 
|---|
| 338 | //! It compares the two inputs (str and txt) to a maximum of the size of
 | 
|---|
| 339 | //! txt. If they match, memory is allocated with malloc and a pointer to
 | 
|---|
| 340 | //! the null-terminated version of str is returned.
 | 
|---|
| 341 | //!
 | 
|---|
| 342 | //! @param str
 | 
|---|
| 343 | //!    A reference to the string which is checked (e.g. "Makefile.am")
 | 
|---|
| 344 | //!
 | 
|---|
| 345 | //! @param txt
 | 
|---|
| 346 | //!    A reference to the part of the string the user has already typed,
 | 
|---|
| 347 | //!    e.g. "Makef"
 | 
|---|
| 348 | //!
 | 
|---|
| 349 | //! @returns
 | 
|---|
| 350 | //!    A pointer to memory allocated with malloc containing the string str
 | 
|---|
| 351 | //
 | 
|---|
| 352 | char *Readline::Compare(const string &str, const string &txt)
 | 
|---|
| 353 | {
 | 
|---|
| 354 |     /*return strncmp(str.c_str(), txt.c_str(), txt.length())==0 ? */
 | 
|---|
| 355 |     return strncasecmp(str.c_str(), txt.c_str(), txt.length())==0 ?
 | 
|---|
| 356 |         strndup(str.c_str(), str.length()) : 0;
 | 
|---|
| 357 | }
 | 
|---|
| 358 | 
 | 
|---|
| 359 | char **Readline::CompletionMatches(const char *text, char *(*func)(const char*, int))
 | 
|---|
| 360 | {
 | 
|---|
| 361 |     return rl_completion_matches(text, func);
 | 
|---|
| 362 | }
 | 
|---|
| 363 | 
 | 
|---|
| 364 | // --------------------------------------------------------------------------
 | 
|---|
| 365 | //
 | 
|---|
| 366 | //! The given vector should be a reference to a vector of strings
 | 
|---|
| 367 | //! containing all possible matches. The actual match-making is then
 | 
|---|
| 368 | //! done in Complete(const char *, int)
 | 
|---|
| 369 | //!
 | 
|---|
| 370 | //! The pointer fCompletion is redirected to the vector for the run time
 | 
|---|
| 371 | //! of the function, but restored afterwards. So by this you can set a
 | 
|---|
| 372 | //! default completion list in case Complete is not called or Completion
 | 
|---|
| 373 | //! not overloaded.
 | 
|---|
| 374 | //!
 | 
|---|
| 375 | //! @param v
 | 
|---|
| 376 | //!    reference to a vector of strings with all possible matches
 | 
|---|
| 377 | //!
 | 
|---|
| 378 | //! @param text
 | 
|---|
| 379 | //!    the text which should be matched (it is just propagated to
 | 
|---|
| 380 | //!    Readline::Completion)
 | 
|---|
| 381 | //!
 | 
|---|
| 382 | char **Readline::Complete(const vector<string> &v, const char *text)
 | 
|---|
| 383 | {
 | 
|---|
| 384 |     const vector<string> *save = fCompletion;
 | 
|---|
| 385 | 
 | 
|---|
| 386 |     fCompletion = &v;
 | 
|---|
| 387 |     char **rc = rl_completion_matches(const_cast<char*>(text), CompleteImp);
 | 
|---|
| 388 |     fCompletion = save;
 | 
|---|
| 389 | 
 | 
|---|
| 390 |     return rc;
 | 
|---|
| 391 | }
 | 
|---|
| 392 | 
 | 
|---|
| 393 | // --------------------------------------------------------------------------
 | 
|---|
| 394 | //
 | 
|---|
| 395 | //! If fCompletion==0 the default is to call readline's
 | 
|---|
| 396 | //! rl_filename_completion_function. Otherwise the contents of fCompletion
 | 
|---|
| 397 | //! are returned. To change fCompletion either initialize it via
 | 
|---|
| 398 | //! SetCompletion() (in this case you must ensure the life time of the
 | 
|---|
| 399 | //! object) or call
 | 
|---|
| 400 | //!    Complete(const vector<string>&, const char*)
 | 
|---|
| 401 | //! from
 | 
|---|
| 402 | //!    Completion(const char * int, int)
 | 
|---|
| 403 | //!
 | 
|---|
| 404 | //! This is the so called generator function, the readline manual says
 | 
|---|
| 405 | //! about this function:
 | 
|---|
| 406 | //!
 | 
|---|
| 407 | //!   The generator function is called repeatedly from
 | 
|---|
| 408 | //!   rl_completion_matches(), returning a string each time. The arguments
 | 
|---|
| 409 | //!   to the generator function are text and state. text is the partial word
 | 
|---|
| 410 | //!   to be completed. state is zero the first time the function is called,
 | 
|---|
| 411 | //!   allowing the generator to perform any necessary initialization, and a
 | 
|---|
| 412 | //!   positive non-zero integer for each subsequent call. The generator
 | 
|---|
| 413 | //!   function returns (char *)NULL to inform rl_completion_matches() that
 | 
|---|
| 414 | //!   there are no more possibilities left. Usually the generator function
 | 
|---|
| 415 | //!   computes the list of possible completions when state is zero, and
 | 
|---|
| 416 | //!   returns them one at a time on subsequent calls. Each string the
 | 
|---|
| 417 | //!   generator function returns as a match must be allocated with malloc();
 | 
|---|
| 418 | //!   Readline frees the strings when it has finished with them.
 | 
|---|
| 419 | //
 | 
|---|
| 420 | char *Readline::Complete(const char* text, int state)
 | 
|---|
| 421 | {
 | 
|---|
| 422 |     if (fCompletion==0)
 | 
|---|
| 423 |         return rl_filename_completion_function(text, state);
 | 
|---|
| 424 | 
 | 
|---|
| 425 |     static vector<string>::const_iterator pos;
 | 
|---|
| 426 |     if (state==0)
 | 
|---|
| 427 |         pos = fCompletion->begin();
 | 
|---|
| 428 | 
 | 
|---|
| 429 |     while (pos!=fCompletion->end())
 | 
|---|
| 430 |     {
 | 
|---|
| 431 |         char *rc = Compare(*pos++, text);
 | 
|---|
| 432 |         if (rc)
 | 
|---|
| 433 |             return rc;
 | 
|---|
| 434 |     }
 | 
|---|
| 435 | 
 | 
|---|
| 436 |     return 0;
 | 
|---|
| 437 | }
 | 
|---|
| 438 | 
 | 
|---|
| 439 | // --------------------------------------------------------------------------
 | 
|---|
| 440 | //
 | 
|---|
| 441 | //! Calls Complete()
 | 
|---|
| 442 | //
 | 
|---|
| 443 | char *Readline::CompleteImp(const char* text, int state)
 | 
|---|
| 444 | {
 | 
|---|
| 445 |     return This->Complete(text, state);
 | 
|---|
| 446 | }
 | 
|---|
| 447 | 
 | 
|---|
| 448 | // --------------------------------------------------------------------------
 | 
|---|
| 449 | //
 | 
|---|
| 450 | //! The readline manual says about this function:
 | 
|---|
| 451 | //!
 | 
|---|
| 452 | //!   A  pointer to an alternative function to create matches. The
 | 
|---|
| 453 | //!   function is called with text, start, and end. start and end are
 | 
|---|
| 454 | //!   indices in rl_line_buffer saying what the boundaries of text are.
 | 
|---|
| 455 | //!   If this function exists and returns NULL, or if this variable is
 | 
|---|
| 456 | //!   set to NULL, then rl_complete() will call the value of
 | 
|---|
| 457 | //!   rl_completion_entry_function to generate matches, otherwise the
 | 
|---|
| 458 | //!   array of strings returned will be used.
 | 
|---|
| 459 | //!
 | 
|---|
| 460 | //! This function is virtual and can be overwritten. It defaults to
 | 
|---|
| 461 | //! a call to rl_completion_matches with CompleteImp as an argument
 | 
|---|
| 462 | //! which defaults to filename completion, but can also be overwritten.
 | 
|---|
| 463 | //!
 | 
|---|
| 464 | //! It is suggested that you call
 | 
|---|
| 465 | //!    Complete(const vector<string>&, const char*)
 | 
|---|
| 466 | //! from here.
 | 
|---|
| 467 | //!
 | 
|---|
| 468 | //! @param text
 | 
|---|
| 469 | //!    A pointer to a char array conatining the text which should be
 | 
|---|
| 470 | //!    completed. The text is null-terminated.
 | 
|---|
| 471 | //!
 | 
|---|
| 472 | //! @param start
 | 
|---|
| 473 | //!    The start index within readline's line buffer rl_line_buffer,
 | 
|---|
| 474 | //!    at which the text starts which presumably should be completed.
 | 
|---|
| 475 | //!
 | 
|---|
| 476 | //! @param end
 | 
|---|
| 477 | //!    The end index within readline's line buffer rl_line_buffer,
 | 
|---|
| 478 | //!    at which the text ends which presumably should be completed.
 | 
|---|
| 479 | //!
 | 
|---|
| 480 | //! @returns
 | 
|---|
| 481 | //!    An array of strings which were allocated with malloc and which
 | 
|---|
| 482 | //!    will be freed by readline with the possible matches.
 | 
|---|
| 483 | //
 | 
|---|
| 484 | char **Readline::Completion(const char *text, int /*start*/, int /*end*/)
 | 
|---|
| 485 | {
 | 
|---|
| 486 |     // To do filename completion call
 | 
|---|
| 487 |     return rl_completion_matches((char*)text, CompleteImp);
 | 
|---|
| 488 | }
 | 
|---|
| 489 | 
 | 
|---|
| 490 | // --------------------------------------------------------------------------
 | 
|---|
| 491 | //
 | 
|---|
| 492 | //! Adds the given string to the history buffer of readline's history by
 | 
|---|
| 493 | //! calling add_history. 
 | 
|---|
| 494 | //!
 | 
|---|
| 495 | //! @param str
 | 
|---|
| 496 | //!    A reference to a string which should be added to readline's
 | 
|---|
| 497 | //!    history.
 | 
|---|
| 498 | //!
 | 
|---|
| 499 | //! @param skip
 | 
|---|
| 500 | //!    If skip is 1 and str matches the last added entry in the history,
 | 
|---|
| 501 | //!    the entry is skipped. If skip==2, all entries matching str are
 | 
|---|
| 502 | //!    removed from the history before the new entry is added as last one.
 | 
|---|
| 503 | //!    <skip==2 is the default>
 | 
|---|
| 504 | //
 | 
|---|
| 505 | void Readline::AddToHistory(const string &str, int skip)
 | 
|---|
| 506 | {
 | 
|---|
| 507 |     if (skip==1 && fLastLine==str)
 | 
|---|
| 508 |         return;
 | 
|---|
| 509 | 
 | 
|---|
| 510 |     if (str.empty())
 | 
|---|
| 511 |         return;
 | 
|---|
| 512 | 
 | 
|---|
| 513 |     int p = -1;
 | 
|---|
| 514 |     while (skip==2)
 | 
|---|
| 515 |     {
 | 
|---|
| 516 |         p = history_search_pos(str.c_str(), 0, p+1);
 | 
|---|
| 517 |         if (p<0)
 | 
|---|
| 518 |             break;
 | 
|---|
| 519 | 
 | 
|---|
| 520 |         // It seems like history_search_pos works more like
 | 
|---|
| 521 |         // history_search_prefix, therefore the identity is checked again
 | 
|---|
| 522 |         const HIST_ENTRY *e = history_get(p+1);
 | 
|---|
| 523 |         if (e && str==e->line)
 | 
|---|
| 524 |             free(remove_history(p));
 | 
|---|
| 525 |     }
 | 
|---|
| 526 | 
 | 
|---|
| 527 |     add_history(str.c_str());
 | 
|---|
| 528 |     fLastLine = str;
 | 
|---|
| 529 | }
 | 
|---|
| 530 | 
 | 
|---|
| 531 | // --------------------------------------------------------------------------
 | 
|---|
| 532 | //
 | 
|---|
| 533 | //! @returns
 | 
|---|
| 534 | //!     a string containing [{fLine}]
 | 
|---|
| 535 | //
 | 
|---|
| 536 | string Readline::GetLinePrompt() const
 | 
|---|
| 537 | {
 | 
|---|
| 538 |     string rc = "[";
 | 
|---|
| 539 |     rc += fLine;
 | 
|---|
| 540 |     return rc+"]";
 | 
|---|
| 541 | }
 | 
|---|
| 542 | 
 | 
|---|
| 543 | // --------------------------------------------------------------------------
 | 
|---|
| 544 | //
 | 
|---|
| 545 | //! Calls rl_set_prompt. This can be used from readline's callback function
 | 
|---|
| 546 | //! to change the prompt while a call to the readline function is in
 | 
|---|
| 547 | //! progress.
 | 
|---|
| 548 | //!
 | 
|---|
| 549 | //! @param prompt
 | 
|---|
| 550 | //!     The new prompt to be shown
 | 
|---|
| 551 | //
 | 
|---|
| 552 | void Readline::UpdatePrompt(const string &prompt) const
 | 
|---|
| 553 | {
 | 
|---|
| 554 |     rl_set_prompt(prompt.c_str());
 | 
|---|
| 555 | }
 | 
|---|
| 556 | 
 | 
|---|
| 557 | // --------------------------------------------------------------------------
 | 
|---|
| 558 | //
 | 
|---|
| 559 | //! This function is used to bind a key sequence via a call to
 | 
|---|
| 560 | //! rl_bind_keyseq.
 | 
|---|
| 561 | //!
 | 
|---|
| 562 | //! Readline's manual says about this function:
 | 
|---|
| 563 | //!
 | 
|---|
| 564 | //!   Bind the key sequence represented by the string keyseq to the
 | 
|---|
| 565 | //!   function function, beginning in the current keymap. This makes
 | 
|---|
| 566 | //!   new keymaps as necessary. The return value is non-zero if keyseq
 | 
|---|
| 567 | //!   is invalid.
 | 
|---|
| 568 | //!
 | 
|---|
| 569 | //! Key sequences are escaped sequences of characters read from an input
 | 
|---|
| 570 | //! stream when a special key is pressed. This is necessary because
 | 
|---|
| 571 | //! there are usually more keys and possible combinations than ascii codes.
 | 
|---|
| 572 | //!
 | 
|---|
| 573 | //! Possible key sequences are for example:
 | 
|---|
| 574 | //!   "\033OP"       F1
 | 
|---|
| 575 | //!   "\033[1;5A"    Ctrl+up
 | 
|---|
| 576 | //!   "\033[1;5B"    Ctrl+down
 | 
|---|
| 577 | //!   "\033[1;3A"    Alt+up
 | 
|---|
| 578 | //!   "\033[1;3B"    Alt+down
 | 
|---|
| 579 | //!   "\033[5;3~"    Alt+page up
 | 
|---|
| 580 | //!   "\033[6;3~"    Alt+page down
 | 
|---|
| 581 | //!   "\033+"        Alt++
 | 
|---|
| 582 | //!   "\033-"        Alt+-
 | 
|---|
| 583 | //!   "\033\t"       Alt+tab
 | 
|---|
| 584 | //!   "\033[1~"      Alt+tab
 | 
|---|
| 585 | //!
 | 
|---|
| 586 | //! @param seq
 | 
|---|
| 587 | //!     The key sequence to be bound
 | 
|---|
| 588 | //!
 | 
|---|
| 589 | //! @param func
 | 
|---|
| 590 | //!     A function of type "int func(int, int)
 | 
|---|
| 591 | //
 | 
|---|
| 592 | void Readline::BindKeySequence(const char *seq, int (*func)(int, int))
 | 
|---|
| 593 | {
 | 
|---|
| 594 |     rl_bind_keyseq(seq, func);
 | 
|---|
| 595 | }
 | 
|---|
| 596 | 
 | 
|---|
| 597 | // --------------------------------------------------------------------------
 | 
|---|
| 598 | //
 | 
|---|
| 599 | //! Calls rl_variable_dumper(1)
 | 
|---|
| 600 | //!
 | 
|---|
| 601 | //!   Print the readline variable names and their current values
 | 
|---|
| 602 | //!   to rl_outstream. If readable is non-zero, the list is formatted
 | 
|---|
| 603 | //!   in such a way that it can be made part of an inputrc file and
 | 
|---|
| 604 | //!   re-read.
 | 
|---|
| 605 | //!
 | 
|---|
| 606 | //! rl_outstream can be redirected using SetStreamOut()
 | 
|---|
| 607 | //!
 | 
|---|
| 608 | //! @returns
 | 
|---|
| 609 | //!     always true
 | 
|---|
| 610 | //
 | 
|---|
| 611 | bool Readline::DumpVariables()
 | 
|---|
| 612 | {
 | 
|---|
| 613 |     rl_variable_dumper(1);
 | 
|---|
| 614 |     return true;
 | 
|---|
| 615 | }
 | 
|---|
| 616 | 
 | 
|---|
| 617 | // --------------------------------------------------------------------------
 | 
|---|
| 618 | //
 | 
|---|
| 619 | //! Calls rl_function_dumper(1)
 | 
|---|
| 620 | //!
 | 
|---|
| 621 | //!   Print the readline function names and the key sequences currently
 | 
|---|
| 622 | //!   bound to them to rl_outstream. If readable is non-zero, the list
 | 
|---|
| 623 | //!   is formatted in such a way that it can be made part of an inputrc
 | 
|---|
| 624 | //!   file and re-read.
 | 
|---|
| 625 | //!
 | 
|---|
| 626 | //! rl_outstream can be redirected using SetStreamOut()
 | 
|---|
| 627 | //!
 | 
|---|
| 628 | //! @returns
 | 
|---|
| 629 | //!     always true
 | 
|---|
| 630 | //
 | 
|---|
| 631 | bool Readline::DumpFunctions()
 | 
|---|
| 632 | {
 | 
|---|
| 633 |     rl_function_dumper(1);
 | 
|---|
| 634 |     return true;
 | 
|---|
| 635 | }
 | 
|---|
| 636 | 
 | 
|---|
| 637 | // --------------------------------------------------------------------------
 | 
|---|
| 638 | //
 | 
|---|
| 639 | //! Calls rl_list_funmap_names()
 | 
|---|
| 640 | //!
 | 
|---|
| 641 | //!    Print the names of all bindable Readline functions to rl_outstream.
 | 
|---|
| 642 | //!
 | 
|---|
| 643 | //! rl_outstream can be redirected using SetStreamOut()
 | 
|---|
| 644 | //!
 | 
|---|
| 645 | //! @returns
 | 
|---|
| 646 | //!     always true
 | 
|---|
| 647 | //
 | 
|---|
| 648 | bool Readline::DumpFunmap()
 | 
|---|
| 649 | {
 | 
|---|
| 650 |     rl_list_funmap_names();
 | 
|---|
| 651 |     return true;
 | 
|---|
| 652 | }
 | 
|---|
| 653 | 
 | 
|---|
| 654 | // --------------------------------------------------------------------------
 | 
|---|
| 655 | //
 | 
|---|
| 656 | //! Sets rl_outstream (the stdio stream to which Readline performs output)
 | 
|---|
| 657 | //! to the new stream.
 | 
|---|
| 658 | //!
 | 
|---|
| 659 | //! @param f
 | 
|---|
| 660 | //!    The new stdio stream to which readline should perform its output
 | 
|---|
| 661 | //!
 | 
|---|
| 662 | //! @return
 | 
|---|
| 663 | //!    The old stream to which readline was performing output
 | 
|---|
| 664 | //
 | 
|---|
| 665 | FILE *Readline::SetStreamOut(FILE *f)
 | 
|---|
| 666 | {
 | 
|---|
| 667 |     FILE *rc = rl_outstream;
 | 
|---|
| 668 |     rl_outstream = f;
 | 
|---|
| 669 |     return rc;
 | 
|---|
| 670 | }
 | 
|---|
| 671 | 
 | 
|---|
| 672 | // --------------------------------------------------------------------------
 | 
|---|
| 673 | //
 | 
|---|
| 674 | //! Sets rl_instream (the stdio stream from which Readline reads input)
 | 
|---|
| 675 | //! to the new stream.
 | 
|---|
| 676 | //!
 | 
|---|
| 677 | //! @param f
 | 
|---|
| 678 | //!    The new stdio stream from which readline should read its input
 | 
|---|
| 679 | //!
 | 
|---|
| 680 | //! @return
 | 
|---|
| 681 | //!    The old stream from which readline was reading it input
 | 
|---|
| 682 | //
 | 
|---|
| 683 | FILE *Readline::SetStreamIn(FILE *f)
 | 
|---|
| 684 | {
 | 
|---|
| 685 |     FILE *rc = rl_instream;
 | 
|---|
| 686 |     rl_instream = f;
 | 
|---|
| 687 |     return rc;
 | 
|---|
| 688 | }
 | 
|---|
| 689 | 
 | 
|---|
| 690 | // --------------------------------------------------------------------------
 | 
|---|
| 691 | //
 | 
|---|
| 692 | //! return rl_display_prompt (the prompt which should currently be
 | 
|---|
| 693 | //! displayed on the screen) while a readline command is in progress
 | 
|---|
| 694 | //
 | 
|---|
| 695 | string Readline::GetPrompt()
 | 
|---|
| 696 | {
 | 
|---|
| 697 |     return rl_display_prompt;
 | 
|---|
| 698 | }
 | 
|---|
| 699 | 
 | 
|---|
| 700 | // --------------------------------------------------------------------------
 | 
|---|
| 701 | //
 | 
|---|
| 702 | //! return rl_line_buffer (the current input line which should currently be
 | 
|---|
| 703 | //! displayed on the screen) while a readline command is in progress
 | 
|---|
| 704 | //!
 | 
|---|
| 705 | //! The length of the current line buffer (rl_end) is available as
 | 
|---|
| 706 | //! GetLineBuffer().size()
 | 
|---|
| 707 | //!
 | 
|---|
| 708 | //! Note that after readline has returned the contents of rl_end might
 | 
|---|
| 709 | //! not reflect the correct buffer length anymore, hence, the returned buffer
 | 
|---|
| 710 | //! might be truncated.
 | 
|---|
| 711 | //
 | 
|---|
| 712 | string Readline::GetBuffer()
 | 
|---|
| 713 | {
 | 
|---|
| 714 |     return string(rl_line_buffer, rl_end);
 | 
|---|
| 715 | }
 | 
|---|
| 716 | 
 | 
|---|
| 717 | // --------------------------------------------------------------------------
 | 
|---|
| 718 | //
 | 
|---|
| 719 | //! return rl_point (the current cursor position within the line buffer)
 | 
|---|
| 720 | //
 | 
|---|
| 721 | int Readline::GetCursor()
 | 
|---|
| 722 | {
 | 
|---|
| 723 |     return rl_point;
 | 
|---|
| 724 | }
 | 
|---|
| 725 | 
 | 
|---|
| 726 | // --------------------------------------------------------------------------
 | 
|---|
| 727 | //
 | 
|---|
| 728 | //! return strlen(rl_display_prompt) + rl_point
 | 
|---|
| 729 | //
 | 
|---|
| 730 | int Readline::GetAbsCursor()
 | 
|---|
| 731 | {
 | 
|---|
| 732 |     return strlen(rl_display_prompt) + rl_point;
 | 
|---|
| 733 | }
 | 
|---|
| 734 | 
 | 
|---|
| 735 | // --------------------------------------------------------------------------
 | 
|---|
| 736 | //
 | 
|---|
| 737 | //! return rl_end (the current total length of the line buffer)
 | 
|---|
| 738 | //! Note that after readline has returned the contents of rl_end might
 | 
|---|
| 739 | //! not reflect the correct buffer length anymore.
 | 
|---|
| 740 | //
 | 
|---|
| 741 | int Readline::GetBufferLength()
 | 
|---|
| 742 | {
 | 
|---|
| 743 |     return rl_end;
 | 
|---|
| 744 | }
 | 
|---|
| 745 | 
 | 
|---|
| 746 | // --------------------------------------------------------------------------
 | 
|---|
| 747 | //
 | 
|---|
| 748 | //! return the length of the prompt plus the length of the line buffer
 | 
|---|
| 749 | //
 | 
|---|
| 750 | int Readline::GetLineLength()
 | 
|---|
| 751 | {
 | 
|---|
| 752 |     return strlen(rl_display_prompt) + rl_end;
 | 
|---|
| 753 | }
 | 
|---|
| 754 | 
 | 
|---|
| 755 | // --------------------------------------------------------------------------
 | 
|---|
| 756 | //
 | 
|---|
| 757 | //! Calls: Function: void rl_resize_terminal()
 | 
|---|
| 758 | //! Update Readline's internal screen size by reading values from the kernel.
 | 
|---|
| 759 | //
 | 
|---|
| 760 | void Readline::Resize()
 | 
|---|
| 761 | {
 | 
|---|
| 762 |     rl_resize_terminal();
 | 
|---|
| 763 | }
 | 
|---|
| 764 | 
 | 
|---|
| 765 | // --------------------------------------------------------------------------
 | 
|---|
| 766 | //
 | 
|---|
| 767 | //! Calls: Function: void rl_set_screen_size (int rows, int cols)
 | 
|---|
| 768 | //! Set Readline's idea of the terminal size to rows rows and cols columns.
 | 
|---|
| 769 | //!
 | 
|---|
| 770 | //! @param width
 | 
|---|
| 771 | //!    Number of columns
 | 
|---|
| 772 | //!
 | 
|---|
| 773 | //! @param height
 | 
|---|
| 774 | //!    Number of rows
 | 
|---|
| 775 | //
 | 
|---|
| 776 | void Readline::Resize(int width, int height)
 | 
|---|
| 777 | {
 | 
|---|
| 778 |     rl_set_screen_size(height, width);
 | 
|---|
| 779 | }
 | 
|---|
| 780 | 
 | 
|---|
| 781 | // --------------------------------------------------------------------------
 | 
|---|
| 782 | //
 | 
|---|
| 783 | //! Get the number of cols readline assumes the screen size to be
 | 
|---|
| 784 | //
 | 
|---|
| 785 | int Readline::GetCols() const
 | 
|---|
| 786 | {
 | 
|---|
| 787 |     int rows, cols;
 | 
|---|
| 788 |     rl_get_screen_size(&rows, &cols);
 | 
|---|
| 789 |     return cols;
 | 
|---|
| 790 | }
 | 
|---|
| 791 | 
 | 
|---|
| 792 | // --------------------------------------------------------------------------
 | 
|---|
| 793 | //
 | 
|---|
| 794 | //! Get the number of rows readline assumes the screen size to be
 | 
|---|
| 795 | //
 | 
|---|
| 796 | int Readline::GetRows() const
 | 
|---|
| 797 | {
 | 
|---|
| 798 |     int rows, cols;
 | 
|---|
| 799 |     rl_get_screen_size(&rows, &cols);
 | 
|---|
| 800 |     return rows;
 | 
|---|
| 801 | }
 | 
|---|
| 802 | 
 | 
|---|
| 803 | // --------------------------------------------------------------------------
 | 
|---|
| 804 | //
 | 
|---|
| 805 | //! Return a list of pointer to the history contents
 | 
|---|
| 806 | //
 | 
|---|
| 807 | vector<const char*> Readline::GetHistory() const
 | 
|---|
| 808 | {
 | 
|---|
| 809 |     HIST_ENTRY **next = history_list();
 | 
|---|
| 810 | 
 | 
|---|
| 811 |     vector<const char*> v;
 | 
|---|
| 812 | 
 | 
|---|
| 813 |     for (; *next; next++)
 | 
|---|
| 814 |         v.push_back((*next)->line);
 | 
|---|
| 815 | 
 | 
|---|
| 816 |     return v;
 | 
|---|
| 817 | }
 | 
|---|
| 818 | 
 | 
|---|
| 819 | // --------------------------------------------------------------------------
 | 
|---|
| 820 | //
 | 
|---|
| 821 | //! Clear readline history (calls clear_history())
 | 
|---|
| 822 | //!
 | 
|---|
| 823 | //! @returns
 | 
|---|
| 824 | //!     always true
 | 
|---|
| 825 | //
 | 
|---|
| 826 | bool Readline::ClearHistory()
 | 
|---|
| 827 | {
 | 
|---|
| 828 |     clear_history();
 | 
|---|
| 829 |     return true;
 | 
|---|
| 830 | }
 | 
|---|
| 831 | 
 | 
|---|
| 832 | // --------------------------------------------------------------------------
 | 
|---|
| 833 | //
 | 
|---|
| 834 | //! Displays the current history on rl_outstream
 | 
|---|
| 835 | //!
 | 
|---|
| 836 | //! rl_outstream can be redirected using SetStreamOut()
 | 
|---|
| 837 | //!
 | 
|---|
| 838 | //! @returns
 | 
|---|
| 839 | //!     always true
 | 
|---|
| 840 | //
 | 
|---|
| 841 | bool Readline::DumpHistory()
 | 
|---|
| 842 | {
 | 
|---|
| 843 |     HIST_ENTRY **next = history_list();
 | 
|---|
| 844 | 
 | 
|---|
| 845 |     if (!next)
 | 
|---|
| 846 |         return true;
 | 
|---|
| 847 | 
 | 
|---|
| 848 |     for (; *next; next++)
 | 
|---|
| 849 |         fprintf(rl_outstream, "%s\n", (*next)->line);
 | 
|---|
| 850 | 
 | 
|---|
| 851 |     return true;
 | 
|---|
| 852 | }
 | 
|---|
| 853 | 
 | 
|---|
| 854 | // --------------------------------------------------------------------------
 | 
|---|
| 855 | //
 | 
|---|
| 856 | //! Print the available commands. This is intended for being overwritten
 | 
|---|
| 857 | //! by deriving classes.
 | 
|---|
| 858 | //!
 | 
|---|
| 859 | //! rl_outstream can be redirected using SetStreamOut()
 | 
|---|
| 860 | //!
 | 
|---|
| 861 | //! @returns
 | 
|---|
| 862 | //!     always true
 | 
|---|
| 863 | //
 | 
|---|
| 864 | //
 | 
|---|
| 865 | bool Readline::PrintCommands()
 | 
|---|
| 866 | {
 | 
|---|
| 867 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 868 |     fprintf(rl_outstream, " Commands:\n");
 | 
|---|
| 869 |     fprintf(rl_outstream, "   No application specific commands defined.\n");
 | 
|---|
| 870 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 871 |     return true;
 | 
|---|
| 872 | }
 | 
|---|
| 873 | 
 | 
|---|
| 874 | // --------------------------------------------------------------------------
 | 
|---|
| 875 | //
 | 
|---|
| 876 | //! Print a general help message. This is intended for being overwritten
 | 
|---|
| 877 | //! by deriving classes.
 | 
|---|
| 878 | //!
 | 
|---|
| 879 | //!
 | 
|---|
| 880 | //! rl_outstream can be redirected using SetStreamOut()
 | 
|---|
| 881 | //!
 | 
|---|
| 882 | //! @returns
 | 
|---|
| 883 | //!     always true
 | 
|---|
| 884 | //
 | 
|---|
| 885 | //
 | 
|---|
| 886 | bool Readline::PrintGeneralHelp()
 | 
|---|
| 887 | {
 | 
|---|
| 888 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 889 |     fprintf(rl_outstream, " General help:\n");
 | 
|---|
| 890 |     fprintf(rl_outstream, "   h,help       Print this help message\n");
 | 
|---|
| 891 |     fprintf(rl_outstream, "   clear        Clear history buffer\n");
 | 
|---|
| 892 |     fprintf(rl_outstream, "   lh,history   Dump the history buffer to the screen\n");
 | 
|---|
| 893 |     fprintf(rl_outstream, "   v,variables  Dump readline variables\n");
 | 
|---|
| 894 |     fprintf(rl_outstream, "   f,functions  Dump readline functions\n");
 | 
|---|
| 895 |     fprintf(rl_outstream, "   m,funmap     Dump readline funmap\n");
 | 
|---|
| 896 |     fprintf(rl_outstream, "   c,commands   Dump available commands\n");
 | 
|---|
| 897 |     fprintf(rl_outstream, "   k,keylist    Dump key bindings\n");
 | 
|---|
| 898 |     fprintf(rl_outstream, "   .q,quit      Quit\n");
 | 
|---|
| 899 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 900 |     fprintf(rl_outstream, " The command history is automatically loaded and saves to\n");
 | 
|---|
| 901 |     fprintf(rl_outstream, " and from %s.\n", GetName().c_str());
 | 
|---|
| 902 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 903 |     return true;
 | 
|---|
| 904 | }
 | 
|---|
| 905 | 
 | 
|---|
| 906 | // --------------------------------------------------------------------------
 | 
|---|
| 907 | //
 | 
|---|
| 908 | //! Print a help text about key bindings. This is intended for being
 | 
|---|
| 909 | //! overwritten by deriving classes.
 | 
|---|
| 910 | //!
 | 
|---|
| 911 | //!
 | 
|---|
| 912 | //! rl_outstream can be redirected using SetStreamOut()
 | 
|---|
| 913 | //!
 | 
|---|
| 914 | //! @returns
 | 
|---|
| 915 | //!     always true
 | 
|---|
| 916 | //
 | 
|---|
| 917 | //
 | 
|---|
| 918 | bool Readline::PrintKeyBindings()
 | 
|---|
| 919 | {
 | 
|---|
| 920 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 921 |     fprintf(rl_outstream, " Key bindings:\n");
 | 
|---|
| 922 |     fprintf(rl_outstream, "   Page-up         Search backward in history\n");
 | 
|---|
| 923 |     fprintf(rl_outstream, "   Page-dn         Search forward in history\n");
 | 
|---|
| 924 |     fprintf(rl_outstream, "   Ctrl-left       One word backward\n");
 | 
|---|
| 925 |     fprintf(rl_outstream, "   Ctrl-right      One word forward\n");
 | 
|---|
| 926 |     fprintf(rl_outstream, "   Ctrl-d          Quit\n");
 | 
|---|
| 927 |     fprintf(rl_outstream, "   Ctrl-y          Delete line\n");
 | 
|---|
| 928 |     fprintf(rl_outstream, "   Alt-end/Ctrl-k  Delete until the end of the line\n");
 | 
|---|
| 929 |     fprintf(rl_outstream, "   F1              Toggle visibility of upper panel\n");
 | 
|---|
| 930 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 931 |     fprintf(rl_outstream, " Default key-bindings are identical with your bash.\n");
 | 
|---|
| 932 |     fprintf(rl_outstream, "\n");
 | 
|---|
| 933 |     return true;
 | 
|---|
| 934 | }
 | 
|---|
| 935 | 
 | 
|---|
| 936 | // --------------------------------------------------------------------------
 | 
|---|
| 937 | //
 | 
|---|
| 938 | //!
 | 
|---|
| 939 | //
 | 
|---|
| 940 | bool Readline::Process(const string &str)
 | 
|---|
| 941 | {
 | 
|---|
| 942 |     // ----------- Readline static -------------
 | 
|---|
| 943 | 
 | 
|---|
| 944 |     if (str=="clear")
 | 
|---|
| 945 |         return ClearHistory();
 | 
|---|
| 946 | 
 | 
|---|
| 947 |     if (str=="lh" || str=="history")
 | 
|---|
| 948 |         return DumpHistory();
 | 
|---|
| 949 | 
 | 
|---|
| 950 |     if (str=="v" || str=="variables")
 | 
|---|
| 951 |         return DumpVariables();
 | 
|---|
| 952 | 
 | 
|---|
| 953 |     if (str=="f" || str=="functions")
 | 
|---|
| 954 |         return DumpFunctions();
 | 
|---|
| 955 | 
 | 
|---|
| 956 |     if (str=="m" || str=="funmap")
 | 
|---|
| 957 |         return DumpFunmap();
 | 
|---|
| 958 | 
 | 
|---|
| 959 |     // ---------- Readline virtual -------------
 | 
|---|
| 960 | 
 | 
|---|
| 961 |     if (str=="h" || str=="help")
 | 
|---|
| 962 |         return PrintGeneralHelp();
 | 
|---|
| 963 | 
 | 
|---|
| 964 |     if (str=="c" || str=="commands")
 | 
|---|
| 965 |         return PrintCommands();
 | 
|---|
| 966 | 
 | 
|---|
| 967 |     if (str=="k" || str=="keylist")
 | 
|---|
| 968 |         return PrintKeyBindings();
 | 
|---|
| 969 | 
 | 
|---|
| 970 |     return false;
 | 
|---|
| 971 | }
 | 
|---|
| 972 | 
 | 
|---|
| 973 | // --------------------------------------------------------------------------
 | 
|---|
| 974 | //
 | 
|---|
| 975 | //! This function is a wrapper around the call to readline. It encapsultes
 | 
|---|
| 976 | //! the return buffer into a std::string and deletes the memory allocated
 | 
|---|
| 977 | //! by readline. Furthermore, it removes leading and trailing whitespaces
 | 
|---|
| 978 | //! before return the result. The result is returned in the given
 | 
|---|
| 979 | //! argument containing the prompt. Before the function returns Shutdown()
 | 
|---|
| 980 | //! is called (as opposed to Startup when readline starts)
 | 
|---|
| 981 | //!
 | 
|---|
| 982 | //! @param str
 | 
|---|
| 983 | //!    The prompt which is to be shown by the readline libarary. it is
 | 
|---|
| 984 | //!    directly given to the call to readline. The result of the
 | 
|---|
| 985 | //!    readline call is returned in this string.
 | 
|---|
| 986 | //!
 | 
|---|
| 987 | //! @returns
 | 
|---|
| 988 | //!    true if the call succeeded as usual, false if EOF was detected
 | 
|---|
| 989 | //!    by the readline call.
 | 
|---|
| 990 | //
 | 
|---|
| 991 | bool Readline::PromptEOF(string &str)
 | 
|---|
| 992 | {
 | 
|---|
| 993 |     char *buf = readline(str.c_str());
 | 
|---|
| 994 |     Shutdown(buf);
 | 
|---|
| 995 | 
 | 
|---|
| 996 |     // Happens when EOF is encountered
 | 
|---|
| 997 |     if (!buf)
 | 
|---|
| 998 |         return false;
 | 
|---|
| 999 | 
 | 
|---|
| 1000 |     str = Tools::Trim(buf);
 | 
|---|
| 1001 | 
 | 
|---|
| 1002 |     free(buf);
 | 
|---|
| 1003 | 
 | 
|---|
| 1004 |     return true;
 | 
|---|
| 1005 | }
 | 
|---|
| 1006 | 
 | 
|---|
| 1007 | // --------------------------------------------------------------------------
 | 
|---|
| 1008 | //
 | 
|---|
| 1009 | //! This function is a wrapper around the call to readline. It encapsultes
 | 
|---|
| 1010 | //! the return buffer into a std::string and deletes the memory allocated
 | 
|---|
| 1011 | //! by readline. Furthermore, it removes leading and trailing whitespaces
 | 
|---|
| 1012 | //! before return the result. Before the function returns Shutdown() is
 | 
|---|
| 1013 | //! called (as opposed to Startup when readline starts)
 | 
|---|
| 1014 | //!
 | 
|---|
| 1015 | //! @param prompt
 | 
|---|
| 1016 | //!    The prompt which is to be shown by the readline libarary. it is
 | 
|---|
| 1017 | //!    directly given to the call to readline.
 | 
|---|
| 1018 | //!
 | 
|---|
| 1019 | //! @returns
 | 
|---|
| 1020 | //!    The result of the readline call
 | 
|---|
| 1021 | //
 | 
|---|
| 1022 | string Readline::Prompt(const string &prompt)
 | 
|---|
| 1023 | {
 | 
|---|
| 1024 |     char *buf = readline(prompt.c_str());
 | 
|---|
| 1025 | 
 | 
|---|
| 1026 |     Shutdown(buf ? buf : "");
 | 
|---|
| 1027 | 
 | 
|---|
| 1028 |     const string str = !buf || (rl_done && rl_pending_input==4)
 | 
|---|
| 1029 |         ? ".q" : Tools::Trim(buf);
 | 
|---|
| 1030 | 
 | 
|---|
| 1031 |     free(buf);
 | 
|---|
| 1032 | 
 | 
|---|
| 1033 |     return str;
 | 
|---|
| 1034 | }
 | 
|---|
| 1035 | 
 | 
|---|
| 1036 | // --------------------------------------------------------------------------
 | 
|---|
| 1037 | //
 | 
|---|
| 1038 | //! This implements a loop over readline calls. A prompt to issue can be
 | 
|---|
| 1039 | //! given. If it is NULL, the prompt is retrieved from GetUpdatePrompt().
 | 
|---|
| 1040 | //! It is updated regularly by means of calls to GetUpdatePrompt() from
 | 
|---|
| 1041 | //! EventHook(). If Prompt() returns with "quit" or ".q" the loop is
 | 
|---|
| 1042 | //! exited. If ".qqq" is entered exit(-1) is called. In case of ".qqqqqq"
 | 
|---|
| 1043 | //! abort(). Both ways to exit the program are not recommended. Empty
 | 
|---|
| 1044 | //! inputs are ignored. After that Process() with the returned string
 | 
|---|
| 1045 | //! is called. If Process returns true the input is not counted and not
 | 
|---|
| 1046 | //! added to the history, otherwise the line counter is increased
 | 
|---|
| 1047 | //! and the input is added to the history.
 | 
|---|
| 1048 | //!
 | 
|---|
| 1049 | //! @param prompt
 | 
|---|
| 1050 | //!    The prompt to be issued or NULL if GetUPdatePrompt should be used
 | 
|---|
| 1051 | //!    instead.
 | 
|---|
| 1052 | //!
 | 
|---|
| 1053 | void Readline::Run(const char *prompt)
 | 
|---|
| 1054 | {
 | 
|---|
| 1055 |     fLine = 0;
 | 
|---|
| 1056 |     while (1)
 | 
|---|
| 1057 |     {
 | 
|---|
| 1058 |         // Before we start we have to make sure that the
 | 
|---|
| 1059 |         // screen looks like and is ordered like expected.
 | 
|---|
| 1060 |         const string str = Prompt(prompt?prompt:GetUpdatePrompt());
 | 
|---|
| 1061 |         if (str.empty())
 | 
|---|
| 1062 |             continue;
 | 
|---|
| 1063 | 
 | 
|---|
| 1064 |         if (str=="quit" || str==".q")
 | 
|---|
| 1065 |             break;
 | 
|---|
| 1066 | 
 | 
|---|
| 1067 |         if (str==".qqq")
 | 
|---|
| 1068 |             exit(-1);
 | 
|---|
| 1069 | 
 | 
|---|
| 1070 |         if (str==".qqqqqq")
 | 
|---|
| 1071 |             abort();
 | 
|---|
| 1072 | 
 | 
|---|
| 1073 |         if (Process(str))
 | 
|---|
| 1074 |             continue;
 | 
|---|
| 1075 | 
 | 
|---|
| 1076 |         fLine++;
 | 
|---|
| 1077 | 
 | 
|---|
| 1078 |         AddToHistory(str);
 | 
|---|
| 1079 |     }
 | 
|---|
| 1080 | }
 | 
|---|
| 1081 | 
 | 
|---|
| 1082 | void Readline::Stop()
 | 
|---|
| 1083 | {
 | 
|---|
| 1084 |     rl_done          = 1;
 | 
|---|
| 1085 |     rl_pending_input = 4; // EOT (end of transmission, ctrl-d)
 | 
|---|
| 1086 | }
 | 
|---|