Blocking console input

If you want a blocking input in the Windows console, use this:

#import "Windows";

kernel32 :: #foreign_system_library "kernel32";

stdin  : HANDLE;
stdout : HANDLE;

ReadConsoleA  :: (
    hConsoleHandle: HANDLE, 
    buff : *u8, 
    chars_to_read : s32,  
    chars_read : *s32, 
    lpInputControl := *void 
) -> BOOL #foreign kernel32;

input :: () -> string {
    MAX_BYTES_TO_READ :: 1024;
    temp : [MAX_BYTES_TO_READ] u8;
    result: string = ---;
    bytes_read : s32;
    
    if !ReadConsoleA( stdin, temp.data, xx temp.count, *bytes_read )
        return "";

    result.data  = alloc(bytes_read);
    result.count = bytes_read;
    memcpy(result.data, temp.data, bytes_read);
    return result;
}

main :: () {
    stdin = GetStdHandle( STD_INPUT_HANDLE );
    str := input();
}

Don’t forget to remove the CR at the end of lines.

If you want a blocking input in the Linux console, this is the corresponding Linux console input example:

#import "Basic";
#import "POSIX";

main :: () {
  buffer: [4096] u8;
  bytes_read := read(STDIN_FILENO, buffer.data, buffer.count-1);
  str := to_string(buffer.data, bytes_read);
  print("Here is the string from console input: %\n", str);
}