diff --git a/stdlib/internal/builtin.codon b/stdlib/internal/builtin.codon index b5c21323..c87488f3 100644 --- a/stdlib/internal/builtin.codon +++ b/stdlib/internal/builtin.codon @@ -34,6 +34,18 @@ def print(*args, sep: str = " ", end: str = "\n", file=_stdout, flush: bool = Fa if flush: _C.fflush(fp) +def input(prompt: str = '') -> str: + if (prompt != ''): + print(prompt, end='') + buf = Ptr[byte]() + sz = 0 + rd = _C.getline(Ptr[Ptr[byte]](__ptr__(buf)), Ptr[int](__ptr__(sz)), _C.seq_stdin()) + if rd == -1: + return '' + if buf[rd - 1] == byte(10): + rd -= 1 + return str(buf, rd) + @extend class __internal__: def print(*args): diff --git a/test/app/input.codon b/test/app/input.codon new file mode 100644 index 00000000..c19ebe8f --- /dev/null +++ b/test/app/input.codon @@ -0,0 +1,3 @@ +print(input("input: "), end=',') +print(input(), end=',') +print(input()) diff --git a/test/app/input.txt b/test/app/input.txt new file mode 100644 index 00000000..f96c779a --- /dev/null +++ b/test/app/input.txt @@ -0,0 +1,3 @@ +aa bb + +cc diff --git a/test/app/test.sh b/test/app/test.sh index 038644f8..d48d037b 100755 --- a/test/app/test.sh +++ b/test/app/test.sh @@ -18,3 +18,6 @@ gcc "$testdir/test.c" -L"$arg" -Wl,-rpath,"$arg" -lcodon_export_test -o "$arg/te # exit code test $codon run "$testdir/exit.codon" || if [[ $? -ne 42 ]]; then exit 4; fi + +# input test +[ "$($codon run "$testdir/input.codon" < "$testdir/input.txt")" == "input: aa bb,,cc" ] || exit 5