can spawn processes w00t

parse-tree
Jordan Orelli 2 years ago
parent bc536132ad
commit fdbc638628

@ -142,12 +142,66 @@ fn newline(stdout: HANDLE) -> Result<()> {
} }
fn eval(line: String) -> Result<()> { fn eval(line: String) -> Result<()> {
if line == "pwd" { let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() == 0 {
return Ok(());
}
let cmd = parts[0];
match cmd {
"pwd" => {
let pb = std::env::current_dir()?; let pb = std::env::current_dir()?;
println!("{}", pb.as_path().as_os_str().to_str().unwrap()); println!("{}", pb.as_path().as_os_str().to_str().unwrap());
return Ok(()); return Ok(());
} }
println!("{}", line); "printenv" => {
if parts.len() > 1 {
let name = parts[1];
match std::env::var(name) {
Ok(val) => println!("{}", val),
Err(e) => println!("ERROR {}", e),
}
} else {
println!("which variable you fucking dork");
}
return Ok(());
}
"which" => {
if parts.len() > 1 {
let path = std::env::var("path").unwrap();
let dirs: Vec<&str> = path.split(";").collect();
for d in dirs {
let dir = std::path::Path::new(d);
let fname = dir.join(parts[1]).with_extension("exe");
if fname.exists() && fname.is_file() {
println!("{}", fname.to_str().unwrap());
return Ok(());
}
}
println!("not found: {}", parts[1]);
return Ok(());
}
}
"echo" => {
let rest: Vec<&str> = parts[1..].to_vec();
println!("{:?}", rest);
}
_ => {
let mut proc = std::process::Command::new(cmd);
match proc.spawn() {
Ok(mut child) => {
if let Err(e) = child.wait() {
println!("error: {}", e)
}
}
Err(e) => {
println!("error: {}", e)
}
}
return Ok(());
}
}
// println!("Error: no such command: {}", cmd);
Ok(()) Ok(())
} }

Loading…
Cancel
Save