🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.perl.misc
Article #4

Re: a question about TUIs and raw input from ttys

#4
From: Ivan Shmakov
Date: Sun, 29 Nov 2026 13:27
90 lines
4397 bytes
>>>>> On 2026-11-29, Eli the Bearded wrote:

 > What's the trick to reading raw input from a tty (xterm in my case)
 > to make Perl not miss things?

 > I've written a rough draft of a text mode (text user interface; TUI)
 > game yesterday and today, and it works, but the controls feel janky,
 > like it might take two key presses to get something done.

	Sounds like a buffering issue.  Let's see.

 > sub set_raw_tty() {
 >   qx( stty raw -echo -isig -icanon -ixon -icrnl );
 > }

	Here you disable in-kernel tty buffering, alright...

 > $nfound = select($rout = $rin, undef, undef, 0.05);
 > if($nfound) {
 >   my $c;
 >   if ( read STDIN, $c, 1 ) {

	... yet here you request in-Perl filehandle buffering instead.

	I suspect the Perl function you'd want to use here is sysread,
	not read.

	What happens here is this.  Suppose user types something like
	^[[31~ on their terminal.  This makes "select" return 1, then
	the following Perl "read" call results in a read(2) syscall
	requesting a pageful (4096 bytes) or so of input, of which
	"read" puts a single "^[" byte (I'm assuming you don't configure
	STDIN for UTF-8 reads here) into $c, and keeps the rest in its
	internal buffer.

	The next read /would/ return the rest of the input, but it never
	gets called, because the next "select" does not return 1 - the
	kernel has already given your process all the data it had!

	I was able to reproduce the issue with the following code: it
	indeed takes a second keypress after a "multibyte" key, and using
	"sysread" in place of "read" resolves the issue.

use common::sense;
require IO::Select;

system (qw { stty raw isig }); ## NB: isig so I can ^C it when done
my $in = \*STDIN;
my $sel = IO::Select->new ();
$sel->add ($in);
while (1) {
    my @rd = $sel->can_read (0.05);
    next
        unless (@rd);
    my $s;
    $rd = $in->read ($s, 1)
        or die ($!);
    print ("Got ", $s, "\n");
}

	FWIW, I don't recall ever trying to implement reading
	DEC VT/ANSI/ECMA 48 ESC-sequences from a terminal in Perl,
	but it turns out that in networking code, mixing buffered
	and unbuffered IO is also a recipe for trouble.  I've
	learned to use syscall tracers, such as "$ strace -ft " and
	ktruss(1), to troubleshoot such issues.

 > I would rather not use a curses library for this, as I want to
 > understand what a library like that is doing for me (besides reading
 > the termcap / terminfo files to generalize the control sequences).

	Unless I be mistaken, the Curses' main feature is that it
	maintains a screen buffer.  Which is handy for fullscreen
	applications, but less applicable for things like Readline
	- where using Termcap / Terminfo support alone, rather than
	the entire Curses library, makes more sense.

	And it annoys me to no end when an application refuses to consult
	the databases and just uses hardcoded ESC sequences instead.
	Like, "why, everyone now supports VT100 colors!"  (VT100, VT220,
	VT320 had no support for colors, if XTerm FAQ is to be believed.)

	^[[34mNever mind that I might be running the command under
	^[[1mscript(1)^[0m (or using "log" function built into GNU Screen.)

 > wants to get input solved before moving on to maze generation

	Reminds me of [1], somehow.

[1] http://linusakesson.net/games/autosokoban/
Technical Details
Message-ID: <wPRxP9j06Ip0AEM2@violet.siamics.net>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <eli$2611290036@qaz.wtf>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Ivan Shmakov <ivan@siamics.netREMOVE.invalid>
Newsgroups: comp.lang.perl.misc
Subject: Re: a question about TUIs and raw input from ttys
Date: Sun, 29 Nov 2026 13:27:30 +0000
Organization: Dbus-free station.
Lines: 90
Message-ID: <wPRxP9j06Ip0AEM2@violet.siamics.net>
References: <eli$2611290036@qaz.wtf>
Injection-Date: Sun, 29 Nov 2026 13:27:53 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3706390"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19cEyquLxdw2lMWLAazSTHl"; posting-host="4506b6559d0c87bf767b29da9c01204b"
Cancel-Lock: sha1:Vz12ox7LAJo9h3sOadAXRdpk3iY= sha256:uVY1k3XjBENhlK4skbpnLXggmHP4Gjt5n8S4H6T1YyY=
	sha1:hxpeyCmTr5132kPLs6X+bigGjRc= sha256:Lt5hGdPnpF77Tst/GLmdVVR+EeZAzoTDbJE9Srzm7JI=
License: CC-BY-SA-3.0+ (original contributions only)