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