aboutsummaryrefslogtreecommitdiffstats
path: root/src/export_fun.rs
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-03-09 20:06:43 +0100
committerJonas Maier <jonas@x77.dev>2026-03-09 20:06:43 +0100
commit2f4558aa3df29d5cd1dfa72ff4278e1fe6fc1576 (patch)
treebe6f6aba211163c0e47c8be8d36437abd446fb59 /src/export_fun.rs
parentb1de2be8c23fbff9ec8f47fe4ac8ba9d197a823a (diff)
downloadpish-2f4558aa3df29d5cd1dfa72ff4278e1fe6fc1576.tar.gz
export functions :))
Diffstat (limited to 'src/export_fun.rs')
-rw-r--r--src/export_fun.rs36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/export_fun.rs b/src/export_fun.rs
index 64ac08b..9c2ab4a 100644
--- a/src/export_fun.rs
+++ b/src/export_fun.rs
@@ -1,14 +1,19 @@
//! allow sub-programs to invoke arbitrary user-defined functions via a unix socket
use crate::Session;
+use crate::run::Input;
+use crate::run::Output;
use crate::run::get_command_kind;
use std::env::current_exe;
use std::ffi::OsStr;
use std::fs;
+use std::fs::File;
use std::io;
use std::io::IoSliceMut;
use std::io::Read;
use std::io::Write;
+use std::os::fd::FromRawFd;
+use std::os::fd::OwnedFd;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::symlink;
use std::os::unix::net::AncillaryData;
@@ -21,8 +26,9 @@ use std::process::exit;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
+use std::time::Duration;
-fn handle_server(session: Arc<Mutex<Session>>, stream: UnixStream) -> io::Result<()> {
+fn handle_server(session: Arc<Mutex<Session>>, mut stream: UnixStream) -> io::Result<()> {
// TODO: figure out how to get a reasonable limit on CLI arg len
let mut buf = [0u8; 8192];
let mut iov = [IoSliceMut::new(&mut buf)];
@@ -64,18 +70,38 @@ fn handle_server(session: Arc<Mutex<Session>>, stream: UnixStream) -> io::Result
let se = session.lock().unwrap();
match get_command_kind(&se, cli_args[0].as_slice()) {
- crate::run::CommandKind::Fun(ast) => todo!(),
+ crate::run::CommandKind::Fun(_) => (),
crate::run::CommandKind::Path(_) | crate::run::CommandKind::Builtin(_) => {
return Ok(());
}
}
+ drop(se);
- todo!()
+ let stdin = File::from(unsafe { OwnedFd::from_raw_fd(fds[0]) });
+ let stdout = File::from(unsafe { OwnedFd::from_raw_fd(fds[1]) });
+
+ let res = crate::run::Executor::execute_fun(
+ session,
+ cli_args,
+ Input::File(stdin),
+ Output::File(stdout),
+ );
+
+ let exit_code = match res {
+ Ok(_) => 0,
+ Err(e) => match e {
+ crate::run::ExecError::UnknownVariable(_) => -3,
+ crate::run::ExecError::ExecError(x) => x,
+ },
+ };
+
+ let _ = stream.set_write_timeout(Some(Duration::from_secs(1)));
+ stream.write_all(&exit_code.to_le_bytes())?;
+
+ Ok(())
}
fn handle_client(mut stream: UnixStream) -> io::Result<()> {
- println!("uhh hi");
-
// give up all my file descriptors descriptors
let mut ancillary_buffer = [0; 128];
let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);