Add option --no-tty-restore / -N that omits tty_restore() call #334

On exit, do not reset the tty device settings to the state they were in
before the program started.
This is effective when sending characters at a low baud rate and then
immediately exiting, to ensure that the last character is output
correctly.
This commit is contained in:
yabu76 2025-12-31 15:14:18 +09:00
parent e73ff6d208
commit c74273ecaa
9 changed files with 34 additions and 3 deletions

View file

@ -122,6 +122,7 @@ Options:
--exclude-drivers <pattern> Exclude drivers by pattern --exclude-drivers <pattern> Exclude drivers by pattern
--exclude-tids <pattern> Exclude topology IDs by pattern --exclude-tids <pattern> Exclude topology IDs by pattern
-n, --no-reconnect Do not reconnect -n, --no-reconnect Do not reconnect
-N, --no-tty-restore Do not restore initial TTY device settings
-e, --local-echo Enable local echo -e, --local-echo Enable local echo
--input-mode normal|hex|line Select input mode (default: normal) --input-mode normal|hex|line Select input mode (default: normal)
--output-mode normal|hex|hexN Select output mode (default: normal) --output-mode normal|hex|hexN Select output mode (default: normal)

View file

@ -19,6 +19,7 @@ output-delay = 0
output-line-delay = 0 output-line-delay = 0
auto-connect = direct auto-connect = direct
no-reconnect = false no-reconnect = false
no-tty-restore = false
local-echo = false local-echo = false
input-mode = normal input-mode = normal
output-mode = normal output-mode = normal

View file

@ -123,6 +123,14 @@ Do not reconnect.
This means that tio will exit if it fails to connect to device or an This means that tio will exit if it fails to connect to device or an
established connection is lost. established connection is lost.
.TP
.BR \-n ", " \-\-no\-tty\-restore
Do not restore initial TTY device settings.
This means that tio will exit without trying to restore TTY device
settings that existed when tio was started.
.TP .TP
.BR \-e ", " "\-\-local\-echo .BR \-e ", " "\-\-local\-echo

View file

@ -94,6 +94,12 @@ OPTIONS
This means that tio will exit if it fails to connect to device or an established connection is lost. This means that tio will exit if it fails to connect to device or an established connection is lost.
-N, --no-tty-restore
Do not restore initial TTY device settings.
This means that tio will exit without trying to restore TTY device settings that existed when tio was started.
-e, --local-echo -e, --local-echo
Enable local echo. Enable local echo.

View file

@ -23,6 +23,7 @@ _tio()
--exclude-drivers \ --exclude-drivers \
--exclude-tids \ --exclude-tids \
-n --no-reconnect \ -n --no-reconnect \
-N --no-tty-restore \
-e --local-echo \ -e --local-echo \
-l --log \ -l --log \
--log-file \ --log-file \

View file

@ -196,6 +196,7 @@ static void config_parse_keys(GKeyFile *key_file, char *group)
config_get_string(key_file, group, "exclude-drivers", &option.exclude_devices, NULL); config_get_string(key_file, group, "exclude-drivers", &option.exclude_devices, NULL);
config_get_string(key_file, group, "exclude-tids", &option.exclude_devices, NULL); config_get_string(key_file, group, "exclude-tids", &option.exclude_devices, NULL);
config_get_bool(key_file, group, "no-reconnect", &option.no_reconnect); config_get_bool(key_file, group, "no-reconnect", &option.no_reconnect);
config_get_bool(key_file, group, "no-tty-restore", &option.no_tty_restore);
config_get_bool(key_file, group, "local-echo", &option.local_echo); config_get_bool(key_file, group, "local-echo", &option.local_echo);
config_get_string(key_file, group, "input-mode", &string, "normal", "hex", "line", NULL); config_get_string(key_file, group, "input-mode", &string, "normal", "hex", "line", NULL);
if (string != NULL) if (string != NULL)

View file

