-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/.uv | ||
/pycmd | ||
/target | ||
|
||
################## | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
pub mod unix; | ||
|
||
use ::anyhow::Result; | ||
use ::std::env; | ||
use ::std::fs::File; | ||
use ::std::io::{self, Write}; | ||
use ::std::process; | ||
|
||
fn open_output(path: &str) -> Result<Box<dyn Write>> { | ||
if path == "-" { | ||
Ok(Box::new(io::stdout())) | ||
} else { | ||
Ok(Box::new(File::create(path)?)) | ||
} | ||
} | ||
|
||
fn tee(outputs: &mut [Box<dyn Write>]) -> Result<()> { | ||
#[cfg(any(target_os = "linux", target_os = "macos"))] | ||
{ | ||
return unix::tee(outputs); | ||
} | ||
|
||
#[cfg(any(target_os = "windows"))] | ||
{ | ||
bail!("unsupported platform"); | ||
} | ||
|
||
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] | ||
bail!("unsupported platform"); | ||
} | ||
|
||
pub fn run() -> Result<()> { | ||
let args: Vec<String> = env::args().collect(); | ||
let mut outputs: Vec<Box<dyn Write>> = vec![Box::new(io::stdout())]; | ||
for path in &args[1..] { | ||
match open_output(path) { | ||
Ok(output) => outputs.push(output), | ||
Err(e) => { | ||
eprintln!("Error opening {}: {}", path, e); | ||
process::exit(1); | ||
} | ||
} | ||
} | ||
|
||
tee(&mut outputs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use ::anyhow::Result; | ||
use ::std::io::{self, Read, Write}; | ||
#[cfg(any(target_os = "linux", target_os = "macos"))] | ||
use ::std::os::unix::io::AsRawFd; | ||
|
||
#[cfg(any(target_os = "linux", target_os = "macos"))] | ||
pub fn tee(outputs: &mut [Box<dyn Write>]) -> Result<()> { | ||
let stdin_fd = io::stdin().as_raw_fd(); | ||
let mut pollfd = ::libc::pollfd { | ||
fd: stdin_fd, | ||
events: ::libc::POLLIN, | ||
revents: 0, | ||
}; | ||
|
||
let mut buffer = Vec::new(); | ||
loop { | ||
// iopoll で入力を待つ | ||
unsafe { | ||
::libc::poll(&mut pollfd, 1, -1); | ||
} | ||
|
||
if pollfd.revents & ::libc::POLLIN != 0 { | ||
// 入力がある場合 | ||
let mut buf = [0; 4096]; | ||
let bytes_read = io::stdin().read(&mut buf)?; | ||
if bytes_read == 0 { | ||
break; // EOF | ||
} | ||
buffer.extend_from_slice(&buf[..bytes_read]); | ||
|
||
// 出力 | ||
let s = String::from_utf8_lossy(&buffer); | ||
for output in outputs.iter_mut() { | ||
output.write_all(s.as_bytes())?; | ||
output.flush()?; | ||
} | ||
buffer.clear(); | ||
} else if pollfd.revents & ::libc::POLLHUP != 0 { | ||
break; // 入力ストリームが閉じられた | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |