1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
Subject: Call stty instead of ioctls to obtain the terminal size
From: Michael Schutte <michi@debian.org>
Bug: http://bugs.debian.org/697065
The ioctl-based determination of the number of rows and columns of the
terminal is fragile (see #697065). Read the output of “stty size”
instead.
Index: devel/lib/ticgit-ng/cli.rb
===================================================================
--- devel.orig/lib/ticgit-ng/cli.rb 2013-06-27 18:03:25.000000000 +0200
+++ devel/lib/ticgit-ng/cli.rb 2013-06-27 18:03:25.000000000 +0200
@@ -171,27 +171,22 @@
class << self
attr_accessor :window_lines, :window_cols
- TIOCGWINSZ_INTEL = 0x5413 # For an Intel processor
- TIOCGWINSZ_PPC = 0x40087468 # For a PowerPC processor
STDOUT_HANDLE = 0xFFFFFFF5 # For windows
def reset_window_width
- try_using(TIOCGWINSZ_PPC) ||
- try_using(TIOCGWINSZ_INTEL) ||
+ try_stty ||
try_windows ||
use_fallback
end
- # Set terminal dimensions using ioctl syscall on *nix platform
- # TODO: find out what is raised here on windows.
- def try_using(mask)
- buf = [0,0,0,0].pack("S*")
-
- if $stdout.ioctl(mask, buf) >= 0
- self.window_lines, self.window_cols = buf.unpack("S2")
- true
+ def try_stty
+ return unless $stdin.tty? and File.executable? "/bin/stty"
+ raw = `/bin/stty size`
+ if $? == 0 and mt = /^(\d+) (\d+)$/.match(raw)
+ self.window_lines, self.window_cols = mt[1].to_i, mt[2].to_i
+ else
+ nil
end
- rescue Errno::EINVAL
end
def try_windows
|