Enter non-interactive mode when piping to tio

Add support for a non interactive mode which allows other application to
pipe data to tio which then forwards the data to the connected serial
device.

Non ineractive means that tio does not react to interactive key commands
in the incoming stream. This allows users to pipe binary data directly
to the connected serial device.

Example use:

$ cat commands.txt | tio /dev/ttyUSB0
This commit is contained in:
Martin Lund 2022-06-16 21:03:33 +02:00
parent a37ad26a88
commit dba77eb912
3 changed files with 27 additions and 9 deletions

View file

@ -59,6 +59,11 @@ int main(int argc, char *argv[])
{ {
stdin_configure(); stdin_configure();
} }
else
{
// Enter non interactive mode
interactive_mode = false;
}
/* Configure output terminal */ /* Configure output terminal */
if (isatty(fileno(stdout))) if (isatty(fileno(stdout)))
@ -85,7 +90,10 @@ int main(int argc, char *argv[])
/* Print launch hints */ /* Print launch hints */
tio_printf("tio v%s", VERSION); tio_printf("tio v%s", VERSION);
tio_printf("Press ctrl-t q to quit"); if (interactive_mode)
{
tio_printf("Press ctrl-t q to quit");
}
/* Open socket */ /* Open socket */
if (option.socket) if (option.socket)
@ -94,7 +102,7 @@ int main(int argc, char *argv[])
} }
/* Connect to tty device */ /* Connect to tty device */
if (option.no_autoconnect) if ((option.no_autoconnect) || (!interactive_mode))
{ {
status = tty_connect(); status = tty_connect();
} }

View file

@ -63,6 +63,8 @@ extern int iossiospeed(int fd, int baudrate);
#define PATH_SERIAL_DEVICES "/dev/serial/by-id/" #define PATH_SERIAL_DEVICES "/dev/serial/by-id/"
#endif #endif
bool interactive_mode = true;
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;
static bool connected = false; static bool connected = false;
@ -855,17 +857,21 @@ int tty_connect(void)
goto error_read; goto error_read;
} }
/* Forward input to output except ctrl-t key */ /* Forward input to output */
output_char = input_char; output_char = input_char;
if (input_char == KEY_CTRL_T)
forward = false;
/* Handle commands */ if (interactive_mode)
handle_command_sequence(input_char, previous_char, &output_char, &forward); {
/* Do not forward ctrl-t key */
if (input_char == KEY_CTRL_T)
forward = false;
/* Save previous key */ /* Handle commands */
previous_char = input_char; handle_command_sequence(input_char, previous_char, &output_char, &forward);
/* Save previous key */
previous_char = input_char;
}
} }
else else
{ {

View file

@ -21,6 +21,8 @@
#pragma once #pragma once
#include <stdbool.h>
#define KEY_QUESTION 0x3f #define KEY_QUESTION 0x3f
#define KEY_B 0x62 #define KEY_B 0x62
#define KEY_C 0x63 #define KEY_C 0x63
@ -48,3 +50,5 @@ void tty_configure(void);
int tty_connect(void); int tty_connect(void);
void tty_wait_for_device(void); void tty_wait_for_device(void);
void list_serial_devices(void); void list_serial_devices(void);
extern bool interactive_mode;