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 | static int fLabel;
|
---|
30 | static std::string fScript;
|
---|
31 |
|
---|
32 | /// Static member function which are used to adapt readline to ncurses
|
---|
33 | static int rl_ncurses_getc(FILE *);
|
---|
34 | static int rl_ncurses_startup();
|
---|
35 | static void rl_ncurses_redisplay();
|
---|
36 | static int rl_ncurses_event_hook();
|
---|
37 | static void rl_ncurses_completion_display(char **matches, int num, int max);
|
---|
38 | static char **rl_ncurses_completion_function(const char *text, int start, int end);
|
---|
39 | static char *CompleteImp(const char* text, int state);
|
---|
40 |
|
---|
41 |
|
---|
42 | protected:
|
---|
43 | /// The non static implementations of the callback funtions above
|
---|
44 | virtual int Getc(FILE *);
|
---|
45 | virtual void Startup();
|
---|
46 | virtual void EventHook();
|
---|
47 | virtual void Shutdown(const char *buf);
|
---|
48 | virtual void Redisplay();
|
---|
49 | virtual void CompletionDisplay(char **matches, int num, int max);
|
---|
50 |
|
---|
51 | /// Functions dealing with auto completion
|
---|
52 | virtual char *Complete(const char* text, int state);
|
---|
53 | virtual char **Completion(const char *text, int start, int end);
|
---|
54 |
|
---|
55 | /// Pointer to a list of possible matched for auto-completion
|
---|
56 | const std::vector<std::string> *fCompletion;
|
---|
57 | void SetCompletion(const std::vector<std::string> *v) { fCompletion = v; }
|
---|
58 | char **Complete(const std::vector<std::string> &v, const char *text);
|
---|
59 |
|
---|
60 | ///
|
---|
61 | virtual void SetSection(int) { }
|
---|
62 | virtual void PrintReadlineError(const std::string &str);
|
---|
63 |
|
---|
64 | public:
|
---|
65 | Readline(const char *prgname);
|
---|
66 | virtual ~Readline();
|
---|
67 |
|
---|
68 | // Access to readline
|
---|
69 | void BindKeySequence(const char *seq, int (*func)(int, int));
|
---|
70 |
|
---|
71 | static bool DumpVariables();
|
---|
72 | static bool DumpFunctions();
|
---|
73 | static bool DumpFunmap();
|
---|
74 | static bool DumpHistory();
|
---|
75 |
|
---|
76 | virtual bool PrintGeneralHelp();
|
---|
77 | virtual bool PrintCommands();
|
---|
78 | virtual bool PrintKeyBindings();
|
---|
79 |
|
---|
80 | // History functions
|
---|
81 | std::string GetName() const { return fName; }
|
---|
82 |
|
---|
83 | void AddToHistory(const std::string &str, int skip=2);
|
---|
84 | static bool ClearHistory();
|
---|
85 | std::vector<const char*> GetHistory() const;
|
---|
86 |
|
---|
87 | void SetMaxSize(int lines) { fMaxLines = lines; }
|
---|
88 |
|
---|
89 | // Prompting
|
---|
90 | void UpdatePrompt(const std::string &prompt) const;
|
---|
91 | void UpdatePrompt() const { UpdatePrompt(GetUpdatePrompt()); }
|
---|
92 |
|
---|
93 | virtual bool PreProcess(const std::string &str);
|
---|
94 | virtual bool Process(const std::string &str);
|
---|
95 | virtual std::string GetUpdatePrompt() const { return ""; }
|
---|
96 | virtual bool PromptEOF(std::string &str);
|
---|
97 | virtual std::string Prompt(const std::string &prompt);
|
---|
98 | virtual void Run(const char *prompt=0);
|
---|
99 | static void Stop();
|
---|
100 | virtual bool ExecuteShellCommand(const std::string &cmd);
|
---|
101 | int Execute(const std::string &fname, const std::map<std::string,std::string> &args=std::map<std::string,std::string>());
|
---|
102 | bool IsStopped() const;
|
---|
103 | void ProcessLine(const std::string &str);
|
---|
104 | static void SetLabel(int l) { fLabel = l; }
|
---|
105 | static int GetLabel() { return fLabel; }
|
---|
106 |
|
---|
107 | static std::string GetScript() { return fScript; }
|
---|
108 |
|
---|
109 | int GetLine() const { return fLine; }
|
---|
110 | virtual std::string GetLinePrompt() const;
|
---|
111 |
|
---|
112 | // Helper
|
---|
113 | static char *Compare(const std::string &str, const std::string &txt);
|
---|
114 | static char **CompletionMatches(const char *text, char *(*func)(const char*, int));
|
---|
115 |
|
---|
116 | // I/O Streams
|
---|
117 | static FILE *SetStreamOut(FILE *f);
|
---|
118 | static FILE *SetStreamIn(FILE *f);
|
---|
119 |
|
---|
120 | // Other global readline variables
|
---|
121 | static std::string GetPrompt();
|
---|
122 | static std::string GetBuffer();
|
---|
123 | static int GetAbsCursor();
|
---|
124 | static int GetCursor();
|
---|
125 | static int GetBufferLength();
|
---|
126 | static int GetLineLength();
|
---|
127 |
|
---|
128 | // Screen size
|
---|
129 | static void Resize();
|
---|
130 | static void Resize(int w, int h);
|
---|
131 | int GetCols() const;
|
---|
132 | int GetRows() const;
|
---|
133 |
|
---|
134 | static Readline *Instance() { return This; }
|
---|
135 | };
|
---|
136 |
|
---|
137 | #endif
|
---|