in reply to Re^2: Failing to get current TTY's rows & columns...
in thread Failing to get current TTY's rows & columns...

So the answer here is that I have to open "/dev/tty" each time I call it rather than reference my already-open "STDOUT"?

I don't have linux, but no, I don't think that is the answer.

I think part of the answer is to do error checking correctly like the faq entry

I also think using S4 is part of the answer, not being an expert on pack/unpack and not having a /dev/tty, I can't run any tests.

Hmmm....I'm disappointed that I need to open another file handle and can't get the rows & columns associated with my already open FH, "STDOUT", directly...

Sounds premature if you ask me, avoiding abstractions always involves some growing pains :)

Shouldn't STDIN, STDOUT map to fd[0], fd[1] => /dev/pts/1 on some level?

They do

$ perl -le " print fileno($_) for STDIN, STDOUT, STDERR " 0 1 2
See, fd 0,1,2. Now regarding tty, using operator -t to test if filehandle is opened to a tty, you can see 0,1,2 are connected to a tty
$ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " 1 1 1
but not when you do redirection, here is STDIN not connected to a tty
$ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " < NUL 0 1 1
here is STDIN and STDERR not connected to a tty
$ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " < NUL 2> NUL 0 1 0
here is STDOUT not connected to a tty
$ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " > out.txt $ cat out.txt 1 0 1
I.e. Shouldn't I be able to use some mapping function on STDOUT to get a file descriptor that's suitable for ioctl?

ioctl says you don't need to, the faq item doesn't, and neither does Term::Size::Any

Replies are listed 'Best First'.
Re^4: Failing to get current TTY's rows & columns...
by perl-diddler (Chaplain) on Apr 15, 2011 at 22:08 UTC
    I think part of the answer is to do error checking correctly like the faq entry
    See my answer to previous poster -- that's a red herring as well as the unpack format.

    What you are missing is:

    "I.e. Shouldn't I be able to use some mapping function on STDOUT to get a file descriptor that's suitable for ioctl?
    ioctl says you don't need to, the faq item doesn't, and neither does Term::Size::Any
    The faq item doesn't use STDOUT. It doesn't show how one would use STDOUT to determine the terminal's size. It opens '/dev/tty' -- ANOTHER file handle -- it doesn't show how to determine the size of the terminal attached to STDOUT.

    Of note -- if STDOUT was redirected to a file, then trying to determine its size woudn't return valid values (presumably the ioctl would return an error).

    So how would you determine the rows/columns of whatever STDOUT is pointing to (without opening another character device like "/dev/tty")?

    Even if you aren't on linux, presumably there's a way to do it on your platform, no? I.e. if you have a 'tty' window, how would you determine the size of the window addressed by 'STDOUT'?

      See my answer to previous poster -- that's a red herring as well as the unpack format.

      Same guy, and no, the red herring is you're confusing c function ioctl with perl function ioctl

      What you are missing is:

      No, I didn't miss it. If you redirect STDIN/STDOUT/STDERR, then you need to give ioctl a handle which is connected to tty, so you open your own to /dev/tty, and it works regardless of the state of STDIN/STDOUT/STDERR

      Even if you aren't on linux, presumably there's a way to do it on your platform, no? I.e. if you have a 'tty' window, how would you determine the size of the window addressed by 'STDOUT'?

      I would use an abstraction like Term::Size::Any, I wouldn't mess around with ioctl

      But in all honesty, unless I'm writing some kind of curses program, I would never need to know the size of the terminal

        Confusing docs for system ioctl & perl ioctl -- got it.

        So my error check has a high level of bogosity! Details! ;-)

        Regardless of the bogus error check, it still tries to extract the row,col meaning that if ioctl(STDOUT...) is valid, it should display the rows & columns.

        I understand it won't work if STDOUT is redirected, but neither will opening "/dev/tty", if the program isn't running with a controlling terminal but DOES have STDOUT directed to a tty. I.e. one can always design a failure case -- but I'm more interested in why what should be a 'success' case doesn't work, namely using ioctl with 'STDOUT' when STDOUT is attached to a terminal.

        Here's a simple program that should work but doesn't. Why doesn't it (I believe I've addressed the error case you raised, as well as the unpack format)?

        #!/usr/bin/perl -w use strict; use feature ':5.10'; { no warnings "all"; $SIG{__WARN__} = sub { }; #pretend "no warnings" works require "sys/ioctl.ph"; } sub getwinsize (;$) { my $recheck=$_[0]; my $winsize=0; state ($maxcols, $maxrows); return ($maxcols,$maxrows) if $maxcols && $maxrows && !$recheck; my $err = ioctl STDOUT, &TIOCGWINSZ, $winsize; unless ($err) { print STDERR "ERROR: ioctl on STDOUT: $!\n"; $err = ioctl fileno(STDOUT), &TIOCGWINSZ, $winsize; unless ($err) { print STDERR "ERROR: ioctl on fileno(STDOUT): $!\n"; } else { printf STDERR "ioctl on fileno(STDOUT) worked\n"; } } else { printf STDERR "ioctl on STDOUT worked\n"; } my ($rows, $cols, $unused1, $unused2) = unpack "S!4", $winsize; printf STDERR "(rows=%u, cols=%u)\n", $rows//0, $cols//0; ($maxcols, $maxrows) = ($cols || -1, $rows || -1); } my ($rows,$cols) = getwinsize; print "rows=$rows, cols=$cols\n"; # vim: ts=2: sw=2
        My output:
        ERROR: ioctl on STDOUT: Bad address ERROR: ioctl on fileno(STDOUT): Bad file descriptor (rows=0, cols=0) rows=-1, cols=-1