From e42c612298c56f6c4969c3e8d6e71a3e775f1611 Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Sun, 15 May 2022 18:26:02 +0200 Subject: grt-readline_none.adb: do not use getline(3) Not available on windows. --- src/grt/grt-readline_none.adb | 43 ++++++++++++++++++++++++------------------- 1 file 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; -- cgit v1.2.3