Can I read with stdin in a noncanonical way?

Is it possible to read stdin in noncanonical mode under Linux? Non-canonical input means that read() calls to stdin should return as soon as the user enters, which is not the default behavior, as you can see by trying:

 // Create a buffer let mut buffer :[u8; 1] = [0]; // Loops over the input from stdin, one character a time while io::stdin().read(&mut buffer).unwrap() > 0 { println!("{:?}", buffer); } 

This code expects the user to press return to print the contents of buffer . It is desirable that it prints as you type. The documentation for Stdin (the structure returned by calling stdin() in the above code) does not refer to how this default behavior could be changed.

+5
source share
1 answer

No, not without external mailboxes or insecure FFI code. You might want to use the termios functions. In particular, see ICANON and tcsetattr . The nix box has bindings for these functions. See here for an example of how to use them in Rust.

+1
source

Source: https://habr.com/ru/post/1235971/


All Articles