Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get_immediate echoe character--compiled error?

96 views
Skip to first unread message

richardthiebaud

unread,
Oct 1, 2023, 10:42:42 PM10/1/23
to
When I build and run the following program using Gnat 11 in Linux Mint
21.2, the keys I press are echoed on the console. According to the Ada
Reference Manual, they should not be echoed. Is this a compiler error?

with ada.text_io; use ada.text_io;
procedure test3 is
c: character;
avail: boolean;
begin
loop
loop
Get_Immediate(c, Avail);
if Avail then
exit;
end if;
delay 0.01;
end loop;
end loop;
end test3;

This does not happen if I call get_immediate without the avail
parameter, i.e. get_immediate(c);

Keith Thompson

unread,
Oct 2, 2023, 1:48:46 AM10/2/23
to
richardthiebaud <thieba...@aol.com> writes:
> When I build and run the following program using Gnat 11 in Linux Mint
> 21.2, the keys I press are echoed on the console. According to the Ada
> Reference Manual, they should not be echoed. Is this a compiler error?

Where does the ARM say that?

> with ada.text_io; use ada.text_io;
> procedure test3 is
> c: character;
> avail: boolean;
> begin
> loop
> loop
> Get_Immediate(c, Avail);
> if Avail then
> exit;
> end if;
> delay 0.01;
> end loop;
> end loop;
> end test3;
>
> This does not happen if I call get_immediate without the avail
> parameter, i.e. get_immediate(c);
>

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

richardthiebaud

unread,
Oct 2, 2023, 4:07:53 PM10/2/23
to
On 10/2/23 01:48, Keith Thompson wrote:
> richardthiebaud <thieba...@aol.com> writes:
>> When I build and run the following program using Gnat 11 in Linux Mint
>> 21.2, the keys I press are echoed on the console. According to the Ada
>> Reference Manual, they should not be echoed. Is this a compiler error?
>
> Where does the ARM say that?
https://www.adaic.org/resources/add_content/standards/05rm/html/RM-A-10-7.html

Keith Thompson

unread,
Oct 2, 2023, 6:28:03 PM10/2/23
to
richardthiebaud <thieba...@aol.com> writes:
> On 10/2/23 01:48, Keith Thompson wrote:
>> richardthiebaud <thieba...@aol.com> writes:
>>> When I build and run the following program using Gnat 11 in Linux Mint
>>> 21.2, the keys I press are echoed on the console. According to the Ada
>>> Reference Manual, they should not be echoed. Is this a compiler error?
>> Where does the ARM say that?
> https://www.adaic.org/resources/add_content/standards/05rm/html/RM-A-10-7.html

I don't see anything there about the character being echoed, or not.

> If a character, either control or graphic, is available from the
> specified File or the default input file, then the character is read;
> Available is True and Item contains the value of this character. If a
> character is not available, then Available is False and the value of
> Item is not specified. Mode_Error is propagated if the mode of the file
> is not In_File. End_Error is propagated if at the end of the file. The
> current column, line and page numbers for the file are not affected.

Are you assuming that not updating the current column, line, and page
numbers for the file implies that the character is not echoed?

[...]

richardthiebaud

unread,
Oct 2, 2023, 6:41:49 PM10/2/23
to
On 10/2/23 18:27, Keith Thompson wrote:
> richardthiebaud <thieba...@aol.com> writes:
>> On 10/2/23 01:48, Keith Thompson wrote:
>>> richardthiebaud <thieba...@aol.com> writes:
>>>> When I build and run the following program using Gnat 11 in Linux Mint
>>>> 21.2, the keys I press are echoed on the console. According to the Ada
>>>> Reference Manual, they should not be echoed. Is this a compiler error?
>>> Where does the ARM say that?
>> https://www.adaic.org/resources/add_content/standards/05rm/html/RM-A-10-7.html
>
> I don't see anything there about the character being echoed, or not.
>
>> If a character, either control or graphic, is available from the
>> specified File or the default input file, then the character is read;
>> Available is True and Item contains the value of this character. If a
>> character is not available, then Available is False and the value of
>> Item is not specified. Mode_Error is propagated if the mode of the file
>> is not In_File. End_Error is propagated if at the end of the file. The
>> current column, line and page numbers for the file are not affected.
>
> Are you assuming that not updating the current column, line, and page
> numbers for the file implies that the character is not echoed?
>
> [...]
>
Yes.

richardthiebaud

