2020-04-21 20:06:09 +02:00
|
|
|
use std::ffi::{OsStr, OsString};
|
2018-10-07 11:54:01 +02:00
|
|
|
use std::fs::File;
|
2020-04-21 21:14:44 +02:00
|
|
|
use std::io::{self, BufRead, BufReader, Read};
|
2018-10-07 11:54:01 +02:00
|
|
|
|
2018-10-07 16:44:59 +02:00
|
|
|
use content_inspector::{self, ContentType};
|
|
|
|
|
2020-04-22 21:45:47 +02:00
|
|
|
use crate::error::*;
|
2018-10-07 11:54:01 +02:00
|
|
|
|
2020-05-16 01:18:10 +02:00
|
|
|
/// A description of an Input source.
|
2020-05-15 23:21:38 +02:00
|
|
|
/// This tells bat how to refer to the input.
|
2020-05-16 01:18:10 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct InputDescription {
|
2020-05-17 00:06:43 +02:00
|
|
|
pub(crate) name: String,
|
|
|
|
|
|
|
|
/// The input title.
|
|
|
|
/// This replaces the name if provided.
|
|
|
|
title: Option<String>,
|
|
|
|
|
|
|
|
/// The input kind.
|
2020-05-16 01:18:10 +02:00
|
|
|
kind: Option<String>,
|
2020-05-17 00:06:43 +02:00
|
|
|
|
|
|
|
/// A summary description of the input.
|
|
|
|
/// Defaults to "{kind} '{name}'"
|
2020-05-16 01:18:10 +02:00
|
|
|
summary: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InputDescription {
|
|
|
|
/// Creates a description for an input.
|
|
|
|
pub fn new(name: impl Into<String>) -> Self {
|
|
|
|
InputDescription {
|
|
|
|
name: name.into(),
|
2020-05-17 00:06:43 +02:00
|
|
|
title: None,
|
2020-05-16 01:18:10 +02:00
|
|
|
kind: None,
|
|
|
|
summary: None,
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 23:21:38 +02:00
|
|
|
|
2020-05-28 01:26:28 +02:00
|
|
|
pub fn set_kind(&mut self, kind: Option<String>) {
|
2020-05-17 00:06:43 +02:00
|
|
|
self.kind = kind;
|
2020-05-16 01:18:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 01:26:28 +02:00
|
|
|
pub fn set_summary(&mut self, summary: Option<String>) {
|
2020-05-17 00:06:43 +02:00
|
|
|
self.summary = summary;
|
2020-05-16 01:18:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 01:26:28 +02:00
|
|
|
pub fn set_title(&mut self, title: Option<String>) {
|
2020-05-17 00:06:43 +02:00
|
|
|
self.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn title(&self) -> &String {
|
|
|
|
match self.title.as_ref() {
|
|
|
|
Some(ref title) => title,
|
|
|
|
None => &self.name,
|
|
|
|
}
|
2020-05-16 01:18:10 +02:00
|
|
|
}
|
2020-05-15 23:21:38 +02:00
|
|
|
|
2020-05-16 01:18:10 +02:00
|
|
|
pub fn kind(&self) -> Option<&String> {
|
|
|
|
self.kind.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn summary(&self) -> String {
|
|
|
|
self.summary.clone().unwrap_or_else(|| match &self.kind {
|
|
|
|
None => self.name.clone(),
|
|
|
|
Some(kind) => format!("{} '{}'", kind.to_lowercase(), self.name),
|
|
|
|
})
|
|
|
|
}
|
2020-04-21 22:24:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) enum InputKind<'a> {
|
2020-04-22 16:27:34 +02:00
|
|
|
OrdinaryFile(OsString),
|
|
|
|
StdIn,
|
2020-04-22 18:10:26 +02:00
|
|
|
CustomReader(Box<dyn Read + 'a>),
|
2020-04-22 16:27:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 01:18:10 +02:00
|
|
|
impl<'a> InputKind<'a> {
|
|
|
|
pub fn description(&self) -> InputDescription {
|
|
|
|
match self {
|
2020-05-17 00:06:43 +02:00
|
|
|
InputKind::OrdinaryFile(ref path) => InputDescription::new(path.to_string_lossy()),
|
2020-05-16 01:18:10 +02:00
|
|
|
InputKind::StdIn => InputDescription::new("STDIN"),
|
|
|
|
InputKind::CustomReader(_) => InputDescription::new("READER"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:27:34 +02:00
|
|
|
#[derive(Clone, Default)]
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) struct InputMetadata {
|
|
|
|
pub(crate) user_provided_name: Option<OsString>,
|
2020-04-22 16:27:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:10:26 +02:00
|
|
|
pub struct Input<'a> {
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) kind: InputKind<'a>,
|
|
|
|
pub(crate) metadata: InputMetadata,
|
2020-05-17 00:06:43 +02:00
|
|
|
pub(crate) description: InputDescription,
|
2020-04-22 16:27:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) enum OpenedInputKind {
|
2020-04-22 16:27:34 +02:00
|
|
|
OrdinaryFile(OsString),
|
|
|
|
StdIn,
|
|
|
|
CustomReader,
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) struct OpenedInput<'a> {
|
|
|
|
pub(crate) kind: OpenedInputKind,
|
|
|
|
pub(crate) metadata: InputMetadata,
|
|
|
|
pub(crate) reader: InputReader<'a>,
|
2020-05-16 01:18:10 +02:00
|
|
|
pub(crate) description: InputDescription,
|
2018-10-07 11:21:41 +02:00
|
|
|
}
|
2018-10-07 11:54:01 +02:00
|
|
|
|
2020-04-22 18:10:26 +02:00
|
|
|
impl<'a> Input<'a> {
|
2020-04-22 16:27:34 +02:00
|
|
|
pub fn ordinary_file(path: &OsStr) -> Self {
|
2020-05-17 00:06:43 +02:00
|
|
|
let kind = InputKind::OrdinaryFile(path.to_os_string());
|
2020-04-22 16:27:34 +02:00
|
|
|
Input {
|
2020-05-17 00:06:43 +02:00
|
|
|
description: kind.description(),
|
2020-04-22 16:27:34 +02:00
|
|
|
metadata: InputMetadata::default(),
|
2020-05-17 00:06:43 +02:00
|
|
|
kind,
|
2018-10-07 11:54:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 22:24:47 +02:00
|
|
|
|
2020-04-22 16:27:34 +02:00
|
|
|
pub fn stdin() -> Self {
|
2020-05-17 00:06:43 +02:00
|
|
|
let kind = InputKind::StdIn;
|
2020-04-22 16:27:34 +02:00
|
|
|
Input {
|
2020-05-17 00:06:43 +02:00
|
|
|
description: kind.description(),
|
2020-04-22 16:27:34 +02:00
|
|
|
metadata: InputMetadata::default(),
|
2020-05-17 00:06:43 +02:00
|
|
|
kind,
|
2020-04-22 16:27:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:10:26 +02:00
|
|
|
pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
|
2020-05-17 00:06:43 +02:00
|
|
|
let kind = InputKind::CustomReader(reader);
|
2020-04-22 18:10:26 +02:00
|
|
|
Input {
|
2020-05-17 00:06:43 +02:00
|
|
|
description: kind.description(),
|
2020-04-22 18:10:26 +02:00
|
|
|
metadata: InputMetadata::default(),
|
2020-05-17 00:06:43 +02:00
|
|
|
kind,
|
2020-04-22 18:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:27:34 +02:00
|
|
|
pub fn is_stdin(&self) -> bool {
|
|
|
|
if let InputKind::StdIn = self.kind {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:41:25 +02:00
|
|
|
pub fn with_name(mut self, provided_name: Option<&OsStr>) -> Self {
|
2020-05-17 00:06:43 +02:00
|
|
|
match provided_name {
|
|
|
|
Some(name) => self.description.name = name.to_string_lossy().to_string(),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:27:34 +02:00
|
|
|
self.metadata.user_provided_name = provided_name.map(|n| n.to_owned());
|
2020-04-22 22:41:25 +02:00
|
|
|
self
|
2020-04-22 16:27:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 00:06:43 +02:00
|
|
|
pub fn description(&self) -> &InputDescription {
|
|
|
|
&self.description
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn description_mut(&mut self) -> &mut InputDescription {
|
|
|
|
&mut self.description
|
2020-05-16 01:18:10 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) fn open<R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> {
|
2020-05-16 01:18:10 +02:00
|
|
|
let description = self.description().clone();
|
2020-04-22 16:27:34 +02:00
|
|
|
match self.kind {
|
|
|
|
InputKind::StdIn => Ok(OpenedInput {
|
|
|
|
kind: OpenedInputKind::StdIn,
|
2020-05-16 01:18:10 +02:00
|
|
|
description,
|
2020-04-22 16:27:34 +02:00
|
|
|
metadata: self.metadata,
|
|
|
|
reader: InputReader::new(stdin),
|
|
|
|
}),
|
|
|
|
InputKind::OrdinaryFile(path) => Ok(OpenedInput {
|
|
|
|
kind: OpenedInputKind::OrdinaryFile(path.clone()),
|
2020-05-16 01:18:10 +02:00
|
|
|
description,
|
2020-04-22 16:27:34 +02:00
|
|
|
metadata: self.metadata,
|
|
|
|
reader: {
|
|
|
|
let file = File::open(&path)
|
|
|
|
.map_err(|e| format!("'{}': {}", path.to_string_lossy(), e))?;
|
|
|
|
if file.metadata()?.is_dir() {
|
|
|
|
return Err(format!("'{}' is a directory.", path.to_string_lossy()).into());
|
|
|
|
}
|
|
|
|
InputReader::new(BufReader::new(file))
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
InputKind::CustomReader(reader) => Ok(OpenedInput {
|
2020-05-16 01:18:10 +02:00
|
|
|
description,
|
2020-04-22 16:27:34 +02:00
|
|
|
kind: OpenedInputKind::CustomReader,
|
|
|
|
metadata: self.metadata,
|
|
|
|
reader: InputReader::new(BufReader::new(reader)),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) struct InputReader<'a> {
|
2020-04-21 22:24:47 +02:00
|
|
|
inner: Box<dyn BufRead + 'a>,
|
|
|
|
pub(crate) first_line: Vec<u8>,
|
|
|
|
pub(crate) content_type: Option<ContentType>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> InputReader<'a> {
|
|
|
|
fn new<R: BufRead + 'a>(mut reader: R) -> InputReader<'a> {
|
|
|
|
let mut first_line = vec![];
|
|
|
|
reader.read_until(b'\n', &mut first_line).ok();
|
|
|
|
|
|
|
|
let content_type = if first_line.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(content_inspector::inspect(&first_line[..]))
|
|
|
|
};
|
|
|
|
|
|
|
|
if content_type == Some(ContentType::UTF_16LE) {
|
|
|
|
reader.read_until(0x00, &mut first_line).ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputReader {
|
|
|
|
inner: Box::new(reader),
|
|
|
|
first_line,
|
|
|
|
content_type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
|
|
|
|
if self.first_line.is_empty() {
|
|
|
|
let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
|
|
|
|
|
|
|
|
if self.content_type == Some(ContentType::UTF_16LE) {
|
|
|
|
self.inner.read_until(0x00, buf).ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
} else {
|
|
|
|
buf.append(&mut self.first_line);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 11:54:01 +02:00
|
|
|
}
|
2018-10-07 12:29:38 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2018-10-07 13:25:49 +02:00
|
|
|
let content = b"#!/bin/bash\necho hello";
|
2020-04-21 21:19:06 +02:00
|
|
|
let mut reader = InputReader::new(&content[..]);
|
2018-10-07 12:29:38 +02:00
|
|
|
|
2018-10-07 13:47:54 +02:00
|
|
|
assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]);
|
2018-10-07 13:25:49 +02:00
|
|
|
|
2018-10-07 12:29:38 +02:00
|
|
|
let mut buffer = vec![];
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(true, res.unwrap());
|
2018-10-07 13:25:49 +02:00
|
|
|
assert_eq!(b"#!/bin/bash\n", &buffer[..]);
|
2018-10-07 12:29:38 +02:00
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(true, res.unwrap());
|
2018-10-07 13:25:49 +02:00
|
|
|
assert_eq!(b"echo hello", &buffer[..]);
|
2018-10-07 12:29:38 +02:00
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(false, res.unwrap());
|
|
|
|
assert!(buffer.is_empty());
|
|
|
|
}
|
2018-10-07 16:44:59 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn utf16le() {
|
|
|
|
let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00";
|
2020-04-21 21:19:06 +02:00
|
|
|
let mut reader = InputReader::new(&content[..]);
|
2018-10-07 16:44:59 +02:00
|
|
|
|
|
|
|
assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &reader.first_line[..]);
|
|
|
|
|
|
|
|
let mut buffer = vec![];
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(true, res.unwrap());
|
|
|
|
assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &buffer[..]);
|
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(true, res.unwrap());
|
|
|
|
assert_eq!(b"\x64\x00", &buffer[..]);
|
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(false, res.unwrap());
|
|
|
|
assert!(buffer.is_empty());
|
|
|
|
}
|