diff --git a/Cargo.lock b/Cargo.lock index 5d7eda7e..060c41e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,6 +99,7 @@ dependencies = [ "assert_cmd", "atty", "clap", + "clircle", "console", "content_inspector", "dirs", @@ -218,6 +219,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.19" @@ -244,6 +251,18 @@ dependencies = [ "vec_map", ] +[[package]] +name = "clircle" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e27a01e782190a8314e65cc94274d9bbcd52e05a9d15b437fe2b31259b854b0d" +dependencies = [ + "cfg-if 1.0.0", + "nix", + "serde", + "winapi", +] + [[package]] name = "console" version = "0.14.0" @@ -280,7 +299,7 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -290,7 +309,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "lazy_static", ] @@ -442,7 +461,7 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da80be589a72651dcda34d8b35bcdc9b7254ad06325611074d9cc0fbb19f60ee" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "crc32fast", "libc", "miniz_oxide", @@ -484,7 +503,7 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "wasi", ] @@ -635,7 +654,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -666,6 +685,18 @@ dependencies = [ "autocfg", ] +[[package]] +name = "nix" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ccba0cfe4fdf15982d1674c69b1fd80bad427d293849982668dfe454bd61f2" +dependencies = [ + "bitflags", + "cc", + "cfg-if 1.0.0", + "libc", +] + [[package]] name = "normalize-line-endings" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 6afe7523..50d7df94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.8" semver = "0.11" path_abs = { version = "0.5", default-features = false } +clircle = "0.2.0" [dependencies.git2] version = "0.13" diff --git a/src/controller.rs b/src/controller.rs index 8dd73986..a1e3f119 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,3 +1,4 @@ +use std::convert::TryFrom; use std::io::{self, Write}; use crate::assets::HighlightingAssets; @@ -66,6 +67,14 @@ impl<'b> Controller<'b> { } let attached_to_pager = output_type.is_pager(); + let rw_cycle_detected = !attached_to_pager && { + let identifiers: Vec<_> = inputs + .iter() + .flat_map(clircle::Identifier::try_from) + .collect(); + clircle::stdout_among_inputs(&identifiers) + }; + let writer = output_type.handle()?; let mut no_errors: bool = true; @@ -78,6 +87,11 @@ impl<'b> Controller<'b> { } }; + if rw_cycle_detected { + print_error(&"The output file is also an input!".into(), writer); + return Ok(false); + } + for (index, input) in inputs.into_iter().enumerate() { match input.open(io::stdin().lock()) { Err(error) => { diff --git a/src/input.rs b/src/input.rs index 5d8493c0..5b6a4e67 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,6 +1,8 @@ +use std::convert::TryFrom; use std::ffi::{OsStr, OsString}; use std::fs::File; use std::io::{self, BufRead, BufReader, Read}; +use std::path::Path; use content_inspector::{self, ContentType}; @@ -191,6 +193,18 @@ impl<'a> Input<'a> { } } +impl TryFrom<&'_ Input<'_>> for clircle::Identifier { + type Error = (); + + fn try_from(input: &Input) -> std::result::Result { + match input.kind { + InputKind::OrdinaryFile(ref path) => Self::try_from(Path::new(path)).map_err(|_| ()), + InputKind::StdIn => Self::try_from(clircle::Stdio::Stdin).map_err(|_| ()), + InputKind::CustomReader(_) => Err(()), + } + } +} + pub(crate) struct InputReader<'a> { inner: Box, pub(crate) first_line: Vec,