Fix quoting for command line args (#5384)

* Fix quoting for command line args

* Replace custom quoting with escape_quote_string

* Use raw string for now
This commit is contained in:
Tomoki Aonuma
2022-05-01 03:23:05 +09:00
committed by GitHub
parent 9da2e142b2
commit ae9c0fc138
5 changed files with 39 additions and 46 deletions

View File

@ -1,7 +1,7 @@
use crate::CliError;
use log::trace;
use nu_engine::eval_block;
use nu_parser::{lex, parse, unescape_unquote_string, Token, TokenContents};
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
use nu_protocol::engine::StateWorkingSet;
use nu_protocol::{
engine::{EngineState, Stack},
@ -36,20 +36,9 @@ fn gather_env_vars(vars: impl Iterator<Item = (String, String)>, engine_state: &
}
fn put_env_to_fake_file(name: &str, val: &str, fake_env_file: &mut String) {
fn push_string_literal(s: &str, fake_env_file: &mut String) {
fake_env_file.push('"');
for c in s.chars() {
if c == '\\' || c == '"' {
fake_env_file.push('\\');
}
fake_env_file.push(c);
}
fake_env_file.push('"');
}
push_string_literal(name, fake_env_file);
fake_env_file.push_str(&escape_quote_string(name));
fake_env_file.push('=');
push_string_literal(val, fake_env_file);
fake_env_file.push_str(&escape_quote_string(val));
fake_env_file.push('\n');
}

View File

@ -1,5 +1,6 @@
use core::fmt::Write;
use nu_engine::get_columns;
use nu_parser::escape_quote_string;
use nu_protocol::ast::{Call, RangeInclusion};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -137,15 +138,10 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
}
Ok(format!("{{{}}}", collection.join(", ")))
}
Value::String { val, .. } => Ok(format!("\"{}\"", escape(val))),
Value::String { val, .. } => Ok(escape_quote_string(val)),
}
}
fn escape(input: &str) -> String {
let output = input.replace('\\', "\\\\");
output.replace('"', "\\\"")
}
fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
let v = input.into_value(call.head);

View File

@ -0,0 +1,14 @@
pub fn escape_quote_string(input: &str) -> 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
}

View File

@ -1,3 +1,4 @@
mod deparse;
mod errors;
mod flatten;
mod known_external;
@ -7,6 +8,7 @@ mod parse_keywords;
mod parser;
mod type_check;
pub use deparse::escape_quote_string;
pub use errors::ParseError;
pub use flatten::{flatten_block, flatten_expression, flatten_pipeline, FlatShape};
pub use known_external::KnownExternal;