- Quit gracefully when Ctrl-D or Ctrl-C is pressed

- Remove "exit" and "quit" as quit commands
This commit is contained in:
Tiiffi
2021-10-30 22:16:29 +03:00
parent b3147ebe43
commit fca278e092

View File

@ -24,8 +24,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include <signal.h> #include <signal.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
@ -123,8 +123,11 @@ void exit_proc(void)
} }
// Check windows & linux behaviour !!! // Check windows & linux behaviour !!!
void sighandler(/*int sig*/) void sighandler(int sig)
{ {
if (sig == SIGINT)
putchar('\n');
global_connection_alive = 0; global_connection_alive = 0;
#ifndef _WIN32 #ifndef _WIN32
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -242,10 +245,7 @@ int main(int argc, char *argv[])
exit_code = EXIT_FAILURE; exit_code = EXIT_FAILURE;
} }
net_close(global_rsock); exit(exit_code);
global_rsock = -1;
return exit_code;
} }
void usage(void) void usage(void)
@ -433,6 +433,7 @@ rc_packet *net_recv_packet(int sd)
return NULL; return NULL;
} }
// NOTE(Tiiffi): This should fail if size is out of spec!
if (psize < 10 || psize > DATA_BUFFSIZE) { if (psize < 10 || psize > DATA_BUFFSIZE) {
fprintf(stderr, "Warning: invalid packet size (%d). Must over 10 and less than %d.\n", psize, DATA_BUFFSIZE); fprintf(stderr, "Warning: invalid packet size (%d). Must over 10 and less than %d.\n", psize, DATA_BUFFSIZE);
@ -654,20 +655,18 @@ int run_terminal_mode(int sock)
int ret = 0; int ret = 0;
char command[DATA_BUFFSIZE] = {0x00}; char command[DATA_BUFFSIZE] = {0x00};
puts("Logged in. Type 'quit' or 'exit' to quit."); puts("Logged in.\nType 'Q' or press Ctrl-D / Ctrl-C to disconnect.");
while (global_connection_alive) { while (global_connection_alive) {
putchar('>'); putchar('>');
int len = get_line(command, DATA_BUFFSIZE); int len = get_line(command, DATA_BUFFSIZE);
if (len < 1) continue;
if ((strcasecmp(command, "exit") && strcasecmp(command, "quit")) == 0) if (strcasecmp(command, "Q") == 0)
break; break;
if(command[0] == 'Q' && command[1] == 0) if (len > 0 && global_connection_alive)
break;
if(len > 0 && global_connection_alive)
ret += rcon_command(sock, command); ret += rcon_command(sock, command);
/* Special case for "stop" command to prevent server-side bug. /* Special case for "stop" command to prevent server-side bug.
@ -681,7 +680,7 @@ int run_terminal_mode(int sock)
break; break;
} }
command[0] = len = 0; //command[0] = len = 0;
} }
return ret; return ret;
@ -691,11 +690,14 @@ int run_terminal_mode(int sock)
int get_line(char *buffer, int bsize) int get_line(char *buffer, int bsize)
{ {
char *ret = fgets(buffer, bsize, stdin); char *ret = fgets(buffer, bsize, stdin);
if (ret == NULL) if (ret == NULL) {
exit(EXIT_FAILURE); if (ferror(stdin)) {
fprintf(stderr, "Error %d: %s\n", errno, strerror(errno));
if (buffer[0] == 0) exit(EXIT_FAILURE);
global_connection_alive = 0; }
putchar('\n');
exit(EXIT_SUCCESS);
}
// remove unwanted characters from the buffer // remove unwanted characters from the buffer
buffer[strcspn(buffer, "\r\n")] = '\0'; buffer[strcspn(buffer, "\r\n")] = '\0';