Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ EXENAME = mcrcon
PREFIX ?= /usr/local

INSTALL = install
LINKER =
LINKER = -lreadline
RM = rm -v -f

CC = gcc
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 25 additions & 11 deletions mcrcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <readline/readline.h>
#include <readline/history.h>
#endif

#define VERSION "0.7.2"
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down