| Kaz Kylheku 2006-10-20, 7:23 pm |
| I'm working with some embedded Linux hardware over a serial console.
The distribution is small, and so it doesn't have the "resize" program
that is part of X11. You need this to set the kernel tty parameters if
you resize your window, and so I wrote a replacement shell script.
This needs the GNU coreutils printf, which I used because it supports
the \e escape sequence for encoding the escape character.
It also relies on the "raw" alias being supported by stty, which stands
for a whole bunch of tty parameters.
#!/bin/sh
#
# resize - script to detect actual terminal window size
# and set the appropriate tty kernel params
# similarly to the 20+ year old resize program from X11.
#
# Kaz Kylheku <kkylheku@gmail.com>
# October 2006
#
if [ $# -ne 0 ] ; then
echo This is not the X11 resize program, but a shell script
imitation.
echo It does not process any arguments.
exit 1
fi
# put tty in raw mode
saved_tty=$(stty -g)
stty raw -echo
# Save cursor position
printf "\e7"
# Ask to position the cursor to location 999,999
printf "\e[999;999H"
# Query the actual cursor position
printf "\e[6n"
# read tty response
# We use dd to read character by character
# until encountering the R letter,
# The response syntax is ESC[<row>;<col>R
while true; do
char=$(dd bs=1 count=1 2> /dev/null)
if [ "$char" = "R" ] ; then
break;
fi
tty_response="$tty_response$char"
done
# Restore cursor position
printf "\e8"
# restore tty
stty $saved_tty
# Set up tty
set_tty_params()
{
stty rows $2 columns $3
cat <<!
COLUMNS=$3;
LINES=$2;
export COLUMNS LINES;
!
}
# Trick: use IFS to parse the VT100 response.
# Since [ ; and R are delimiters, this means
# that the ESC, <row> and <col> will be
# extracted as tokens, and passed to the
# function as $1, $2 and $3.
IFS='[;R'
set_tty_params $tty_response
|