Class ShellExperimental

Jay Mathis code@mathis.network (https://github.com/mathiscode)

The Shell class handles the Terminal environment and interaction with the Kernel.

Implements
  • Shell
Index

Shell configuration

credentials: Credentials = ...

Current user's credentials

interpreter: Interpreter = ...

The shared statement walk. This shell supplies how a pipeline runs (see hostInterface).

onExit?: (code: number) => void

Called when exit runs at the prompt, so a login session ends instead of only setting $?.

state: ShellState = ...

The language state shared with /bin/ecmash: variables, arrays, local frames, readonly, functions, aliases, traps and options. The underscore names below are views onto it.

  • get arrays(): Map<string, Map<string | number, string>>
    Experimental

    Indexed arrays, with mutators for =(...), a[i]=, unset a[i] and a+=(...). A name declared local is read and written in its frame.

    Returns Map<string, Map<string | number, string>>

  • get context(): BoundContext
    Experimental

    Shell context

    Returns BoundContext

  • set context(value: BoundContext): void
    Experimental

    A new context starts with this shell's file creation mask, so a login or su that rebinds it does not lose umask.

    Parameters

    • value: BoundContext

    Returns void

  • get cwd(): string
    Experimental

    Current working directory

    Returns string

  • set cwd(path: string): void
    Experimental

    Setting the working directory also sets PWD and OLDPWD.

    Parameters

    • path: string

    Returns void

  • get envObject(): Record<string, string>
    Experimental

    This shell's environment as a plain object: the envp of every child. Shell-internal names (see SHELL_ONLY_VARIABLES) are left out.

    Returns Record<string, string>

  • get functions(): Map<string, Statement[]>
    Experimental

    Registered name() { ... } function bodies, keyed by name

    Returns Map<string, Statement[]>

  • get getoptsCharIndex(): number
    Experimental

    getopts's position within the argument it is currently reading. See _getoptsCharIndex.

    Returns number

  • set getoptsCharIndex(index: number): void
    Experimental

    getopts's position within the argument it is currently reading, for clustered options like -abc. $OPTIND counts arguments and cannot express that; assigning OPTIND=1 resets this too.

    Parameters

    • index: number

    Returns void

  • get interrupted(): boolean
    Experimental

    Whether the line currently running was interrupted by ^C.

    Returns boolean

  • get positionalParameters(): string[]
    Experimental

    $1, $2, ... in order, up to the first gap; $0 is excluded.

    Returns string[]

  • get process(): Process
    Experimental

    This shell's own process; see _process. Created lazily because /dev is not mounted when the shell is constructed.

    Returns Process

  • get shellOptions(): ShellOptions
    Experimental

    The name of a set flag; see ShellOptionState for what each one does.

    Returns ShellOptions

  • get umask(): number
    Experimental

    The file mode creation mask. The programs this shell starts take it, and so do the files it creates for a redirection; a file system with a mask of its own is given the same one.

    Returns number

  • set umask(mask: number): void
    Experimental

    Parameters

    • mask: number

    Returns void

  • Experimental

    Appends elements to an array (a+=(...)), creating it if needed.

    Parameters

    • name: string
    • values: string[]

    Returns void

  • Experimental

    Apply a set flag. See _shellOptions for what each one does.

    Parameters

    • flag: keyof ShellOptionState
    • enabled: boolean

    Returns void

  • Experimental

    Attaches a terminal to the shell

    Parameters

    • terminal: Terminal

    Returns void

  • Experimental

    Resumes a stopped job with SIGCONT in the background, without waiting for it.

    Parameters

    • Optionalspec: string

    Returns Job | undefined

  • Experimental

    True for the first caller after an interrupt, false for the rest, so an interrupted pipeline reports once rather than once per stage. Cleared when the next command starts.

    Returns boolean

  • Experimental

    Makes this shell the one globalThis.process.env mirrors. Called when its TTY becomes active.

    Returns void

  • Experimental

    Clears positional parameters

    Returns void

  • Experimental

    Declare name as local to the current function call, defaulting to '' until assigned.

    Parameters

    • name: string
    • Optionalvalue: string

    Returns void

  • Experimental

    Delivers a signal sent to this shell with kill: runs the trap for it, if any. False when there is no trap.

    Parameters

    • signal: number

    Returns Promise<boolean>

  • Experimental

    How a command name resolves, for type and command -v: alias, keyword, builtin, function or $PATH file, in that order.

    Parameters

    • name: string

    Returns Promise<
        | {
            kind: "function"
            | "file"
            | "alias"
            | "keyword"
            | "builtin";
            path?: string;
        }
        | undefined,
    >

  • Experimental

    What the shell's own messages start with: ecmaos:, or ./script: line N: while a script file runs, as bash does.

    Returns string

  • Experimental

    Executes a line typed at the prompt. Lines with control flow go through parseStatements and executeStatements; a line that does not parse is a syntax error (status 2) and runs nothing.

    Parameters

    • line: string

    Returns Promise<number>

  • Experimental

    Runs a full script's text to completion; the entry point of Kernel.runScriptFile.

    Parameters

    • script: string
    • Optionalfile: string
    • Optionalargs: string[]

    Returns Promise<number>

  • Experimental

    Runs text in this shell (source, eval): variables, functions and cd persist. The EXIT trap does not run, return is caught, and exit is not. positional replaces $1.. for the duration.

    Parameters

    • text: string
    • Optionalpositional: string[]
    • Optionalfile: string

    Returns Promise<number>

  • Experimental

    Runs a statement tree: plain lines through execute(), control-flow nodes interpreted here. break, continue, return and exit are builtins that throw signals caught by the nearest loop, callFunction or executeScriptText.

    Parameters

    • statements: Statement[]

    Returns Promise<number>

  • Experimental

    Expands ~, ~/ and ~/path to the user's home, outside single quotes.

    Parameters

    • input: string

    Returns string

  • Experimental

    Builds the ExpansionContext bound to this shell's variables, arrays and positional parameters.

    Returns ExpansionContext

  • Experimental

    Resumes a stopped job with SIGCONT, or brings a background job to the foreground, and waits for it.

    Parameters

    • Optionalspec: string

    Returns Promise<number | undefined>

  • Experimental

    Resolves a job spec: %N, %%/%+, %-, a pid, or none (the current job). Undefined if nothing matches.

    Parameters

    • Optionalspec: string

    Returns Job | undefined

  • Experimental

    The handler registered for signal, if any.

    Parameters

    • signal: string

    Returns string | undefined

  • Experimental

    True when PROMPT_COMMAND has something to run, so the prompt can skip the async hop when it does not.

    Returns boolean

  • Experimental

    Whether name was marked readonly, so an assignment or an unset must be refused.

    Parameters

    • name: string

    Returns boolean

  • Experimental

    All tracked jobs, oldest first. See _jobs's doc comment for retention behavior.

    Returns Job[]

  • Experimental

    Every registered trap as [signal, handler] pairs, for bare trap's listing.

    Returns [string, string][]

  • Experimental

    Runs the startup files in the order given by shell-core/startup-files. Does not fire the EXIT trap, and a file that fails does not stop the others.

    Returns Promise<void>

  • Experimental

    Reads name's array from the innermost frame that declares it, else from the shell's own.

    Parameters

    • name: string

    Returns Map<string | number, string> | undefined

  • Experimental

    Resolves a variable: the innermost local frame first, then the environment.

    Parameters

    • name: string

    Returns string | undefined

  • Experimental

    Marks name readonly. There is deliberately no way back, as in bash.

    Parameters

    • name: string

    Returns void

  • Experimental

    Whether pid is this shell's own process, the one $$ names.

    Parameters

    • pid: number

    Returns boolean

  • Experimental

    Publishes the exported environment to globalThis.process.env if this shell owns that mirror; a no-op otherwise.

    Returns void

  • Experimental

    Every readonly name, for readonly -p.

    Returns string[]

  • Experimental

    Files this console's login in /run/utmp and /var/log/wtmp. A failure to record is never a failure to log in.

    Parameters

    • username: string

    Returns Promise<void>

  • Experimental

    Forgets a job without touching its processes (disown): no more jobs entry and no Done notice.

    Parameters

    • job: Job

    Returns void

  • Experimental

    Runs the handler registered for signal, if any (and it isn't the '' ignore marker). Errors from the handler are swallowed - a bad trap must not crash the shell.

    Parameters

    • signal: string

    Returns Promise<void>

  • Experimental

    Replaces an array's entire contents.

    Parameters

    • name: string
    • values: string[]

    Returns void

  • Experimental

    Sets one array element by index, creating the array if needed.

    Parameters

    • name: string
    • index: number
    • value: string

    Returns void

  • Experimental

    Sets positional parameters

    Parameters

    • args: string[]

    Returns void

  • Experimental

    Sets, clears or ignores a trap. Names are normalized (SIGINT and INT are the same).

    Parameters

    • handler: string | undefined
    • signal: string

    Returns void

  • Experimental

    Sets a variable in the innermost local frame if one is active, else in the environment.

    Parameters

    • name: string
    • value: string
    • append: boolean = false

    Returns void

    when name is readonly

  • Experimental

    Notices for finished background jobs, polled at prompt time. Reporting retires the job.

    Returns string[]

  • Experimental

    Removes an entire array (unset a).

    Parameters

    • name: string

    Returns void

  • Experimental

    Removes one array element by index (unset a[i]).

    Parameters

    • name: string
    • index: number

    Returns void

  • Experimental

    Every variable visible now, local frames over the environment, without shell-internal names. What export -p and bare env list.

    Returns Map<string, string>

  • Experimental

    Waits for one job or pid, or for every running or stopped background job when called with none.

    Parameters

    • Optionalspec: string

    Returns Promise<number | undefined>