2019-08-02 21:15:07 +02:00
|
|
|
use crate::commands::Command;
|
2019-08-09 06:51:21 +02:00
|
|
|
use crate::parser::{hir, TokenNode};
|
2019-05-22 09:12:03 +02:00
|
|
|
use crate::prelude::*;
|
2019-05-26 08:54:41 +02:00
|
|
|
use bytes::{BufMut, BytesMut};
|
2019-06-15 19:52:55 +02:00
|
|
|
use futures::stream::StreamExt;
|
2019-05-26 08:54:41 +02:00
|
|
|
use futures_codec::{Decoder, Encoder, Framed};
|
2019-06-22 05:43:37 +02:00
|
|
|
use log::{log_enabled, trace};
|
2019-05-26 08:54:41 +02:00
|
|
|
use std::io::{Error, ErrorKind};
|
2019-05-22 09:12:03 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use subprocess::Exec;
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-05-25 21:07:52 +02:00
|
|
|
/// A simple `Codec` implementation that splits up data into lines.
|
|
|
|
pub struct LinesCodec {}
|
|
|
|
|
|
|
|
impl Encoder for LinesCodec {
|
|
|
|
type Item = String;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
|
|
|
dst.put(item);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decoder for LinesCodec {
|
|
|
|
type Item = String;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
|
|
|
match src.iter().position(|b| b == &b'\n') {
|
|
|
|
Some(pos) if !src.is_empty() => {
|
|
|
|
let buf = src.split_to(pos + 1);
|
|
|
|
String::from_utf8(buf.to_vec())
|
|
|
|
.map(Some)
|
|
|
|
.map_err(|e| Error::new(ErrorKind::InvalidData, e))
|
|
|
|
}
|
|
|
|
_ if !src.is_empty() => {
|
|
|
|
let drained = src.take();
|
|
|
|
String::from_utf8(drained.to_vec())
|
|
|
|
.map(Some)
|
|
|
|
.map_err(|e| Error::new(ErrorKind::InvalidData, e))
|
|
|
|
}
|
2019-05-26 08:54:41 +02:00
|
|
|
_ => Ok(None),
|
2019-05-25 21:07:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 09:12:03 +02:00
|
|
|
|
2019-05-24 09:29:16 +02:00
|
|
|
crate struct ClassifiedInputStream {
|
|
|
|
crate objects: InputStream,
|
|
|
|
crate stdin: Option<std::fs::File>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClassifiedInputStream {
|
|
|
|
crate fn new() -> ClassifiedInputStream {
|
|
|
|
ClassifiedInputStream {
|
2019-07-03 22:31:15 +02:00
|
|
|
objects: VecDeque::new().into(),
|
2019-05-24 09:29:16 +02:00
|
|
|
stdin: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
crate fn from_input_stream(stream: impl Into<InputStream>) -> ClassifiedInputStream {
|
|
|
|
ClassifiedInputStream {
|
|
|
|
objects: stream.into(),
|
2019-05-24 09:29:16 +02:00
|
|
|
stdin: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn from_stdout(stdout: std::fs::File) -> ClassifiedInputStream {
|
|
|
|
ClassifiedInputStream {
|
2019-07-03 22:31:15 +02:00
|
|
|
objects: VecDeque::new().into(),
|
2019-05-24 09:29:16 +02:00
|
|
|
stdin: Some(stdout),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 08:54:41 +02:00
|
|
|
crate struct ClassifiedPipeline {
|
|
|
|
crate commands: Vec<ClassifiedCommand>,
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
crate enum ClassifiedCommand {
|
2019-06-06 08:34:59 +02:00
|
|
|
#[allow(unused)]
|
2019-06-22 05:43:37 +02:00
|
|
|
Expr(TokenNode),
|
2019-05-22 09:12:03 +02:00
|
|
|
Internal(InternalCommand),
|
|
|
|
External(ExternalCommand),
|
|
|
|
}
|
|
|
|
|
2019-07-09 06:31:26 +02:00
|
|
|
impl ClassifiedCommand {
|
2019-07-12 21:22:08 +02:00
|
|
|
#[allow(unused)]
|
2019-07-09 06:31:26 +02:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match self {
|
|
|
|
ClassifiedCommand::Expr(token) => token.span(),
|
|
|
|
ClassifiedCommand::Internal(internal) => internal.name_span.into(),
|
|
|
|
ClassifiedCommand::External(external) => external.name_span.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
crate struct InternalCommand {
|
2019-08-02 21:15:07 +02:00
|
|
|
crate command: Arc<Command>,
|
2019-08-05 10:54:29 +02:00
|
|
|
crate name_span: Span,
|
2019-07-24 00:22:11 +02:00
|
|
|
crate args: hir::Call,
|
2019-05-22 09:12:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 09:29:16 +02:00
|
|
|
impl InternalCommand {
|
|
|
|
crate async fn run(
|
|
|
|
self,
|
|
|
|
context: &mut Context,
|
|
|
|
input: ClassifiedInputStream,
|
2019-07-24 00:22:11 +02:00
|
|
|
source: Text,
|
2019-05-24 09:29:16 +02:00
|
|
|
) -> Result<InputStream, ShellError> {
|
2019-07-03 22:31:15 +02:00
|
|
|
if log_enabled!(log::Level::Trace) {
|
2019-07-12 21:22:08 +02:00
|
|
|
trace!(target: "nu::run::internal", "->");
|
|
|
|
trace!(target: "nu::run::internal", "{}", self.command.name());
|
2019-07-24 00:22:11 +02:00
|
|
|
trace!(target: "nu::run::internal", "{}", self.args.debug(&source));
|
2019-07-03 22:31:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 21:22:08 +02:00
|
|
|
let objects: InputStream =
|
|
|
|
trace_stream!(target: "nu::trace_stream::internal", "input" = input.objects);
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-08-17 05:53:39 +02:00
|
|
|
let result = context.run_command(
|
|
|
|
self.command,
|
|
|
|
self.name_span.clone(),
|
|
|
|
context.source_map.clone(),
|
|
|
|
self.args,
|
|
|
|
source,
|
|
|
|
objects,
|
|
|
|
);
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
let mut result = result.values;
|
|
|
|
|
2019-06-15 19:52:55 +02:00
|
|
|
let mut stream = VecDeque::new();
|
|
|
|
while let Some(item) = result.next().await {
|
2019-07-03 22:31:15 +02:00
|
|
|
match item? {
|
|
|
|
ReturnSuccess::Action(action) => match action {
|
2019-06-15 19:52:55 +02:00
|
|
|
CommandAction::ChangePath(path) => {
|
2019-08-07 19:49:11 +02:00
|
|
|
context.shell_manager.set_path(path);
|
2019-06-14 00:49:16 +02:00
|
|
|
}
|
2019-07-19 21:48:14 +02:00
|
|
|
CommandAction::AddSpanSource(uuid, span_source) => {
|
|
|
|
context.add_span_source(uuid, span_source);
|
|
|
|
}
|
2019-07-16 21:10:25 +02:00
|
|
|
CommandAction::Exit => std::process::exit(0),
|
2019-08-14 19:02:39 +02:00
|
|
|
CommandAction::EnterValueShell(value) => {
|
2019-08-19 10:07:55 +02:00
|
|
|
context
|
|
|
|
.shell_manager
|
|
|
|
.insert_at_current(Box::new(ValueShell::new(value)));
|
2019-08-14 19:02:39 +02:00
|
|
|
}
|
2019-08-07 19:49:11 +02:00
|
|
|
CommandAction::EnterShell(location) => {
|
|
|
|
let path = std::path::Path::new(&location);
|
|
|
|
|
|
|
|
if path.is_dir() {
|
|
|
|
// If it's a directory, add a new filesystem shell
|
2019-08-19 10:07:55 +02:00
|
|
|
context.shell_manager.insert_at_current(Box::new(
|
|
|
|
FilesystemShell::with_location(
|
2019-08-10 07:02:15 +02:00
|
|
|
location,
|
|
|
|
context.registry().clone(),
|
2019-08-19 10:07:55 +02:00
|
|
|
)?,
|
|
|
|
));
|
2019-08-07 19:49:11 +02:00
|
|
|
} else {
|
|
|
|
// If it's a file, attempt to open the file as a value and enter it
|
|
|
|
let cwd = context.shell_manager.path();
|
|
|
|
|
|
|
|
let full_path = std::path::PathBuf::from(cwd);
|
|
|
|
|
2019-08-10 22:18:14 +02:00
|
|
|
let (file_extension, contents, contents_tag, span_source) =
|
2019-08-07 19:49:11 +02:00
|
|
|
crate::commands::open::fetch(
|
|
|
|
&full_path,
|
|
|
|
&location,
|
|
|
|
Span::unknown(),
|
2019-08-24 21:36:19 +02:00
|
|
|
)
|
|
|
|
.await?;
|
2019-08-07 19:49:11 +02:00
|
|
|
|
2019-08-10 22:18:14 +02:00
|
|
|
if let Some(uuid) = contents_tag.origin {
|
|
|
|
// If we have loaded something, track its source
|
|
|
|
context.add_span_source(uuid, span_source);
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:49:11 +02:00
|
|
|
match contents {
|
|
|
|
Value::Primitive(Primitive::String(string)) => {
|
|
|
|
let value = crate::commands::open::parse_as_value(
|
|
|
|
file_extension,
|
|
|
|
string,
|
|
|
|
contents_tag,
|
|
|
|
Span::unknown(),
|
|
|
|
)?;
|
|
|
|
|
2019-08-19 10:07:55 +02:00
|
|
|
context
|
|
|
|
.shell_manager
|
|
|
|
.insert_at_current(Box::new(ValueShell::new(value)));
|
2019-08-07 19:49:11 +02:00
|
|
|
}
|
2019-08-19 10:07:55 +02:00
|
|
|
value => context.shell_manager.insert_at_current(Box::new(
|
|
|
|
ValueShell::new(value.tagged(contents_tag)),
|
|
|
|
)),
|
2019-08-07 19:49:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CommandAction::PreviousShell => {
|
|
|
|
context.shell_manager.prev();
|
|
|
|
}
|
|
|
|
CommandAction::NextShell => {
|
|
|
|
context.shell_manager.next();
|
|
|
|
}
|
|
|
|
CommandAction::LeaveShell => {
|
2019-08-19 10:07:55 +02:00
|
|
|
context.shell_manager.remove_at_current();
|
2019-08-07 19:49:11 +02:00
|
|
|
if context.shell_manager.is_empty() {
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 19:52:55 +02:00
|
|
|
},
|
2019-05-24 09:29:16 +02:00
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
ReturnSuccess::Value(v) => {
|
2019-06-15 19:52:55 +02:00
|
|
|
stream.push_back(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 22:31:15 +02:00
|
|
|
|
|
|
|
Ok(stream.into())
|
2019-05-24 09:29:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
crate struct ExternalCommand {
|
|
|
|
crate name: String,
|
2019-06-15 06:56:18 +02:00
|
|
|
#[allow(unused)]
|
2019-08-05 10:54:29 +02:00
|
|
|
crate name_span: Span,
|
2019-08-01 03:58:42 +02:00
|
|
|
crate args: Vec<Tagged<String>>,
|
2019-05-22 09:12:03 +02:00
|
|
|
}
|
2019-05-24 09:29:16 +02:00
|
|
|
|
2019-05-24 20:48:33 +02:00
|
|
|
crate enum StreamNext {
|
|
|
|
Last,
|
|
|
|
External,
|
|
|
|
Internal,
|
|
|
|
}
|
|
|
|
|
2019-05-24 09:29:16 +02:00
|
|
|
impl ExternalCommand {
|
|
|
|
crate async fn run(
|
|
|
|
self,
|
|
|
|
context: &mut Context,
|
2019-05-24 21:35:22 +02:00
|
|
|
input: ClassifiedInputStream,
|
2019-05-24 20:48:33 +02:00
|
|
|
stream_next: StreamNext,
|
2019-05-24 09:29:16 +02:00
|
|
|
) -> Result<ClassifiedInputStream, ShellError> {
|
2019-07-03 22:31:15 +02:00
|
|
|
let stdin = input.stdin;
|
2019-08-01 03:58:42 +02:00
|
|
|
let inputs: Vec<Tagged<Value>> = input.objects.into_vec().await;
|
2019-07-09 06:31:26 +02:00
|
|
|
let name_span = self.name_span.clone();
|
2019-06-01 05:19:03 +02:00
|
|
|
|
2019-07-12 21:22:08 +02:00
|
|
|
trace!(target: "nu::run::external", "-> {}", self.name);
|
|
|
|
trace!(target: "nu::run::external", "inputs = {:?}", inputs);
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-06-01 05:19:03 +02:00
|
|
|
let mut arg_string = format!("{}", self.name);
|
|
|
|
for arg in &self.args {
|
2019-06-22 05:43:37 +02:00
|
|
|
arg_string.push_str(&arg);
|
2019-06-01 05:19:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 09:51:54 +02:00
|
|
|
let mut process;
|
2019-06-18 04:04:34 +02:00
|
|
|
|
2019-06-02 09:51:54 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
process = Exec::shell(&self.name);
|
|
|
|
|
|
|
|
if arg_string.contains("$it") {
|
|
|
|
let mut first = true;
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-06-02 09:51:54 +02:00
|
|
|
for i in &inputs {
|
2019-06-15 06:20:58 +02:00
|
|
|
if i.as_string().is_err() {
|
|
|
|
let mut span = None;
|
|
|
|
for arg in &self.args {
|
|
|
|
if arg.item.contains("$it") {
|
2019-08-01 03:58:42 +02:00
|
|
|
span = Some(arg.span());
|
2019-06-15 06:20:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(span) = span {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"External $it needs string data",
|
|
|
|
"given object instead of string data",
|
|
|
|
span,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::string("Error: $it needs string data"));
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 09:51:54 +02:00
|
|
|
if !first {
|
|
|
|
process = process.arg("&&");
|
|
|
|
process = process.arg(&self.name);
|
|
|
|
} else {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for arg in &self.args {
|
2019-06-22 05:43:37 +02:00
|
|
|
if arg.chars().all(|c| c.is_whitespace()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-17 05:53:39 +02:00
|
|
|
process = process.arg(&arg.replace("$it", &i.as_string()?));
|
2019-06-02 09:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-06-01 05:19:03 +02:00
|
|
|
for arg in &self.args {
|
2019-08-02 09:25:25 +02:00
|
|
|
let arg_chars: Vec<_> = arg.chars().collect();
|
2019-08-05 10:54:29 +02:00
|
|
|
if arg_chars.len() > 1
|
|
|
|
&& arg_chars[0] == '"'
|
|
|
|
&& arg_chars[arg_chars.len() - 1] == '"'
|
|
|
|
{
|
2019-08-02 09:25:25 +02:00
|
|
|
// quoted string
|
|
|
|
let new_arg: String = arg_chars[1..arg_chars.len() - 1].iter().collect();
|
|
|
|
process = process.arg(new_arg);
|
|
|
|
} else {
|
|
|
|
process = process.arg(arg.item.clone());
|
|
|
|
}
|
2019-06-02 09:51:54 +02:00
|
|
|
}
|
2019-06-01 05:19:03 +02:00
|
|
|
}
|
2019-06-02 09:51:54 +02:00
|
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
let mut new_arg_string = self.name.to_string();
|
|
|
|
|
|
|
|
if arg_string.contains("$it") {
|
|
|
|
let mut first = true;
|
|
|
|
for i in &inputs {
|
2019-06-15 06:20:58 +02:00
|
|
|
if i.as_string().is_err() {
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut span = name_span;
|
2019-06-15 06:20:58 +02:00
|
|
|
for arg in &self.args {
|
|
|
|
if arg.item.contains("$it") {
|
2019-08-05 10:54:29 +02:00
|
|
|
span = arg.span();
|
2019-06-15 06:20:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 10:54:29 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
2019-06-19 07:51:24 +02:00
|
|
|
"External $it needs string data",
|
|
|
|
"given object instead of string data",
|
|
|
|
span,
|
|
|
|
));
|
2019-06-15 06:20:58 +02:00
|
|
|
}
|
2019-06-02 09:51:54 +02:00
|
|
|
if !first {
|
2019-06-03 02:37:16 +02:00
|
|
|
new_arg_string.push_str("&&");
|
2019-06-02 09:51:54 +02:00
|
|
|
new_arg_string.push_str(&self.name);
|
|
|
|
} else {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for arg in &self.args {
|
2019-06-22 05:43:37 +02:00
|
|
|
if arg.chars().all(|c| c.is_whitespace()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-02 09:51:54 +02:00
|
|
|
new_arg_string.push_str(" ");
|
|
|
|
new_arg_string.push_str(&arg.replace("$it", &i.as_string().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for arg in &self.args {
|
|
|
|
new_arg_string.push_str(" ");
|
|
|
|
new_arg_string.push_str(&arg);
|
|
|
|
}
|
2019-06-01 05:19:03 +02:00
|
|
|
}
|
2019-06-18 04:04:34 +02:00
|
|
|
|
2019-06-02 09:51:54 +02:00
|
|
|
process = Exec::shell(new_arg_string);
|
2019-05-26 00:23:35 +02:00
|
|
|
}
|
2019-08-07 19:49:11 +02:00
|
|
|
process = process.cwd(context.shell_manager.path());
|
2019-05-24 09:29:16 +02:00
|
|
|
|
2019-05-24 20:48:33 +02:00
|
|
|
let mut process = match stream_next {
|
|
|
|
StreamNext::Last => process,
|
|
|
|
StreamNext::External | StreamNext::Internal => {
|
|
|
|
process.stdout(subprocess::Redirection::Pipe)
|
|
|
|
}
|
|
|
|
};
|
2019-05-24 09:29:16 +02:00
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
if let Some(stdin) = stdin {
|
2019-05-24 09:29:16 +02:00
|
|
|
process = process.stdin(stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut popen = process.popen().unwrap();
|
|
|
|
|
2019-05-24 20:48:33 +02:00
|
|
|
match stream_next {
|
|
|
|
StreamNext::Last => {
|
|
|
|
popen.wait()?;
|
|
|
|
Ok(ClassifiedInputStream::new())
|
|
|
|
}
|
|
|
|
StreamNext::External => {
|
|
|
|
let stdout = popen.stdout.take().unwrap();
|
|
|
|
Ok(ClassifiedInputStream::from_stdout(stdout))
|
|
|
|
}
|
|
|
|
StreamNext::Internal => {
|
|
|
|
let stdout = popen.stdout.take().unwrap();
|
|
|
|
let file = futures::io::AllowStdIo::new(stdout);
|
|
|
|
let stream = Framed::new(file, LinesCodec {});
|
2019-08-05 10:54:29 +02:00
|
|
|
let stream = stream.map(move |line| {
|
|
|
|
Tagged::from_simple_spanned_item(Value::string(line.unwrap()), name_span)
|
|
|
|
});
|
2019-07-03 22:31:15 +02:00
|
|
|
Ok(ClassifiedInputStream::from_input_stream(
|
2019-08-01 03:58:42 +02:00
|
|
|
stream.boxed() as BoxStream<'static, Tagged<Value>>
|
2019-07-03 22:31:15 +02:00
|
|
|
))
|
2019-05-24 09:29:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|