unread,
Oct 2, 2023, 6:47:06 PM10/2/23
to
On 10/2/23 18:27, Keith Thompson wrote:
> richardthiebaud <thieba...@aol.com> writes:
>> On 10/2/23 01:48, Keith Thompson wrote:
>>> richardthiebaud <thieba...@aol.com> writes:
>>>> When I build and run the following program using Gnat 11 in Linux Mint
>>>> 21.2, the keys I press are echoed on the console. According to the Ada
>>>> Reference Manual, they should not be echoed. Is this a compiler error?
>>> Where does the ARM say that?
>> https://www.adaic.org/resources/add_content/standards/05rm/html/RM-A-10-7.html
>
> I don't see anything there about the character being echoed, or not.
>
>> If a character, either control or graphic, is available from the
>> specified File or the default input file, then the character is read;
>> Available is True and Item contains the value of this character. If a
>> character is not available, then Available is False and the value of
>> Item is not specified. Mode_Error is propagated if the mode of the file
>> is not In_File. End_Error is propagated if at the end of the file. The
>> current column, line and page numbers for the file are not affected.
>
> Are you assuming that not updating the current column, line, and page
> numbers for the file implies that the character is not echoed?
>
> [...]
>
In any case, when it echos the character. it increases the current
column by 1, and that does contradict the Ada Reference Manual.

Niklas Holsti

unread,
Oct 3, 2023, 4:41:08 AM10/3/23
to
On 2023-10-03 1:47, richardthiebaud wrote:
> On 10/2/23 18:27, Keith Thompson wrote:
>> richardthiebaud <thieba...@aol.com> writes:
>>> On 10/2/23 01:48, Keith Thompson wrote:
>>>> richardthiebaud <thieba...@aol.com> writes:
>>>>> When I build and run the following program using Gnat 11 in Linux Mint
>>>>> 21.2, the keys I press are echoed on the console. According to the Ada
>>>>> Reference Manual, they should not be echoed. Is this a compiler error?
>>>> Where does the ARM say that?
>>> https://www.adaic.org/resources/add_content/standards/05rm/html/RM-A-10-7.html
>>
>> I don't see anything there about the character being echoed, or not.


Nor do I. But perhaps there should be something, since "not echoing" is
useful behavior and the program can itself echo characters if that is
desired.

Possibly this is why AdaCore have given different echoing behaviors to
the two forms of Get_Immediate, with and without the "Available"
parameter. If so, this echo difference is unfortunately coupled with the
wait/no-wait behavior difference, and that coupling may be unwanted.

There are (or have been) computer terminals with local echo, where the
program cannot prevent the display of each keystroke. So the "no echo"
behavior cannot be an absolute requirement in the Ada manual, but it
could be Implementation Advice.


>>> If a character, either control or graphic, is available from the
>>> specified File or the default input file, then the character is read;
>>> Available is True and Item contains the value of this character. If a
>>> character is not available, then Available is False and the value of
>>> Item is not specified. Mode_Error is propagated if the mode of the file
>>> is not In_File. End_Error is propagated if at the end of the file. The
>>> current column, line and page numbers for the file are not affected.
>>
>> Are you assuming that not updating the current column, line, and page
>> numbers for the file implies that the character is not echoed?
>>
>> [...]
>>
> In any case, when it echos the character. it increases the current
> column by 1, and that does contradict the Ada Reference Manual.

You are assuming that "current column" in the Ada Reference Manual means
the same as the "current column position of the terminal/screen cursor",
which is not the case, so there is no formal contradiction. The Ada
"current column" refers to an internal state of the file.

For an unknown type of terminal/screen, deducing the current cursor
column from the stream of input characters and output characters is not
feasible, because it depends on the device's interpretation of
formatting control characters such as TABs and on the width of the
screen or terminal window.

Simon Wright

unread,
Oct 3, 2023, 6:20:44 AM10/3/23
to
Niklas Holsti <niklas...@tidorum.invalid> writes:

> Possibly this is why AdaCore have given different echoing behaviors to
> the two forms of Get_Immediate, with and without the "Available"
> parameter. If so, this echo difference is unfortunately coupled with
> the wait/no-wait behavior difference, and that coupling may be
> unwanted.

The low-level Get_Immediate implementation is in sysdep.c (probably not
in the adainclude/ directory in an installed compiler), in
getc_immediate() and getc_immediate_nowait(), both of which call
getc_immediate_common(), and I can't see any difference! ECHO gets
turned off in getc_immediate_common(), regardless of caller - see link.

https://github.com/gcc-mirror/gcc/blob/3ca09d684e496240a87c0327687e2898060c2363/gcc/ada/sysdep.c#L387

G.B.

unread,
Oct 3, 2023, 5:00:44 PM10/3/23
to
On 02.10.23 04:42, richardthiebaud wrote:
> When I build and run the following program using Gnat 11 in Linux Mint 21.2, the keys I press are echoed on the console.

Which console?

Can you try to run a C program in the same console
that tests for it to be y TTY? See Simon Wright's link
to GNAT's implementation. The C program would be
calling isatty(0) or isatty(fileno(your_stream));

