FACT++  1.0
Readline.h
Go to the documentation of this file.
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:
19  static Readline *This;
20 
21 private:
22  std::string fName;
23  int fMaxLines;
24 
25  std::ofstream fCommandLog;
26 
27  std::string fLastLine;
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 
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 
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 
59  virtual char *Complete(const char* text, int state);
60  virtual char **Completion(const char *text, int start, int end);
61 
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 
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;
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
int start(int initState)
Definition: feeserver.c:1740
int GetLine() const
Definition: Readline.h:121
static char * Compare(const std::string &str, const std::string &txt)
Definition: Readline.cc:409
void UpdatePrompt() const
Definition: Readline.h:98
static void Resize()
Definition: Readline.cc:816
virtual ~Readline()
Definition: Readline.cc:167
static Readline * This
Definition: Readline.h:19
virtual void Redisplay()
Default: call rl_redisplay()
Definition: Readline.cc:362
static std::string StaticPrompt(const std::string &prompt)
Definition: Readline.cc:1346
static char * CompleteImp(const char *text, int state)
Calls Complete()
Definition: Readline.cc:500
const std::vector< std::string > * fCompletion
Pointer to a list of possible matched for auto-completion.
Definition: Readline.h:63
void SetCompletion(const std::vector< std::string > *v)
Definition: Readline.h:64
static int rl_ncurses_event_hook()
Definition: Readline.cc:260
virtual bool PrintCommands()
Definition: Readline.cc:962
static int GetLineLength()
return the length of the prompt plus the length of the line buffer
Definition: Readline.cc:806
static bool DumpHistory()
Definition: Readline.cc:897
static int fScriptDepth
Definition: Readline.h:32
char str[80]
Definition: test_client.c:7
static int GetScriptDepth()
Definition: Readline.h:114
int GetRows() const
Get the number of rows readline assumes the screen size to be.
Definition: Readline.cc:852
static int GetBufferLength()
Definition: Readline.cc:797
static bool RedirectionWrapper(std::ostream &out, bool(*function)())
Definition: Readline.cc:192
virtual bool Process(const std::string &str)
Definition: Readline.cc:1094
Readline(const char *prgname)
Definition: Readline.cc:105
std::ofstream fCommandLog
Maximum number of lines in the history file.
Definition: Readline.h:25
void BindKeySequence(const char *seq, int(*func)(int, int))
Definition: Readline.cc:648
int fMaxLines
Filename for the history file compiled in the constructor.
Definition: Readline.h:23
virtual std::string GetUpdatePrompt() const
Definition: Readline.h:102
static void Stop()
Definition: Readline.cc:1534
static char ** CompletionMatches(const char *text, char *(*func)(const char *, int))
Definition: Readline.cc:416
virtual void PrintReadlineError(const std::string &str)
Definition: Readline.cc:1553
static int rl_ncurses_startup()
Definition: Readline.cc:237
virtual void SetSection(int)
Definition: Readline.h:68
static void StaticPopHistory(const std::string &fname)
Definition: Readline.cc:1322
std::vector< const char * > GetHistory() const
Return a list of pointer to the history contents.
Definition: Readline.cc:863
virtual std::string Prompt(const std::string &prompt)
Definition: Readline.cc:1281
virtual bool ExecuteShellCommand(const std::string &cmd)
Definition: Readline.cc:920
virtual void Shutdown(const char *buf)
Definition: Readline.cc:353
std::string fName
Definition: Readline.h:22
static std::string GetBuffer()
Definition: Readline.cc:768
static void StaticPushHistory(const std::string &fname)
Definition: Readline.cc:1303
virtual void Run(const char *prompt=0)
Definition: Readline.cc:1406
static bool DumpFunctions()
Definition: Readline.cc:687
static bool IsScriptStopped()
Definition: Readline.h:113
static std::string fExternalInput
Definition: Readline.h:35
static void SetScriptDepth(unsigned int d)
Definition: Readline.h:115
static std::string GetPrompt()
Definition: Readline.cc:751
std::string fLastLine
Definition: Readline.h:27
int Execute(const std::string &fname, const std::map< std::string, std::string > &args=std::map< std::string, std::string >())
Definition: Readline.cc:1446
int GetCols() const
Get the number of cols readline assumes the screen size to be.
Definition: Readline.cc:841
static char ** rl_ncurses_completion_function(const char *text, int start, int end)
Definition: Readline.cc:286
static void StopScript()
Definition: Readline.h:112
virtual void Startup()
Default: Do nothing.
Definition: Readline.cc:304
int fSection
Definition: Readline.h:30
double end
static void SetExternalInput(const std::string &inp)
Definition: Readline.h:116
static void rl_ncurses_completion_display(char **matches, int num, int max)
Definition: Readline.cc:281
static int GetAbsCursor()
return strlen(rl_display_prompt) + rl_point
Definition: Readline.cc:786
static std::string GetExternalInput()
Definition: Readline.h:119
virtual void CompletionDisplay(char **matches, int num, int max)
Default: call rl_completion_display_matches()
Definition: Readline.cc:386
virtual char ** Completion(const char *text, int start, int end)
Definition: Readline.cc:541
static int rl_ncurses_getc(FILE *)
Static member function which are used to adapt readline to ncurses.
Definition: Readline.cc:227
static std::string fScript
Definition: Readline.h:48
virtual char * Complete(const char *text, int state)
Functions dealing with auto completion.
Definition: Readline.cc:477
static Readline * Instance()
Definition: Readline.h:146
virtual bool PromptEOF(std::string &str)
Definition: Readline.cc:1250
static bool ClearHistory()
Definition: Readline.cc:882
void SetLabel(int l)
Definition: Readline.h:111
void ProcessLine(const std::string &str)
Definition: Readline.cc:1370
int fLine
Last line adde to history.
Definition: Readline.h:29
virtual int Getc(FILE *)
The non static implementations of the callback funtions above.
Definition: Readline.cc:295
static int GetCursor()
return rl_point (the current cursor position within the line buffer)
Definition: Readline.cc:777
std::string GetName() const
Definition: Readline.h:88
static FILE * SetStreamIn(FILE *f)
Definition: Readline.cc:739
virtual bool PreProcess(const std::string &str)
Definition: Readline.cc:1049
bool IsStopped() const
Definition: Readline.cc:1548
virtual void EventHook(bool newline=false)
Definition: Readline.cc:315
void SetMaxSize(int lines)
Definition: Readline.h:94
C++ wrapper for GNU&#39;s readline library.
Definition: Readline.h:10
static bool DumpFunmap()
Definition: Readline.cc:704
static std::string GetScript()
Definition: Readline.h:118
void AddToHistory(const std::string &str, int skip=2)
Definition: Readline.cc:562
static bool DumpVariables()
Definition: Readline.cc:667
static void rl_ncurses_redisplay()
Definition: Readline.cc:249
virtual std::string GetLinePrompt() const
Definition: Readline.cc:592
int fLabel
Definition: Readline.h:31
virtual bool PrintGeneralHelp()
Definition: Readline.cc:983
static FILE * SetStreamOut(FILE *f)
Definition: Readline.cc:721
virtual bool PrintKeyBindings()
Definition: Readline.cc:1025
static bool fStopScript
Definition: Readline.h:33