@ -82,6 +82,7 @@ struct option_t option =
.dcd_pulse_duration = 100, .dcd_pulse_duration = 100,
.ri_pulse_duration = 100, .ri_pulse_duration = 100,
.no_reconnect = false, .no_reconnect = false,
.no_tty_restore = false,
.auto_connect = AUTO_CONNECT_DIRECT, .auto_connect = AUTO_CONNECT_DIRECT,
.log = false, .log = false,
.log_append = false, .log_append = false,
@ -153,6 +154,7 @@ void option_print_help(char *argv[])
printf(" --exclude-drivers <pattern> Exclude drivers by pattern\n"); printf(" --exclude-drivers <pattern> Exclude drivers by pattern\n");
printf(" --exclude-tids <pattern> Exclude topology IDs by pattern\n"); printf(" --exclude-tids <pattern> Exclude topology IDs by pattern\n");
printf(" -n, --no-reconnect Do not reconnect\n"); printf(" -n, --no-reconnect Do not reconnect\n");
printf(" -N, --no-tty-restore Do not restore initial TTY device settings\n");
printf(" -e, --local-echo Enable local echo\n"); printf(" -e, --local-echo Enable local echo\n");
printf(" --input-mode normal|hex|line Select input mode (default: normal)\n"); printf(" --input-mode normal|hex|line Select input mode (default: normal)\n");
printf(" --output-mode normal|hex|hexN Select output mode (default: normal)\n"); printf(" --output-mode normal|hex|hexN Select output mode (default: normal)\n");
@ -844,6 +846,7 @@ void option_parse_mappings(const char *map)
void options_print() void options_print()
{ {
/* note: negative true/false settings are rephrased as affirmative no/yes. */
tio_printf(" Device: %s", device_name); tio_printf(" Device: %s", device_name);
tio_printf(" Baudrate: %u", option.baudrate); tio_printf(" Baudrate: %u", option.baudrate);
tio_printf(" Databits: %d", option.databits); tio_printf(" Databits: %d", option.databits);
@ -858,6 +861,7 @@ void options_print()
tio_printf(" Output line delay char: %s", option.output_line_delay_char == '\r' ? "cr" : "lf"); tio_printf(" Output line delay char: %s", option.output_line_delay_char == '\r' ? "cr" : "lf");
tio_printf(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect)); tio_printf(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect));
tio_printf(" Automatic reconnect: %s", option.no_reconnect ? "true" : "false"); tio_printf(" Automatic reconnect: %s", option.no_reconnect ? "true" : "false");
tio_printf(" TTY device settings restore: %s", option.no_tty_restore ? "true" : "false");
// clang-format off // clang-format off
tio_printf(" Pulse duration: DTR=%d RTS=%d CTS=%d DSR=%d DCD=%d RI=%d", option.dtr_pulse_duration, tio_printf(" Pulse duration: DTR=%d RTS=%d CTS=%d DSR=%d DCD=%d RI=%d", option.dtr_pulse_duration,
option.rts_pulse_duration, option.rts_pulse_duration,
@ -933,6 +937,7 @@ void options_parse(int argc, char *argv[])
{"exclude-drivers", required_argument, 0, OPT_EXCLUDE_DRIVERS }, {"exclude-drivers", required_argument, 0, OPT_EXCLUDE_DRIVERS },
{"exclude-tids", required_argument, 0, OPT_EXCLUDE_TIDS }, {"exclude-tids", required_argument, 0, OPT_EXCLUDE_TIDS },
{"no-reconnect", no_argument, 0, 'n' }, {"no-reconnect", no_argument, 0, 'n' },
{"no-tty-restore", no_argument, 0, 'N' },
{"local-echo", no_argument, 0, 'e' }, {"local-echo", no_argument, 0, 'e' },
{"timestamp", no_argument, 0, 't' }, {"timestamp", no_argument, 0, 't' },
{"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT }, {"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT },
@ -967,7 +972,7 @@ void options_parse(int argc, char *argv[])
int option_index = 0; int option_index = 0;
/* Parse argument using getopt_long */ /* Parse argument using getopt_long */
c = getopt_long(argc, argv, "b:d:f:s:p:o:O:a:netLlS:m:c:xrvh", long_options, &option_index); c = getopt_long(argc, argv, "b:d:f:s:p:o:O:a:nNetLlS:m:c:xrvh", long_options, &option_index);
/* Detect the end of the options */ /* Detect the end of the options */
if (c == -1) if (c == -1)
@ -1041,6 +1046,10 @@ void options_parse(int argc, char *argv[])
option.no_reconnect = true; option.no_reconnect = true;
break; break;
case 'N':
option.no_tty_restore = true;
break;
case 'e': case 'e':
option.local_echo = true; option.local_echo = true;
break; break;

View file

@ -62,6 +62,7 @@ struct option_t
int dcd_pulse_duration; int dcd_pulse_duration;
int ri_pulse_duration; int ri_pulse_duration;
bool no_reconnect; bool no_reconnect;
bool no_tty_restore;
auto_connect_t auto_connect; auto_connect_t auto_connect;
bool log; bool log;
bool log_append; bool log_append;

View file

@ -2706,10 +2706,13 @@ int tty_connect(void)
rs485_mode_enable(device_fd); rs485_mode_enable(device_fd);
} }
/* Make sure we restore tty settings on exit */ /* Make sure we restore tty settings on exit unless ordered to keep current settings */
if (first) if (first)
{
if (option.no_tty_restore == false)
{ {
atexit(&tty_restore); atexit(&tty_restore);
}
first = false; first = false;
} }