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