Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need ability to specify which file descriptor should be used for input? #599

Closed
n1000 opened this issue Feb 12, 2022 · 8 comments · Fixed by #601
Closed

Need ability to specify which file descriptor should be used for input? #599

n1000 opened this issue Feb 12, 2022 · 8 comments · Fixed by #601

Comments

@n1000
Copy link

n1000 commented Feb 12, 2022

I believe the ability to swap out what FD to use for input is needed in cases where the program using rustyline is having its stdin redirected from a file. In these cases, rustyreadline needs to use a FD still connected to the terminal to read input from, instead of using FD=0...

I was looking at this issue: PaulJuliusMartinez/jless#7 (note that I have no affiliation with this project)

And noticed that src/tty/unix.rs directly refered to STDIN_FILENO, without support for passing in / using another FD (such as a fd pointing to /dev/tty).

This seems to sort of be related to #542 (but this would be a simpler change).

A hacked up version of what this might look like is here: https://github.com/kkawakam/rustyline/compare/master...n1000:configurable_input?expand=1

I'm a Rust beginner, so I mostly worked on this to try and further my learning... The changes above are a hack, but demonstrate the concept. I've realized it's time to go back and reread some parts of "Programming Rust"... :)

It may also be the case that some much simpler solution to this exists, and no change in rustyline is needed...

@PaulJuliusMartinez
Copy link

I had considered filing a bug for this, but I believe it can be handled on the user's side by using freopen, based on this StackOverflow post. I haven't had a chance to test this out yet though.

Even if this does work, I think allowing for some configuration of the input file descriptor, or just supporting a toggle to read from /dev/tty makes sense.

@n1000
Copy link
Author

n1000 commented Feb 13, 2022

For reference:

* https://github.com/wez/wezterm/blob/main/termwiz/src/terminal/unix.rs#L248-L253

* https://github.com/wez/wezterm/blob/main/termwiz/src/terminal/windows.rs#L630-L635

Thanks, updated src/tty/windows.rs to use "CONIN$" instead of "CON:", and also opened it with both read and write permissions (otherwise was getting a access denied when attempting to call SetConsoleMode()). Did a quick test in a Windows VM, and seems to work.

I had considered filing a bug for this, but I believe it can be handled on the user's side by using freopen, based on this StackOverflow post. I haven't had a chance to test this out yet though.

Agree on this, but unfortunately there also may be some difficulty using freopen, based on this PR: rust-lang/libc#7 . I think that issue is still unresolved based on my searching so far (but I may have missed something also).

@PaulJuliusMartinez
Copy link

I was able to use freopen to get around this, but, as mentioned by @n1000, had to bring in an external crate to get a reference to the FILE * associated with stdin.

@gwenn
Copy link
Collaborator

gwenn commented Feb 16, 2022

Could you please give a try to this branch ?

let config = Config::builder()
        .behavior(Behavior::PreferTerm)
        .build();
let mut rl = Editor::with_config(config);
echo x | cargo run --example example > debug.log
1> ...

Like Haskeline, with PreferTerm behavior, /dev/tty is used both for input and output on unix platform.
By default, rustyline keeps using stdin / stdout: default Behavior is Stdio.
Thanks.

I still need to fix: https://github.com/gwenn/rustyline/blob/dev_tty/src/tty/windows.rs#L495-L515 (see https://github.com/judah/haskeline/blob/c03e7029b2d9c3d16da5480306b42b8d4ebe03cf/System/Console/Haskeline/Backend/Win32.hsc#L238-L240 or https://github.com/rust-lang/rust/blob/master/library/std/src/sys/windows/stdio.rs#L53-L60)

@n1000
Copy link
Author

n1000 commented Feb 19, 2022

Works fine for me on Linux (did not try out Windows).

@gwenn gwenn mentioned this issue Feb 21, 2022
@gwenn
Copy link
Collaborator

gwenn commented Jul 17, 2022

Version 10.0.0 released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants