From ee1e4e38b2f53966bc3d4c0b459b331e0ce48009 Mon Sep 17 00:00:00 2001 From: Byson94 Date: Sun, 24 Aug 2025 12:48:31 +0530 Subject: [PATCH] feat: added std::command module to rhai --- CHANGELOG.md | 1 + crates/iirhai/src/providers/stdlib/command.rs | 27 +++++++++++++++++ crates/iirhai/src/providers/stdlib/mod.rs | 5 +++- docs/src/modules/stdlib.md | 29 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 crates/iirhai/src/providers/stdlib/command.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c60d77..2616ded 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). - Proper error handling for invalid external module code. - **call-fns** command for calling a Rhai function. Note: The function can only see poll/listen variables as their initial value. - **update** command with **--inject-vars** flag to update widget state. Note: All poll/listen variables will reset to their initial values. +- `std::command` module for running shell commands. ### Changed diff --git a/crates/iirhai/src/providers/stdlib/command.rs b/crates/iirhai/src/providers/stdlib/command.rs new file mode 100644 index 0000000..b3dd7e6 --- /dev/null +++ b/crates/iirhai/src/providers/stdlib/command.rs @@ -0,0 +1,27 @@ +use rhai::plugin::*; +use rhai::EvalAltResult; +use std::process::Command; + +#[export_module] +pub mod command { + #[rhai_fn(return_raw)] + pub fn run(cmd: &str) -> Result<(), Box> { + Command::new("sh") + .arg("-c") + .arg(cmd) + .status() + .map_err(|e| format!("Failed to run command: {}", e))?; + Ok(()) + } + + #[rhai_fn(return_raw)] + pub fn run_and_read(cmd: &str) -> Result> { + let output = Command::new("sh") + .arg("-c") + .arg(cmd) + .output() + .map_err(|e| format!("Failed to run command: {}", e))?; + + Ok(String::from_utf8_lossy(&output.stdout).to_string()) + } +} diff --git a/crates/iirhai/src/providers/stdlib/mod.rs b/crates/iirhai/src/providers/stdlib/mod.rs index 58c41ea..04de8fe 100644 --- a/crates/iirhai/src/providers/stdlib/mod.rs +++ b/crates/iirhai/src/providers/stdlib/mod.rs @@ -1,3 +1,4 @@ +pub mod command; pub mod env; pub mod json; pub mod math; @@ -9,7 +10,7 @@ use rhai::module_resolvers::StaticModuleResolver; pub fn register_stdlib(resolver: &mut StaticModuleResolver) { use crate::providers::stdlib::{ - env::env, json::json, math::math, monitor::monitor, text::text, + env::env, json::json, math::math, monitor::monitor, text::text, command::command }; // adding modules @@ -18,6 +19,7 @@ pub fn register_stdlib(resolver: &mut StaticModuleResolver) { let monitor_mod = exported_module!(monitor); let json_mod = exported_module!(json); let math_mod = exported_module!(math); + let command_mod = exported_module!(command); // inserting modules resolver.insert("std::text", text_mod); @@ -25,4 +27,5 @@ pub fn register_stdlib(resolver: &mut StaticModuleResolver) { resolver.insert("std::monitor", monitor_mod); resolver.insert("std::json", json_mod); resolver.insert("std::math", math_mod); + resolver.insert("std::command", command_mod); } diff --git a/docs/src/modules/stdlib.md b/docs/src/modules/stdlib.md index 635be72..947f097 100644 --- a/docs/src/modules/stdlib.md +++ b/docs/src/modules/stdlib.md @@ -260,3 +260,32 @@ All functions in this module work with floating-point numbers (f64). If you pass an integer (e.g. `0`), Rhai will report an error because there is no math::cos(i64). Use a floating-point literal instead (e.g. `0.0`). All math functions return `f64`. If you need an integer result, use `to_int` to convert. + +## `std::command` + +The `std::command` module provides functions which you can use to run shell commands on your system. + +### Usage + +```rust,ignore +import "std::command" as command; + +// run a command +command::run("notify-send Hello!"); + +// run a command and read output from stdout +let output = command::run_and_read("pwd"); // example output: /home/foo/.config/ewwii/ +``` + +### Functions + +| Function | Description | +| ----------------- | -------------------------------------------------- | +| `run(x)` | Run the shell command in `x` | +| `run_and_read(x)` | Run the shell command in `x` and return the stdout | + +### Note + +The functions in `std::command` execute arbitrary shell commands. Only run scripts you trust, as misuse can compromise your system. + +This, along with features like `poll`, `listen`, `onclick`, `onhover`, etc., which also run shell commands, can be abused by bad actors. Always verify and trust a package before installing it via [eiipm](https://github.com/Ewwii-sh/eiipm). Even if a package is registered in [eii-manifests](https://github.com/Ewwii-sh/eii-manifests), bad actors could change the code of their package without the awareness of maintainers.