More escaping/unescaping fixes (#5403)

This commit is contained in:
JT 2022-05-02 09:49:31 +12:00 committed by GitHub
parent 07255e576d
commit 96f8691c8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 34 deletions

View File

@ -2,7 +2,7 @@ use crate::util::report_error;
use log::info; use log::info;
use miette::Result; use miette::Result;
use nu_engine::{convert_env_values, eval_block}; use nu_engine::{convert_env_values, eval_block};
use nu_parser::{parse, trim_quotes}; use nu_parser::parse;
use nu_protocol::engine::Stack; use nu_protocol::engine::Stack;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, StateDelta, StateWorkingSet}, engine::{EngineState, StateDelta, StateWorkingSet},
@ -22,19 +22,7 @@ pub fn evaluate_commands(
let (block, delta) = { let (block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
let (input, _) = if commands.item.starts_with('\'') let (output, err) = parse(&mut working_set, None, commands.item.as_bytes(), false, &[]);
|| commands.item.starts_with('"')
|| commands.item.starts_with('`')
{
(
trim_quotes(commands.item.as_bytes()),
commands.span.start + 1,
)
} else {
(commands.item.as_bytes(), commands.span.start)
};
let (output, err) = parse(&mut working_set, None, input, false, &[]);
if let Some(err) = err { if let Some(err) = err {
report_error(&working_set, &err); report_error(&working_set, &err);

View File

@ -21,6 +21,21 @@ macro_rules! nu {
pub use std::process::{Command, Stdio}; pub use std::process::{Command, Stdio};
pub use $crate::NATIVE_PATH_ENV_VAR; pub use $crate::NATIVE_PATH_ENV_VAR;
pub fn escape_quote_string(input: String) -> String {
let mut output = String::with_capacity(input.len() + 2);
output.push('"');
for c in input.chars() {
if c == '"' || c == '\\' {
output.push('\\');
}
output.push(c);
}
output.push('"');
output
}
// let commands = &*format!( // let commands = &*format!(
// " // "
// {} // {}
@ -58,7 +73,7 @@ macro_rules! nu {
// .arg("--no-history") // .arg("--no-history")
// .arg("--config-file") // .arg("--config-file")
// .arg($crate::fs::DisplayPath::display_path(&$crate::fs::fixtures().join("playground/config/default.toml"))) // .arg($crate::fs::DisplayPath::display_path(&$crate::fs::fixtures().join("playground/config/default.toml")))
.arg(format!("-c '{}'", $crate::fs::DisplayPath::display_path(&path))) .arg(format!("-c {}", escape_quote_string($crate::fs::DisplayPath::display_path(&path))))
.stdout(Stdio::piped()) .stdout(Stdio::piped())
// .stdin(Stdio::piped()) // .stdin(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())

View File

@ -86,11 +86,7 @@ fn main() -> Result<()> {
} else if arg.starts_with('-') { } else if arg.starts_with('-') {
// Cool, it's a flag // Cool, it's a flag
let flag_value = match arg.as_ref() { let flag_value = match arg.as_ref() {
"--commands" | "-c" => { "--commands" | "-c" => args.next().map(|a| escape_quote_string(&a)),
// FIXME: Use proper quoting. `escape_quote_string()` can't be used for now due to https://github.com/nushell/nushell/issues/5383.
args.next().map(|a| format!("`{}`", a))
}
"--config" | "--env-config" => args.next().map(|a| escape_quote_string(&a)), "--config" | "--env-config" => args.next().map(|a| escape_quote_string(&a)),
"--log-level" | "--testbin" | "--threads" | "-t" => args.next(), "--log-level" | "--testbin" | "--threads" | "-t" => args.next(),
_ => None, _ => None,
@ -329,23 +325,27 @@ fn parse_commandline_args(
fn extract_contents( fn extract_contents(
expression: Option<Expression>, expression: Option<Expression>,
engine_state: &mut EngineState, ) -> Result<Option<Spanned<String>>, ShellError> {
) -> Option<Spanned<String>> { if let Some(expr) = expression {
expression.map(|expr| { let str = expr.as_string();
let contents = engine_state.get_span_contents(&expr.span); if let Some(str) = str {
Ok(Some(Spanned {
Spanned { item: str,
item: String::from_utf8_lossy(contents).to_string(), span: expr.span,
span: expr.span, }))
} else {
Err(ShellError::TypeMismatch("string".into(), expr.span))
} }
}) } else {
Ok(None)
}
} }
let commands = extract_contents(commands, engine_state); let commands = extract_contents(commands)?;
let testbin = extract_contents(testbin, engine_state); let testbin = extract_contents(testbin)?;
let config_file = extract_contents(config_file, engine_state); let config_file = extract_contents(config_file)?;
let env_file = extract_contents(env_file, engine_state); let env_file = extract_contents(env_file)?;
let log_level = extract_contents(log_level, engine_state); let log_level = extract_contents(log_level)?;
let help = call.has_flag("help"); let help = call.has_flag("help");