From 65b3353f57e2500dac88ed5f936ec5004b703052 Mon Sep 17 00:00:00 2001 From: Braden Young Date: Fri, 14 Apr 2023 11:18:27 -0700 Subject: [PATCH 1/3] Support input mapping modes for sockets --- src/socket.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/socket.c b/src/socket.c index 16d9621..89a10e3 100644 --- a/src/socket.c +++ b/src/socket.c @@ -41,6 +41,9 @@ static int sockfd; static int clientfds[MAX_SOCKET_CLIENTS]; static int socket_family = AF_UNSPEC; static int port_number = SOCKET_PORT_DEFAULT; +static bool map_i_nl_cr = false; +static bool map_i_cr_nl = false; +static bool map_ign_cr = false; static const char *socket_filename(void) { @@ -123,6 +126,9 @@ void socket_configure(void) struct sockaddr_in6 sockaddr_inet6 = {}; struct sockaddr *sockaddr_p; socklen_t socklen; + bool token_found = true; + char *token = NULL; + char *buffer; /* Parse socket string */ @@ -175,6 +181,41 @@ void socket_configure(void) exit(EXIT_FAILURE); } + /* Configure any specified input mappings */ + buffer = strdup(option.map); + while (token_found == true) + { + if (token == NULL) + { + token = strtok(buffer,","); + } + else + { + token = strtok(NULL, ","); + } + + if (token != NULL) + { + if (strcmp(token,"INLCR") == 0) + { + map_i_nl_cr = true; + } + else if (strcmp(token,"IGNCR") == 0) + { + map_ign_cr = true; + } + else if (strcmp(token,"ICRNL") == 0) + { + map_i_cr_nl = true; + } + } + else + { + token_found = false; + } + } + free(buffer); + /* Configure socket */ switch (socket_family) @@ -340,11 +381,26 @@ bool socket_handle_input(fd_set *rdfs, char *output_char) clientfds[i] = -1; continue; } - /* match the behavior of a terminal in raw mode */ - if (*output_char == '\n') + + /* If INLCR is set, a received NL character shall be translated into a CR character */ + if (*output_char == '\n' && map_i_nl_cr) { *output_char = '\r'; } + else if (*output_char == '\r') + { + /* If IGNCR is set, a received CR character shall be ignored (not read). */ + if (map_ign_cr) + { + return false; + } + + /* If IGNCR is not set and ICRNL is set, a received CR character shall be translated into an NL character. */ + if (map_i_cr_nl) + { + *output_char = '\n'; + } + } return true; } } From 5651c1c5d7f8303a9131232970f67f25815e62a4 Mon Sep 17 00:00:00 2001 From: Braden Young Date: Fri, 14 Apr 2023 13:08:48 -0700 Subject: [PATCH 2/3] Configure socket mapping flags from tty parsing logic. Remove duplicate parsing logic in socket --- src/socket.c | 44 +++----------------------------------------- src/socket.h | 4 ++++ src/tty.c | 3 +++ 3 files changed, 10 insertions(+), 41 deletions(-) diff --git a/src/socket.c b/src/socket.c index 89a10e3..1d7d15d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -41,9 +41,9 @@ static int sockfd; static int clientfds[MAX_SOCKET_CLIENTS]; static int socket_family = AF_UNSPEC; static int port_number = SOCKET_PORT_DEFAULT; -static bool map_i_nl_cr = false; -static bool map_i_cr_nl = false; -static bool map_ign_cr = false; +bool map_i_nl_cr = false; +bool map_i_cr_nl = false; +bool map_ign_cr = false; static const char *socket_filename(void) { @@ -126,9 +126,6 @@ void socket_configure(void) struct sockaddr_in6 sockaddr_inet6 = {}; struct sockaddr *sockaddr_p; socklen_t socklen; - bool token_found = true; - char *token = NULL; - char *buffer; /* Parse socket string */ @@ -181,41 +178,6 @@ void socket_configure(void) exit(EXIT_FAILURE); } - /* Configure any specified input mappings */ - buffer = strdup(option.map); - while (token_found == true) - { - if (token == NULL) - { - token = strtok(buffer,","); - } - else - { - token = strtok(NULL, ","); - } - - if (token != NULL) - { - if (strcmp(token,"INLCR") == 0) - { - map_i_nl_cr = true; - } - else if (strcmp(token,"IGNCR") == 0) - { - map_ign_cr = true; - } - else if (strcmp(token,"ICRNL") == 0) - { - map_i_cr_nl = true; - } - } - else - { - token_found = false; - } - } - free(buffer); - /* Configure socket */ switch (socket_family) diff --git a/src/socket.h b/src/socket.h index 2caffaf..53c343e 100644 --- a/src/socket.h +++ b/src/socket.h @@ -25,6 +25,10 @@ #include #include +extern bool map_i_nl_cr; +extern bool map_i_cr_nl; +extern bool map_ign_cr; + void socket_configure(void); void socket_write(char input_char); int socket_add_fds(fd_set *fds, bool connected); diff --git a/src/tty.c b/src/tty.c index 92bcb7e..9c4c8c1 100644 --- a/src/tty.c +++ b/src/tty.c @@ -987,14 +987,17 @@ void tty_configure(void) if (strcmp(token,"INLCR") == 0) { tio.c_iflag |= INLCR; + map_i_nl_cr = true; } else if (strcmp(token,"IGNCR") == 0) { tio.c_iflag |= IGNCR; + map_ign_cr = true; } else if (strcmp(token,"ICRNL") == 0) { tio.c_iflag |= ICRNL; + map_i_cr_nl = true; } else if (strcmp(token,"OCRNL") == 0) { From afc9e3be5b787787e99966a6d21485ce0930ceba Mon Sep 17 00:00:00 2001 From: Braden Young Date: Fri, 14 Apr 2023 15:00:03 -0700 Subject: [PATCH 3/3] Move map variables to tty to keep them all in one spot --- src/socket.c | 4 +--- src/socket.h | 4 ---- src/tty.c | 3 +++ src/tty.h | 3 +++ 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/socket.c b/src/socket.c index 1d7d15d..23b1249 100644 --- a/src/socket.c +++ b/src/socket.c @@ -33,6 +33,7 @@ #include "socket.h" #include "options.h" #include "print.h" +#include "tty.h" #define MAX_SOCKET_CLIENTS 16 #define SOCKET_PORT_DEFAULT 3333 @@ -41,9 +42,6 @@ static int sockfd; static int clientfds[MAX_SOCKET_CLIENTS]; static int socket_family = AF_UNSPEC; static int port_number = SOCKET_PORT_DEFAULT; -bool map_i_nl_cr = false; -bool map_i_cr_nl = false; -bool map_ign_cr = false; static const char *socket_filename(void) { diff --git a/src/socket.h b/src/socket.h index 53c343e..2caffaf 100644 --- a/src/socket.h +++ b/src/socket.h @@ -25,10 +25,6 @@ #include #include -extern bool map_i_nl_cr; -extern bool map_i_cr_nl; -extern bool map_ign_cr; - void socket_configure(void); void socket_write(char input_char); int socket_add_fds(fd_set *fds, bool connected); diff --git a/src/tty.c b/src/tty.c index 9c4c8c1..54ea980 100644 --- a/src/tty.c +++ b/src/tty.c @@ -129,6 +129,9 @@ const char random_array[] = }; bool interactive_mode = true; +bool map_i_nl_cr = false; +bool map_i_cr_nl = false; +bool map_ign_cr = false; static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old; static unsigned long rx_total = 0, tx_total = 0; diff --git a/src/tty.h b/src/tty.h index c4b6797..add8bf1 100644 --- a/src/tty.h +++ b/src/tty.h @@ -24,6 +24,9 @@ #include extern bool interactive_mode; +extern bool map_i_nl_cr; +extern bool map_i_cr_nl; +extern bool map_ign_cr; void stdout_configure(void); void stdin_configure(void);