Skip to main content

Module shell_syntax

Module shell_syntax 

Source
Expand description

Shell syntax for the simulated sandbox: quoting, variables, globs, redirection, pipes and command lists.

A small, honest subset of POSIX sh, enough for lessons to teach what a real shell does with a line before any program runs. Parsing is pure: variable values come from a lookup closure, and globs are only marked here (the sandbox expands them against its own files).

Not supported, and reported as such: command substitution ($(...), backticks), background jobs (&), here-documents, subshells.

Structs§

Pipeline
Commands joined by |
Stage
One command in a pipeline
Word
One word after quoting and variable expansion

Enums§

Joiner
How a pipeline follows the one before it
Redirect
Where a stream goes, or comes from

Constants§

STATUS_MARK
Stands for $? in text expanded for a real shell: render turns it back into "$?", which the shell fills in with a number as it runs. Private-use characters, so ordinary typing never produces it (and if it did, the worst it can become is that number).

Functions§

fnmatch
Pattern match without the shell’s hidden-file rule (as find -name does)
glob_match
Does name match the glob pattern the way the shell expands it? Supports *, ?, [abc], [a-z] and [!abc]; a leading . must be matched explicitly, so * skips hidden files.
is_name
Is this a valid shell variable name?
parse
Parse one line into pipelines joined by ;, && and ||.
render
Write an expanded command list back as one line for a real sh -c, quoted so the shell takes every word, value and file name literally and expands nothing a second time: no variables, no ~, no globs (expand those before rendering). What a guard checks in this line is what runs. The one thing left for the shell is STATUS_MARK, written as "$?".
split_list
Split a line at unquoted ;, && and ||, without expanding anything, so each part can be expanded just before it runs (as sh does: in X=5; echo $X the echo sees 5). A comment ends the line.