Skip to main content

Shell

Struct Shell 

Source
pub struct Shell { /* private fields */ }
Expand description

A POSIX shell interpreter instance.

Each Shell maintains its own variable scope, working directory, function definitions, and options — safe for concurrent use across threads (one Shell per thread; Shell itself is not Sync).

§Quick start

use epsh::eval::Shell;
let mut shell = Shell::new();
let exit_code = shell.run_script("echo hello");

§Builder pattern

use epsh::eval::Shell;
use std::path::PathBuf;
let mut shell = Shell::builder()
    .cwd(PathBuf::from("/project"))
    .errexit(true)
    .build();

Implementations§

Source§

impl Shell

Source

pub fn builder() -> ShellBuilder

Create a ShellBuilder for configuring a new shell instance.

Source

pub fn new() -> Self

Create a new shell with default settings and inherited environment.

Source

pub fn set_cancel_flag(&mut self, flag: Arc<AtomicBool>)

Set a cancellation flag. When the flag is set to true, the shell will abort execution at the next check point with ShellError::Cancelled.

Source

pub fn set_timeout(&mut self, duration: Duration)

Set an execution timeout. The shell will abort with ShellError::TimedOut when the deadline is exceeded, checked at the same points as cancellation.

Source

pub fn vars(&self) -> &Variables

Variable storage.

Source

pub fn vars_mut(&mut self) -> &mut Variables

Mutable variable storage.

Source

pub fn functions(&self) -> &HashMap<String, Command>

Defined functions.

Source

pub fn exit_status(&self) -> ExitStatus

Last command’s exit status ($?).

Source

pub fn pid(&self) -> u32

Shell’s PID ($$).

Source

pub fn cwd(&self) -> &Path

Current working directory.

Source

pub fn opts(&self) -> &ShellOpts

Shell options.

Source

pub fn opts_mut(&mut self) -> &mut ShellOpts

Mutable shell options.

Source

pub fn traps(&self) -> &HashMap<String, String>

Trap handlers.

Source

pub fn set_stdout_sink(&mut self, sink: Arc<Mutex<dyn Write + Send>>)

Set a stdout sink. Builtin output will be written here instead of fd 1.

Source

pub fn set_stderr_sink(&mut self, sink: Arc<Mutex<dyn Write + Send>>)

Set a stderr sink. Error output will be written here instead of fd 2.

Source

pub fn set_external_handler(&mut self, handler: ExternalHandler)

Set a callback that replaces the default external command execution.

When set, the handler is called instead of eval_external for commands that are not builtins or functions. Redirections are already applied to fds before the handler runs. The handler receives:

  • args: expanded arguments (args[0] is the command name)
  • env: prefix assignment pairs (FOO=bar cmd[("FOO", "bar")])
Source

pub fn set_cwd(&mut self, dir: PathBuf)

Set the shell’s working directory.

Source

pub fn set_args_bytes(&mut self, args: &[ShellBytes])

Set shell arguments as raw shell bytes.

Source

pub fn resolve_path(&self, p: &str) -> PathBuf

Resolve a path against the shell’s working directory. Absolute paths are returned as-is; relative paths are joined with self.cwd.

Source

pub fn resolve_path_bytes(&self, p: &ShellBytes) -> PathBuf

Resolve a path against the shell’s working directory from raw shell bytes.

Source

pub fn run_script(&mut self, source: &str) -> i32

Execute a shell script from source text.

Source

pub fn run_program(&mut self, program: &Program) -> ExitStatus

Execute a pre-parsed program. Returns the final exit status. This is the library-facing entry point — parse once with Parser, then execute via run_program.

Source

pub fn set_args(&mut self, args: &[&str])

Set shell arguments ($0, $1, $2, …).

Source

pub fn set_var(&mut self, name: &str, value: &str) -> Result<(), String>

Set a variable. Returns error if the variable is readonly.

Source

pub fn set_var_bytes( &mut self, name: &str, value: ShellBytes, ) -> Result<(), String>

Set a variable from raw shell bytes.

Source

pub fn get_var(&self, name: &str) -> Option<&str>

Get a variable’s value.

Source

pub fn get_var_bytes(&self, name: &str) -> Option<&ShellBytes>

Get a variable’s raw bytes.

Source

pub fn eval_command(&mut self, cmd: &Command) -> Result<ExitStatus>

Evaluate a command node, returning its exit status.

Source

pub fn command_subst(&mut self, cmd: &Command) -> Result<String>

Execute a command substitution and return its output.

Trait Implementations§

Source§

impl Default for Shell

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl ShellExpand for Shell

Source§

fn vars(&self) -> &Variables

Source§

fn vars_mut(&mut self) -> &mut Variables

Source§

fn exit_status(&self) -> ExitStatus

Source§

fn pid(&self) -> u32

Source§

fn cwd(&self) -> &Path

Source§

fn shell_flags(&self) -> String

Active shell option flags for $- (e.g. “eux”).
Source§

fn last_bg_pid(&self) -> Option<u32>

Last background PID for $!, if any.
Source§

fn nounset(&self) -> bool

Whether set -u (nounset) is active.
Source§

fn noglob(&self) -> bool

Whether set -f (noglob) is active.
Source§

fn command_subst(&mut self, cmd: &Command) -> Result<String>

Auto Trait Implementations§

§

impl Freeze for Shell

§

impl !RefUnwindSafe for Shell

§

impl Send for Shell

§

impl !Sync for Shell

§

impl Unpin for Shell

§

impl UnsafeUnpin for Shell

§

impl !UnwindSafe for Shell

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.