mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
dircolors: escape "'" and ":"
This commit is contained in:
parent
029e0dc613
commit
46b4b94c42
2 changed files with 46 additions and 0 deletions
|
@ -344,6 +344,8 @@ where
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let line = escape(line);
|
||||||
|
|
||||||
let (key, val) = line.split_two();
|
let (key, val) = line.split_two();
|
||||||
if val.is_empty() {
|
if val.is_empty() {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
|
@ -411,3 +413,37 @@ where
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Escape single quotes because they are not allowed between single quotes in shell code, and code
|
||||||
|
/// enclosed by single quotes is what is returned by `parse()`.
|
||||||
|
///
|
||||||
|
/// We also escape ":" to make the "quote" test pass in the GNU test suite:
|
||||||
|
/// <https://github.com/coreutils/coreutils/blob/master/tests/misc/dircolors.pl>
|
||||||
|
fn escape(s: &str) -> String {
|
||||||
|
let mut result = String::new();
|
||||||
|
let mut previous = ' ';
|
||||||
|
|
||||||
|
for c in s.chars() {
|
||||||
|
match c {
|
||||||
|
'\'' => result.push_str("'\\''"),
|
||||||
|
':' if previous != '\\' => result.push_str("\\:"),
|
||||||
|
_ => result.push_str(&c.to_string()),
|
||||||
|
}
|
||||||
|
previous = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::escape;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_escape() {
|
||||||
|
assert_eq!("", escape(""));
|
||||||
|
assert_eq!("'\\''", escape("'"));
|
||||||
|
assert_eq!("\\:", escape(":"));
|
||||||
|
assert_eq!("\\:", escape("\\:"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -141,6 +141,16 @@ fn test_stdin() {
|
||||||
.no_stderr();
|
.no_stderr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_quoting() {
|
||||||
|
new_ucmd!()
|
||||||
|
.pipe_in("exec 'echo Hello;:'\n")
|
||||||
|
.args(&["-b", "-"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is("LS_COLORS='ex='\\''echo Hello;\\:'\\'':';\nexport LS_COLORS\n")
|
||||||
|
.no_stderr();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extra_operand() {
|
fn test_extra_operand() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue