-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Console.c
58 lines (49 loc) · 1.49 KB
/
Console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <Server/Commands/Commands.h>
#include <Server/Server.h>
#include <Server/Structs/ServerStruct.h>
#include <Util/Log.h>
#include <pthread.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdio.h>
volatile int ctrlc = 0;
void readline_new_line(int signal)
{
(void) signal; // To prevent a warning about unused variable
ctrlc = 1;
printf("\n");
LOG_INFO_WITHOUT_TIME("Are you sure you want to exit? (Y/n)");
if (write(STDIN_FILENO, "\n", sizeof("\n")) != sizeof("\n")) {
LOG_DEBUG("epic write fail");
}
rl_replace_line("", 0);
rl_on_new_line();
rl_redisplay();
}
void* server_console(void* arg)
{
(void) arg;
char* buf;
server_t* server = (server_t*) arg;
while ((buf = readline("\x1B[0;34mConsole> \x1B[0;37m")) != NULL) {
if (ctrlc) { // "Are you sure?" prompt
if (strcmp("n", buf) != 0) {
break;
}
ctrlc = 0;
} else if (strlen(buf) > 0) {
add_history(buf);
pthread_mutex_lock(&server_lock);
command_handle(server, 255, buf, 1);
pthread_mutex_unlock(&server_lock);
}
free(buf);
}
rl_clear_history();
stop_server();
sleep(5); // wait 5 seconds for the server to stop
// if this thread is not dead at this point, then we need to stop the server by force >:)
LOG_ERROR("Server did not respond for 5 seconds. Killing it with fire...");
exit(-1);
return 0;
}