use std::io::{Read, Write};
use crate::{CliError, CliResult};
pub fn run_cat(args: Vec<String>) -> CliResult {
let mut files: Vec<String> = Vec::new();
let mut only_positional = false;
for arg in args {
if only_positional {
files.push(arg);
continue;
}
match arg.as_str() {
"--" => only_positional = true,
"--help" => {
print!("{HELP}");
return Ok(());
},
"--version" => {
println!("oxcat {}", env!("CARGO_PKG_VERSION"));
return Ok(());
},
"-" => files.push(arg),
other if other.starts_with('-') && other.len() > 1 => {
return Err(CliError::usage(format!("unknown flag: {other}")));
},
_ => files.push(arg),
}
}
let stdout = std::io::stdout();
let mut out = stdout.lock();
if files.is_empty() {
let stdin = std::io::stdin();
return cat_stream(stdin.lock(), &mut out);
}
for file in &files {
if file == "-" {
let stdin = std::io::stdin();
cat_stream(stdin.lock(), &mut out)?;
} else {
let input = std::fs::File::open(file)
.map_err(|error| CliError::runtime(format!("cannot read {file}: {error}")))?;
cat_stream(input, &mut out)?;
}
}
Ok(())
}
fn cat_stream<R: Read, W: Write>(input: R, out: &mut W) -> CliResult {
let mut input = libarchive_oxide::FilterReader::new(input)
.map_err(|error| CliError::runtime(error.to_string()))?;
std::io::copy(&mut input, out)
.map(|_| ())
.map_err(|error| CliError::runtime(format!("cannot copy stream: {error}")))
}
const HELP: &str = "\
oxcat: bsdcat-compatible transparent decompressor
USAGE:
oxcat [FILE...] Decompress each FILE to stdout ('-' or none = stdin).
Auto-detects gzip/zstd/xz/lz4; uncompressed input is passed through. Decompression is
capped to defend against bombs (a documented safe default).
--help, --version
EXIT CODES: 0 success, 1 runtime failure, 2 usage error.
";