Can we capture the response of a terminal?
Posted: 21 Sep 2021 10:31
There are control sequences that you can send to a virtual terminal emulator to request certain information. The response is written to the stdin which seems to make it quite difficult to read it in a Batch code. The example below is a small code which demonstrates what I'm talking about all along. It tries to capture the Device Attributes. I've chosen this because also the V2 console (Win 10) sends a response to this request. (So, it's not necessary to have Windows Terminal or any other 3rd party terminal installed.)
My question: Is there a possibility to get the response string captured in a variable or file without any further user interaction? I'm pretty sure this can be done with the aid of another scripting language like PowerShell. However, I'd like to know if there is a pure Batch solution in the first place.
Steffen
My question: Is there a possibility to get the response string captured in a variable or file without any further user interaction? I'm pretty sure this can be done with the aid of another scripting language like PowerShell. However, I'd like to know if there is a pure Batch solution in the first place.
Steffen
Code: Select all
@echo off &setlocal
for /f %%i in ('echo prompt $E^|cmd') do set "ESC=%%i"
set "CSI=%ESC%[" &REM Control Sequence Introducer
set "wr=<nul set /p =" &REM write without new line
:::::::::::::::::::::::::::::::::::::::::::::::::::
echo(
echo Press [Enter] to capture the value ...
set "devattr="
:: send the sequence using the prompt of SET /P and use the behavior of SET /P to wait for input at the same time
:: however, the response isn't assigned without pressing Enter
set /p "devattr=%CSI%c"
echo "%devattr:~3,-1%"
pause
echo(
echo Press [Enter], [Ctrl]+[Z], [Enter] to capture the value ...
set "devattr="
:: send the sequence
%wr%%CSI%c
:: print the buffer of the console input device using TYPE
:: however, the input stream does not have an EOF which is the reason why [Ctrl]+[Z] is necessary to interrupt the stream processing
for /f "delims=" %%i in ('type conin$') do set "devattr=%%i"
echo "%devattr:~3,-1%"
pause
echo(
echo Press [Enter], [Ctrl]+[Z], [Enter] to capture the value ...
set "devattr="
:: send the sequence
%wr%%CSI%c
:: pretty much the same behavior here, the stream has no EOF and the copy command is waiting for more input
copy conin$ "devattr.~tmp"
<"devattr.~tmp" set /p "devattr="
del "devattr.~tmp"
echo "%devattr:~3,-1%"
pause