Skip to content

Commit f142334

Browse files
committed
Fix build and parser for quoted shell args
1 parent 11db222 commit f142334

3 files changed

Lines changed: 25 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
mod parser;
22

3-
use std::fs::OpenOptions;
43
use std::io::{self, Write};
5-
use std::process::{Child, Command, ExitStatus, Stdio};
4+
use std::process::{Command, Stdio};
65

76
fn main() {
87
println!("Welcome to TruShell Native Engine");
@@ -37,10 +36,11 @@ fn main() {
3736

3837
let parts = split_posix_words(trimmed_input);
3938
if parts.first().map(String::as_str) == Some("cd") {
40-
let new_dir = parts.get(1).map(String::as_str).unwrap_or_else(|| {
41-
std::env::var("HOME").as_deref().unwrap_or(".")
42-
});
43-
if let Err(e) = std::env::set_current_dir(new_dir) {
39+
let new_dir = parts
40+
.get(1)
41+
.cloned()
42+
.unwrap_or_else(|| std::env::var("HOME").unwrap_or_else(|_| ".".to_string()));
43+
if let Err(e) = std::env::set_current_dir(new_dir.as_str()) {
4444
eprintln!("trushell: cd: {}: {}", new_dir, e);
4545
}
4646
continue;
@@ -153,6 +153,9 @@ fn split_posix_words(input: &str) -> Vec<String> {
153153
}
154154
_ => current.push(ch),
155155
},
156+
Some(other) => {
157+
current.push(other);
158+
}
156159
}
157160
}
158161

src/parser.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ impl<'a> Lexer<'a> {
178178
return Err(ParseError::new("Unexpected '&' without redirect"));
179179
}
180180
}
181-
'"' => {
182-
tokens.push(self.lex_string()?);
181+
'"' | '\'' => {
182+
tokens.push(self.lex_string(ch)?);
183183
}
184184
'=' => {
185185
self.chars.next();
@@ -397,13 +397,20 @@ impl<'a> Lexer<'a> {
397397
Ok(Token::Word(value))
398398
}
399399

400-
fn lex_string(&mut self) -> Result<Token> {
400+
fn lex_string(&mut self, quote: char) -> Result<Token> {
401401
self.chars.next();
402402
let mut value = String::new();
403403

404404
while let Some(ch) = self.chars.next() {
405405
match ch {
406-
'"' => return Ok(Token::StringLiteral(value)),
406+
ch if ch == quote => return Ok(Token::StringLiteral(value)),
407+
'\\' if quote == '"' => {
408+
if let Some(next) = self.chars.next() {
409+
value.push(next);
410+
} else {
411+
value.push('\\');
412+
}
413+
}
407414
other => value.push(other),
408415
}
409416
}

0 commit comments

Comments
 (0)