| 1 | #ifndef FACT_Readline
|
|---|
| 2 | #define FACT_Readline
|
|---|
| 3 |
|
|---|
| 4 | #include <string>
|
|---|
| 5 | #include <vector>
|
|---|
| 6 | #include <fstream>
|
|---|
| 7 |
|
|---|
| 8 | class Readline
|
|---|
| 9 | {
|
|---|
| 10 | public:
|
|---|
| 11 | static bool RedirectionWrapper(std::ostream &out, bool (*function)());
|
|---|
| 12 |
|
|---|
| 13 | protected:
|
|---|
| 14 | /// A pointer to the unique instance of Readline to let the static
|
|---|
| 15 | /// functions used as callback for readline call member function
|
|---|
| 16 | /// with an object. This makes overwriting easier.
|
|---|
| 17 | static Readline *This;
|
|---|
| 18 |
|
|---|
| 19 | private:
|
|---|
| 20 | std::string fName; /// Filename for the history file compiled in the constructor
|
|---|
| 21 | int fMaxLines; /// Maximum number of lines in the history file
|
|---|
| 22 |
|
|---|
| 23 | std::ofstream fCommandLog;
|
|---|
| 24 |
|
|---|
| 25 | std::string fLastLine; /// Last line adde to history
|
|---|
| 26 |
|
|---|
| 27 | int fLine;
|
|---|
| 28 |
|
|---|
| 29 | // Static member function which are used to adapt readline to ncurses
|
|---|
| 30 | static int rl_ncurses_getc(FILE *);
|
|---|
| 31 | static int rl_ncurses_startup();
|
|---|
| 32 | static void rl_ncurses_redisplay();
|
|---|
| 33 | static int rl_ncurses_event_hook();
|
|---|
| 34 | static void rl_ncurses_completion_display(char **matches, int num, int max);
|
|---|
| 35 | static char **rl_ncurses_completion_function(const char *text, int start, int end);
|
|---|
| 36 | static char *CompleteImp(const char* text, int state);
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | protected:
|
|---|
| 40 | // The non static implementations of the callback funtions above
|
|---|
| 41 | virtual int Getc(FILE *);
|
|---|
| 42 | virtual void Startup();
|
|---|
| 43 | virtual void EventHook();
|
|---|
| 44 | virtual void Shutdown(const char *buf);
|
|---|
| 45 | virtual void Redisplay();
|
|---|
| 46 | virtual void CompletionDisplay(char **matches, int num, int max);
|
|---|
| 47 |
|
|---|
| 48 | // Functions dealing with auto completion
|
|---|
| 49 | virtual char *Complete(const char* text, int state);
|
|---|
| 50 | virtual char **Completion(const char *text, int start, int end);
|
|---|
| 51 |
|
|---|
| 52 | /// Pointer to a list of possible matched for auto-completion
|
|---|
| 53 | const std::vector<std::string> *fCompletion;
|
|---|
| 54 | void SetCompletion(const std::vector<std::string> *v) { fCompletion = v; }
|
|---|
| 55 | char **Complete(const std::vector<std::string> &v, const char *text);
|
|---|
| 56 |
|
|---|
| 57 | public:
|
|---|
| 58 | Readline(const char *prgname);
|
|---|
| 59 | virtual ~Readline();
|
|---|
| 60 |
|
|---|
| 61 | // Access to readline
|
|---|
| 62 | void BindKeySequence(const char *seq, int (*func)(int, int));
|
|---|
| 63 |
|
|---|
| 64 | static bool DumpVariables();
|
|---|
| 65 | static bool DumpFunctions();
|
|---|
| 66 | static bool DumpFunmap();
|
|---|
| 67 | static bool DumpHistory();
|
|---|
| 68 |
|
|---|
| 69 | virtual bool PrintGeneralHelp();
|
|---|
| 70 | virtual bool PrintCommands();
|
|---|
| 71 | virtual bool PrintKeyBindings();
|
|---|
| 72 |
|
|---|
| 73 | // History functions
|
|---|
| 74 | std::string GetName() const { return fName; }
|
|---|
| 75 |
|
|---|
| 76 | void AddToHistory(const std::string &str, int skip=2);
|
|---|
| 77 | static bool ClearHistory();
|
|---|
| 78 | std::vector<const char*> GetHistory() const;
|
|---|
| 79 |
|
|---|
| 80 | void SetMaxSize(int lines) { fMaxLines = lines; }
|
|---|
| 81 |
|
|---|
| 82 | // Prompting
|
|---|
| 83 | void UpdatePrompt(const std::string &prompt) const;
|
|---|
| 84 | void UpdatePrompt() const { UpdatePrompt(GetUpdatePrompt()); }
|
|---|
| 85 |
|
|---|
| 86 | virtual bool Process(const std::string &str);
|
|---|
| 87 | virtual std::string GetUpdatePrompt() const { return ""; }
|
|---|
| 88 | virtual bool PromptEOF(std::string &str);
|
|---|
| 89 | virtual std::string Prompt(const std::string &prompt);
|
|---|
| 90 | virtual void Run(const char *prompt=0);
|
|---|
| 91 | static void Stop();
|
|---|
| 92 | virtual bool ExecuteShellCommand(const std::string &cmd);
|
|---|
| 93 | int Execute(const std::string &fname);
|
|---|
| 94 | bool IsStopped() const;
|
|---|
| 95 | void ProcessLine(const std::string &str);
|
|---|
| 96 |
|
|---|
| 97 | int GetLine() const { return fLine; }
|
|---|
| 98 | virtual std::string GetLinePrompt() const;
|
|---|
| 99 |
|
|---|
| 100 | // Helper
|
|---|
| 101 | static char *Compare(const std::string &str, const std::string &txt);
|
|---|
| 102 | static char **CompletionMatches(const char *text, char *(*func)(const char*, int));
|
|---|
| 103 |
|
|---|
| 104 | // I/O Streams
|
|---|
| 105 | static FILE *SetStreamOut(FILE *f);
|
|---|
| 106 | static FILE *SetStreamIn(FILE *f);
|
|---|
| 107 |
|
|---|
| 108 | // Other global readline variables
|
|---|
| 109 | static std::string GetPrompt();
|
|---|
| 110 | static std::string GetBuffer();
|
|---|
| 111 | static int GetAbsCursor();
|
|---|
| 112 | static int GetCursor();
|
|---|
| 113 | static int GetBufferLength();
|
|---|
| 114 | static int GetLineLength();
|
|---|
| 115 |
|
|---|
| 116 | // Screen size
|
|---|
| 117 | static void Resize();
|
|---|
| 118 | static void Resize(int w, int h);
|
|---|
| 119 | int GetCols() const;
|
|---|
| 120 | int GetRows() const;
|
|---|
| 121 |
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| 124 | #endif
|
|---|