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 miette::Result;
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::{EngineState, StateDelta, StateWorkingSet},
@ -22,19 +22,7 @@ pub fn evaluate_commands(
let (block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state);
let (input, _) = if commands.item.starts_with('\'')
|| 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, &[]);
let (output, err) = parse(&mut working_set, None, commands.item.as_bytes(), false, &[]);
if let Some(err) = err {
report_error(&working_set, &err);

View file

@ -21,6 +21,21 @@ macro_rules! nu {
pub use std::process::{Command, Stdio};
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!(
// "
// {}
@ -58,7 +73,7 @@ macro_rules! nu {
// .arg("--no-history")
// .arg("--config-file")
// .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())
// .stdin(Stdio::piped())
.stderr(Stdio::piped())

View file

@ -86,11 +86,7 @@ fn main() -> Result<()> {
} else if arg.starts_with('-') {
// Cool, it's a flag
let flag_value = match arg.as_ref() {
"--commands" | "-c" => {
// 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))
}
"--commands" | "-c" => 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(),
_ => None,
@ -329,23 +325,27 @@ fn parse_commandline_args(
fn extract_contents(
expression: Option<Expression>,
engine_state: &mut EngineState,
) -> Option<Spanned<String>> {
expression.map(|expr| {
let contents = engine_state.get_span_contents(&expr.span);
Spanned {
item: String::from_utf8_lossy(contents).to_string(),
span: expr.span,
) -> Result<Option<Spanned<String>>, ShellError> {
if let Some(expr) = expression {
let str = expr.as_string();
if let Some(str) = str {
Ok(Some(Spanned {
item: str,
span: expr.span,
}))
} else {
Err(ShellError::TypeMismatch("string".into(), expr.span))
}
})
} else {
Ok(None)
}
}
let commands = extract_contents(commands, engine_state);
let testbin = extract_contents(testbin, engine_state);
let config_file = extract_contents(config_file, engine_state);
let env_file = extract_contents(env_file, engine_state);
let log_level = extract_contents(log_level, engine_state);
let commands = extract_contents(commands)?;
let testbin = extract_contents(testbin)?;
let config_file = extract_contents(config_file)?;
let env_file = extract_contents(env_file)?;
let log_level = extract_contents(log_level)?;
let help = call.has_flag("help");