diff options
author | raczman <none@none> | 2009-06-08 20:19:13 +0200 |
---|---|---|
committer | raczman <none@none> | 2009-06-08 20:19:13 +0200 |
commit | 5ae74c5b589ff6530d0258bb77d32f4cba03020e (patch) | |
tree | 9016707a580b4e13388f1dab13dbaeca9362da84 | |
parent | c7c092f54bc6aa988991c601d532322c5cb8df32 (diff) |
Added readline support into CLI.
Supports scrolling through past commands,
and tab-completion for commands (no subcommands, sorry).
Requires libreadline package to build from now on!
for debian/ubuntu: apt-get install libreadline5-dev
Linux only, windows users shouldnt notice any difference.
--HG--
branch : trunk
-rw-r--r-- | src/game/Chat.h | 4 | ||||
-rw-r--r-- | src/trinitycore/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/trinitycore/CliRunnable.cpp | 88 |
3 files changed, 75 insertions, 18 deletions
diff --git a/src/game/Chat.h b/src/game/Chat.h index a8dbb5b98a5..cb5070a354d 100644 --- a/src/game/Chat.h +++ b/src/game/Chat.h @@ -73,7 +73,7 @@ class ChatHandler int ParseCommands(const char* text); virtual char const* GetName() const; - + static ChatCommand* getCommandTable(); protected: explicit ChatHandler() : m_session(NULL) {} // for CLI subclass @@ -89,8 +89,6 @@ class ChatHandler bool ShowHelpForCommand(ChatCommand *table, const char* cmd); bool ShowHelpForSubCommands(ChatCommand *table, char const* cmd, char const* subcmd); - ChatCommand* getCommandTable(); - bool HandleAccountCommand(const char* args); bool HandleAccountCreateCommand(const char* args); bool HandleAccountDeleteCommand(const char* args); diff --git a/src/trinitycore/CMakeLists.txt b/src/trinitycore/CMakeLists.txt index 2c7c4544577..0a9801c5a19 100644 --- a/src/trinitycore/CMakeLists.txt +++ b/src/trinitycore/CMakeLists.txt @@ -44,6 +44,7 @@ trinityconfig vmaps ZThread g3dlite +readline ${SCRIPT_LIB} ${MYSQL_LIBRARIES} ${POSTGRE_LIBS} diff --git a/src/trinitycore/CliRunnable.cpp b/src/trinitycore/CliRunnable.cpp index 839358e7034..179ee4db80b 100644 --- a/src/trinitycore/CliRunnable.cpp +++ b/src/trinitycore/CliRunnable.cpp @@ -37,6 +37,56 @@ #include "Player.h" #include "Chat.h" +#if PLATFORM != WINDOWS +#include <readline/readline.h> +#include <readline/history.h> + +char * command_finder(const char* text, int state) +{ + static int idx,len; + const char* ret; + ChatCommand *cmd = ChatHandler::getCommandTable(); + + if(!state) + { + idx = 0; + len = strlen(text); + } + + while(ret = cmd[idx].Name) + { + if(!cmd[idx].AllowConsole) + { + idx++; + continue; + } + + idx++; + //printf("Checking %s \n", cmd[idx].Name); + if (strncmp(ret, text, len) == 0) + return strdup(ret); + if(cmd[idx].Name == NULL) + break; + } + + return ((char*)NULL); + +} + +char ** cli_completion(const char * text, int start, int end) +{ + char ** matches; + matches = (char**)NULL; + + if(start == 0) + matches = rl_completion_matches((char*)text,&command_finder); + else + rl_bind_key('\t',rl_abort); + return (matches); +} + +#endif + void utf8print(const char* str) { #if PLATFORM == PLATFORM_WINDOWS @@ -316,10 +366,10 @@ void CliRunnable::run() WorldDatabase.ThreadStart(); // let thread do safe mySQL requests char commandbuf[256]; - + bool canflush = true; ///- Display the list of available CLI functions then beep sLog.outString(); - + rl_attempted_completion_function = cli_completion; if(sConfig.GetBoolDefault("BeepAtStart", true)) printf("\a"); // \a = Alert @@ -331,15 +381,16 @@ void CliRunnable::run() while (!World::IsStopped()) { fflush(stdout); - #ifdef linux - while (!kb_hit_return() && !World::IsStopped()) - // With this, we limit CLI to 10commands/second - usleep(100); - if (World::IsStopped()) - break; - #endif - char *command_str = fgets(commandbuf,sizeof(commandbuf),stdin); - if (command_str != NULL) + + char *command_str ; // = fgets(commandbuf,sizeof(commandbuf),stdin); + + #if PLATFORM == WINDOWS + command_str = fgets(commandbuf,sizeof(commandbuf),stdin); + #else + command_str = readline("TC>"); + rl_bind_key('\t',rl_complete); + #endif + if (command_str != NULL) { for(int x=0;command_str[x];x++) if(command_str[x]=='\r'||command_str[x]=='\n') @@ -351,23 +402,30 @@ void CliRunnable::run() if(!*command_str) { - printf("TC>"); + #if PLATFORM == WINDOWS + printf("TC>"); + #endif continue; } std::string command; if(!consoleToUtf8(command_str,command)) // convert from console encoding to utf8 { - printf("TC>"); + #if PLATFORM == WINDOWS + printf("TC>"); + #endif continue; } - + fflush(stdout); sWorld.QueueCliCommand(&utf8print,command.c_str()); - } + add_history(command.c_str()); + + } else if (feof(stdin)) { World::StopNow(SHUTDOWN_EXIT_CODE); } + } ///- End the database thread |