Some IDEs have a console window that is not
a TTY in the sense of termios(4)/tcsetattr(3).
Echoing is different, then.

Keith Thompson

unread,
Oct 3, 2023, 8:13:21 PM10/3/23
to
I haven't really looked into this, but I *think* what's happening is
that for the versions with the Available parameter, ECHO hasn't yet been
turned off when the user types the character. If you type 'x', it
echoes immediately, because the program has no way of knowing that the
character will later be consumed by a call to Get_Immediate. Presumably
if the user hasn't typed anything, causing Available to be set to false,
Get_Immediate will turn echoing off and back on again very quickly.
Echoing is disabled only for small fraction of a second it takes for
Get_Immediate to be executed.

The Get_Immediate functions without the Available parameter block until
a character is entered. They can disable echoing before the character
is entered. Echoing will typically be disabled for minutes or seconds,
from the time Get_Immediate is called and the time the user types
something.

The only solution I can think of would be to disable echoing (in some
non-portable manner; I don't think the standard library provides this)
before the user starts typing. (Perhaps you want to run the
Get_Immediate without the Available parameter in a separate task?)

Simon Wright

unread,
Oct 4, 2023, 4:22:10 AM10/4/23
to
Keith Thompson <Keith.S.T...@gmail.com> writes:
Great analysis! Is this worth raising a PR on GCC Bugzilla? (maybe only
on the documentation?)

Or, alternatively, don't turn echoing off at all - what's the use case
for turning it off? After all, the ARM says nothing about it.

Jeffrey R.Carter

unread,
Oct 4, 2023, 6:48:44 AM10/4/23
to
On 2023-10-04 10:22, Simon Wright wrote:
>
> Or, alternatively, don't turn echoing off at all - what's the use case
> for turning it off? After all, the ARM says nothing about it.

The use case is inputting passwords and the like. See Password_Line
(https://github.com/jrcarter/Encryption-utilities/blob/master/password_line.ads)
for an example. Note that this has identical behavior with GNAT/Linux and
ObjectAda/Windows.

--
Jeff Carter
“[A]bout half the patterns in the 'Gang of Four'
book only exist because of defects in C++ ...”
Brian Drummond
174

Simon Wright

unread,
Oct 4, 2023, 7:38:56 AM10/4/23
to
"Jeffrey R.Carter" <spam.jrc...@spam.acm.org.not> writes:

> On 2023-10-04 10:22, Simon Wright wrote:
>> Or, alternatively, don't turn echoing off at all - what's the use
>> case
>> for turning it off? After all, the ARM says nothing about it.
>
> The use case is inputting passwords and the like. See Password_Line
> (https://github.com/jrcarter/Encryption-utilities/blob/master/password_line.ads)
> for an example. Note that this has identical behavior with GNAT/Linux
> and ObjectAda/Windows.

Obviously you need to turn echoing off for password input. But neither
the ARM nor the GNAT RM says anything about Get_Immediate's echoing
behaviour, so it's hard to explain why OA does the same thing. Does its
manual specify this behaviour?

Jeffrey R.Carter

unread,
Oct 4, 2023, 9:05:08 AM10/4/23
to
On 2023-10-04 13:38, Simon Wright wrote:
>
> Obviously you need to turn echoing off for password input. But neither
> the ARM nor the GNAT RM says anything about Get_Immediate's echoing
> behaviour, so it's hard to explain why OA does the same thing. Does its
> manual specify this behaviour?

Unfortunately, Ada does not provide a standard way to turn off echo.

I agree that the ARM says nothing about echo for any of its operations on
Standard_Input, but clearly there is a broad consensus of Ada.Text_IO writers
and users who think this is desirable behavior.

Niklas Holsti

unread,
Oct 4, 2023, 12:55:54 PM10/4/23
to
Yes indeed.

A possible solution in Text_IO would be for Get_Immediate with Available
not to enable echo when it exits. Get_Immediate with Available is
typically called repeatedly, with no other input from the terminal in
between these calls, so it should be ok to keep echo disabled from one
such call to another. Any non-immediate input operation on the terminal
(that is, on this Text_IO file) should start by re-enabling echo if it
was disabled. Possibly the same should apply also to Get_Immediate
without Available, that is, it should leave echo disabled, until some
non-immediate input operation re-enables echo.

Keith Thompson

unread,
Oct 4, 2023, 3:39:32 PM10/4/23
to
The *first* character typed would still echo.

I suggest that what's needed is a way to turn echoing on and off.

Meanwhile, would calling Get_Immediate *without* the Available parameter
(which blocks and turns echoing off until after a character is typed) in
a separate task work? I haven't tried it. Of course you'd need to be
careful not to have I/O calls from separate tasks interfere with each
other.

Jeffrey R.Carter

unread,
Oct 4, 2023, 5:14:14 PM10/4/23
to
On 2023-10-02 04:42, richardthiebaud wrote:
>
> with ada.text_io; use ada.text_io;
> procedure test3 is
>   c: character;
>   avail: boolean;
> begin
>   loop
>     loop
>       Get_Immediate(c, Avail);
>       if Avail then
>         exit;
>       end if;
>       delay 0.01;
>     end loop;
>   end loop;
> end test3;

I should have checked this earlier, but this does not echo with ObjectAda.

Niklas Holsti

unread,
Oct 4, 2023, 5:20:10 PM10/4/23
to
Only if the user is quick enough to type it before the first call of
Get_Immediate.

If Get_Immediate is called for example to enter a password, usually the
program will first prompt the user to "Enter password:" and then at once
call Get_Immediate. Only a user who starts typing before the prompt is
visible would have time to type something before the (first) call of
Get_Immediate.


> I suggest that what's needed is a way to turn echoing on and off.


The user could still be quick enough to type characters before the echo
is turned off, so they would echo.


> Meanwhile, would calling Get_Immediate *without* the Available parameter
> (which blocks and turns echoing off until after a character is typed) in
> a separate task work? I haven't tried it.


That should work, provided that the Ada run-time system does not block
the whole program when one task blocks on an I/O request. There have
been, and perhaps still are, Ada programming systems where the whole Ada
program appears to the OS as a single OS thread so that one Ada task
waiting on a blocking OS call blocks all other tasks in the program.


> Of course you'd need to be careful not to have I/O calls from
> separate tasks interfere with each other.

Yes, but other tasks should be able to output text through
Standard_Output even while one task is reading Standard_Input using a
blocking I/O call. Except under a one-thread run-time system.

Keith Thompson

unread,
Oct 4, 2023, 6:12:44 PM10/4/23
to
"Jeffrey R.Carter" <spam.jrc...@spam.acm.org.not> writes:
> On 2023-10-02 04:42, richardthiebaud wrote:
>> with ada.text_io; use ada.text_io;
>> procedure test3 is
>>   c: character;
>>   avail: boolean;
>> begin
>>   loop
>>     loop
>>       Get_Immediate(c, Avail);
>>       if Avail then
>>         exit;
>>       end if;
>>       delay 0.01;
>>     end loop;
>>   end loop;
>> end test3;
>
> I should have checked this earlier, but this does not echo with ObjectAda.

On what target system?

Randy Brukardt

unread,
Oct 4, 2023, 8:43:33 PM10/4/23
to
"Jeffrey R.Carter" <spam.jrc...@spam.acm.org.not> wrote in message
news:ufjnu0$62ki$1...@dont-email.me...
> On 2023-10-04 13:38, Simon Wright wrote:
>>
>> Obviously you need to turn echoing off for password input. But neither
>> the ARM nor the GNAT RM says anything about Get_Immediate's echoing
>> behaviour, so it's hard to explain why OA does the same thing. Does its
>> manual specify this behaviour?
>
> Unfortunately, Ada does not provide a standard way to turn off echo.
>
> I agree that the ARM says nothing about echo for any of its operations on
> Standard_Input, but clearly there is a broad consensus of Ada.Text_IO
> writers and users who think this is desirable behavior.

For what it's worth, Janus/Ada turns off echoing, and that was decided
without referring to any other implementation's choice in the matter.
Rather, it was done to provide a way using standard calls to provide
functionality that had always been available in Janus/Ada in a non-standard
way.

Specifically, Janus/Ada has always had a predefined file name "KBD:" (or
"/dev/kbd" on Unix), which provides raw access to the keyboard device (or
standard input on more modern systems). This did not echo (or do any line
editing) on CP/M and MS-DOS, and we carried that same behavior over into
more modern systems.

For instance, the "Continue or Abort?" question in the compiler uses KBD: to
take and discard input immediately without any waiting (usual standard input
is line buffered and usually input is not processed until "enter" or similar
is pressed). It seemed to us that the Get_Immediate function was intending
the same sorts of uses. Note that implementing it this way makes it hard to
get meaningful results if Get_Immediate is mixed with other input on the
same file. (That's why we treated it as a special file in the beginning, but
even that gets confused if someone else reads from Standard_Input.)

Randy.


Jeffrey R.Carter

unread,
Oct 5, 2023, 5:51:19 AM10/5/23
to
On 2023-10-05 00:12, Keith Thompson wrote:
> "Jeffrey R.Carter" <spam.jrc...@spam.acm.org.not> writes:
>>
>> I should have checked this earlier, but this does not echo with ObjectAda.
>
> On what target system?

Windows.

--
Jeff Carter
"You cheesy lot of second-hand electric donkey-bottom biters."
Monty Python & the Holy Grail
14

0 new messages