Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
CliRunnable.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
9
10#include "Common.h"
12#include "ObjectMgr.h"
13#include "World.h"
14#include "WorldSession.h"
15
16#include "AccountMgr.h"
17#include "Chat.h"
18#include "CliRunnable.h"
19#include "Language.h"
20#include "Log.h"
21#include "MapManager.h"
22#include "Player.h"
23#include "Util.h"
24
25#if PLATFORM != PLATFORM_WINDOWS
26#include <readline/readline.h>
27#include <readline/history.h>
28
29char* command_finder(const char* text, int state)
30{
31 static size_t idx, len;
32 const char* ret;
33 std::vector<ChatCommand> const& cmd = ChatHandler::getCommandTable();
34
35 if (!state)
36 {
37 idx = 0;
38 len = strlen(text);
39 }
40
41 while (idx < cmd.size())
42 {
43 ret = cmd[idx].Name;
44 if (!cmd[idx].AllowConsole)
45 {
46 ++idx;
47 continue;
48 }
49
50 ++idx;
51 //printf("Checking %s \n", cmd[idx].Name);
52 if (strncmp(ret, text, len) == 0)
53 return strdup(ret);
54 }
55
56 return ((char*)NULL);
57}
58
59char** cli_completion(const char* text, int start, int /*end*/)
60{
61 char** matches = NULL;
62
63 if (start)
64 rl_bind_key('\t', rl_abort);
65 else
66 matches = rl_completion_matches((char*)text, &command_finder);
67 return matches;
68}
69
71{
72 if (World::IsStopped())
73 rl_done = 1;
74 return 0;
75}
76
77#endif
78
79void utf8print(void* /*arg*/, const char* str)
80{
81#if PLATFORM == PLATFORM_WINDOWS
82 wchar_t wtemp_buf[6000];
83 size_t wtemp_len = 6000 - 1;
84 if (!Utf8toWStr(str, strlen(str), wtemp_buf, wtemp_len))
85 return;
86
87 char temp_buf[6000];
88 CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len + 1);
89 printf(temp_buf);
90#else
91 {
92 printf("%s", str);
93 fflush(stdout);
94 }
95#endif
96}
97
98void commandFinished(void*, bool /*success*/)
99{
100 printf("SF> ");
101 fflush(stdout);
102}
103
106{
108 //SF_LOG_INFO("server.worldserver", "");
109#if PLATFORM != PLATFORM_WINDOWS
110 rl_attempted_completion_function = cli_completion;
111 rl_event_hook = cli_hook_func;
112#endif
113
114 if (sConfigMgr->GetBoolDefault("BeepAtStart", true))
115 printf("\a"); // \a = Alert
116
117 // print this here the first time
118 // later it will be printed after command queue updates
119 printf("SF>");
120
122 while (!World::IsStopped())
123 {
124 fflush(stdout);
125
126 char* command_str; // = fgets(commandbuf, sizeof(commandbuf), stdin);
127
128#if PLATFORM == PLATFORM_WINDOWS
129 char commandbuf[256];
130 command_str = fgets(commandbuf, sizeof(commandbuf), stdin);
131#else
132 command_str = readline("SF>");
133 rl_bind_key('\t', rl_complete);
134#endif
135
136 if (command_str != NULL)
137 {
138 for (int x = 0; command_str[x]; ++x)
139 if (command_str[x] == '\r' || command_str[x] == '\n')
140 {
141 command_str[x] = 0;
142 break;
143 }
144
145 if (!*command_str)
146 {
147#if PLATFORM == PLATFORM_WINDOWS
148 printf("SF>");
149#else
150 free(command_str);
151#endif
152 continue;
153 }
154
155 std::string command;
156 if (!consoleToUtf8(command_str, command)) // convert from console encoding to utf8
157 {
158#if PLATFORM == PLATFORM_WINDOWS
159 printf("SF>");
160#else
161 free(command_str);
162#endif
163 continue;
164 }
165
166 fflush(stdout);
167 sWorld->QueueCliCommand(new CliCommandHolder(NULL, command.c_str(), &utf8print, &commandFinished));
168#if PLATFORM != PLATFORM_WINDOWS
169 add_history(command.c_str());
170 free(command_str);
171#endif
172 }
173 else if (feof(stdin))
174 {
176 }
177 }
178}
#define sConfigMgr
Definition Config.h:64
bool consoleToUtf8(const std::string &conStr, std::string &utf8str)
Definition Util.cpp:458
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition Util.cpp:305
static std::vector< ChatCommand > const & getCommandTable()
Definition Chat.cpp:29
static void StopNow(uint8 exitcode)
Definition World.h:674
static bool IsStopped()
Definition World.h:675
void Run()
Thread start
void utf8print(void *, const char *str)
char ** cli_completion(const char *text, int start, int)
char * command_finder(const char *text, int state)
int cli_hook_func()
void commandFinished(void *, bool)
#define sWorld
Definition World.h:910
@ SHUTDOWN_EXIT_CODE
Definition World.h:64
Storage class for commands issued for delayed execution.
Definition World.h:524