implement load-to-string, revert odd Cargo.toml mistake

This commit is contained in:
Ava Apples Affine 2023-03-30 20:47:22 -07:00
parent dcbc855783
commit d11545bc31
Signed by: affine
GPG key ID: 3A4645B8CF806069
3 changed files with 69 additions and 39 deletions

View file

@ -165,6 +165,48 @@ fn load_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) -> Resu
}
}
const LOAD_TO_STRING_DOCSTRING: &str = "Calls a binary off disk with given arguments.
Arguments may be of any type except lambda. If a symbol is not defined it will be passed as a string.
first argument (command name) will be found on path (or an error returned).
examples:
(l ping google.com)
(let ((ping-count 4))
(l ping -c ping-count google.com))
(l emacs -nw (concat HOME '/.relishrc'))
Unlike with the normal load function, the load to string function collects stdout output and returns it as a string.
";
fn load_to_string_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) -> Result<Ctr, String> {
if ast.is_empty() {
Err("need at least one argument".to_string())
} else {
let mut args = VecDeque::from(args_from_ast(ast, syms));
if args.is_empty() {
Err("empty command".to_string())
} else {
if let Some(filepath) = run::find_on_path(args.pop_front().unwrap()) {
let res = Command::new(filepath).args(args).output();
if let Ok(output) = res {
if let Some(code) = output.status.code() {
state.last_exit_code = code;
}
if let Ok(string) = String::from_utf8(output.stdout) {
Ok(Ctr::String(string))
} else {
Err(format!("could'nt marshall utf-8 command output into a string"))
}
} else {
Err(format!("{}", res.err().unwrap()))
}
} else {
Err("file not found".to_string())
}
}
}
}
// TODO: rework this callback to reduce spaghett
const LOAD_WITH_DOCSTRING: &str = "";
fn load_with_callback(ast: &Seg, syms: &mut SymTable, state: &mut ShellState) -> Result<Ctr, String> {
@ -346,6 +388,7 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
let bg_ss = shell_state.clone();
let q_ss = shell_state.clone();
let lw_ss = shell_state.clone();
let lts_ss = shell_state.clone();
syms.insert(
String::from("l"),
@ -417,6 +460,19 @@ pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc<RefCell<ShellState>
},
);
syms.insert(
String::from("load-to-string"),
Symbol {
name: String::from("load-to-string"),
args: Args::Infinite,
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())
})),
..Default::default()
},
);
if let Err(e) = ctrlc::set_handler(move || println!("POSIX layer caught SIG-something")) {
eprintln!("WARNING: couldn't set sig handler: {}", e);