62 #include <sys/ioctl.h> 63 #include <readline/readline.h> 64 #include <readline/history.h> 66 #include <boost/version.hpp> 67 #include <boost/filesystem.hpp> 72 namespace fs = boost::filesystem;
106 fMaxLines(500), fLine(0), fSection(-4), fLabel(-1), fCompletion(0)
110 cout <<
"ERROR - Readline can only be instatiated once!" << endl;
120 #if BOOST_VERSION < 104600 121 static const string fname = boost::filesystem::path(prgname).filename();
123 static const string fname = boost::filesystem::path(prgname).filename().string();
125 rl_readline_name = fname.c_str();
128 fName = string(prgname)+
".his";
131 read_history(
fName.c_str());
146 rl_bind_keyseq(
"\e[1~", rl_named_function(
"beginning-of-line"));
147 rl_bind_keyseq(
"\e[3~", rl_named_function(
"delete-char"));
148 rl_bind_keyseq(
"\e[4~", rl_named_function(
"end-of-line"));
149 rl_bind_keyseq(
"\e[5~", rl_named_function(
"history-search-backward"));
150 rl_bind_keyseq(
"\e[6~", rl_named_function(
"history-search-forward"));
151 rl_bind_keyseq(
"\033[1;3F", rl_named_function(
"kill-line"));
152 rl_bind_keyseq(
"\033[1;5D", rl_named_function(
"backward-word"));
153 rl_bind_keyseq(
"\033[1;5C", rl_named_function(
"forward-word"));
154 rl_bind_key(25, rl_named_function(
"kill-whole-line"));
170 if (write_history(
fName.c_str()))
171 cout <<
"WARNING - Write " <<
fName.c_str() <<
": " << strerror(errno) << endl;
175 cout <<
"WARNING - Truncate " <<
fName.c_str() <<
": " << strerror(errno) << endl;
195 const bool rc =
function();
198 const bool empty = ftell(file)==0;
204 out <<
" <empty>" << endl;
211 const int c = getc(file);
322 rl_replace_line(cpy.c_str(), 1);
333 if (rl_prompt==p && !newline)
339 rl_get_screen_size(&h, &w);
340 cout <<
'\r' << string(w+1,
' ') <<
'\r';
341 rl_forced_update_display();
364 static int W=-1, H=-1;
367 rl_get_screen_size(&h, &w);
374 cout <<
'\r' << string(w+1,
' ') <<
'\r';
379 rl_forced_update_display();
388 rl_display_match_list(matches, num, max);
389 rl_forced_update_display();
412 return strncasecmp(str.c_str(), txt.c_str(), txt.length())==0 ?
413 strndup(str.c_str(), str.length()) : 0;
418 return rl_completion_matches(text, func);
444 char **rc = rl_completion_matches(const_cast<char*>(text),
CompleteImp);
480 return rl_filename_completion_function(text, state);
482 static vector<string>::const_iterator pos;
488 char *rc =
Compare(*pos++, text);
544 return rl_completion_matches((
char*)text,
CompleteImp);
573 p = history_search_pos(str.c_str(), 0, p+1);
577 HIST_ENTRY *e = remove_history(p--);
583 add_history(str.c_str());
595 str <<
'[' <<
fLine <<
']';
610 rl_set_prompt(prompt.c_str());
650 rl_bind_keyseq(seq, func);
669 rl_variable_dumper(1);
689 rl_function_dumper(1);
706 rl_list_funmap_names();
723 FILE *rc = rl_outstream;
741 FILE *rc = rl_instream;
753 return rl_display_prompt;
770 return string(rl_line_buffer, rl_end);
788 return strlen(rl_display_prompt) + rl_point;
808 return strlen(rl_display_prompt) + rl_end;
818 rl_resize_terminal();
834 rl_set_screen_size(height, width);
844 rl_get_screen_size(&rows, &cols);
855 rl_get_screen_size(&rows, &cols);
865 HIST_ENTRY **next = history_list();
867 vector<const char*> v;
869 for (; *next; next++)
870 v.push_back((*next)->line);
899 HIST_ENTRY **next = history_list();
904 for (; *next; next++)
905 fprintf(rl_outstream,
"%s\n", (*next)->line);
922 FILE *pipe = popen(cmd.c_str(),
"r");
925 fprintf(rl_outstream,
"ERROR - Could not create pipe '%s': %m\n", cmd.c_str());
933 const size_t sz = fread(buf, 1, 1024, pipe);
935 fwrite(buf, 1, sz, rl_outstream);
937 if (feof(pipe) || ferror(pipe))
942 fprintf(rl_outstream,
"ERROR - Reading from pipe '%s': %m\n", cmd.c_str());
946 fprintf(rl_outstream,
"\n");
964 fprintf(rl_outstream,
"\n");
965 fprintf(rl_outstream,
" Commands:\n");
966 fprintf(rl_outstream,
" No application specific commands defined.\n");
967 fprintf(rl_outstream,
"\n");
985 fprintf(rl_outstream,
"\n");
986 fprintf(rl_outstream,
" General help:\n");
987 fprintf(rl_outstream,
" h,help Print this help message\n");
988 fprintf(rl_outstream,
" clear Clear history buffer\n");
989 fprintf(rl_outstream,
" lh,history Dump the history buffer to the screen\n");
990 fprintf(rl_outstream,
" v,variables Dump readline variables\n");
991 fprintf(rl_outstream,
" f,functions Dump readline functions\n");
992 fprintf(rl_outstream,
" m,funmap Dump readline funmap\n");
993 fprintf(rl_outstream,
" c,commands Dump available commands\n");
994 fprintf(rl_outstream,
" k,keylist Dump key bindings\n");
995 fprintf(rl_outstream,
" .! command Execute a shell command\n");
996 fprintf(rl_outstream,
" .w n Sleep n milliseconds\n");
997 fprintf(rl_outstream,
" .x file .. Execute a script of commands (+optional argumnets)\n");
998 fprintf(rl_outstream,
" .x file:N .. Execute a script of commands, start at label N\n");
999 fprintf(rl_outstream,
" .j N Forward jump to label N\n");
1000 fprintf(rl_outstream,
" .lt f0 f1 N If float f0 lower than float f1, jump to label N\n");
1001 fprintf(rl_outstream,
" .gt f0 f1 N If float f0 greater than float f1, jump to label N\n");
1002 fprintf(rl_outstream,
" .eq i0 i1 N If int i0 equal int i1, jump to label N\n");
1003 fprintf(rl_outstream,
" : N Defines a label (N=number)\n");
1004 fprintf(rl_outstream,
" # comment Ignored\n");
1005 fprintf(rl_outstream,
" .q,quit Quit\n");
1006 fprintf(rl_outstream,
"\n");
1007 fprintf(rl_outstream,
" The command history is automatically loaded and saves to\n");
1008 fprintf(rl_outstream,
" and from %s.\n",
GetName().c_str());
1009 fprintf(rl_outstream,
"\n");
1027 fprintf(rl_outstream,
"\n");
1028 fprintf(rl_outstream,
" Key bindings:\n");
1029 fprintf(rl_outstream,
" Page-up Search backward in history\n");
1030 fprintf(rl_outstream,
" Page-dn Search forward in history\n");
1031 fprintf(rl_outstream,
" Ctrl-left One word backward\n");
1032 fprintf(rl_outstream,
" Ctrl-right One word forward\n");
1033 fprintf(rl_outstream,
" Home Beginning of line\n");
1034 fprintf(rl_outstream,
" End End of line\n");
1035 fprintf(rl_outstream,
" Ctrl-d Quit\n");
1036 fprintf(rl_outstream,
" Ctrl-y Delete line\n");
1037 fprintf(rl_outstream,
" Alt-end/Ctrl-k Delete until the end of the line\n");
1038 fprintf(rl_outstream,
" F1 Toggle visibility of upper panel\n");
1039 fprintf(rl_outstream,
"\n");
1040 fprintf(rl_outstream,
" Default key-bindings are identical with your bash.\n");
1041 fprintf(rl_outstream,
"\n");
1063 catch (
const logic_error &e)
1080 if (str.substr(0, 3)==
".j ")
1082 fLabel = atoi(str.substr(3).c_str());
1098 if (str.substr(0, 3)==
".w ")
1100 usleep(stoi(str.substr(3))*1000);
1104 if (str.substr(0, 3)==
".x ")
1106 string opt(str.substr(3));
1132 if (str.substr(0, 2)==
".!")
1138 if (str.substr(0, 4)==
".gt ")
1140 istringstream in(str.substr(4));
1145 in >> v0 >> v1 >> label;
1159 if (str.substr(0, 4)==
".lt ")
1161 istringstream in(str.substr(4));
1166 in >> v0 >> v1 >> label;
1180 if (str.substr(0, 4)==
".eq ")
1182 istringstream in(str.substr(4));
1186 in >> v0 >> v1 >> label;
1206 if (str==
"lh" || str==
"history")
1209 if (str==
"v" || str==
"variables")
1212 if (str==
"f" || str==
"functions")
1215 if (str==
"m" || str==
"funmap")
1220 if (str==
"h" || str==
"help")
1223 if (str==
"c" || str==
"commands")
1226 if (str==
"k" || str==
"keylist")
1252 char *buf = readline(str.c_str());
1283 char *buf = readline(prompt.c_str());
1287 const string str = !buf || (rl_done && rl_pending_input==4)
1305 fs::path his = fs::path(
This->
fName).parent_path();
1311 read_history(his.string().c_str());
1324 fs::path his = fs::path(
This->
fName).parent_path();
1327 write_history(his.string().c_str());
1328 history_truncate_file(his.string().c_str(), 1000);
1348 char *buf = readline(prompt.c_str());
1352 const string str(buf);
1354 if (history_length==0 || history_search_pos(str.c_str(), -1, history_length-1)!=history_length-1)
1417 if (str==
"quit" || str==
".q")
1461 const size_t p = name.find_last_of(
':');
1462 if (p!=string::npos)
1464 fLabel = atoi(name.substr(p+1).c_str());
1465 name = name.substr(0, p);
1468 ifstream fin(name.c_str());
1490 while (getline(fin, buffer,
'\n') && !
fStopScript)
1498 if (buffer==
"quit" || buffer==
".q")
1505 for (
auto it=args.begin(); it!=args.end(); it++)
1507 const string find =
"${"+it->first+
"}";
1508 for (
size_t pos=0; (pos=buffer.find(find, pos))!=string::npos; pos+=find.length())
1509 buffer.replace(pos, find.size(), it->second);
1516 fCommandLog <<
"# " <<
Time() <<
" - " << name <<
" (FINISHED[" << fScriptDepth<<
"])" << endl;
1518 if (--fScriptDepth==0)
1537 rl_pending_input = 4;
1550 return rl_done==1 && rl_pending_input==4;
1555 fprintf(rl_outstream,
"%s\n", str.c_str());
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0
static char * Compare(const std::string &str, const std::string &txt)
void UpdatePrompt() const
virtual void Redisplay()
Default: call rl_redisplay()
static std::string StaticPrompt(const std::string &prompt)
static char * CompleteImp(const char *text, int state)
Calls Complete()
const std::vector< std::string > * fCompletion
Pointer to a list of possible matched for auto-completion.
static int rl_ncurses_event_hook()
virtual bool PrintCommands()
static int GetLineLength()
return the length of the prompt plus the length of the line buffer
static bool DumpHistory()
Adds some functionality to boost::posix_time::ptime for our needs.
int GetRows() const
Get the number of rows readline assumes the screen size to be.
static int GetBufferLength()
static bool RedirectionWrapper(std::ostream &out, bool(*function)())
virtual bool Process(const std::string &str)
Readline(const char *prgname)
std::ofstream fCommandLog
Maximum number of lines in the history file.
void BindKeySequence(const char *seq, int(*func)(int, int))
int fMaxLines
Filename for the history file compiled in the constructor.
virtual std::string GetUpdatePrompt() const
static char ** CompletionMatches(const char *text, char *(*func)(const char *, int))
virtual void PrintReadlineError(const std::string &str)
static int rl_ncurses_startup()
virtual void SetSection(int)
static void StaticPopHistory(const std::string &fname)
std::vector< const char * > GetHistory() const
Return a list of pointer to the history contents.
virtual std::string Prompt(const std::string &prompt)
virtual bool ExecuteShellCommand(const std::string &cmd)
virtual void Shutdown(const char *buf)
static std::string GetBuffer()
static void StaticPushHistory(const std::string &fname)
virtual void Run(const char *prompt=0)
static bool DumpFunctions()
static std::string fExternalInput
static std::string GetPrompt()
int Execute(const std::string &fname, const std::map< std::string, std::string > &args=std::map< std::string, std::string >())
int GetCols() const
Get the number of cols readline assumes the screen size to be.
static char ** rl_ncurses_completion_function(const char *text, int start, int end)
virtual void Startup()
Default: Do nothing.
static void rl_ncurses_completion_display(char **matches, int num, int max)
static int GetAbsCursor()
return strlen(rl_display_prompt) + rl_point
virtual void CompletionDisplay(char **matches, int num, int max)
Default: call rl_completion_display_matches()
virtual char ** Completion(const char *text, int start, int end)
static int rl_ncurses_getc(FILE *)
Static member function which are used to adapt readline to ncurses.
static std::string fScript
virtual char * Complete(const char *text, int state)
Functions dealing with auto completion.
virtual bool PromptEOF(std::string &str)
static bool ClearHistory()
void ProcessLine(const std::string &str)
int fLine
Last line adde to history.
virtual int Getc(FILE *)
The non static implementations of the callback funtions above.
static int GetCursor()
return rl_point (the current cursor position within the line buffer)
std::string GetName() const
static FILE * SetStreamIn(FILE *f)
virtual bool PreProcess(const std::string &str)
virtual void EventHook(bool newline=false)
C++ wrapper for GNU's readline library.
void AddToHistory(const std::string &str, int skip=2)
static bool DumpVariables()
static void rl_ncurses_redisplay()
virtual std::string GetLinePrompt() const
virtual bool PrintGeneralHelp()
static FILE * SetStreamOut(FILE *f)
virtual bool PrintKeyBindings()