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