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

Vendor dependency os_pipe #822

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ edition = "2018"

[dependencies]
jobserver = { version = "0.1.16", optional = true }
os_pipe = "1"

[target.'cfg(unix)'.dependencies]
libc = "0.2.62"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.48.0", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_Security", "Win32_System_Threading"] }
NobodyXu marked this conversation as resolved.
Show resolved Hide resolved

[features]
parallel = ["jobserver"]
Expand Down
11 changes: 11 additions & 0 deletions src/bin/cat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![cfg_attr(test, allow(dead_code))]

/// Windows doesn't have a native equivalent for cat, so we use this little
/// Rust implementation instead.
use std::io::{copy, stdin, stdout};

fn main() {
let stdin_handle = stdin();
let stdout_handle = stdout();
copy(&mut stdin_handle.lock(), &mut stdout_handle.lock()).unwrap();
}
18 changes: 18 additions & 0 deletions src/bin/cat_both.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! This little test binary reads stdin, and then writes what it read to both
//! stdout and stderr, with a little tag to differentiate them. We use it to
//! test duping the standard file descriptors.

#![cfg_attr(test, allow(dead_code))]

use std::io::{self, prelude::*};

fn main() {
let mut input = Vec::new();
io::stdin().read_to_end(&mut input).unwrap();

print!("stdout: ");
io::stdout().write_all(&input).unwrap();

eprint!("stderr: ");
io::stderr().write_all(&input).unwrap();
}
48 changes: 48 additions & 0 deletions src/bin/swap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#![cfg_attr(test, allow(dead_code))]

/// This little test binary reads stdin and write what it reads to both
/// stdout and stderr. It depends on os_pipe's parent_* functions, and
/// we use it to test them.
use std::{env::args_os, fs::File, io, mem::ManuallyDrop, process::Command};

#[cfg(windows)]
use std::os::windows::prelude::*;

#[cfg(unix)]
use std::os::unix::prelude::*;

#[cfg(windows)]
fn dup(f: &dyn AsRawHandle) -> File {
let handle = f.as_raw_handle();
ManuallyDrop::new(unsafe { File::from_raw_handle(handle) })
.try_clone()
.unwrap()
}

#[cfg(unix)]
fn dup(f: &dyn AsRawFd) -> File {
let handle = f.as_raw_fd();
ManuallyDrop::new(unsafe { File::from_raw_fd(handle) })
.try_clone()
.unwrap()
}

fn main() {
let stdin = dup(&io::stdin());
let stdout = dup(&io::stdout());
let stderr = dup(&io::stderr());

let mut args = args_os();
args.next().unwrap(); // Ignore args[0]
let mut child = Command::new(args.next().unwrap()); // Run args[1]
child.args(args); // Feed rest of the arg into the program

// Swap stdout and stderr in the child. Set stdin too, just for testing,
// though this should be the same as the default behavior.
child.stdin(stdin);
child.stdout(stderr);
child.stderr(stdout);

// Run the child. This method is kind of confusingly named...
child.status().unwrap();
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};

mod os_pipe;

// These modules are all glue to support reading the MSVC version from
// the registry and from COM interfaces
#[cfg(windows)]
Expand Down
Loading