Skip to main content

ShellState

Struct ShellState 

Source
pub struct ShellState {
Show 15 fields pub env: ShellEnv, pub positional: Vec<SmolStr>, pub last_status: i32, pub cwd: String, pub lineno: u32, pub random_seed: Cell<u32>, pub start_time: Instant, pub func_stack: Vec<SmolStr>, pub source_stack: Vec<SmolStr>, pub script_name: Option<SmolStr>, pub shell_pid: u32, pub last_background_pid: Option<u32>, pub last_argument: SmolStr, pub dir_stack: Vec<SmolStr>, pub umask: u32,
}
Expand description

Complete shell state: variables, positional params, status, cwd, functions.

Fields§

§env: ShellEnv

Variable scopes.

§positional: Vec<SmolStr>

Positional parameters ($1, $2, …).

§last_status: i32

Last exit status ($?).

§cwd: String

Current working directory ($PWD).

§lineno: u32

Current line number ($LINENO), updated by the runtime.

§random_seed: Cell<u32>

PRNG seed for $RANDOM (XorShift32). Uses Cell so get_var can remain &self.

§start_time: Instant

Seconds elapsed since shell start ($SECONDS).

§func_stack: Vec<SmolStr>

Function call stack for $FUNCNAME.

§source_stack: Vec<SmolStr>

Source file stack for $BASH_SOURCE.

§script_name: Option<SmolStr>

Override for $0 when executing a script (e.g. bash script.sh). When None, $0 returns the default "wasmsh".

§shell_pid: u32

Pseudo shell pid used for $$.

§last_background_pid: Option<u32>

Last background pid used for $!.

§last_argument: SmolStr

Last argument of the previously executed command ($_).

§dir_stack: Vec<SmolStr>

Directory stack used by dirs/pushd/popd.

§umask: u32

Shell umask used by the umask builtin.

Implementations§

Source§

impl ShellState

Source

pub fn new() -> Self

Source

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

Get the value of a special parameter or named variable. For arrays accessed as scalars, returns all values joined by space. Dynamic variables ($RANDOM, $LINENO, $SECONDS, $FUNCNAME, $BASH_SOURCE) are resolved on access.

Source

pub fn set_last_argument(&mut self, arg: impl Into<SmolStr>)

Record the last argument for $_.

Source

pub fn set_last_background_pid(&mut self, pid: Option<u32>)

Update the tracked background pid used by $!.

Source

pub fn set_var(&mut self, name: SmolStr, value: SmolStr)

Set a named variable (not a special parameter). Preserves exported and readonly flags if the variable already exists. If the variable is readonly, the write is silently skipped (like bash). Setting a scalar value on an existing array variable replaces element 0 for indexed arrays, or replaces it entirely with a scalar for assoc arrays.

Source

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

Set a variable, returning an error if it is readonly.

Source

pub fn set_readonly(&mut self, name: SmolStr, value: SmolStr)

Mark a variable as readonly with the given value.

Source

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

Remove a variable. Returns error if readonly.

Source

pub fn set_nounset_error(&mut self, name: &str)

Record that name was expanded while nounset was active. The pending error is consumed by ShellState::take_nounset_error at the next command dispatch point, which matches bash semantics of reporting the first offender and aborting the current simple command.

Source

pub fn take_nounset_error(&mut self) -> Option<SmolStr>

Consume the pending nounset error, if any. Returns the offending variable name and clears the sentinel so the next command starts clean.

Source

pub fn var_names_with_prefix(&self, prefix: &str) -> Vec<SmolStr>

Return all variable names (across all scopes) that start with the given prefix. Innermost scope wins for shadowed names. Results are sorted alphabetically.

Source

pub fn get_array_element(&self, name: &str, index: &str) -> Option<SmolStr>

Get a single element from an array (or scalar when index is “0”). For indexed arrays, the index is parsed as usize. For associative arrays, the index is used as-is. For scalars, index “0” returns the scalar value.

Source

pub fn set_array_element(&mut self, name: SmolStr, index: &str, value: SmolStr)

Set a single element in an array. Creates an indexed array if the variable does not exist. Converts a scalar to an indexed array if needed.

Source

pub fn get_array_keys(&self, name: &str) -> Vec<String>

Get all keys/indices of an array variable.

Source

pub fn get_array_values(&self, name: &str) -> Vec<SmolStr>

Get all values of an array variable.

Source

pub fn get_array_length(&self, name: &str) -> usize

Get the number of elements in an array.

Source

pub fn append_array(&mut self, name: &str, values: Vec<SmolStr>)

Append values to an indexed array (arr+=(val1 val2)). If the variable is a scalar, it is first converted to an indexed array with the scalar as element 0.

Source

pub fn unset_array_element(&mut self, name: &str, index: &str)

Remove a single element from an array.

Source

pub fn init_indexed_array(&mut self, name: SmolStr)

Initialize an empty indexed array variable.

Source

pub fn init_assoc_array(&mut self, name: SmolStr)

Initialize an empty associative array variable.

Trait Implementations§

Source§

impl Clone for ShellState

Source§

fn clone(&self) -> ShellState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ShellState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ShellState

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.