Deutsch   English   Français   Italiano  
<vrvv30$spj$1@nnrp.usenet.blueworldhosting.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED!not-for-mail
From: "Arti F. Idiot" <addr@is.invalid>
Newsgroups: comp.lang.awk
Subject: Re: getchar implementation without GNUishms
Date: Tue, 25 Mar 2025 22:16:32 -0600
Organization: Anarchists of America
Message-ID: <vrvv30$spj$1@nnrp.usenet.blueworldhosting.com>
References: <slrnvu4v4s.1su2.anthk@openbsd.home>
 <vrua0r$3d1du$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 26 Mar 2025 04:16:32 -0000 (UTC)
Injection-Info: nnrp.usenet.blueworldhosting.com;
	logging-data="29491"; mail-complaints-to="usenet@blueworldhosting.com"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:LpfaWYJWUQr9/Noxkr9EowI3QVs= sha256:KXBNpuZa/2CkOhj+p3xBs0JVz4ny8qxbPkD4paU1xIo=
	sha1:AEXr8Am+EO054z6YXR/Ms6tOgJE= sha256:nq2i8PkU9eZ7yW76lCb+L6dEJIpmyPjB+E8e70X8JUU=
In-Reply-To: <vrua0r$3d1du$1@dont-email.me>
Content-Language: en-US
Bytes: 2839
Lines: 54

On 3/25/25 7:10 AM, Janis Papanagnou wrote:
> On 25.03.2025 10:51, anthk wrote:
>> Hello the guy from https://luxferre.top and gopher://hoi.st
>> has pretty interesting stuff, such as
>>
>> [ shell specific or other tries to emulate some getchar function ]
>>
>> Could it be possible to implement a true portable getchar?
> 
> Those who think that getchar is a useful function may implement that
> natively in Awk. (It avoids external dependencies and all the issues
> that the posted/quoted code has made obvious.)


I know you can iterate over input strings a character at a time in AWK
but I don't think you can read a single character from stdin without
also providing a newline via ENTER, which is perhaps what the OP was
actually wanting to do?

I kind of like making CLI interactive stuff and like not having to
press ENTER after entering single character menu choices.  Tried a
few options using bash and lua before landing on stty(1) and dd(1),
both POSIX tools:

--
# getchar_posix.awk - read exactly one char from stdin and return it
#                     without waiting for ENTER to be pressed.
#

BEGIN {
     printf "enter a char: "
     Char = getchar()
     printf "\n you entered: %s\n", Char
}

function getchar(   ,Cmd, Chr) {
     # put TTY in "raw" mode..
     system ("stty -icanon")
     #
     # read  Chr via dd(1)..
     Cmd = "dd bs=1 count=1 2>/dev/null"
     Cmd | getline Chr
     close (Cmd)
     #
     # put TTY in "normal" mode..
     system ("stty icanon")
     #
     return Chr
}
--

Saving the TTY state via 'stty -g' beforehand would probably be a good
addition so if things go sideways the TTY isn't a mess.

-A