Refactoring

This commit is contained in:
Martin Lund 2022-06-23 22:24:12 +02:00
parent 941e8d5b04
commit 4e08c68533

130
src/tty.c
View file

@ -693,6 +693,62 @@ void tty_restore(void)
} }
} }
void forward_to_tty(int fd, char output_char)
{
int status;
/* Map output character */
if ((output_char == 127) && (map_o_del_bs))
{
output_char = '\b';
}
if ((output_char == '\r') && (map_o_cr_nl))
{
output_char = '\n';
}
/* Map newline character */
if ((output_char == '\n' || output_char == '\r') && (map_o_nl_crnl))
{
const char *crlf = "\r\n";
optional_local_echo(crlf[0]);
optional_local_echo(crlf[1]);
status = write(fd, crlf, 2);
if (status < 0)
{
warning_printf("Could not write to tty device");
}
tx_total += 2;
delay(option.output_delay);
}
else
{
if (print_mode == HEX)
{
output_hex(output_char);
}
else
{
/* Send output to tty device */
optional_local_echo(output_char);
status = write(fd, &output_char, 1);
if (status < 0)
{
warning_printf("Could not write to tty device");
}
fsync(fd);
/* Update transmit statistics */
tx_total++;
}
/* Insert output delay */
delay(option.output_delay);
}
}
int tty_connect(void) int tty_connect(void)
{ {
fd_set rdfs; /* Read file descriptor set */ fd_set rdfs; /* Read file descriptor set */
@ -879,8 +935,6 @@ int tty_connect(void)
} }
else if (FD_ISSET(STDIN_FILENO, &rdfs)) else if (FD_ISSET(STDIN_FILENO, &rdfs))
{ {
forward = true;
/* Input from stdin ready */ /* Input from stdin ready */
status = read(STDIN_FILENO, &input_char, 1); status = read(STDIN_FILENO, &input_char, 1);
if (status <= 0) if (status <= 0)
@ -891,6 +945,8 @@ int tty_connect(void)
/* Forward input to output */ /* Forward input to output */
output_char = input_char; output_char = input_char;
forward = true;
output_char = input_char;
if (interactive_mode) if (interactive_mode)
{ {
@ -905,73 +961,29 @@ int tty_connect(void)
/* Save previous key */ /* Save previous key */
previous_char = input_char; previous_char = input_char;
if (print_mode == HEX)
{
if (!is_valid_hex(input_char))
{
warning_printf("Invalid hex character: '%d' (0x%02x)", input_char, input_char);
forward = false;
}
}
} }
if (print_mode == HEX) if (forward)
{ {
if (!is_valid_hex(input_char)) forward_to_tty(fd, output_char);
{
warning_printf("Invalid hex character: '%d' (0x%02x)", input_char, input_char);
continue;
}
} }
} }
else else
{ {
forward = socket_handle_input(&rdfs, &output_char); forward = socket_handle_input(&rdfs, &output_char);
}
if (forward) if (forward)
{
/* Map output character */
if ((output_char == 127) && (map_o_del_bs))
{ {
output_char = '\b'; forward_to_tty(fd, output_char);
}
if ((output_char == '\r') && (map_o_cr_nl))
{
output_char = '\n';
}
/* Map newline character */
if ((output_char == '\n' || output_char == '\r') && (map_o_nl_crnl))
{
const char *crlf = "\r\n";
optional_local_echo(crlf[0]);
optional_local_echo(crlf[1]);
status = write(fd, crlf, 2);
if (status < 0)
{
warning_printf("Could not write to tty device");
}
tx_total += 2;
delay(option.output_delay);
}
else
{
if (print_mode == HEX)
{
output_hex(output_char);
}
else
{
/* Send output to tty device */
optional_local_echo(output_char);
status = write(fd, &output_char, 1);
if (status < 0)
{
warning_printf("Could not write to tty device");
}
fsync(fd);
/* Update transmit statistics */
tx_total++;
}
/* Insert output delay */
delay(option.output_delay);
} }
} }
} }