Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

I have never seen anything fill up a vacuum so fast and still suck. -- Rob Pike, on X.


devel / comp.unix.programmer / Getting a keypress

SubjectAuthor
* Getting a keypressJanis Papanagnou
+* Getting a keypressMuttley
|+- Getting a keypressJanis Papanagnou
|`* Getting a keypressJanis Papanagnou
| `* Getting a keypressMuttley
|  `* Getting a keypressJanis Papanagnou
|   +* Getting a keypressKaz Kylheku
|   |+- Getting a keypressJanis Papanagnou
|   |`* Getting a keypressJulieta Shem
|   | `* Getting a keypressKaz Kylheku
|   |  `* Getting a keypressJulieta Shem
|   |   +- Getting a keypressNuno Silva
|   |   `* [OT] Vim user experience (was Re: Getting a keypress)Janis Papanagnou
|   |    `- [OT] Vim user experience (was Re: Getting a keypress)Julieta Shem
|   `* Getting a keypressKaz Kylheku
|    `- Getting a keypressJanis Papanagnou
+* Getting a keypressMuttley
|`- Getting a keypressKaz Kylheku
+- Getting a keypressJanis Papanagnou
+* Getting a keypressScott Lurndal
|+* Getting a keypressNicolas George
||+- Getting a keypressScott Lurndal
||`* Getting a keypressKaz Kylheku
|| `* Getting a keypressAndreas Kempe
||  `* Getting a keypressScott Lurndal
||   `- Getting a keypressNicolas George
|`* Getting a keypressJanis Papanagnou
| +* Getting a keypressScott Lurndal
| |`- Getting a keypressJanis Papanagnou
| `- Getting a keypressRainer Weikusat
`- Getting a keypressKaz Kylheku

Pages:12
Getting a keypress

<ulroor$116$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10883&group=comp.unix.programmer#10883

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!nntp.comgw.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Getting a keypress
Date: Tue, 19 Dec 2023 10:46:02 +0100
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <ulroor$116$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Dec 2023 09:46:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ddbd8a01525bb248976411cfd182113";
logging-data="1062"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18evSNeARZ3UNZReoUjgJwd"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:EwH4KELB1NmAHydbeH0SJ9dS6RU=
X-Mozilla-News-Host: news://news.eternal-september.org:119
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Tue, 19 Dec 2023 09:46 UTC

I'm trying to get some pressed keys control a piece of software.
And I'm using ANSI escapes to control terminal screen output.
Both works nicely in separate tests, but assembled the former
seems to affect the latter. So I am looking for hints/caveats on
what I am possibly doing wrong.

The "poll pressed key" function is basically derived from Steven's
APUE book (tty_cbreak). The main functional parts are the following
settings...

fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);

and

struct termios buf;
buf.c_lflag &= ~(ECHO | ICANON);
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
tcsetattr(fd, TCSAFLUSH, &buf);

Given that there are bazillions of flags to set or clear; is
there anything I should add or omit? (Or something else I should
have an eye on?)

Janis

Re: Getting a keypress

<ulrpqq$70r$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10884&group=comp.unix.programmer#10884

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Mutt...@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 10:04:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <ulrpqq$70r$1@dont-email.me>
References: <ulroor$116$1@dont-email.me>
Injection-Date: Tue, 19 Dec 2023 10:04:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eb4a811d0d70cc3af0db0116d18af34c";
logging-data="7195"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kLgyvLCxI2We6swZ7dus0"
Cancel-Lock: sha1:HEuSAEmWmBTCsUv1R5RtrgFi1bE=
 by: Mutt...@dastardlyhq.com - Tue, 19 Dec 2023 10:04 UTC

On Tue, 19 Dec 2023 10:46:02 +0100
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>I'm trying to get some pressed keys control a piece of software.
>And I'm using ANSI escapes to control terminal screen output.

Make sure your terminal is 100% ANSI compatible. Not all are - some codes will
work, others won't and others can cause odd behaviour.

>Both works nicely in separate tests, but assembled the former
>seems to affect the latter. So I am looking for hints/caveats on
>what I am possibly doing wrong.
>
>The "poll pressed key" function is basically derived from Steven's
>APUE book (tty_cbreak). The main functional parts are the following
>settings...
>
> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);

I've never had to use that, try removing it to see if it helps.

>and
>
> struct termios buf;
> buf.c_lflag &= ~(ECHO | ICANON);
> buf.c_cc[VMIN] = 1;
> buf.c_cc[VTIME] = 0;
> tcsetattr(fd, TCSAFLUSH, &buf);

Instead of TCSAFLUSH try TCSANOW and make sure you check the return code
for -1.

Re: Getting a keypress

<ulrpvl$80n$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10885&group=comp.unix.programmer#10885

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Mutt...@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 10:06:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <ulrpvl$80n$1@dont-email.me>
References: <ulroor$116$1@dont-email.me>
Injection-Date: Tue, 19 Dec 2023 10:06:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eb4a811d0d70cc3af0db0116d18af34c";
logging-data="8215"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Oli51akBqWTNvM+7JzMZQ"
Cancel-Lock: sha1:s7LiULJOZjMAYMHBo7vYKWqyBmg=
 by: Mutt...@dastardlyhq.com - Tue, 19 Dec 2023 10:06 UTC

On Tue, 19 Dec 2023 10:46:02 +0100
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>I'm trying to get some pressed keys control a piece of software.
>And I'm using ANSI escapes to control terminal screen output.
>Both works nicely in separate tests, but assembled the former
>seems to affect the latter. So I am looking for hints/caveats on
>what I am possibly doing wrong.
>
>The "poll pressed key" function is basically derived from Steven's
>APUE book (tty_cbreak). The main functional parts are the following
>settings...
>
> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
>
>and
>
> struct termios buf;
> buf.c_lflag &= ~(ECHO | ICANON);
> buf.c_cc[VMIN] = 1;
> buf.c_cc[VTIME] = 0;
> tcsetattr(fd, TCSAFLUSH, &buf);

Also are you sure fd is stdin? Perhaps use STDIN_FILENO.

Re: Getting a keypress

<ulsh9o$4b4l$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10886&group=comp.unix.programmer#10886

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 17:44:39 +0100
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <ulsh9o$4b4l$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Dec 2023 16:44:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ddbd8a01525bb248976411cfd182113";
logging-data="142485"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19a3DDZWsYdhKeRlI1pIRYB"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Vuo4JVpRtzKL5aONVGX9/ClFgSI=
In-Reply-To: <ulrpqq$70r$1@dont-email.me>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Tue, 19 Dec 2023 16:44 UTC

On 19.12.2023 11:04, Muttley@dastardlyhq.com wrote:
> On Tue, 19 Dec 2023 10:46:02 +0100
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> I'm trying to get some pressed keys control a piece of software.
>> And I'm using ANSI escapes to control terminal screen output.
>
> Make sure your terminal is 100% ANSI compatible. Not all are - some codes will
> work, others won't and others can cause odd behaviour.

Yes, as said, individually the two features work flawlessly.
I am using ANSI escapes on a regular basis without problems.
And I avoid (if possible) escapes that are marked to be (in
any way) "specific".

>
>> Both works nicely in separate tests, but assembled the former
>> seems to affect the latter. So I am looking for hints/caveats on
>> what I am possibly doing wrong.
>>
>> The "poll pressed key" function is basically derived from Steven's
>> APUE book (tty_cbreak). The main functional parts are the following
>> settings...
>>
>> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
>
> I've never had to use that, try removing it to see if it helps.

I would think that non-blocking read(2) is what I need to
_poll_ the status; am I missing something here?

>
>> and
>>
>> struct termios buf;
>> buf.c_lflag &= ~(ECHO | ICANON);
>> buf.c_cc[VMIN] = 1;
>> buf.c_cc[VTIME] = 0;
>> tcsetattr(fd, TCSAFLUSH, &buf);
>
> Instead of TCSAFLUSH try TCSANOW and make sure you check the return code
> for -1.

All system calls in my programs check for the respective exit
codes. I just omitted all that ballast for posting terseness
and focused on the functional parts only.

Elsethread you asked:
> Also are you sure fd is stdin? Perhaps use STDIN_FILENO.

Yes, fd is a parameter from my poll function that is defined
as STDIN_FILENO.

Richard Stevens, where I took the basic pieces of code from,
was very painstaking with his code samples. :-)

Thanks.

Janis

Re: Getting a keypress

<ulshe7$4buf$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10887&group=comp.unix.programmer#10887

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 17:47:02 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <ulshe7$4buf$1@dont-email.me>
References: <ulroor$116$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Dec 2023 16:47:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ddbd8a01525bb248976411cfd182113";
logging-data="143311"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+TBK9kBo2x1ES8aAkA1MYQ"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:vnarSBmXCUK7Hzef193ovXT2zyo=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <ulroor$116$1@dont-email.me>
 by: Janis Papanagnou - Tue, 19 Dec 2023 16:47 UTC

On 19.12.2023 10:46, Janis Papanagnou wrote:
> I'm trying to get some pressed keys control a piece of software.
> And I'm using ANSI escapes to control terminal screen output.
> Both works nicely in separate tests, but assembled the former
> seems to affect the latter. So I am looking for hints/caveats on
> what I am possibly doing wrong.
>
> The "poll pressed key" function is basically derived from Steven's
> APUE book (tty_cbreak). The main functional parts are the following
> settings...
>
> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
>
> and
>
> struct termios buf;
> buf.c_lflag &= ~(ECHO | ICANON);
> buf.c_cc[VMIN] = 1;
> buf.c_cc[VTIME] = 0;
> tcsetattr(fd, TCSAFLUSH, &buf);
>
> Given that there are bazillions of flags to set or clear; is
> there anything I should add or omit? (Or something else I should
> have an eye on?)

Just to clarify the issue I seem to have after the first replies
and emphasize what works...

Keystrokes can be obtained in a test program using the depicted
method, and ANSI escapes for screen control generally work as well.
But in combination the same screen drawing procedures don't work
any more; that's why I suspected some interference of these termios
flags with the ANSI controls, where the latter are just output by
the C functions printf() or fputs().

Janis

Re: Getting a keypress

<92kgN.11545$5Hnd.7072@fx03.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10888&group=comp.unix.programmer#10888

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.nntp4.net!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx03.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Getting a keypress
Newsgroups: comp.unix.programmer
References: <ulroor$116$1@dont-email.me>
Lines: 47
Message-ID: <92kgN.11545$5Hnd.7072@fx03.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 19 Dec 2023 17:04:05 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 19 Dec 2023 17:04:05 GMT
X-Received-Bytes: 2270
 by: Scott Lurndal - Tue, 19 Dec 2023 17:04 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>I'm trying to get some pressed keys control a piece of software.
>And I'm using ANSI escapes to control terminal screen output.
>Both works nicely in separate tests, but assembled the former
>seems to affect the latter. So I am looking for hints/caveats on
>what I am possibly doing wrong.
>
>The "poll pressed key" function is basically derived from Steven's
>APUE book (tty_cbreak). The main functional parts are the following
>settings...
>
> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
>
>and
>
> struct termios buf;
> buf.c_lflag &= ~(ECHO | ICANON);
> buf.c_cc[VMIN] = 1;
> buf.c_cc[VTIME] = 0;
> tcsetattr(fd, TCSAFLUSH, &buf);
>
>Given that there are bazillions of flags to set or clear; is
>there anything I should add or omit? (Or something else I should
>have an eye on?)

Use poll(2) instead of read(2). IIRC, O_NONBLOCK doesn't necessarily
apply to PTYs.

diag = ::tcgetattr(u_ttyfd, &u_term_attr_orig);
if (diag == -1) {
u_logger->log("%s: Unable to get attributes for '%s': %s\n",
u_appname, devname, strerror(errno));
goto leave;
}

u_term_attr_cur = u_term_attr_orig;
::cfmakeraw(&u_term_attr_cur);
u_term_attr_cur.c_lflag = 0;
u_term_attr_cur.c_oflag |= OPOST|ONLCR;

diag = ::tcsetattr(u_ttyfd, TCSANOW, &u_term_attr_cur);
if (diag == -1) {
u_logger->log("%s: Unable to set attributes for '%s': %s\n",
u_appname, devname, strerror(errno));
goto leave;
}
u_term_attr_set = true;

Re: Getting a keypress

<6581e124$0$6111$426a74cc@news.free.fr>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10889&group=comp.unix.programmer#10889

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-b.proxad.net!nnrp1-1.free.fr!not-for-mail
Newsgroups: comp.unix.programmer
From: nicolas$...@salle-s.org (Nicolas George)
Subject: Re: Getting a keypress
Sender: george@phare.invalid (Nicolas George)
X-Newsreader: Flrn (0.9.20070704)
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 19 Dec 2023 18:29:56 GMT
Lines: 10
Message-ID: <6581e124$0$6111$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 19 Dec 2023 19:29:56 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1703010596 news-3.free.fr 6111 129.199.129.80:48450
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Tue, 19 Dec 2023 18:29 UTC

Scott Lurndal, dans le message <92kgN.11545$5Hnd.7072@fx03.iad>, a
écrit :
> Use poll(2) instead of read(2). IIRC, O_NONBLOCK doesn't necessarily
> apply to PTYs.

O_NONBLOCK applies to ptys, and if it did not then poll() would not either.

> diag = ::tcgetattr

What is this ugly horror?

Re: Getting a keypress

<IulgN.72152$Wp_8.21697@fx17.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10890&group=comp.unix.programmer#10890

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!tncsrv06.tnetconsulting.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx17.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Getting a keypress
Newsgroups: comp.unix.programmer
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad> <6581e124$0$6111$426a74cc@news.free.fr>
Lines: 21
Message-ID: <IulgN.72152$Wp_8.21697@fx17.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 19 Dec 2023 18:42:48 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 19 Dec 2023 18:42:48 GMT
X-Received-Bytes: 1244
 by: Scott Lurndal - Tue, 19 Dec 2023 18:42 UTC

Nicolas George <nicolas$george@salle-s.org> writes:
>Scott Lurndal, dans le message <92kgN.11545$5Hnd.7072@fx03.iad>, a
> �crit�:
>> Use poll(2) instead of read(2). IIRC, O_NONBLOCK doesn't necessarily
>> apply to PTYs.
>
>O_NONBLOCK applies to ptys, and if it did not then poll() would not either.

Actually, poll should work just fine without O_NONBLOCK,
but that said, you are correct that O_NONBLOCK applies to ptys.

I use:

u_ttyfd = ::open(devname, O_RDWR|O_NOCTTY|O_NONBLOCK, 0);

>
>> diag = ::tcgetattr
>
>What is this ugly horror?

Why do you care? This isn't comp.lang.c.

Re: Getting a keypress

<ulspcn$5o9p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10891&group=comp.unix.programmer#10891

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 20:02:46 +0100
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <ulspcn$5o9p$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Dec 2023 19:02:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ddbd8a01525bb248976411cfd182113";
logging-data="188729"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tzHldMunKfazT7uW/7rKK"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:6es56xwfCpMcREnwqI7lWvHLb1s=
In-Reply-To: <ulrpqq$70r$1@dont-email.me>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Tue, 19 Dec 2023 19:02 UTC

On 19.12.2023 11:04, Muttley@dastardlyhq.com wrote:
> On Tue, 19 Dec 2023 10:46:02 +0100
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>>
>> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
>
> I've never had to use that, try removing it to see if it helps.

I am confused. - I removed that fcntl() call completely and the output
wasn't affected any more in the observed negative way. - Why that?
Don't I need to use non-blocking I/O for a poll?
(Or is maybe buf.c_cc[VMIN] = 1 serving the same purpose?)
But why has a non-blocking input definition an effect on _output_?
You see me puzzled.

>>
>> struct termios buf;
>> buf.c_lflag &= ~(ECHO | ICANON);
>> buf.c_cc[VMIN] = 1;
>> buf.c_cc[VTIME] = 0;
>> tcsetattr(fd, TCSAFLUSH, &buf);
>
> Instead of TCSAFLUSH try TCSANOW [...]

I tried this change, but it had no visible effect (neither to the good
nor to the bad).

So I'll try to continue my code without the above fcntl() call.
Thanks.

Explanations and more suggestions still welcome.

Janis

Re: Getting a keypress

<ulsq95$5ti4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10892&group=comp.unix.programmer#10892

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 20:17:56 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <ulsq95$5ti4$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Dec 2023 19:17:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ddbd8a01525bb248976411cfd182113";
logging-data="194116"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19TMcR5CGF6SkFM0g9sTnhY"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:bhvpiLzDxcaEpAAKq6TD0TGUD74=
In-Reply-To: <92kgN.11545$5Hnd.7072@fx03.iad>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Tue, 19 Dec 2023 19:17 UTC

On 19.12.2023 18:04, Scott Lurndal wrote:
>
> Use poll(2) instead of read(2). [...]

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

It isn't explicitly mentioned in the man-page, but I assume a
timeout value of 0 will actually do the non-blocking behavior
with poll(2).

>
> diag = ::tcgetattr(u_ttyfd, &u_term_attr_orig);
> if (diag == -1) {
> u_logger->log("%s: Unable to get attributes for '%s': %s\n",
> u_appname, devname, strerror(errno));
> goto leave;
> }
>
> u_term_attr_cur = u_term_attr_orig;
> ::cfmakeraw(&u_term_attr_cur);
> u_term_attr_cur.c_lflag = 0;
> u_term_attr_cur.c_oflag |= OPOST|ONLCR;
>
> diag = ::tcsetattr(u_ttyfd, TCSANOW, &u_term_attr_cur);
> if (diag == -1) {
> u_logger->log("%s: Unable to set attributes for '%s': %s\n",
> u_appname, devname, strerror(errno));
> goto leave;
> }
> u_term_attr_set = true;
>

At the moment I don't understand what this code does; I suppose
I'll have to look up all those flags first.

Thanks.

Janis

Re: Getting a keypress

<Y3ngN.3131$SyNd.677@fx33.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10893&group=comp.unix.programmer#10893

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx33.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Getting a keypress
Newsgroups: comp.unix.programmer
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad> <ulsq95$5ti4$1@dont-email.me>
Lines: 59
Message-ID: <Y3ngN.3131$SyNd.677@fx33.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 19 Dec 2023 20:30:48 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 19 Dec 2023 20:30:48 GMT
X-Received-Bytes: 2626
 by: Scott Lurndal - Tue, 19 Dec 2023 20:30 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>On 19.12.2023 18:04, Scott Lurndal wrote:
>>
>> Use poll(2) instead of read(2). [...]
>
> int poll(struct pollfd *fds, nfds_t nfds, int timeout);
>
>It isn't explicitly mentioned in the man-page, but I assume a
>timeout value of 0 will actually do the non-blocking behavior
>with poll(2).
>
>>
>> diag = ::tcgetattr(u_ttyfd, &u_term_attr_orig);
>> if (diag == -1) {
>> u_logger->log("%s: Unable to get attributes for '%s': %s\n",
>> u_appname, devname, strerror(errno));
>> goto leave;
>> }
>>
>> u_term_attr_cur = u_term_attr_orig;
>> ::cfmakeraw(&u_term_attr_cur);
>> u_term_attr_cur.c_lflag = 0;
>> u_term_attr_cur.c_oflag |= OPOST|ONLCR;
>>
>> diag = ::tcsetattr(u_ttyfd, TCSANOW, &u_term_attr_cur);
>> if (diag == -1) {
>> u_logger->log("%s: Unable to set attributes for '%s': %s\n",
>> u_appname, devname, strerror(errno));
>> goto leave;
>> }
>> u_term_attr_set = true;
>>
>
>At the moment I don't understand what this code does; I suppose
>I'll have to look up all those flags first.

The key is cfmakeraw which sets up the attributes
for raw mode.

Raw mode

From the man page

cfmakeraw() sets the terminal to something like the "raw" mode
of the old Version 7 terminal driver: input is available character
by character, echoing is disabled, and all special processing of
terminal input and output characters is disabled. The terminal
attributes are set as follows:

termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
termios_p->c_oflag &= ~OPOST;
termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
termios_p->c_cflag &= ~(CSIZE | PARENB);
termios_p->c_cflag |= CS8;

Comes from BSD originally.

(You can ignore the leading :: on system calls, that's a C++-ism).

Re: Getting a keypress

<20231219124310.642@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10894&group=comp.unix.programmer#10894

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 20:47:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <20231219124310.642@kylheku.com>
References: <ulroor$116$1@dont-email.me> <ulrpvl$80n$1@dont-email.me>
Injection-Date: Tue, 19 Dec 2023 20:47:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68da8d647119cc56964fc1502348dad9";
logging-data="218921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pIJ0YCVKz6EHzzxscb49jrARBEtvfUOA="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:QrQE9rQHi5txhDW8RlSynqKyf1E=
 by: Kaz Kylheku - Tue, 19 Dec 2023 20:47 UTC

On 2023-12-19, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> Also are you sure fd is stdin? Perhaps use STDIN_FILENO.

I suspect the purpose of STDIN_FILENO is readability, not abstraction
(as in the possibility of it being redefined).

Newcomers to Unix codebases may be confused if they see a hard
coded literal zero.

There is no way in hell stdin can be changed to anything other
than zero.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Getting a keypress

<ulsvss$6qdm$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10895&group=comp.unix.programmer#10895

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 21:53:47 +0100
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <ulsvss$6qdm$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad>
<ulsq95$5ti4$1@dont-email.me> <Y3ngN.3131$SyNd.677@fx33.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Dec 2023 20:53:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ddbd8a01525bb248976411cfd182113";
logging-data="223670"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uttB9GEx94Rm3PzRzUFV5"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:0OkZHQRspYCx6VzehvrQQseFGCo=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <Y3ngN.3131$SyNd.677@fx33.iad>
 by: Janis Papanagnou - Tue, 19 Dec 2023 20:53 UTC

On 19.12.2023 21:30, Scott Lurndal wrote:
> [...]

So far I haven't stumbled across cfmakeraw(), thanks.

And thanks for the explanations.

>
> (You can ignore the leading :: on system calls, that's a C++-ism).

I'm familiar with C++ ::-)

Janis ;-)

Re: Getting a keypress

<20231219124752.349@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10896&group=comp.unix.programmer#10896

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 20:55:19 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <20231219124752.349@kylheku.com>
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad>
<6581e124$0$6111$426a74cc@news.free.fr>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Dec 2023 20:55:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68da8d647119cc56964fc1502348dad9";
logging-data="218921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fGwBLtROJewmQsTaXmaNgzVUj0KzAcpY="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:pTX0qNDNeksnhI2uL1rvEj+JDsY=
 by: Kaz Kylheku - Tue, 19 Dec 2023 20:55 UTC

On 2023-12-19, Nicolas George <nicolas$george@salle-s.org> wrote:
> Scott Lurndal, dans le message <92kgN.11545$5Hnd.7072@fx03.iad>, a
> écrit :
>> Use poll(2) instead of read(2). IIRC, O_NONBLOCK doesn't necessarily
>> apply to PTYs.
>
> O_NONBLOCK applies to ptys, and if it did not then poll() would not either.

I don't believe that true. There is no requirement that non-blocking
objects be used with poll, at least if you are only reading.

When a descriptor polls positive for input, then a subsequent input
operation will not block. There is no requirement to use O_NONBLOCK, or
for the device to support it.

You need O_NONBLOCK if you're polling for writing, because a positive
write poll only tells you that there is some buffer space available,
without indicating how much. If the device is blocking and the
subsequent write is too large, it can still block.

However, I think, logically, one byte must be guaranteed: if a blocking
device polls positive for writing, we can be confident we can do a one
byte write without blocking, provided the device isn't shared with
any other process that could stuff it behind our back.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Getting a keypress

<20231219125631.12@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10897&group=comp.unix.programmer#10897

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 21:06:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <20231219125631.12@kylheku.com>
References: <ulroor$116$1@dont-email.me>
Injection-Date: Tue, 19 Dec 2023 21:06:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68da8d647119cc56964fc1502348dad9";
logging-data="227266"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0WoGNH6Vm/uUvDAc8hhCKWu1PWPttl0Q="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:ovNs079YZz0Eh1ATsxGeO9cWigw=
 by: Kaz Kylheku - Tue, 19 Dec 2023 21:06 UTC

On 2023-12-19, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> struct termios buf;
> buf.c_lflag &= ~(ECHO | ICANON);
> buf.c_cc[VMIN] = 1;
> buf.c_cc[VTIME] = 0;
> tcsetattr(fd, TCSAFLUSH, &buf);

This is fine. It's not fully raw. For instance, the Ctrl-C (or
whatever is your break character) to SIGINT mapping is still active.

Maybe you want that; not every character-at-a-time-input program
wants to disable Ctrl-C; just those that handle it as a character
and implement it themselves. See IGNBRK, BRKINT.

E.g. in an interpreted language's listener, we want Ctrl-C to
just cancel the current line of input and start a new one.
A nice way to do that is to just treat it as a command character,
rather than mess around with signals.

I think that in non-canonical mode, CR-NL translation is
may still be in effect (ICRNL, INLCR). If you want that raw,
those have t obe turned off.

You can experiment with all these flags in a shell script,
since they are available via stty.

You can do a precise one byte read from the tty using:

dd bs=1 count=1

But when code is running, we do want the interrupt, so we flip out
of that raw mode when the command line is not being edited.

Be sure to save the tty state using

tty_state=$(stty -g)

that returns a string which can be passed as an argument to stty
to restore the settings (like in an EXIT or INT trap of the
shell script). I think, POSIX says that this stty -g string does
not require quoting:

stty $tty_state

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Getting a keypress

<ult3oh$2j14$1@nyheter.lysator.liu.se>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10898&group=comp.unix.programmer#10898

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!newsreader4.netcologne.de!news.netcologne.de!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!nyheter.lysator.liu.se!.POSTED!not-for-mail
From: kem...@lysator.liu.se (Andreas Kempe)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Tue, 19 Dec 2023 21:59:45 -0000 (UTC)
Organization: Lysator ACS
Message-ID: <ult3oh$2j14$1@nyheter.lysator.liu.se>
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad>
<6581e124$0$6111$426a74cc@news.free.fr> <20231219124752.349@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Dec 2023 21:59:45 -0000 (UTC)
Injection-Info: nyheter.lysator.liu.se; posting-account="kempe";
logging-data="85028"; mail-complaints-to="newsmaster@lysator.liu.se"
User-Agent: slrn/1.0.3 (FreeBSD)
X-Received-Bytes: 3210
 by: Andreas Kempe - Tue, 19 Dec 2023 21:59 UTC

Den 2023-12-19 skrev Kaz Kylheku <433-929-6894@kylheku.com>:
> On 2023-12-19, Nicolas George <nicolas$george@salle-s.org> wrote:
>> Scott Lurndal, dans le message <92kgN.11545$5Hnd.7072@fx03.iad>, a
>> écrit :
>>> Use poll(2) instead of read(2). IIRC, O_NONBLOCK doesn't necessarily
>>> apply to PTYs.
>>
>> O_NONBLOCK applies to ptys, and if it did not then poll() would not either.
>
> I don't believe that true. There is no requirement that non-blocking
> objects be used with poll, at least if you are only reading.
>
> When a descriptor polls positive for input, then a subsequent input
> operation will not block. There is no requirement to use O_NONBLOCK, or
> for the device to support it.
>

You're right that O_NONBLOCK is not needed for polling since polling
consists of the polling process asking the driver if it has data
available and if the device doesn't, the process is put to sleep until
data is available or the timeout is hit.

On the other hand, I can't really imagine a scenario where it would
make sense for a device to be pollable, but not support non-blocking
reads. If you can tell whether data is ready, you should be able to
support non-blocking reads.

> You need O_NONBLOCK if you're polling for writing, because a positive
> write poll only tells you that there is some buffer space available,
> without indicating how much. If the device is blocking and the
> subsequent write is too large, it can still block.
>
> However, I think, logically, one byte must be guaranteed: if a blocking
> device polls positive for writing, we can be confident we can do a one
> byte write without blocking, provided the device isn't shared with
> any other process that could stuff it behind our back.
>

Depends on the implementation in the driver being polled really. A
sane implementation would of course not signal that it is ready for
writing unless it has some space available. In a multi-processor
system, or for a device conceivably being shared with some other
hardware, you could have races for the buffer space. You would then
likely get an EAGAIN error when you try to write, indicating that no
space is available. This is of course also true for non-blocking reads
with parallel access.

Re: Getting a keypress

<fzogN.78580$7sbb.66186@fx16.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10899&group=comp.unix.programmer#10899

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!newsfeed.hasname.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Getting a keypress
Newsgroups: comp.unix.programmer
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad> <6581e124$0$6111$426a74cc@news.free.fr> <20231219124752.349@kylheku.com> <ult3oh$2j14$1@nyheter.lysator.liu.se>
Lines: 29
Message-ID: <fzogN.78580$7sbb.66186@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 19 Dec 2023 22:12:27 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 19 Dec 2023 22:12:27 GMT
X-Received-Bytes: 2039
 by: Scott Lurndal - Tue, 19 Dec 2023 22:12 UTC

Andreas Kempe <kempe@lysator.liu.se> writes:
>Den 2023-12-19 skrev Kaz Kylheku <433-929-6894@kylheku.com>:
>> On 2023-12-19, Nicolas George <nicolas$george@salle-s.org> wrote:
>>> Scott Lurndal, dans le message <92kgN.11545$5Hnd.7072@fx03.iad>, a
>>> écrit :
>>>> Use poll(2) instead of read(2). IIRC, O_NONBLOCK doesn't necessarily
>>>> apply to PTYs.
>>>
>>> O_NONBLOCK applies to ptys, and if it did not then poll() would not either.
>>
>> I don't believe that true. There is no requirement that non-blocking
>> objects be used with poll, at least if you are only reading.
>>
>> When a descriptor polls positive for input, then a subsequent input
>> operation will not block. There is no requirement to use O_NONBLOCK, or
>> for the device to support it.
>>
>
>You're right that O_NONBLOCK is not needed for polling since polling
>consists of the polling process asking the driver if it has data
>available and if the device doesn't, the process is put to sleep until
>data is available or the timeout is hit.
>
>On the other hand, I can't really imagine a scenario where it would
>make sense for a device to be pollable, but not support non-blocking
>reads. If you can tell whether data is ready, you should be able to
>support non-blocking reads.

int diag = ioctl(tty_fd, FIONREAD, &num_to_read);

Re: Getting a keypress

<6582191b$0$6097$426a34cc@news.free.fr>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10900&group=comp.unix.programmer#10900

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!proxad.net!feeder1-2.proxad.net!cleanfeed1-a.proxad.net!nnrp1-1.free.fr!not-for-mail
Newsgroups: comp.unix.programmer
From: nicolas$...@salle-s.org (Nicolas George)
Subject: Re: Getting a keypress
Sender: george@phare.invalid (Nicolas George)
X-Newsreader: Flrn (0.9.20070704)
References: <ulroor$116$1@dont-email.me> <92kgN.11545$5Hnd.7072@fx03.iad> <6581e124$0$6111$426a74cc@news.free.fr> <20231219124752.349@kylheku.com> <ult3oh$2j14$1@nyheter.lysator.liu.se> <fzogN.78580$7sbb.66186@fx16.iad>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 19 Dec 2023 22:28:43 GMT
Lines: 28
Message-ID: <6582191b$0$6097$426a34cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 19 Dec 2023 23:28:43 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1703024923 news-4.free.fr 6097 129.199.129.80:47366
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Tue, 19 Dec 2023 22:28 UTC

[The reply has not reached my server, I reply to this one:]

>>Den 2023-12-19 skrev Kaz Kylheku <433-929-6894@kylheku.com>:
>>>> O_NONBLOCK applies to ptys, and if it did not then poll() would not either.
>>> I don't believe that true. There is no requirement that non-blocking
>>> objects be used with poll, at least if you are only reading.
>>>
>>> When a descriptor polls positive for input, then a subsequent input
>>> operation will not block. There is no requirement to use O_NONBLOCK, or
>>> for the device to support it.

Read better: I have not said that you had to use non-blocking IO along with
poll, I have said that if O_NONBLOCK does not work, the poll() does not work
either.

If you think I am wrong, then just provide a counter-example.

But you will not be able to, because non-blocking IO and pollable IO are
internally the same thing.

(Strange OS-specific devices might not respect that statement; that is
irrelevant for the discussion.)

Scott Lurndal, dans le message <fzogN.78580$7sbb.66186@fx16.iad>, a
écrit :
> int diag = ioctl(tty_fd, FIONREAD, &num_to_read);

Are you trying to say something?

Re: Getting a keypress

<ulub17$gcmj$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10901&group=comp.unix.programmer#10901

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Mutt...@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Wed, 20 Dec 2023 09:10:00 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <ulub17$gcmj$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me> <ulspcn$5o9p$1@dont-email.me>
Injection-Date: Wed, 20 Dec 2023 09:10:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="271c6f40211988fcfd5e73c5f872eb22";
logging-data="537299"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ITVVjjVEdEdvDMnvcGrfP"
Cancel-Lock: sha1:yY7PWz3tOwtqKaUA/95KYznnpzw=
 by: Mutt...@dastardlyhq.com - Wed, 20 Dec 2023 09:10 UTC

On Tue, 19 Dec 2023 20:02:46 +0100
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>On 19.12.2023 11:04, Muttley@dastardlyhq.com wrote:
>> On Tue, 19 Dec 2023 10:46:02 +0100
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>>>
>>> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
>>
>> I've never had to use that, try removing it to see if it helps.
>
>I am confused. - I removed that fcntl() call completely and the output
>wasn't affected any more in the observed negative way. - Why that?
>Don't I need to use non-blocking I/O for a poll?
>(Or is maybe buf.c_cc[VMIN] = 1 serving the same purpose?)
>But why has a non-blocking input definition an effect on _output_?
>You see me puzzled.
>
>>>
>>> struct termios buf;
>>> buf.c_lflag &= ~(ECHO | ICANON);
>>> buf.c_cc[VMIN] = 1;
>>> buf.c_cc[VTIME] = 0;
>>> tcsetattr(fd, TCSAFLUSH, &buf);
>>
>> Instead of TCSAFLUSH try TCSANOW [...]
>
>I tried this change, but it had no visible effect (neither to the good
>nor to the bad).

Sorry I can't help any more. The above code has always worked for me.

Re: Getting a keypress

<ulujcr$hkcd$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10902&group=comp.unix.programmer#10902

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Wed, 20 Dec 2023 12:32:42 +0100
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <ulujcr$hkcd$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
<ulspcn$5o9p$1@dont-email.me> <ulub17$gcmj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Dec 2023 11:32:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="1e55bbf2f99f2c6876da2d3c8bec2f6a";
logging-data="577933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fJDtRSxRqv0vfwZe50m69"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:FAAOv5icX2GnOv/IUapS6iqhkkA=
In-Reply-To: <ulub17$gcmj$1@dont-email.me>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Wed, 20 Dec 2023 11:32 UTC

On 20.12.2023 10:10, Muttley@dastardlyhq.com wrote:
> On Tue, 19 Dec 2023 20:02:46 +0100
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> On 19.12.2023 11:04, Muttley@dastardlyhq.com wrote:
>>> On Tue, 19 Dec 2023 10:46:02 +0100
>>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>>>>
>>>> fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
>>>
>>> I've never had to use that, try removing it to see if it helps.
>>
>> I am confused. - I removed that fcntl() call completely and the output
>> wasn't affected any more in the observed negative way. - Why that?
>> Don't I need to use non-blocking I/O for a poll?
>> (Or is maybe buf.c_cc[VMIN] = 1 serving the same purpose?)
>> But why has a non-blocking input definition an effect on _output_?
>> You see me puzzled.
>>
>>>>
>>>> struct termios buf;
>>>> buf.c_lflag &= ~(ECHO | ICANON);
>>>> buf.c_cc[VMIN] = 1;
>>>> buf.c_cc[VTIME] = 0;
>>>> tcsetattr(fd, TCSAFLUSH, &buf);
>>>
>>> Instead of TCSAFLUSH try TCSANOW [...]
>>
>> I tried this change, but it had no visible effect (neither to the good
>> nor to the bad).
>
> Sorry I can't help any more. The above code has always worked for me.

Just to clarify; your suggestion to remove the fcntl() call already
solved the issue! [*]

Your proposal also works with TCSAFLUSH, so TCSANOW at least doesn't
seem necessary. Since you said I should "try" TCSANOW - which seemed
to imply no strict reason to use it - I also wanted to give feedback
on that part.

So thanks again. I don't know *why* it works (or rather, why it didn't
work before), but now [without the fcntl()] it does. :-)

Thanks also to all the other posters who helped clarifying the issue,
for the suggestions, and discussions!

Janis

[*] It would certainly have been good if I'd understand why the fcntl()
on stdin was in the way, and why it affected output. - But okay, that's
just my honest desire to try to understand what I'm doing. ;-)

Re: Getting a keypress

<20231220100242.808@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10903&group=comp.unix.programmer#10903

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Wed, 20 Dec 2023 18:10:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <20231220100242.808@kylheku.com>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
<ulspcn$5o9p$1@dont-email.me> <ulub17$gcmj$1@dont-email.me>
<ulujcr$hkcd$1@dont-email.me>
Injection-Date: Wed, 20 Dec 2023 18:10:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7a1874e90cebb8b9038f2df9c2b8fd4c";
logging-data="701596"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18N1GKj4wvlOnzGlAchvgTW18gJ8pyH1L8="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:KZUS2S5AZG5Ft+BBVi2zW2PDs4s=
 by: Kaz Kylheku - Wed, 20 Dec 2023 18:10 UTC

On 2023-12-20, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> Your proposal also works with TCSAFLUSH, so TCSANOW at least doesn't
> seem necessary. Since you said I should "try" TCSANOW - which seemed
> to imply no strict reason to use it - I also wanted to give feedback
> on that part.

TCSAFLUSH is evil; if your user is a fast typist who can enter keyboard
input before your program has initialized, they will find that their
buffered keystrokes were lost. You won't easily see it in a small
program whose executable and libraries are cache hot, and which doesn't
read a lot of startup files and such.

(Vim does this annoying flush thing. I posted about it to the mailing
list many years ago and was rudely dismissed.)

If you only care about changing input parameters, you want TCSANOW.
If any of your options affect output, you may want any previously
written, buffered output (not just by your program but anything that
came before!) to be drained before the changes take effect, for
which there is TCSADRAIN.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Getting a keypress

<20231220101407.755@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10904&group=comp.unix.programmer#10904

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!nntp.comgw.net!paganini.bofh.team!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Wed, 20 Dec 2023 18:18:42 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <20231220101407.755@kylheku.com>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
<ulspcn$5o9p$1@dont-email.me> <ulub17$gcmj$1@dont-email.me>
<ulujcr$hkcd$1@dont-email.me>
Injection-Date: Wed, 20 Dec 2023 18:18:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7a1874e90cebb8b9038f2df9c2b8fd4c";
logging-data="701596"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Ph/NLuVw1ZLnP+KCCKqnvbtBKSS7XpP0="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:kyXwVc5m6tJk+icJII5/oi+Vba0=
 by: Kaz Kylheku - Wed, 20 Dec 2023 18:18 UTC

On 2023-12-20, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> So thanks again. I don't know *why* it works (or rather, why it didn't
> work before), but now [without the fcntl()] it does. :-)

Are you using C stdio after setting STDIN_FILENO to nonblocking?

If so, you will be constantly getting errors on stdin whenever the
underlying descriptor returns EWOULDBLOCK. Every time that happens
you have to call clearerr(stdin).

The only reason you'd want the descriptor nonblocking is that your
program has some busy calculation to do during which it needs to
periodically check for TTY input.

As soon as your program has nothing to do but wait for input,
you'd need to use poll() or select(), or else switch the descriptor
to blocking again. So as not to sit in a tight loop calling the
nonblocking read.

> [*] It would certainly have been good if I'd understand why the fcntl()
> on stdin was in the way, and why it affected output. - But okay, that's
> just my honest desire to try to understand what I'm doing. ;-)

Probably because your program blows through the non-blocking reads,
which affects its behavior. Output that would not have been produced
until a keystroke is read is now produced immediately, or whatever.
>

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Getting a keypress

<um11b9$118ml$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10905&group=comp.unix.programmer#10905

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!nntp.comgw.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Thu, 21 Dec 2023 10:43:03 +0100
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <um11b9$118ml$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
<ulspcn$5o9p$1@dont-email.me> <ulub17$gcmj$1@dont-email.me>
<ulujcr$hkcd$1@dont-email.me> <20231220100242.808@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Dec 2023 09:43:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="676cc045f7b7794a11e892f6f657ce78";
logging-data="1090261"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pEkWOz0QwBg2/L7qu7oTO"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Ngi/8JZYEpZhEker3QaTTtEMD/8=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <20231220100242.808@kylheku.com>
 by: Janis Papanagnou - Thu, 21 Dec 2023 09:43 UTC

On 20.12.2023 19:10, Kaz Kylheku wrote:
>
> TCSAFLUSH is evil; if your user is a fast typist who can enter keyboard
> input before your program has initialized, they will find that their
> buffered keystrokes were lost. You won't easily see it in a small
> program whose executable and libraries are cache hot, and which doesn't
> read a lot of startup files and such.

This may actually be a good property in my case; I want to use only
the keystrokes that are typed after initialization. (It's also just
a "small program"; a game that prints some hints and waits to start
after a keystroke, and then gets controlled by further keystrokes.)

> [...]
>
> If you only care about changing input parameters, you want TCSANOW.
> If any of your options affect output, you may want any previously
> written, buffered output (not just by your program but anything that
> came before!) to be drained before the changes take effect, for
> which there is TCSADRAIN.

Okay, thanks.

Janis

Re: Getting a keypress

<um11ll$11a5l$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10906&group=comp.unix.programmer#10906

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!nntp.comgw.net!paganini.bofh.team!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Thu, 21 Dec 2023 10:48:36 +0100
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <um11ll$11a5l$1@dont-email.me>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
<ulspcn$5o9p$1@dont-email.me> <ulub17$gcmj$1@dont-email.me>
<ulujcr$hkcd$1@dont-email.me> <20231220101407.755@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Dec 2023 09:48:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="676cc045f7b7794a11e892f6f657ce78";
logging-data="1091765"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WhcU8uy4+yC1419kQJ1+D"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:IM2xLyXIKHaRgSRx/IDjwVmf6lU=
In-Reply-To: <20231220101407.755@kylheku.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Thu, 21 Dec 2023 09:48 UTC

On 20.12.2023 19:18, Kaz Kylheku wrote:
> On 2023-12-20, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> So thanks again. I don't know *why* it works (or rather, why it didn't
>> work before), but now [without the fcntl()] it does. :-)
>
> Are you using C stdio after setting STDIN_FILENO to nonblocking?

I had been using C stdout after setting STDIN_FILENO to nonblocking
but not C stdin; input came solely from the keystroke "commands".

>
> If so, you will be constantly getting errors on stdin whenever the
> underlying descriptor returns EWOULDBLOCK. Every time that happens
> you have to call clearerr(stdin).
>
> The only reason you'd want the descriptor nonblocking is that your
> program has some busy calculation to do during which it needs to
> periodically check for TTY input.

This is the case. It constantly does something and the processing is
only affected by the ad hoc keystrokes.

>
> As soon as your program has nothing to do but wait for input,
> you'd need to use poll() or select(), or else switch the descriptor
> to blocking again. So as not to sit in a tight loop calling the
> nonblocking read.

When the processing steps are done for all simulated entities the
program calls usleep() for a calculated well defined period and gets
effectively suspended.

>
>> [*] It would certainly have been good if I'd understand why the fcntl()
>> on stdin was in the way, and why it affected output. - But okay, that's
>> just my honest desire to try to understand what I'm doing. ;-)
>
> Probably because your program blows through the non-blocking reads,
> which affects its behavior. Output that would not have been produced
> until a keystroke is read is now produced immediately, or whatever.

The effect was the opposite; that output that had previously been
created did not show up any more. (After removing the non-blocking
_input_ control setting the _output_ showed up again, as desired.)

Janis

Re: Getting a keypress

<87sf3v5rbj.fsf@yaxenu.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10907&group=comp.unix.programmer#10907

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jsh...@yaxenu.org (Julieta Shem)
Newsgroups: comp.unix.programmer
Subject: Re: Getting a keypress
Date: Thu, 21 Dec 2023 15:44:00 -0300
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <87sf3v5rbj.fsf@yaxenu.org>
References: <ulroor$116$1@dont-email.me> <ulrpqq$70r$1@dont-email.me>
<ulspcn$5o9p$1@dont-email.me> <ulub17$gcmj$1@dont-email.me>
<ulujcr$hkcd$1@dont-email.me> <20231220100242.808@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="3c7ee5e5a25e8c5993d76e1953b1f108";
logging-data="1250304"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZMn0SNCcU6uwY1NkishzdhVw9jTwXPKA="
Cancel-Lock: sha1:VqYFWkz8CfPXmdquePifztHVRsI=
sha1:RrMXGBykkHcHz46X+yfHNQbMnkM=
 by: Julieta Shem - Thu, 21 Dec 2023 18:44 UTC

Kaz Kylheku <433-929-6894@kylheku.com> writes:

> On 2023-12-20, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> Your proposal also works with TCSAFLUSH, so TCSANOW at least doesn't
>> seem necessary. Since you said I should "try" TCSANOW - which seemed
>> to imply no strict reason to use it - I also wanted to give feedback
>> on that part.
>
> TCSAFLUSH is evil; if your user is a fast typist who can enter keyboard
> input before your program has initialized, they will find that their
> buffered keystrokes were lost. You won't easily see it in a small
> program whose executable and libraries are cache hot, and which doesn't
> read a lot of startup files and such.
>
> (Vim does this annoying flush thing. I posted about it to the mailing
> list many years ago and was rudely dismissed.)

Lol. Do you have a message-id or something? I'd love to have a look.

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor