diff --git a/README.md b/README.md index 9d969bd..287c143 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ IO::Stty - Change and print terminal line settings # What settings do we have anyway? print IO::Stty::stty(\*STDIN,'-a'); + # Or with no arguments (same as -a): + print IO::Stty::stty(\*STDIN); + # DESCRIPTION This is the PERL POSIX compliant stty. @@ -365,9 +368,11 @@ Linux and 255 on macOS/BSD). IO::Stty::stty(\*STDIN, @params); - Returns a string for query options (`-a`, `-g`, `-v`), `undef` if - the handle is not a terminal or if the terminal parameters could not be - read, and a true value on success when setting parameters. + When called with no parameters, returns the current terminal settings in + human-readable form (equivalent to `-a`), matching POSIX `stty` + behavior. Returns a string for query options (`-a`, `-g`, `-v`), + `undef` if the handle is not a terminal or if the terminal parameters + could not be read, and a true value on success when setting parameters. From comments: diff --git a/lib/IO/Stty.pm b/lib/IO/Stty.pm index af61fb0..55ce885 100644 --- a/lib/IO/Stty.pm +++ b/lib/IO/Stty.pm @@ -70,6 +70,9 @@ IO::Stty - Change and print terminal line settings # What settings do we have anyway? print IO::Stty::stty(\*STDIN,'-a'); + # Or with no arguments (same as -a): + print IO::Stty::stty(\*STDIN); + =head1 DESCRIPTION This is the PERL POSIX compliant stty. @@ -483,9 +486,11 @@ sub _parse_char_value { IO::Stty::stty(\*STDIN, @params); -Returns a string for query options (C<-a>, C<-g>, C<-v>), C if -the handle is not a terminal or if the terminal parameters could not be -read, and a true value on success when setting parameters. +When called with no parameters, returns the current terminal settings in +human-readable form (equivalent to C<-a>), matching POSIX C +behavior. Returns a string for query options (C<-a>, C<-g>, C<-v>), +C if the handle is not a terminal or if the terminal parameters +could not be read, and a true value on success when setting parameters. From comments: @@ -500,8 +505,6 @@ From comments: sub stty { my $tty_handle = shift; # This should be a \*HANDLE - @_ or die("No parameters passed to stty"); - # Notice fileno() instead of handle->fileno(). I want it to work with # normal fhs. my ($file_num) = fileno($tty_handle); @@ -537,6 +540,14 @@ sub stty { # OK.. we have our crap. + # No arguments: display current settings (POSIX stty behavior). + unless (@_) { + return show_me_the_crap( + $c_cflag, $c_iflag, $ispeed, $c_lflag, $c_oflag, + $ospeed, \%control_chars + ); + } + my @parameters; if ( @_ == 1 ) { diff --git a/t/01-functional.t b/t/01-functional.t index 2c5a7c9..da9fb20 100644 --- a/t/01-functional.t +++ b/t/01-functional.t @@ -176,6 +176,21 @@ subtest '-a output format' => sub { like($output, qr/intr\s*=/, '-a shows intr control char'); }; +# ── 3b. No-argument display (POSIX stty behavior) ─────────────────── + +subtest 'no arguments returns current settings' => sub { + my ($pty, $slave) = fresh_pty(); + my $output = IO::Stty::stty($slave); + ok(defined $output, 'no-arg stty returns output'); + like($output, qr/speed \d+ baud/, 'no-arg output contains speed line'); + like($output, qr/echo/, 'no-arg output mentions echo'); + like($output, qr/icanon/, 'no-arg output mentions icanon'); + + # Should be identical to -a output + my $a_output = IO::Stty::stty($slave, '-a'); + is($output, $a_output, 'no-arg output matches -a output'); +}; + # ── 4. Combination settings ────────────────────────────────────────── subtest 'raw mode' => sub { @@ -432,6 +447,13 @@ subtest 'non-tty handle returns undef' => sub { close $fh; }; +subtest 'non-tty handle with no args returns undef' => sub { + open my $fh, '<', '/dev/null' or die "open /dev/null: $!"; + my $result = IO::Stty::stty($fh); + is($result, undef, 'non-tty with no args returns undef (no die)'); + close $fh; +}; + # ── 9. iexten flag ───────────────────────────────────────────────────── subtest 'toggle iexten flag' => sub {