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