diff --git a/Makefile b/Makefile index c755128..051b09c 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ EXENAME = mcrcon PREFIX ?= /usr/local INSTALL = install -LINKER = +LINKER = -lreadline RM = rm -v -f CC = gcc diff --git a/README.md b/README.md index 9961fab..f6e8570 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ See https://pkgs.org/download/mcrcon for available packages in various Linux dis ##### building from sources: ```sh git clone https://github.com/Tiiffi/mcrcon.git +sudo apt install libreadline-dev cd mcrcon make sudo make install diff --git a/mcrcon.c b/mcrcon.c index 79c8aba..76e8823 100644 --- a/mcrcon.c +++ b/mcrcon.c @@ -45,6 +45,8 @@ #include #include #include + #include + #include #endif #define VERSION "0.7.2" @@ -236,7 +238,7 @@ int main(int argc, char *argv[]) // auth & commands if (rcon_auth(global_rsock, pass)) { if (terminal_mode) - run_terminal_mode(global_rsock); + exit_code = run_terminal_mode(global_rsock); else exit_code = run_commands(argc, argv); } @@ -605,6 +607,8 @@ int rcon_auth(int sock, char *passwd) int rcon_command(int sock, char *command) { + if (strlen(command) < 1) return 1; + rc_packet *packet = packet_build(RCON_PID, RCON_EXEC_COMMAND, command); if (packet == NULL) { global_connection_alive = 0; @@ -653,21 +657,30 @@ int run_commands(int argc, char *argv[]) int run_terminal_mode(int sock) { int ret = 0; - char command[DATA_BUFFSIZE] = {0x00}; + char *command = NULL; puts("Logged in.\nType 'Q' or press Ctrl-D / Ctrl-C to disconnect."); - while (global_connection_alive) { - putchar('>'); + using_history(); - int len = get_line(command, DATA_BUFFSIZE); - if (len < 1) continue; + while (global_connection_alive) { + command = readline(""); + int len = strlen(command); + if (len < 1) { + free(command); + command = NULL; + continue; + } - if (strcasecmp(command, "Q") == 0) + if (strcasecmp(command, "Q") == 0) { + ret = 1; break; + } - if (len > 0 && global_connection_alive) - ret += rcon_command(sock, command); + if (len > 0 && global_connection_alive) { + add_history(command); + ret = rcon_command(sock, command); + } /* Special case for "stop" command to prevent server-side bug. * https://bugs.mojang.com/browse/MC-154617 @@ -680,10 +693,11 @@ int run_terminal_mode(int sock) break; } - //command[0] = len = 0; + free(command); + command = NULL; } - return ret; + return ret ? EXIT_SUCCESS : EXIT_FAILURE; } // gets line from stdin and deals with rubbish left in the input buffer