From owner-svn-src-head@freebsd.org Wed Apr 13 00:30:44 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4ADD0B0DEEB; Wed, 13 Apr 2016 00:30:44 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 0AAA81F92; Wed, 13 Apr 2016 00:30:43 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u3D0Uhhn007875; Wed, 13 Apr 2016 00:30:43 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u3D0UggB007871; Wed, 13 Apr 2016 00:30:42 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201604130030.u3D0UggB007871@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: "Conrad E. Meyer" Date: Wed, 13 Apr 2016 00:30:42 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r297897 - in head/usr.bin: . resizewin X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Apr 2016 00:30:44 -0000 Author: cem Date: Wed Apr 13 00:30:42 2016 New Revision: 297897 URL: https://svnweb.freebsd.org/changeset/base/297897 Log: Add a small tool, resizewin(1), to query terminal for window size Submitted by: Daniel O'Connor Reviewed by: kan, wblock, cem Sponsored by: EMC / Isilon Storage Division Differential Revision: https://reviews.freebsd.org/D4438 Added: head/usr.bin/resizewin/ head/usr.bin/resizewin/Makefile (contents, props changed) head/usr.bin/resizewin/resizewin.1 (contents, props changed) head/usr.bin/resizewin/resizewin.c (contents, props changed) Modified: head/usr.bin/Makefile Modified: head/usr.bin/Makefile ============================================================================== --- head/usr.bin/Makefile Wed Apr 13 00:10:42 2016 (r297896) +++ head/usr.bin/Makefile Wed Apr 13 00:30:42 2016 (r297897) @@ -125,6 +125,7 @@ SUBDIR= alias \ protect \ rctl \ renice \ + resizewin \ rev \ revoke \ rpcinfo \ Added: head/usr.bin/resizewin/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.bin/resizewin/Makefile Wed Apr 13 00:30:42 2016 (r297897) @@ -0,0 +1,6 @@ +# $FreeBSD$ + +PROG= resizewin + +.include + Added: head/usr.bin/resizewin/resizewin.1 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.bin/resizewin/resizewin.1 Wed Apr 13 00:30:42 2016 (r297897) @@ -0,0 +1,64 @@ +.\" resizewin +.\" +.\" Query terminal for size and inform the kernel +.\" +.\" Copyright 2015 EMC / Isilon Storage Division +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. + +.\" $FreeBSD$ +.\" +.Dd March 17, 2016 +.Dt RESIZEWIN 1 +.Os +.Sh NAME +.Nm resizewin +.Nd update the kernel window size for the current TTY +.Sh DESCRIPTION +Query the terminal emulator window size with the +.Dv TIOCSWINSZ +ioctl and set the window size known by the kernel to the new values. +The terminal is assumed to be VT100/ANSI compatible. +.Nm +is functionally similar to +.Xr resize 1 , +which is part of the +.Xr xterm 1 +distribution. +However, +.Nm +only works with VT100/ANSI-compatible terminals and does +not emit commands to set environment variables. +.Pp +After a terminal window has been resized, running +.Nm +updates the kernel's window size to match the new size. +.Pp +Note that virtually all modern terninals support VT100/ANSI escape +sequences, including xterm, konsole, gnome-terminal iTerm, +Terminal.app, and puTTY. +.Sh SEE ALSO +.Xr resize 1 , +.Xr stty 1 +.Sh HISTORY +.Nm +appeared in FreeBSD 11. Added: head/usr.bin/resizewin/resizewin.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.bin/resizewin/resizewin.c Wed Apr 13 00:30:42 2016 (r297897) @@ -0,0 +1,129 @@ +/* + * resizewin + * + * Query terminal for size and inform the kernel + * + * Copyright 2015 EMC / Isilon Storage Division + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* screen doesn't support ESC[18t (return terminal size) so do it the hard way */ +static const char query[] = + "\0337" /* Save cursor position */ + "\033[r" /* Scroll whole screen */ + "\033[999;999H" /* Move cursor */ + "\033[6n" /* Get cursor position */ + "\0338"; /* Restore cursor position */ +int +main(__unused int argc, __unused char **argv) +{ + struct termios old, new; + struct winsize w; + int ret, fd, cnt, err; + char data[20]; + struct timeval then, now; + + err = 0; + + if ((fd = open("/dev/tty", O_RDWR | O_NONBLOCK)) == -1) + exit(1); + + /* Disable echo */ + if (tcgetattr(fd, &old) == -1) + exit(1); + + new = old; + new.c_cflag |= (CLOCAL | CREAD); + new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + if (tcsetattr(fd, TCSANOW, &new) == -1) + exit(1); + + if (write(fd, query, sizeof(query)) != sizeof(query)) { + err = 1; + goto out; + } + + /* Read the response */ + bzero(data, sizeof(data)); + gettimeofday(&then, NULL); + cnt = 0; + while (1) { + ret = read(fd, data + cnt, 1); + + if (ret == -1) { + if (errno == EAGAIN) { + gettimeofday(&now, NULL); + timersub(&now, &then, &now); + if (now.tv_sec >= 2) { + fprintf(stderr, "\n\n\nTimeout reading from terminal\n"); + fprintf(stderr, "Read %d bytes, %s\n", cnt, data); + err = 1; + goto out; + } + + usleep(20000); + continue; + } + err = 1; + goto out; + } + if (data[cnt] == 'R') + break; + + cnt++; + if (cnt == sizeof(data) - 2) { + fprintf(stderr, "Response too long\n"); + err = 1; + goto out; + } + } + + /* Parse */ + if (sscanf(data, "\033[%hu;%huR", &w.ws_row, &w.ws_col) != 2) { + err = 1; + fprintf(stderr, "Unable to parse response\n"); + goto out; + } + + /* Finally, what we want */ + if (ioctl(fd, TIOCSWINSZ, &w) == -1) + err = 1; + out: + /* Restore echo */ + tcsetattr(fd, TCSANOW, &old); + + close(fd); + exit(err); +}