Merge pull request #194 from somewear-labs/socketInputModes

Support input mapping modes for sockets
This commit is contained in:
Martin Lund 2023-04-15 00:09:40 +02:00 committed by GitHub
commit ec2cf476bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View file

@ -33,6 +33,7 @@
#include "socket.h" #include "socket.h"
#include "options.h" #include "options.h"
#include "print.h" #include "print.h"
#include "tty.h"
#define MAX_SOCKET_CLIENTS 16 #define MAX_SOCKET_CLIENTS 16
#define SOCKET_PORT_DEFAULT 3333 #define SOCKET_PORT_DEFAULT 3333
@ -340,11 +341,26 @@ bool socket_handle_input(fd_set *rdfs, char *output_char)
clientfds[i] = -1; clientfds[i] = -1;
continue; 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'; *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; return true;
} }
} }

View file

@ -129,6 +129,9 @@ const char random_array[] =
}; };
bool interactive_mode = true; 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 struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old;
static unsigned long rx_total = 0, tx_total = 0; static unsigned long rx_total = 0, tx_total = 0;
@ -987,14 +990,17 @@ void tty_configure(void)
if (strcmp(token,"INLCR") == 0) if (strcmp(token,"INLCR") == 0)
{ {
tio.c_iflag |= INLCR; tio.c_iflag |= INLCR;
map_i_nl_cr = true;
} }
else if (strcmp(token,"IGNCR") == 0) else if (strcmp(token,"IGNCR") == 0)
{ {
tio.c_iflag |= IGNCR; tio.c_iflag |= IGNCR;
map_ign_cr = true;
} }
else if (strcmp(token,"ICRNL") == 0) else if (strcmp(token,"ICRNL") == 0)
{ {
tio.c_iflag |= ICRNL; tio.c_iflag |= ICRNL;
map_i_cr_nl = true;
} }
else if (strcmp(token,"OCRNL") == 0) else if (strcmp(token,"OCRNL") == 0)
{ {

View file

@ -24,6 +24,9 @@
#include <stdbool.h> #include <stdbool.h>
extern bool interactive_mode; 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 stdout_configure(void);
void stdin_configure(void); void stdin_configure(void);