diff --git a/src/main.c b/src/main.c index e5c99eb..7f31645 100644 --- a/src/main.c +++ b/src/main.c @@ -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(); } diff --git a/src/signals.c b/src/signals.c index 364de8b..02ccbbe 100644 --- a/src/signals.c +++ b/src/signals.c @@ -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); } diff --git a/src/tty.c b/src/tty.c index 0a17150..7dc906b 100644 --- a/src/tty.c +++ b/src/tty.c @@ -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,51 +839,56 @@ void tty_wait_for_device(void) /* Loop until device pops up */ while (true) { - if (first) + if (interactive_mode) { - /* Don't wait first time */ - tv.tv_sec = 0; - tv.tv_usec = 1; - first = false; - } - else - { - /* Wait up to 1 second */ - tv.tv_sec = 1; - tv.tv_usec = 0; - } - - FD_ZERO(&rdfs); - FD_SET(STDIN_FILENO, &rdfs); - maxfd = MAX(STDIN_FILENO, socket_add_fds(&rdfs, false)); - - /* Block until input becomes available or timeout */ - status = select(maxfd + 1, &rdfs, NULL, NULL, &tv); - if (status > 0) - { - if (FD_ISSET(STDIN_FILENO, &rdfs)) + /* In interactive mode, while waiting for tty device, we need to + * read from stdin to react on input key commands. */ + if (first) { - /* Input from stdin ready */ - - /* Read one character */ - status = read(STDIN_FILENO, &input_char, 1); - if (status <= 0) - { - tio_error_printf("Could not read from stdin"); - exit(EXIT_FAILURE); - } - - /* Handle commands */ - handle_command_sequence(input_char, previous_char, NULL, NULL); - - previous_char = input_char; + /* Don't wait first time */ + tv.tv_sec = 0; + tv.tv_usec = 1; + first = false; + } + else + { + /* Wait up to 1 second for input */ + tv.tv_sec = 1; + tv.tv_usec = 0; + } + + FD_ZERO(&rdfs); + FD_SET(STDIN_FILENO, &rdfs); + maxfd = MAX(STDIN_FILENO, socket_add_fds(&rdfs, false)); + + /* Block until input becomes available or timeout */ + status = select(maxfd + 1, &rdfs, NULL, NULL, &tv); + if (status > 0) + { + if (FD_ISSET(STDIN_FILENO, &rdfs)) + { + /* Input from stdin ready */ + + /* Read one character */ + status = read(STDIN_FILENO, &input_char, 1); + if (status <= 0) + { + tio_error_printf("Could not read from stdin"); + exit(EXIT_FAILURE); + } + + /* Handle commands */ + handle_command_sequence(input_char, previous_char, NULL, NULL); + + previous_char = input_char; + } + socket_handle_input(&rdfs, NULL); + } + else if (status == -1) + { + tio_error_printf("select() failed (%s)", strerror(errno)); + exit(EXIT_FAILURE); } - socket_handle_input(&rdfs, NULL); - } - else if (status == -1) - { - tio_error_printf("select() failed (%s)", strerror(errno)); - exit(EXIT_FAILURE); } /* Test for accessible device file */ @@ -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); + } } }