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