From: Pádraig Brady Date: Thu, 9 Apr 2026 15:09:38 +0000 (+0100) Subject: tests: tty-eof.pl: address FIXME re hardcoded Ctrl-d X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e5912211de70b89cc99df013e0eb4257c167b14e;p=thirdparty%2Fcoreutils.git tests: tty-eof.pl: address FIXME re hardcoded Ctrl-d * tests/tty/tty-eof.pl: Try to explicitly set EOF char to Ctrl-d in case it's different. --- diff --git a/tests/tty/tty-eof.pl b/tests/tty/tty-eof.pl index 4820dafe8f..a57c8a7462 100755 --- a/tests/tty/tty-eof.pl +++ b/tests/tty/tty-eof.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl # Test whether programs exit upon a single EOF from a tty. -# Ensure that e.g., cat exits upon a single EOF (^D) from a tty. +# Ensure that e.g., cat exits upon a single configured EOF from a tty. # Do the same for all programs that can read stdin, # require no arguments and that write to standard output. @@ -22,14 +22,43 @@ use strict; (my $ME = $0) =~ s|.*/||; +eval { require POSIX; }; +$@ + and CuSkip::skip "$ME: this script requires Perl's POSIX module\n"; + +use POSIX qw(:termios_h); + # Some older versions of Expect.pm (e.g. 1.07) lack the log_user method, # so check for that, too. eval { require Expect; Expect->require_version('1.11') }; $@ and CuSkip::skip "$ME: this script requires Perl's Expect package >=1.11\n"; +# Try to explicitly set eof character in case it's not the usual Ctrl-d +sub +set_tty_eof_char ($$) +{ + my ($tty, $eof_char) = @_; + + return eval + { + my $termios = POSIX::Termios->new; + my $fd = fileno ($tty); + defined $fd + or die "no file descriptor for tty"; + $termios->getattr ($fd) + or die "tcgetattr failed: $!"; + $termios->setcc (VEOF, ord $eof_char); + $termios->setattr ($fd, TCSANOW) + or die "tcsetattr failed: $!"; + 1; + }; +} + { my $fail = 0; + my $eof_char = "\cD"; + my $eof_name = '^D'; my @stdin_reading_commands = qw( b2sum base32 @@ -82,6 +111,7 @@ $@ $exp->spawn("$cmd 2> $stderr") or (warn "$ME: cannot run '$cmd' ($mode): $!\n"), $fail=1, next; + set_tty_eof_char ($exp->slave, $eof_char); my $found; if ($with_input) @@ -91,7 +121,7 @@ $@ $echo =~ s/\n$//; $exp->send($input); - $exp->send("\cD"); # This is Control-D. FIXME: what if not EOF? + $exp->send($eof_char); $exp->expect (0, '-re', "^$echo\\r?\$"); $found = $exp->expect (1, '-re', "^.+\$"); $found and warn "F: $found: " . $exp->exp_match () . "\n"; @@ -101,12 +131,12 @@ $@ } else { - $exp->send("\cD"); # This is Control-D. FIXME: what if not EOF? + $exp->send($eof_char); } $exp->expect(10, 'eof'); defined $exp->exitstatus - or (warn "$ME: $cmd didn't exit after ^D from standard input" + or (warn "$ME: $cmd didn't exit after $eof_name from standard input" . " ($mode)\n"), $fail=1, next; my $s = $exp->exitstatus;