Skip to contentSkip to conversation

base-files/profile: Add universal resize function

PATCH In review Jason Wessel 2 participants 5 messages Nov 2, 2020 – Nov 3, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 base-files/profile: Add universal resize function

Using an editor or any kind of command line that wraps beyond the column width of the session on a serial port is quite problematic unless you are using an 80x24 session.

The original /etc/profile tried to use the resize binary if it was available. The problem is that you only get the resize binary if xterm, or busybox is installed.

This updated /etc/profile will add a resize function available to the shell when no xterm or busybox resize binary is found. More care is taken in this new version to test that terminal is interactive. The EDITOR and SHLVL environment variables are checked to prevent resize from running necessarily.

The function definitions are not indented intentionally to keep them to the 80 column width.

---
 .../base-files/base-files/profile             | 48 ++++++++++++++++---
 1 file changed, 41 insertions(+), 7 deletions(-)
meta/recipes-core/base-files/base-files/profile +41 -7
index 9e4283e0c7..cc37e1ba77 100644
@@ -2,7 +2,6 @@
 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
 
 PATH="/usr/local/bin:/usr/bin:/bin"
-EDITOR="vi"			# needed for packages like cron, git-commit
 [ "$TERM" ] || TERM="vt100"	# Basic terminal capab. For screen etc.
 
 # Add /sbin & co to $PATH for the root user
@@ -20,13 +19,48 @@ if [ -d /etc/profile.d ]; then
 	unset i
 fi
 
