Quit from non-interactive mode using ctrl-c

When piping to tio it will automatically enter "non-interactive" mode
which means it will not react to any input key sequences but simple read
the input stream and write it to the tty device.

This also means that ctrl-t q can not be used to quit and so tio would
hang forever when used in non-interactive mode.

This change allows to send the standard termination signal by pressing
ctrl-c on tio in non-interactive mode to make it quit.
This commit is contained in:
Martin Lund 2022-07-20 17:59:13 +02:00
parent eadcc7e384
commit 5f46136b28
3 changed files with 77 additions and 45 deletions

View file

@ -96,6 +96,10 @@ int main(int argc, char *argv[])
if (interactive_mode)
{
tio_printf("Press ctrl-%c q to quit", option.prefix_key);
} else
{
tio_printf("Non-interactive mode enabled");
tio_printf("Press ctrl-c to quit");
}
/* Open socket */
@ -105,7 +109,7 @@ int main(int argc, char *argv[])
}
/* Connect to tty device */
if ((option.no_autoconnect) || (!interactive_mode))
if (option.no_autoconnect)
{
status = tty_connect();
}

View file

@ -27,15 +27,24 @@
#include "error.h"
#include "print.h"
#include "misc.h"
#include "tty.h"
static void signal_handler(int signum)
{
UNUSED(signum);
tio_printf("Received hangup signal!");
switch (signum)
{
case SIGHUP:
tio_printf("Received SIGHUP signal!");
break;
case SIGINT:
tio_printf("Received SIGINT signal!");
break;
}
exit(EXIT_FAILURE);
}
void signal_handlers_install(void)
{
signal(SIGHUP, signal_handler);
signal(SIGINT, signal_handler);
}

View file

@ -586,6 +586,12 @@ void stdout_configure(void)
/* Reconfigure stdout (RAW configuration) */
cfmakeraw(&stdout_new);
/* Allow ^C / SIGINT (to allow termination when piping to tio) */
if (!interactive_mode)
{
stdout_new.c_lflag |= ISIG;
}
/* Control characters */
stdout_new.c_cc[VTIME] = 0; /* Inter-character timer unused */
stdout_new.c_cc[VMIN] = 1; /* Blocking read until 1 character received */
@ -833,6 +839,10 @@ void tty_wait_for_device(void)
/* Loop until device pops up */
while (true)
{
if (interactive_mode)
{
/* In interactive mode, while waiting for tty device, we need to
* read from stdin to react on input key commands. */
if (first)
{
/* Don't wait first time */
@ -842,7 +852,7 @@ void tty_wait_for_device(void)
}
else
{
/* Wait up to 1 second */
/* Wait up to 1 second for input */
tv.tv_sec = 1;
tv.tv_usec = 0;
}
@ -879,6 +889,7 @@ void tty_wait_for_device(void)
tio_error_printf("select() failed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
}
/* Test for accessible device file */
status = access(option.tty_device, R_OK);
@ -893,6 +904,14 @@ void tty_wait_for_device(void)
tio_printf("Waiting for tty device..");
last_errno = errno;
}
if (!interactive_mode)
{
/* In non-interactive mode we do not need to handle input key
* commands so we simply sleep 1 second between checking for
* presence of tty device */
sleep(1);
}
}
}