aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs3
-rw-r--r--src/run/mod.rs47
2 files changed, 35 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs
index efcbd3c..925c62c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::io::{self, IsTerminal, Read, Write};
@@ -62,6 +63,7 @@ pub struct Session {
history: Vec<BString>,
dispatch: CommandDispatch,
prev_path: BString,
+ vars: HashMap<BString, BString>,
/// n before end of history.len()
/// 0 == not checking history
@@ -202,6 +204,7 @@ fn event_loop() {
dispatch: CommandDispatch::new(),
prev_path: vec![b'.'],
history_visit: 0,
+ vars: HashMap::new(),
};
print!("{}", se.prompt());
diff --git a/src/run/mod.rs b/src/run/mod.rs
index 18d3f0a..db0ed05 100644
--- a/src/run/mod.rs
+++ b/src/run/mod.rs
@@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
-use crate::parse::{Ast, PreExpansion};
+use crate::parse::{Ast, PostExpansion, PreExpansion};
use crate::*;
mod builtin;
@@ -44,17 +44,11 @@ impl std::io::Write for ArcWriter {
}
impl Executor {
- fn execute(
+ fn execute_pipeline(
&mut self,
- ast: Ast<parse::PostExpansion>,
+ pipes: parse::Pipes<parse::PostExpansion>,
capture: Option<&mut Vec<u8>>,
) -> Result<(), ExecError> {
- let Ast::Pipes(pipes) = ast else {
- todo!("can only handle pipes");
- };
-
- //print!("exec {}", format!("{pipes:?}\n").replace("\n", "\r\n"));
-
let mut children = Vec::new();
let mut threads = Vec::new();
let mut prev_reader = None;
@@ -170,12 +164,10 @@ impl Executor {
for jh in threads {
match jh.join() {
Ok(Ok(())) => (),
- Ok(Err(e)) => {
- match e {
- BuiltinError::IO(_) => code = -1,
- BuiltinError::Exit(c) => code = c,
- }
- }
+ Ok(Err(e)) => match e {
+ BuiltinError::IO(_) => code = -1,
+ BuiltinError::Exit(c) => code = c,
+ },
Err(_) => code = 127,
}
}
@@ -209,12 +201,37 @@ impl Executor {
}
}
}
+
+ fn execute_var_assign(&mut self, va: parse::VarAssign<PostExpansion>) -> Result<(), ExecError> {
+ self.se.lock().unwrap().vars.insert(va.var, va.val);
+ Ok(())
+ }
+
+ fn execute_fun_decl(&mut self, fd: parse::FunDecl<PostExpansion>) -> Result<(), ExecError> {
+ todo!()
+ }
+
+ fn execute(
+ &mut self,
+ ast: Ast<parse::PostExpansion>,
+ capture: Option<&mut Vec<u8>>,
+ ) -> Result<(), ExecError> {
+ match ast {
+ Ast::FunDecl(fd) => self.execute_fun_decl(fd),
+ Ast::VarAssign(va) => self.execute_var_assign(va),
+ Ast::Pipes(pipes) => self.execute_pipeline(pipes, capture),
+ }
+ }
}
impl parse::Expander for Executor {
type Error = ExecError;
fn expand_var(&mut self, var: BString) -> Result<BString, Self::Error> {
+ if let Some(val) = self.se.lock().unwrap().vars.get(&var) {
+ return Ok(val.clone())
+ }
+
match std::env::var_os(OsStr::from_bytes(&var)) {
Some(val) => Ok(val.as_bytes().to_vec()),
None => Err(ExecError::UnknownVariable(var)),