This repository contains a Unix-v6-like shell implemented in Rust.
I wrote this code to exercise with Rust. I do not plan on maintaining this code base or on adding additional features. It's likely the case that this code behaves differently from the original v6 shell.
You need rustc
and cargo
(possibly via rustup
).
To build:
cargo build
To run:
cargo run
The design is quite traditional for a compiler-like tool:
-
there is a lexer (see src/lexer.rs) that implements the
scan
stage; -
the lexer feeds a parser (see src/parser.rs), which produces a parse tree in the
parse
stage; -
we validate the parse tree and transform it for easier execution (see src/translator.rs) in the
plan
stage; -
we interpret the transformed output (see src/interp.rs) to execute shell commands in the
run
stage.
When we encounter commands between (
and )
we execute them in
a subshell. We pass code to the subshell by serializing the specific
portion of the parse tree using src/serializer.rs.
The -x
command line flag prints each command before it's executed.
The --stage STAGE
flag stops processing at the given STAGE
and shows internal data structures.
The -c COMMANDS
command allows a shell (or a sub-shell) to
execute a sequence of commands.
See mit-pdos/xv6-riscv's sh.c for the source code that I started from. The implementation diverged quickly but there are still original-code ideas. For this reason, the copyright is the original one plus mine.
SPDX-License-Identifier: MIT