diff --git a/src/include/tio/tty.h b/src/include/tio/tty.h index 0e4d651..2d216f6 100644 --- a/src/include/tio/tty.h +++ b/src/include/tio/tty.h @@ -29,9 +29,9 @@ #define KEY_T 0x74 #define KEY_CTRL_T 0x14 -void configure_stdout(void); -void restore_stdout(void); -int connect_tty(void); -void wait_for_tty_device(void); +void stdout_configure(void); +void stdout_restore(void); +int tty_connect(void); +void tty_wait_for_device(void); #endif diff --git a/src/main.c b/src/main.c index 3d57627..b7d3c8a 100644 --- a/src/main.c +++ b/src/main.c @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) parse_options(argc, argv); /* Configure output terminal */ - configure_stdout(); + stdout_configure(); /* Install log exit handler */ atexit(&log_exit); @@ -51,14 +51,14 @@ int main(int argc, char *argv[]) /* Connect to tty device */ if (option.no_autoconnect) - status = connect_tty(); + status = tty_connect(); else { /* Enter connect loop */ while (true) { - wait_for_tty_device(); - status = connect_tty(); + tty_wait_for_device(); + status = tty_connect(); } } diff --git a/src/options.c b/src/options.c index d1dc5e6..cac9e95 100644 --- a/src/options.c +++ b/src/options.c @@ -47,7 +47,7 @@ struct option_t option = "none" // Parity }; -void print_options_help(char *argv[]) +void print_help(char *argv[]) { printf("Usage: %s [] \n", argv[0]); printf("\n"); @@ -74,7 +74,7 @@ void parse_options(int argc, char *argv[]) if (argc == 1) { - print_options_help(argv); + print_help(argv); exit(EXIT_SUCCESS); } @@ -336,7 +336,7 @@ void parse_options(int argc, char *argv[]) break; case 'h': - print_options_help(argv); + print_help(argv); exit(EXIT_SUCCESS); break; diff --git a/src/tty.c b/src/tty.c index ff6f7ae..b6f1abf 100644 --- a/src/tty.c +++ b/src/tty.c @@ -114,7 +114,47 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c } } -void wait_for_tty_device(void) +void stdout_configure(void) +{ + /* Save current stdout settings */ + if (tcgetattr(STDOUT_FILENO, &old_stdout) < 0) + { + error_printf("Saving current stdio settings failed"); + exit(EXIT_FAILURE); + } + + /* Prepare new stdout settings */ + bzero(&new_stdout, sizeof(new_stdout)); + + /* Control, input, output, local modes for stdout */ + new_stdout.c_cflag = 0; + new_stdout.c_iflag = 0; + new_stdout.c_oflag = 0; + new_stdout.c_lflag = 0; + + /* Control characters */ + new_stdout.c_cc[VTIME] = 0; /* Inter-character timer unused */ + new_stdout.c_cc[VMIN] = 1; /* Blocking read until 1 character received */ + + /* Activate new stdout settings */ + tcsetattr(STDOUT_FILENO, TCSANOW, &new_stdout); + tcsetattr(STDOUT_FILENO, TCSAFLUSH, &new_stdout); + + /* Print launch hints */ + tio_printf("tio v%s", VERSION); + tio_printf("Press ctrl-t + q to quit"); + + /* Make sure we restore old stdout settings on exit */ + atexit(&stdout_restore); +} + +void stdout_restore(void) +{ + tcsetattr(STDOUT_FILENO, TCSANOW, &old_stdout); + tcsetattr(STDOUT_FILENO, TCSAFLUSH, &old_stdout); +} + +void tty_wait_for_device(void) { fd_set rdfs; int status; @@ -172,47 +212,7 @@ void wait_for_tty_device(void) } } -void configure_stdout(void) -{ - /* Save current stdout settings */ - if (tcgetattr(STDOUT_FILENO, &old_stdout) < 0) - { - error_printf("Saving current stdio settings failed"); - exit(EXIT_FAILURE); - } - - /* Prepare new stdout settings */ - bzero(&new_stdout, sizeof(new_stdout)); - - /* Control, input, output, local modes for stdout */ - new_stdout.c_cflag = 0; - new_stdout.c_iflag = 0; - new_stdout.c_oflag = 0; - new_stdout.c_lflag = 0; - - /* Control characters */ - new_stdout.c_cc[VTIME] = 0; /* Inter-character timer unused */ - new_stdout.c_cc[VMIN] = 1; /* Blocking read until 1 character received */ - - /* Activate new stdout settings */ - tcsetattr(STDOUT_FILENO, TCSANOW, &new_stdout); - tcsetattr(STDOUT_FILENO, TCSAFLUSH, &new_stdout); - - /* Print launch hints */ - tio_printf("tio v%s", VERSION); - tio_printf("Press ctrl-t + q to quit"); - - /* Make sure we restore old stdout settings on exit */ - atexit(&restore_stdout); -} - -void restore_stdout(void) -{ - tcsetattr(STDOUT_FILENO, TCSANOW, &old_stdout); - tcsetattr(STDOUT_FILENO, TCSAFLUSH, &old_stdout); -} - -void disconnect_tty(void) +void tty_disconnect(void) { if (connected) { @@ -223,16 +223,16 @@ void disconnect_tty(void) } } -void restore_tty(void) +void tty_restore(void) { tcsetattr(fd, TCSANOW, &old_tio); tcsetattr(fd, TCSAFLUSH, &old_tio); if (connected) - disconnect_tty(); + tty_disconnect(); } -int connect_tty(void) +int tty_connect(void) { fd_set rdfs; /* Read file descriptor set */ int maxfd; /* Maximum file descriptor used */ @@ -279,7 +279,7 @@ int connect_tty(void) /* Make sure we restore tty settings on exit */ if (first) { - atexit(&restore_tty); + atexit(&tty_restore); first = false; } @@ -384,7 +384,7 @@ int connect_tty(void) error_tcgetattr: error_isatty: error_read: - disconnect_tty(); + tty_disconnect(); error_open: return TIO_ERROR; }