2022-02-11 19:38:10 +01:00
|
|
|
use nu_protocol::ast::Expr;
|
|
|
|
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
|
|
|
use nu_protocol::{ast::Call, engine::Command, ShellError, Signature};
|
2022-02-21 23:22:21 +01:00
|
|
|
use nu_protocol::{PipelineData, Spanned};
|
2022-02-11 19:38:10 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct KnownExternal {
|
|
|
|
pub name: String,
|
|
|
|
pub signature: Box<Signature>,
|
|
|
|
pub usage: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Command for KnownExternal {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
*self.signature.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
&self.usage
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_known_external(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
// FIXME: This is a bit of a hack, and it'd be nice for the parser/AST to be able to handle the original
|
|
|
|
// order of the parameters. Until then, we need to recover the original order.
|
2022-03-09 19:01:23 +01:00
|
|
|
|
|
|
|
// FIXME: This is going to be a bit expensive, but we need to do it to ensure any new block/subexpression
|
|
|
|
// we find when parsing the external call is handled properly.
|
|
|
|
let mut engine_state = engine_state.clone();
|
|
|
|
|
2022-02-11 19:38:10 +01:00
|
|
|
let call_span = call.span();
|
|
|
|
let contents = engine_state.get_span_contents(&call_span);
|
|
|
|
|
2022-02-25 20:51:31 +01:00
|
|
|
let redirect_stdout = call.redirect_stdout;
|
|
|
|
let redirect_stderr = call.redirect_stderr;
|
|
|
|
|
2022-02-11 19:38:10 +01:00
|
|
|
let (lexed, _) = crate::lex(contents, call_span.start, &[], &[], true);
|
|
|
|
|
|
|
|
let spans: Vec<_> = lexed.into_iter().map(|x| x.span).collect();
|
2022-03-09 19:01:23 +01:00
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2022-03-18 20:03:57 +01:00
|
|
|
let (external_call, _) = crate::parse_external_call(&mut working_set, &spans, &[]);
|
2022-03-09 19:01:23 +01:00
|
|
|
let delta = working_set.render();
|
|
|
|
engine_state.merge_delta(delta, None, ".")?;
|
2022-02-11 19:38:10 +01:00
|
|
|
|
|
|
|
match external_call.expr {
|
|
|
|
Expr::ExternalCall(head, args) => {
|
|
|
|
let decl_id = engine_state
|
2022-02-17 15:55:17 +01:00
|
|
|
.find_decl("run-external".as_bytes())
|
2022-02-11 19:38:10 +01:00
|
|
|
.ok_or(ShellError::ExternalNotSupported(head.span))?;
|
|
|
|
|
|
|
|
let command = engine_state.get_decl(decl_id);
|
|
|
|
|
|
|
|
let mut call = Call::new(head.span);
|
|
|
|
|
|
|
|
call.positional.push(*head);
|
|
|
|
|
|
|
|
for arg in args {
|
|
|
|
call.positional.push(arg.clone())
|
|
|
|
}
|
|
|
|
|
2022-02-25 20:51:31 +01:00
|
|
|
if redirect_stdout {
|
2022-02-21 23:22:21 +01:00
|
|
|
call.named.push((
|
|
|
|
Spanned {
|
|
|
|
item: "redirect-stdout".into(),
|
|
|
|
span: call_span,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-02-25 20:51:31 +01:00
|
|
|
if redirect_stderr {
|
2022-02-21 23:22:21 +01:00
|
|
|
call.named.push((
|
|
|
|
Spanned {
|
|
|
|
item: "redirect-stderr".into(),
|
|
|
|
span: call_span,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
}
|
2022-02-11 19:38:10 +01:00
|
|
|
|
2022-03-09 19:01:23 +01:00
|
|
|
command.run(&engine_state, stack, &call, input)
|
2022-02-11 19:38:10 +01:00
|
|
|
}
|
|
|
|
x => {
|
|
|
|
println!("{:?}", x);
|
|
|
|
panic!("internal error: known external not actually external")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|