Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add escape sequences to shell #558

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,36 @@ fn glob(arg: &str) -> Vec<String> {
matches
}

pub fn parse_str(s: &str) -> String {
let mut res = String::new();
let mut is_escaped = false;
for c in s.chars() {
match c {
'\\' if !is_escaped => {
is_escaped = true;
continue;
}
_ if !is_escaped => res.push(c),
'\\' => res.push(c),
'"' => res.push(c),
'n' => res.push('\n'),
'r' => res.push('\r'),
't' => res.push('\t'),
'b' => res.push('\x08'),
'e' => res.push('\x1B'),
_ => {},
}
is_escaped = false;
}
res
}

pub fn split_args(cmd: &str) -> Vec<String> {
let mut args = Vec::new();
let mut i = 0;
let mut n = cmd.len();
let mut is_quote = false;
let mut is_escaped = false;

for (j, c) in cmd.char_indices() {
if c == '#' && !is_quote {
Expand All @@ -188,13 +213,18 @@ pub fn split_args(cmd: &str) -> Vec<String> {
}
}
i = j + 1;
} else if c == '"' {
} else if c == '"' && !is_escaped {
is_quote = !is_quote;
if !is_quote {
args.push(cmd[i..j].to_string());
args.push(parse_str(&cmd[i..j]));
}
i = j + 1;
}
if c == '\\' && !is_escaped {
is_escaped = true;
} else {
is_escaped = false;
}
}

if i < n {
Expand Down
Loading