load-to-string pulls out newline, also fix issue with nested calls

This commit is contained in:
Ava Apples Affine 2023-04-19 18:24:15 -07:00
parent ee1ef9700d
commit d7864ee628
Signed by: affine
GPG key ID: 3A4645B8CF806069
3 changed files with 27 additions and 21 deletions

View file

@ -28,7 +28,10 @@ use {
std::{
collections::VecDeque,
cell::RefCell,
cell::{
RefCell, RefMut,
BorrowMutError,
},
rc::Rc,
fs::{File, OpenOptions},
path::Path,
@ -292,9 +295,9 @@ fn load_to_string_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellStat
state.last_exit_code = code;
}
if let Ok(string) = String::from_utf8(output.stdout) {
Ok(Ctr::String(string))
Ok(Ctr::String(string.trim_end().to_string()))
} else {
Err(format!("could'nt marshall utf-8 command output into a string"))
Err(format!("couldn't marshall utf-8 command output into a string"))
}
} else {
Err(format!("{}", res.err().unwrap()))
@ -558,7 +561,7 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
conditional_branches: true,
docs: String::from(BG_DOCSTRING),
value: ValueType::Internal(Rc::new(move |ast: &Seg, symtable: &mut SymTable| -> Result<Ctr, String> {
bg_callback(ast, symtable, &mut bg_ss.clone().borrow_mut())
bg_callback(ast, symtable, &mut bg_ss.borrow_mut())
})),
..Default::default()
},
@ -572,7 +575,7 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
conditional_branches: false,
docs: String::from(Q_DOCSTRING),
value: ValueType::Internal(Rc::new(move |ast: &Seg, symtable: &mut SymTable| -> Result<Ctr, String> {
q_callback(ast, symtable, &mut q_ss.clone().borrow_mut())
q_callback(ast, symtable, &mut q_ss.borrow_mut())
})),
..Default::default()
},
@ -586,7 +589,7 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
conditional_branches: true,
docs: String::from(LOAD_WITH_DOCSTRING),
value: ValueType::Internal(Rc::new(move |ast: &Seg, symtable: &mut SymTable| -> Result<Ctr, String> {
load_with_callback(ast, symtable, &mut lw_ss.clone().borrow_mut())
load_with_callback(ast, symtable, &mut lw_ss.borrow_mut())
})),
..Default::default()
},
@ -600,7 +603,22 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
conditional_branches: true,
docs: String::from(LOAD_TO_STRING_DOCSTRING),
value: ValueType::Internal(Rc::new(move |ast: &Seg, symtable: &mut SymTable| -> Result<Ctr, String> {
load_to_string_callback(ast, symtable, &mut lts_ss.clone().borrow_mut())
// extra nonsense needed to allow nested calls
load_to_string_callback(
ast,
symtable,
&mut lts_ss
.try_borrow_mut()
.or(Ok::<RefMut<'_, ShellState>, BorrowMutError>(
RefCell::from(ShellState{
parent_pid: pid,
parent_pgid: pgid,
children: vec![],
last_exit_code: 0,
}).borrow_mut()
))
.unwrap()
)
})),
..Default::default()
},
@ -614,7 +632,7 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
conditional_branches: true,
docs: String::from(PIPE_DOCSTRING),
value: ValueType::Internal(Rc::new(move |ast: &Seg, symtable: &mut SymTable| -> Result<Ctr, String> {
pipe_callback(ast, symtable, &mut p_ss.clone().borrow_mut())
pipe_callback(ast, symtable, &mut p_ss.borrow_mut())
})),
..Default::default()
},