aboutsummaryrefslogtreecommitdiffstats
path: root/src/grt
diff options
context:
space:
mode:
authorTristan Gingold <tgingold@free.fr>2022-05-15 18:26:02 +0200
committerTristan Gingold <tgingold@free.fr>2022-05-15 18:26:02 +0200
commite42c612298c56f6c4969c3e8d6e71a3e775f1611 (patch)
tree15cb106dfcd1180c961eca5b8399635375cfb540 /src/grt
parentdfc2238813b6a8caa6dfae91ae6838dbd8ef218a (diff)
downloadghdl-e42c612298c56f6c4969c3e8d6e71a3e775f1611.tar.gz
ghdl-e42c612298c56f6c4969c3e8d6e71a3e775f1611.tar.bz2
ghdl-e42c612298c56f6c4969c3e8d6e71a3e775f1611.zip
grt-readline_none.adb: do not use getline(3)
Not available on windows.
Diffstat (limited to 'src/grt')
-rw-r--r--src/grt/grt-readline_none.adb43
1 files changed, 24 insertions, 19 deletions
diff --git a/src/grt/grt-readline_none.adb b/src/grt/grt-readline_none.adb
index cbcf35b19..4532e03b8 100644
--- a/src/grt/grt-readline_none.adb
+++ b/src/grt/grt-readline_none.adb
@@ -20,36 +20,41 @@
-- covered by the GNU General Public License. This exception does not
-- however invalidate any other reasons why the executable file might be
-- covered by the GNU Public License.
-with System; use System;
with Grt.C; use Grt.C;
with Grt.Stdio; use Grt.Stdio;
package body Grt.Readline_None is
- function getline (Linep : Address; Linecapp : Address; Stream : FILEs)
- return ssize_t;
- pragma Import (C, getline);
-
function Readline (Prompt : Ghdl_C_String) return Ghdl_C_String
is
- Len : ssize_t;
Linep : Ghdl_C_String;
- Linecapp : size_t;
- T : int;
+ Cap : size_t;
+ Len : Positive;
+ C : int;
begin
- T := fputs (To_Address (Prompt), stdout);
- pragma Unreferenced (T);
+ C := fputs (To_Address (Prompt), stdout);
- Linep := null;
- Linecapp := 0;
- Len := getline (Linep'Address, Linecapp'Address, stdin);
- if Len <= 0 then
- free (Linep);
+ -- Mimic getline(), which is not available on windows.
+ Cap := 64;
+ Linep := To_Ghdl_C_String (Malloc (Cap));
+ if Linep = null then
return null;
end if;
- -- Remove end of line.
- if Linep (Natural (Len)) = ASCII.LF then
- Linep (Natural (Len)) := ASCII.NUL;
- end if;
+
+ Len := 1;
+ loop
+ C := fgetc (stdin);
+ exit when C < 0 or C = Character'Pos (ASCII.LF);
+ Len := Len + 1;
+ if size_t (Len) = Cap then
+ Cap := Cap * 2;
+ Linep := To_Ghdl_C_String (Realloc (To_Address (Linep), Cap));
+ if Linep = null then
+ return null;
+ end if;
+ end if;
+ Linep (Len - 1) := Character'Val (C);
+ end loop;
+ Linep (Len) := ASCII.NUL;
return Linep;
end Readline;