-# Make sure we are on a serial console (i.e. the device used starts with
-# /dev/tty[A-z]), otherwise we confuse e.g. the eclipse launcher which tries do
-# use ssh
-case $(tty 2>/dev/null) in
-	/dev/tty[A-z]*) [ -x @BINDIR@/resize ] && @BINDIR@/resize >/dev/null;;
-esac
+if [ -t 0 -a $# -eq 0 ]; then
+	if [ ! -x @BINDIR@/resize ] ; then
+		if [ -n "$BASH_VERSION" ] ; then
+# Optimized resize funciton for bash
+resize() {
+	local x y
+	IFS='[;' read -t 2 -p $(printf '\e7\e[r\e[999;999H\e[6n\e8') -sd R _ y x _
+	[ -n "$y" ] && \
+	echo -e "COLUMNS=$x;\nLINES=$y;\nexport COLUMNS LINES;" && \
+	stty cols $x rows $y
+}
+		else
+# Portable resize function for ash/bash/dash/ksh
+# with subshell to avoid local variables
+resize() {
+	(o=$(stty -g)
+	stty -echo raw min 0 time 2
+	printf '\0337\033[r\033[999;999H\033[6n\0338'
+	if echo R | read -d R x 2> /dev/null; then
+		IFS='[;R' read -t 2 -d R -r z y x _
+	else
+		IFS='[;R' read -r _ y x _
+	fi
+	stty "$o"
+	[ -z "$y" ] && y=${z##*[}&&x=${y##*;}&&y=${y%%;*}
+	[ -n "$y" ] && \
+	echo "COLUMNS=$x;"&&echo "LINES=$y;"&&echo "export COLUMNS LINES;"&& \
+	stty cols $x rows $y)
+}
+		fi
+	fi
+	# Use the EDITOR not being set as a trigger to call resize
+	# and only do this for /dev/tty[A-z] which are typically
+	# serial ports
+	if [ -z "$EDITOR" -a "$SHLVL" = 1 ] ; then
+		case $(tty 2>/dev/null) in
+			/dev/tty[A-z]*) resize >/dev/null;;
+		esac
+	fi
+fi
 
+EDITOR="vi"			# needed for packages like cron, git-commit
 export PATH PS1 OPIEDIR QPEDIR QTDIR EDITOR TERM
 
 umask 022
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 2/2 systemd-serialgetty: Switch to TERM=linux

Long ago in commit 473ff65c2f69de4ece3204fadfae7c5cb992149a (serial-getty service: Add xterm as default TERM), the xterm became the default for the serial port terminal.

Using the version of vim.tiny in oe-core master with the serial port connected in xterm version 322 (which is one of the most widely deployed versions at the current time) causes artifacts and missed characters.

The example sequence is the following:
* Start vim
* Press "i" to enter input mode
* Type "123"
* Press Escape to enter command mode
* Press "a" to enter append mode
* Type "456"

At this point if you are using xterm less than version 535 you will see on your screen "12456" instead of "123456".

Changing the TERM variable to "linux" will still allow you to have all the same functionality with colorization, ansi character escapes etc..., but will avoid the extra xterm specific escape sequence that only exists in the most recent versions of xterm.

This patch allows the end user to set the serial terminal type to something other than the new default of "linux" by changing the SERIAL_TERM variable in local.conf. For example:
SERIAL_TERM = "xterm"

---
 meta/recipes-core/systemd/systemd-serialgetty.bb                | 2 ++
 .../systemd/systemd-serialgetty/[email protected]           | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
meta/recipes-core/systemd/systemd-serialgetty.bb +2 -0
index 0cc0dc8c79..d142b30f7e 100644
@@ -6,6 +6,7 @@ LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/GPL-2.0;md5=80
 PR = "r5"
 
 SERIAL_CONSOLES ?= "115200;ttyS0"
+SERIAL_TERM ?= "linux"
 
 SRC_URI = "file://[email protected]"
 
@@ -22,6 +23,7 @@ do_install() {
 		install -d ${D}${sysconfdir}/systemd/system/getty.target.wants/
 		install -m 0644 ${WORKDIR}/[email protected] ${D}${systemd_unitdir}/system/
 		sed -i -e "s/\@BAUDRATE\@/$default_baudrate/g" ${D}${systemd_unitdir}/system/[email protected]
+		sed -i -e "s/\@TERM\@/${SERIAL_TERM}/g" ${D}${systemd_unitdir}/system/[email protected]
 
 		tmp="${SERIAL_CONSOLES}"
 		for entry in $tmp ; do
meta/recipes-core/systemd/systemd-serialgetty/[email protected] +1 -1
index 549d566009..b16fe1188e 100644
@@ -29,7 +29,7 @@ Conflicts=rescue.service
 Before=rescue.service
 
 [Service]
-Environment="TERM=xterm"
+Environment="TERM=@TERM@"
 ExecStart=-/sbin/agetty -8 -L %I @BAUDRATE@ $TERM
 Type=idle
 Restart=always
Signature · 1 line
2.17.1
#2 lore raw
In reply to Jason Wessel
Quoted 77 lines · Jason Wessel

Using an editor or any kind of command line that wraps beyond the column width of the session on a serial port is quite problematic unless you are using an 80x24 session.

The original /etc/profile tried to use the resize binary if it was available. The problem is that you only get the resize binary if xterm, or busybox is installed.

This updated /etc/profile will add a resize function available to the shell when no xterm or busybox resize binary is found. More care is taken in this new version to test that terminal is interactive. The EDITOR and SHLVL environment variables are checked to prevent resize from running necessarily.

The function definitions are not indented intentionally to keep them to the 80 column width.

---
 .../base-files/base-files/profile             | 48 ++++++++++++++++---
 1 file changed, 41 insertions(+), 7 deletions(-)
meta/recipes-core/base-files/base-files/profile +32 -7
index 9e4283e0c7..cc37e1ba77 100644
@@ -2,7 +2,6 @@
 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
 
 PATH="/usr/local/bin:/usr/bin:/bin"
-EDITOR="vi"                    # needed for packages like cron, git-commit
 [ "$TERM" ] || TERM="vt100"    # Basic terminal capab. For screen etc.
 
 # Add /sbin & co to $PATH for the root user
@@ -20,13 +19,48 @@ if [ -d /etc/profile.d ]; then
        unset i
 fi
 
-# Make sure we are on a serial console (i.e. the device used starts with
-# /dev/tty[A-z]), otherwise we confuse e.g. the eclipse launcher which tries do
-# use ssh
-case $(tty 2>/dev/null) in
-       /dev/tty[A-z]*) [ -x @BINDIR@/resize ] && @BINDIR@/resize >/dev/null;;
-esac
+if [ -t 0 -a $# -eq 0 ]; then
+       if [ ! -x @BINDIR@/resize ] ; then
+               if [ -n "$BASH_VERSION" ] ; then
+# Optimized resize funciton for bash
+resize() {
+       local x y
+       IFS='[;' read -t 2 -p $(printf '\e7\e[r\e[999;999H\e[6n\e8') -sd R _ y x _
+       [ -n "$y" ] && \
+       echo -e "COLUMNS=$x;\nLINES=$y;\nexport COLUMNS LINES;" && \
+       stty cols $x rows $y
+}
+               else
+# Portable resize function for ash/bash/dash/ksh
+# with subshell to avoid local variables
+resize() {
+       (o=$(stty -g)
+       stty -echo raw min 0 time 2
+       printf '\0337\033[r\033[999;999H\033[6n\0338'
+       if echo R | read -d R x 2> /dev/null; then
+               IFS='[;R' read -t 2 -d R -r z y x _
+       else
+               IFS='[;R' read -r _ y x _
+       fi
+       stty "$o"
+       [ -z "$y" ] && y=${z##*[}&&x=${y##*;}&&y=${y%%;*}
+       [ -n "$y" ] && \
+       echo "COLUMNS=$x;"&&echo "LINES=$y;"&&echo "export COLUMNS LINES;"&& \
+       stty cols $x rows $y)
+}
+               fi
+       fi
+       # Use the EDITOR not being set as a trigger to call resize

meta/recipes-core/base-files/base-files/profile if [ -d /etc/profile.d ]; then · line 63

Quoted 10 lines
meta/recipes-core/base-files/base-files/profile +9 -0
+       # and only do this for /dev/tty[A-z] which are typically
+       # serial ports
+       if [ -z "$EDITOR" -a "$SHLVL" = 1 ] ; then
+               case $(tty 2>/dev/null) in
+                       /dev/tty[A-z]*) resize >/dev/null;;
+               esac
+       fi
+fi
 
+EDITOR="vi"                    # needed for packages like cron, git-commit

If we set vi as the default, should it be in the RDEPENDS for base-files ?

It's a bit of a grey area, since base-files has some special dependency tweaks, and you actually need to trigger some functionality for this to be used .. hence it isn't a hard rdepends.

Bruce

Quoted 9 lines
meta/recipes-core/base-files/base-files/profile +0 -1
 export PATH PS1 OPIEDIR QPEDIR QTDIR EDITOR TERM
 
 umask 022
--

2.17.1

Signature · 3 lines
- Thou shalt not follow the NULL pointer, for chaos and madness await
thee at its end
- "Use the force Harry" - Gandalf, Star Trek II
#3 lore raw
Quoted 87 lines · Bruce Ashfield
Quoted 86 lines · Jason Wessel

Using an editor or any kind of command line that wraps beyond the column width of the session on a serial port is quite problematic unless you are using an 80x24 session.

The original /etc/profile tried to use the resize binary if it was available. The problem is that you only get the resize binary if xterm, or busybox is installed.

This updated /etc/profile will add a resize function available to the shell when no xterm or busybox resize binary is found. More care is taken in this new version to test that terminal is interactive. The EDITOR and SHLVL environment variables are checked to prevent resize from running necessarily.

The function definitions are not indented intentionally to keep them to the 80 column width.

---
 .../base-files/base-files/profile             | 48 ++++++++++++++++---
 1 file changed, 41 insertions(+), 7 deletions(-)
meta/recipes-core/base-files/base-files/profile +40 -7
index 9e4283e0c7..cc37e1ba77 100644
@@ -2,7 +2,6 @@
 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
 
 PATH="/usr/local/bin:/usr/bin:/bin"
-EDITOR="vi"                    # needed for packages like cron, git-commit
 [ "$TERM" ] || TERM="vt100"    # Basic terminal capab. For screen etc.
 
 # Add /sbin & co to $PATH for the root user
@@ -20,13 +19,48 @@ if [ -d /etc/profile.d ]; then
        unset i
 fi
 
-# Make sure we are on a serial console (i.e. the device used starts with
-# /dev/tty[A-z]), otherwise we confuse e.g. the eclipse launcher which tries do
-# use ssh
-case $(tty 2>/dev/null) in
-       /dev/tty[A-z]*) [ -x @BINDIR@/resize ] && @BINDIR@/resize >/dev/null;;
-esac
+if [ -t 0 -a $# -eq 0 ]; then
+       if [ ! -x @BINDIR@/resize ] ; then
+               if [ -n "$BASH_VERSION" ] ; then
+# Optimized resize funciton for bash
+resize() {
+       local x y
+       IFS='[;' read -t 2 -p $(printf '\e7\e[r\e[999;999H\e[6n\e8') -sd R _ y x _
+       [ -n "$y" ] && \
+       echo -e "COLUMNS=$x;\nLINES=$y;\nexport COLUMNS LINES;" && \
+       stty cols $x rows $y
+}
+               else
+# Portable resize function for ash/bash/dash/ksh
+# with subshell to avoid local variables
+resize() {
+       (o=$(stty -g)
+       stty -echo raw min 0 time 2
+       printf '\0337\033[r\033[999;999H\033[6n\0338'
+       if echo R | read -d R x 2> /dev/null; then
+               IFS='[;R' read -t 2 -d R -r z y x _
+       else
+               IFS='[;R' read -r _ y x _
+       fi
+       stty "$o"
+       [ -z "$y" ] && y=${z##*[}&&x=${y##*;}&&y=${y%%;*}
+       [ -n "$y" ] && \
+       echo "COLUMNS=$x;"&&echo "LINES=$y;"&&echo "export COLUMNS LINES;"&& \
+       stty cols $x rows $y)
+}
+               fi
+       fi
+       # Use the EDITOR not being set as a trigger to call resize
+       # and only do this for /dev/tty[A-z] which are typically
+       # serial ports
+       if [ -z "$EDITOR" -a "$SHLVL" = 1 ] ; then
+               case $(tty 2>/dev/null) in
+                       /dev/tty[A-z]*) resize >/dev/null;;
+               esac
+       fi
+fi
 
Quoted 7 lines

meta/recipes-core/base-files/base-files/profile if [ -d /etc/profile.d ]; then · line 63

Quoted 1 line
meta/recipes-core/base-files/base-files/profile +1 -0
+EDITOR="vi"                    # needed for packages like cron, git-commit

If we set vi as the default, should it be in the RDEPENDS for base-files ?

It's a bit of a grey area, since base-files has some special dependency tweaks, and you actually need to trigger some functionality for this to be used .. hence it isn't a hard rdepends.

The issue you are pointing out has nothing to do with the problem the patch is solving. As far as I can tell the EDITOR=vi variable has been set for the entire existence oe-core. I just moved it as an extra safety check for not continuing to run resize in subshells, which does happen today.

If that part is controversial for some reason, I'll revert that part and just check SHLVL.

Jason.

#4 lore raw
Quoted 100 lines · Jason Wessel
Quoted 87 lines · Bruce Ashfield
Quoted 86 lines · Jason Wessel

Using an editor or any kind of command line that wraps beyond the column width of the session on a serial port is quite problematic unless you are using an 80x24 session.

The original /etc/profile tried to use the resize binary if it was available. The problem is that you only get the resize binary if xterm, or busybox is installed.

This updated /etc/profile will add a resize function available to the shell when no xterm or busybox resize binary is found. More care is taken in this new version to test that terminal is interactive. The EDITOR and SHLVL environment variables are checked to prevent resize from running necessarily.

The function definitions are not indented intentionally to keep them to the 80 column width.

---
 .../base-files/base-files/profile             | 48 ++++++++++++++++---
 1 file changed, 41 insertions(+), 7 deletions(-)
meta/recipes-core/base-files/base-files/profile +40 -7
index 9e4283e0c7..cc37e1ba77 100644
@@ -2,7 +2,6 @@
 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
 
 PATH="/usr/local/bin:/usr/bin:/bin"
-EDITOR="vi"                    # needed for packages like cron, git-commit
 [ "$TERM" ] || TERM="vt100"    # Basic terminal capab. For screen etc.
 
 # Add /sbin & co to $PATH for the root user
@@ -20,13 +19,48 @@ if [ -d /etc/profile.d ]; then
        unset i
 fi
 
-# Make sure we are on a serial console (i.e. the device used starts with
-# /dev/tty[A-z]), otherwise we confuse e.g. the eclipse launcher which tries do
-# use ssh
-case $(tty 2>/dev/null) in
-       /dev/tty[A-z]*) [ -x @BINDIR@/resize ] && @BINDIR@/resize >/dev/null;;
-esac
+if [ -t 0 -a $# -eq 0 ]; then
+       if [ ! -x @BINDIR@/resize ] ; then
+               if [ -n "$BASH_VERSION" ] ; then
+# Optimized resize funciton for bash
+resize() {
+       local x y
+       IFS='[;' read -t 2 -p $(printf '\e7\e[r\e[999;999H\e[6n\e8') -sd R _ y x _
+       [ -n "$y" ] && \
+       echo -e "COLUMNS=$x;\nLINES=$y;\nexport COLUMNS LINES;" && \
+       stty cols $x rows $y
+}
+               else
+# Portable resize function for ash/bash/dash/ksh
+# with subshell to avoid local variables
+resize() {
+       (o=$(stty -g)
+       stty -echo raw min 0 time 2
+       printf '\0337\033[r\033[999;999H\033[6n\0338'
+       if echo R | read -d R x 2> /dev/null; then
+               IFS='[;R' read -t 2 -d R -r z y x _
+       else
+               IFS='[;R' read -r _ y x _
+       fi
+       stty "$o"
+       [ -z "$y" ] && y=${z##*[}&&x=${y##*;}&&y=${y%%;*}
+       [ -n "$y" ] && \
+       echo "COLUMNS=$x;"&&echo "LINES=$y;"&&echo "export COLUMNS LINES;"&& \
+       stty cols $x rows $y)
+}
+               fi
+       fi
+       # Use the EDITOR not being set as a trigger to call resize
+       # and only do this for /dev/tty[A-z] which are typically
+       # serial ports
+       if [ -z "$EDITOR" -a "$SHLVL" = 1 ] ; then
+               case $(tty 2>/dev/null) in
+                       /dev/tty[A-z]*) resize >/dev/null;;
+               esac
+       fi
+fi
 
Quoted 7 lines

meta/recipes-core/base-files/base-files/profile if [ -d /etc/profile.d ]; then · line 63

Quoted 1 line
meta/recipes-core/base-files/base-files/profile +1 -0
+EDITOR="vi"                    # needed for packages like cron, git-commit

If we set vi as the default, should it be in the RDEPENDS for base-files ?

It's a bit of a grey area, since base-files has some special dependency tweaks, and you actually need to trigger some functionality for this to be used .. hence it isn't a hard rdepends.

Quoted 6 lines

The issue you are pointing out has nothing to do with the problem the patch is solving. As far as I can tell the EDITOR=vi variable has been set for the entire existence oe-core. I just moved it as an extra safety check for not continuing to run resize in subshells, which does happen today.

It just shows up as an addition in the patch at hand, so I was reacting to that.

I didn't look into the history at all, but now that I scan up the patch, I see that it is just relocated to where it stands now .. so yah, nothing to see here. Leave it as-is.

Bruce

Quoted 4 lines

If that part is controversial for some reason, I'll revert that part and just check SHLVL.

Jason.

Signature · 3 lines
- Thou shalt not follow the NULL pointer, for chaos and madness await
thee at its end
- "Use the force Harry" - Gandalf, Star Trek II