pub struct ShellCore<'os> { /* private fields */ }Expand description
The shared session state and execution machinery.
The core owns the session’s working directory, environment, and the
CommandRuntime that actually launches commands. External commands are
resolved through PATH and the runtime’s executable bindings; builtins
are dispatched through the ShellDialect the shell was created with.
Implementations§
Source§impl<'os> ShellCore<'os>
impl<'os> ShellCore<'os>
Sourcepub fn new(
os: &'os UnixOs,
credentials: Credentials,
dialect: ShellDialect,
) -> Result<Self, OsError>
pub fn new( os: &'os UnixOs, credentials: Credentials, dialect: ShellDialect, ) -> Result<Self, OsError>
Creates a core for a session, executing in the given shell dialect.
Sourcepub fn cwd(&self) -> &VirtualPath
pub fn cwd(&self) -> &VirtualPath
The current working directory.
Sourcepub fn set_cwd(&mut self, cwd: VirtualPath)
pub fn set_cwd(&mut self, cwd: VirtualPath)
Sets the working directory (used by non-interactive interpreter invocations, which start in the caller’s directory).
Sourcepub fn environment(&self) -> &Environment
pub fn environment(&self) -> &Environment
The shell environment.
Sourcepub fn environment_mut(&mut self) -> &mut Environment
pub fn environment_mut(&mut self) -> &mut Environment
The shell environment, mutable.
Sourcepub fn credentials(&self) -> &Credentials
pub fn credentials(&self) -> &Credentials
The credentials this shell runs as.
Sourcepub fn record_history(&mut self, line: &str)
pub fn record_history(&mut self, line: &str)
Records one completed command line.
The interactive application calls this once per executed line — the
whole of a multi-line construct, not its continuation chunks — so
history shows exactly what the user typed. Internal re-execution
(rc sourcing, alias expansion, function bodies) never records.
Sourcepub fn aliases_mut(&mut self) -> &mut BTreeMap<String, String>
pub fn aliases_mut(&mut self) -> &mut BTreeMap<String, String>
The aliases, mutable.
Sourcepub fn functions_mut(&mut self) -> &mut BTreeMap<String, String>
pub fn functions_mut(&mut self) -> &mut BTreeMap<String, String>
The functions, mutable.
Sourcepub fn set_terminal_size(&mut self, rows: u16, cols: u16)
pub fn set_terminal_size(&mut self, rows: u16, cols: u16)
Reports the terminal size, exported as COLUMNS/LINES.
Sourcepub fn source_startup_files(&mut self)
pub fn source_startup_files(&mut self)
Sources the dialect’s startup files (system-wide then per-user), so aliases, functions, and variables from real rc files exist in every session. Missing files are simply skipped, as real shells do.
Sourcepub fn install_shell_bindings(&mut self)
pub fn install_shell_bindings(&mut self)
Binds the interpreter behaviors onto the standard shell programs so
scripts execute through the same implementation as interactive
commands: /bin/sh and /bin/bash speak the Bash
dialect, /bin/zsh speaks the Zsh dialect, wherever those programs
exist on this system. Shebang interpreters resolve through these
bindings too.
Sourcepub fn execute(
&mut self,
line: &str,
stdin: &mut dyn Input,
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn execute( &mut self, line: &str, stdin: &mut dyn Input, stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
Executes a line of shell input.
Sourcepub fn execute_with_terminal(
&mut self,
line: &str,
stdin: &mut dyn Input,
stdout: &mut dyn Output,
stderr: &mut dyn Output,
terminal: Option<&mut dyn Terminal>,
) -> ExitStatus
pub fn execute_with_terminal( &mut self, line: &str, stdin: &mut dyn Input, stdout: &mut dyn Output, stderr: &mut dyn Output, terminal: Option<&mut dyn Terminal>, ) -> ExitStatus
Executes a line, optionally handing a terminal to a command that asks for one.
Sourcepub fn request_exit(&mut self, status_argument: Option<&str>) -> ExitStatus
pub fn request_exit(&mut self, status_argument: Option<&str>) -> ExitStatus
Records an exit [N] request and returns its status.
The status argument follows real shells: a missing or non-numeric
argument means success; an out-of-range value wraps modulo 256
(exit 300 exits with status 44, like bash).
Sourcepub fn take_exit_request(&mut self) -> Option<ExitStatus>
pub fn take_exit_request(&mut self) -> Option<ExitStatus>
Takes a pending exit request, if any.
The application calls this after executing a line: when it returns
Some, the session is over and the carried status is the process
exit code.
Sourcepub fn resolve_with_hash(
&mut self,
name: &str,
) -> Result<CommandResolution, OsError>
pub fn resolve_with_hash( &mut self, name: &str, ) -> Result<CommandResolution, OsError>
Resolves a command name, consulting the hash table first.
Sourcepub fn resolve_external_path(&mut self, name: &str) -> Option<String>
pub fn resolve_external_path(&mut self, name: &str) -> Option<String>
Resolves an external command to its filesystem path, without hashing.
Sourcepub fn resolve_external_paths(&mut self, name: &str) -> Vec<String>
pub fn resolve_external_paths(&mut self, name: &str) -> Vec<String>
Every filesystem location that resolves the name as an executable,
in PATH order (the Zsh where builtin prints them all).
Sourcepub fn executable_path(&self, path: &VirtualPath) -> Option<VirtualPath>
pub fn executable_path(&self, path: &VirtualPath) -> Option<VirtualPath>
The absolute path of an existing executable, if any.
Sourcepub fn display_path(&self, path: &VirtualPath) -> String
pub fn display_path(&self, path: &VirtualPath) -> String
The home-relative display form of a path (/Users/rey → ~).
Sourcepub fn is_directory(&self, path: &VirtualPath) -> bool
pub fn is_directory(&self, path: &VirtualPath) -> bool
Whether path is an existing directory.
Sourcepub fn expand_glob(&self, pattern: &str) -> Vec<String>
pub fn expand_glob(&self, pattern: &str) -> Vec<String>
Expands a glob (*, ?) in an argument via the virtual filesystem.
Sourcepub fn resolve_path(&self, argument: &str) -> Result<VirtualPath, OsError>
pub fn resolve_path(&self, argument: &str) -> Result<VirtualPath, OsError>
Resolves a path argument: absolute paths are used directly, relative
paths are joined onto the working directory, honoring . and ..
components. An empty argument is an error, like the real tools.
Sourcepub fn write_redirect(
&mut self,
path_str: &str,
bytes: &[u8],
append: bool,
) -> Result<(), OsError>
pub fn write_redirect( &mut self, path_str: &str, bytes: &[u8], append: bool, ) -> Result<(), OsError>
Writes captured output to a redirection target (creating it when needed), or discards it for the null device.
Sourcepub fn execute_script(
&mut self,
script_path: &str,
stdin: &mut dyn Input,
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn execute_script( &mut self, script_path: &str, stdin: &mut dyn Input, stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
Executes a script file line by line.
Sourcepub fn cd_to(
&mut self,
argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn cd_to( &mut self, argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The cd builtin: changes the working directory, with cd -
returning to the previous directory (OLDPWD).
Sourcepub fn pwd_of(&self, stdout: &mut dyn Output) -> ExitStatus
pub fn pwd_of(&self, stdout: &mut dyn Output) -> ExitStatus
The pwd builtin.
Sourcepub fn list_jobs(&mut self, stdout: &mut dyn Output) -> ExitStatus
pub fn list_jobs(&mut self, stdout: &mut dyn Output) -> ExitStatus
The jobs builtin: lists this session’s background jobs with their
virtual-time state. A pending job shows Running until
the clock reaches its completion; then it shows Done.
Sourcepub fn foreground_job(
&mut self,
argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn foreground_job( &mut self, argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The fg builtin: brings a background job to the foreground. Waiting
for a pending job is advancing the virtual clock to its completion
time; the invocation’s exit status is the job’s.
Sourcepub fn background_job(
&mut self,
argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn background_job( &mut self, argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The bg builtin: re-reports a background job as running in the
background. Execution is sequential, so there is nothing to resume:
a pending job is already running, and a finished one stays finished.
Sourcepub fn wait_for_jobs(
&mut self,
argv: &[String],
stderr: &mut dyn Output,
) -> ExitStatus
pub fn wait_for_jobs( &mut self, argv: &[String], stderr: &mut dyn Output, ) -> ExitStatus
The wait builtin: advances the virtual clock until background jobs
complete. With no arguments, waits for all pending ones;
otherwise waits for each %spec/pid argument. Returns the last
waited job’s exit status.
Sourcepub fn builtin_echo(
&self,
argv: &[String],
stdout: &mut dyn Output,
) -> ExitStatus
pub fn builtin_echo( &self, argv: &[String], stdout: &mut dyn Output, ) -> ExitStatus
The echo builtin, shared by Bash and Zsh: -n suppresses the
newline, -e/-E enable and disable escape processing. The default
escape behavior is the dialect’s (Bash off, Zsh on).
Sourcepub fn builtin_export(
&mut self,
argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn builtin_export( &mut self, argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The export builtin, shared by Bash and Zsh: assignments set the
environment; with no arguments the listing prefix is the dialect’s
(declare -x in Bash, export in Zsh).
Sourcepub fn builtin_history(&self, stdout: &mut dyn Output) -> ExitStatus
pub fn builtin_history(&self, stdout: &mut dyn Output) -> ExitStatus
The history builtin, identical in Bash and Zsh: prints the
numbered command history.
Sourcepub fn builtin_alias(
&mut self,
argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn builtin_alias( &mut self, argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The alias builtin, shared by Bash and Zsh: definitions set
aliases; with no arguments the listing prefix is the dialect’s
(alias in Bash, nothing in Zsh).
Sourcepub fn builtin_unalias(
&mut self,
argv: &[String],
stderr: &mut dyn Output,
) -> ExitStatus
pub fn builtin_unalias( &mut self, argv: &[String], stderr: &mut dyn Output, ) -> ExitStatus
The unalias builtin, identical in Bash and Zsh.
Sourcepub fn resolve_name(
&mut self,
name: &str,
is_builtin: fn(&str) -> bool,
) -> NameResolution
pub fn resolve_name( &mut self, name: &str, is_builtin: fn(&str) -> bool, ) -> NameResolution
How a name resolves for the type/which/where/whence family:
one shared precedence walk (alias → function → builtin → PATH), with
the phrasing owned by each shell’s reporter.
Sourcepub fn hash_table(
&mut self,
argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn hash_table( &mut self, argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The hash builtin: manages the command hash table.
Sourcepub fn print_dirs(&self, stdout: &mut dyn Output) -> ExitStatus
pub fn print_dirs(&self, stdout: &mut dyn Output) -> ExitStatus
The dirs builtin: prints the directory stack, current directory
first, with ~ for the home directory.
Sourcepub fn push_directory(
&mut self,
argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn push_directory( &mut self, argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The pushd builtin: pushes the current directory onto the stack and
changes to the argument.
Sourcepub fn pop_directory(
&mut self,
_argv: &[String],
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn pop_directory( &mut self, _argv: &[String], stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The popd builtin: pops the top of the directory stack and changes
into it.
Sourcepub fn unset_names(
&mut self,
argv: &[String],
stderr: &mut dyn Output,
) -> ExitStatus
pub fn unset_names( &mut self, argv: &[String], stderr: &mut dyn Output, ) -> ExitStatus
The unset builtin: removes variables, or functions with -f.
Sourcepub fn source_file(
&mut self,
argv: &[String],
stdin: &mut dyn Input,
stdout: &mut dyn Output,
stderr: &mut dyn Output,
) -> ExitStatus
pub fn source_file( &mut self, argv: &[String], stdin: &mut dyn Input, stdout: &mut dyn Output, stderr: &mut dyn Output, ) -> ExitStatus
The source/. builtin: reads the named file and executes it in
the current shell.
Sourcepub fn null_command(&self) -> ExitStatus
pub fn null_command(&self) -> ExitStatus
The : null command: does nothing and succeeds.
Sourcepub fn clear_screen(&self, stdout: &mut dyn Output) -> ExitStatus
pub fn clear_screen(&self, stdout: &mut dyn Output) -> ExitStatus
The clear builtin: emits the terminal clear sequence (including
the scrollback erase, like zsh’s clear on macOS).
Source§impl<'os> ShellCore<'os>
impl<'os> ShellCore<'os>
Sourcepub fn expand_word(&mut self, raw: &str) -> String
pub fn expand_word(&mut self, raw: &str) -> String
Expands a word with quoting semantics, discarding the glob marker.
Sourcepub fn expand_word_fields(&mut self, raw: &str) -> Vec<String>
pub fn expand_word_fields(&mut self, raw: &str) -> Vec<String>
Expands a word and splits it into fields on the default IFS
characters (space, tab, newline), as unquoted expansions are in
real shells: v="a b"; ls $v passes two arguments. Characters
from quoted sections never split.
An empty expansion yields no fields; the caller decides whether an empty argument is meaningful.
Sourcepub fn expand_word_with_glob(&mut self, raw: &str) -> (String, bool)
pub fn expand_word_with_glob(&mut self, raw: &str) -> (String, bool)
Expands a word with quoting semantics, reporting whether it contains unquoted glob characters.