summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMartin Nordholts <enselic@gmail.com>2021-01-06 14:30:55 +0100
committerMartin Nordholts <enselic@gmail.com>2021-01-06 14:30:55 +0100
commit46487b201ff0f4cdcbfdea9d328cbac6d29a22d1 (patch)
tree2d9f3723135391a8edbd9d7779db0e13662860c4 /src
parent478233f7952d1c634258a76589d2f8151d7c51c0 (diff)
parentb600f62ab68e0a11a9bb69dcf45648632d3f52ee (diff)
Merge remote-tracking branch 'origin/master' into fix-1063
Diffstat (limited to 'src')
-rw-r--r--src/controller.rs14
-rw-r--r--src/input.rs14
2 files changed, 28 insertions, 0 deletions
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<clircle::Identifier, ()> {
+ 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<dyn BufRead + 'a>,
pub(crate) first_line: Vec<u8>,