Skip to content

Commit

Permalink
do not expand some expansions in alias define; misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitnk committed May 25, 2019
1 parent 4361f2a commit 067ad68
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ glob = "0.2.0"
lazy_static = "1.3.0"
libc = "0.2.0"
linefeed = "0.6.0"
nix = "0.13.0"
nix = "0.14.0"
pest = "2.0"
pest_derive = "2.0"
regex = "1.0.0"
regex = "1"
yaml-rust = "0.4.0"

[dependencies.rusqlite]
Expand Down
11 changes: 1 addition & 10 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,12 @@ pub fn run_procs(sh: &mut shell::Shell,
fn drain_env_tokens(tokens: &mut Tokens) -> HashMap<String, String> {
let mut envs: HashMap<String, String> = HashMap::new();
let mut n = 0;
let re = Regex::new(r"^([a-zA-Z0-9_]+)=(.*)$").unwrap();
for (sep, text) in tokens.iter() {
if !sep.is_empty() || !libs::re::re_contains(text, r"^([a-zA-Z0-9_]+)=(.*)$") {
break;
}

let re;
match Regex::new(r"^([a-zA-Z0-9_]+)=(.*)$") {
Ok(x) => {
re = x;
}
Err(e) => {
println_stderr!("Regex new: {:?}", e);
return envs;
}
}
for cap in re.captures_iter(text) {
let name = cap[1].to_string();
let value = parsers::parser_line::unquote(&cap[2]);
Expand Down
19 changes: 18 additions & 1 deletion src/parsers/parser_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ pub fn tokens_to_args(tokens: &Tokens) -> Vec<String> {
pub fn tokens_to_line(tokens: &Tokens) -> String {
let mut result = String::new();
for t in tokens {
result.push_str(format!("{}{}{} ", t.0, t.1, t.0).as_str());
if t.0.is_empty() {
result.push_str(&t.1);
} else {
let s = tools::wrap_sep_string(&t.0, &t.1);
result.push_str(&s);
}
result.push(' ');
}
if result.ends_with(' ') {
let len = result.len();
Expand Down Expand Up @@ -554,6 +560,7 @@ mod tests {
use super::line_to_cmds;
use super::line_to_plain_tokens;
use super::Tokens;
use super::tokens_to_line;

fn _assert_vec_tuple_eq(a: Tokens, b: Vec<(&str, &str)>) {
assert_eq!(a.len(), b.len());
Expand Down Expand Up @@ -1001,4 +1008,14 @@ mod tests {
assert_eq!(crs.tokens, tokens_expect);
assert_eq!(crs.redirects, redirects_expect);
}

#[test]
fn test_tokens_to_line() {
let tokens = vec![
("".to_string(), "echo".to_string()),
("\"".to_string(), "a\"b".to_string()),
];
let line_exp = "echo \"a\\\"b\"";
assert_eq!(tokens_to_line(&tokens), line_exp);
}
}
12 changes: 5 additions & 7 deletions src/scripting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,7 @@ fn is_args_in_token(token: &str) -> bool {
}

fn expand_args_for_single_token(token: &str, args: &[String]) -> String {
let re;
if let Ok(x) = Regex::new(r"^(.*?)\$\{?([0-9]+|@)\}?(.*)$") {
re = x;
} else {
println_stderr!("cicada: re new error");
return String::new();
}
let re = Regex::new(r"^(.*?)\$\{?([0-9]+|@)\}?(.*)$").unwrap();
if !re.is_match(token) {
return token.to_string();
}
Expand Down Expand Up @@ -454,6 +448,10 @@ mod tests {
let line_new = expand_args(line, &args);
assert_eq!(line_new, "echo foo bar baz");

let line = "echo \"a\\\"b\"";
let line_new = expand_args(line, &args);
assert_eq!(line_new, "echo \"a\\\"b\"");

let line = "echo \"$@\"";
let line_new = expand_args(line, &args);
assert_eq!(line_new, "echo \"foo bar baz\"");
Expand Down
20 changes: 17 additions & 3 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,12 @@ fn expand_home(tokens: &mut types::Tokens) {
}

fn env_in_token(token: &str) -> bool {
if libs::re::re_contains(token, r"\$\{?[a-zA-Z][a-zA-Z0-9_]*\}?") {
return !libs::re::re_contains(token, r"='.*\$\{?[a-zA-Z][a-zA-Z0-9_]*\}?.*'$");
}

libs::re::re_contains(token, r"\$\{?\$\}?") ||
libs::re::re_contains(token, r"\$\{?\?\}?") ||
libs::re::re_contains(token, r"\$\{?[a-zA-Z][a-zA-Z0-9_]*\}?")
libs::re::re_contains(token, r"\$\{?\?\}?")
}

pub fn expand_env(sh: &Shell, tokens: &mut types::Tokens) {
Expand All @@ -715,7 +718,8 @@ pub fn expand_env(sh: &Shell, tokens: &mut types::Tokens) {
}

fn should_do_dollar_command_extension(line: &str) -> bool {
libs::re::re_contains(line, r"\$\([^\)]+\)")
libs::re::re_contains(line, r"\$\([^\)]+\)") &&
!libs::re::re_contains(line, r"='.*\$\([^\)]+\).*'$")
}

fn do_command_substitution_for_dollar(sh: &mut Shell, tokens: &mut types::Tokens) {
Expand Down Expand Up @@ -912,6 +916,7 @@ mod tests {
fn test_should_do_dollar_command_extension() {
assert!(!should_do_dollar_command_extension("ls $HOME"));
assert!(!should_do_dollar_command_extension("echo $[pwd]"));
assert!(!should_do_dollar_command_extension("='pwd is $(pwd).'"));
assert!(should_do_dollar_command_extension("echo $(pwd)"));
assert!(should_do_dollar_command_extension("echo $(pwd) foo"));
assert!(should_do_dollar_command_extension("echo $(foo bar)"));
Expand All @@ -937,6 +942,15 @@ mod tests {
expand_env(&sh, &mut tokens);
assert_eq!(tokens, exp_tokens);

let mut tokens = make_tokens(&vec![
("", "alias"), ("", "foo=\'echo $PWD\'")
]);
let exp_tokens = vec![
("", "alias"), ("", "foo=\'echo $PWD\'")
];
expand_env(&sh, &mut tokens);
assert_vec_eq(tokens, exp_tokens);

let mut tokens = vec![
("".to_string(), "echo".to_string()),
("\"".to_string(), "$test_foo_expand_env1".to_string()),
Expand Down
7 changes: 7 additions & 0 deletions tests/scripts/plain-003.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# regression tests
echo "a\"b"

echo 'cicada, is not a "cicada", but a "unix shell".' \
| awk -F "[ ,.\"]+" '{for(i=1;i<=NF;i++)A[$i]++}END{for(k in A)print k, A[k]}' \
| sort -k2nr \
| head -n5
6 changes: 6 additions & 0 deletions tests/scripts/plain-003.sh.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a"b
a 2
cicada 2
but 1
is 1
not 1

0 comments on commit 067ad68

Please sign in to comment.