Problems Interfacing Modula-2 with GNU Readline library
From:
Alice Osako
Subject:
Problems Interfacing Modula-2 with GNU Readline library
Date:
Mon, 4 Mar 2024 14:20:20 -0500
User-agent:
Mozilla Thunderbird
I am working on a
medium-sized project in Modula-2 (specifically, an implementation
of the MAL interpreter - see https://github.com/kanaka/mal for
details), and one of the soft requirements/recommended
implementation details is editable input and input history. For
this purpose, I intend to use the existing Readline library, and
to this end I created a simple definition module as a binding.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DEFINITION MODULE FOR "C" readline;
FROM SYSTEM IMPORT ADDRESS;
EXPORT UNQUALIFIED readline, add_history;
PROCEDURE readline (prompt: ARRAY OF CHAR) : ADDRESS;
PROCEDURE add_history (line: ARRAY OF CHAR);
END readline.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
However, I am running into a problem when testing these bindings.
When I run the following test program:
END rl_test.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I expected that this would result in something like the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Write something: foo
You Wrote: foo
Write something: bar
You Wrote: bar
Write something:
No input
Write something: bar
You Wrote: bar
Write something: quux
You Wrote: quux
Write something: exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
However, the output I actually got was:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Write something: foo
Write something: bar
Write something:
Write something: bar
Write something: quux
Write something: exit
You Wrote: foo
You Wrote: bar
No input
You Wrote: bar
You Wrote: quux
You Wrote: exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So my issue is, why is the response not getting flushed until
after the input is completed?
Oddly enough, when I add
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FROM InOut IMPORT WriteCard;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and insert this following the input but before the IF statement:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WriteCard(Length(input), 3);
WriteLn;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The result is different:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Write something: foo
3Write something: bar
You Wrote: foo
3Write something:
You Wrote: bar
0Write something: bar
No input
3Write something: quux
You Wrote: bar
4Write something: exit
You Wrote: quux
4
You Wrote: exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Why this particular delayed interleaving is happening, and why the
WriteLn is getting ignored, is puzzling to me.
Is there some known issue with interfacing the C libraries, or the
Readline library specifically, that I need to be aware of?