+function raw_mode!(mode::Bool)
+ if ccall(:jl_tty_set_mode, Int32, (Ptr{Void}, Int32), STDIN.handle, mode) != 0
+ throw("FATAL: Terminal unable to enter raw mode.")
+ end
+end
+
+function getKey()
+ raw_mode!(true)
+ byte = readbytes(STDIN, 1)[1]
+ raw_mode!(false)
+
+ if byte == 0x0d
+ return 0x0a
+ elseif byte == 127
+ return 0x08
+ else
+ return byte
+ end
+end
+
+KEY_CFA = defPrimWord("KEY", () -> begin
+ pushPS(Int(getKey()))
+ return NEXT
+end)
+
+function getLineFromSTDIN()
+ line = ""
+ while true
+ key = Char(getKey())
+
+ if key == '\n'
+ print(" ")
+ return ASCIIString(line)
+
+ elseif key == '\x04'
+ if isempty(line)
+ return string("\x04")
+ end
+
+ elseif key == '\b'
+ if !isempty(line)
+ line = line[1:length(line)-1]
+ print("\b \b")
+ end
+
+ elseif key == '\e'
+ # Strip ANSI escape sequence
+ nextKey = Char(getKey())
+ if nextKey == '['
+ while true
+ nextKey = Char(getKey())
+ if nextKey >= '@' || nextKey <= '~'
+ break
+ end
+ end
+ end
+
+ else
+ print(key)
+ line = string(line, key)
+ end
+ end
+end
+