How mahakagarwal7/os-shell works
Purpose and Entry Point
This repository implements a small Unix-like shell for learning OS process and parsing concepts. The user-facing entry point is msh.sh, which orchestrates shell startup, input handling, parsing, built-ins, and command execution.
Shell Orchestration
msh.sh is the top-level control loop. It likely sources the shell libraries, reads commands interactively, dispatches built-in commands, and delegates external commands to the execution layer.
Paths: msh.sh, lib/utils.sh
Parsing Boundary
Command-line parsing is split between shell integration and native code. The shell-facing parser wrapper lives in lib/parser.sh; C parsing interfaces and implementation are defined by include/parser.h and c_src/parser.c. This establishes a native parsing boundary rather than implementing all tokenization in Bash.
Paths: lib/parser.sh, include/parser.h, c_src/parser.c
Built-in Command Handling
Commands that must affect shell process state, such as directory or environment-oriented operations, belong in the built-in layer rather than being forked as external programs. lib/builtin.sh is the dedicated dispatch/implementation boundary for these commands.
Paths: lib/builtin.sh, msh.sh
External Process Execution
External commands are delegated to an executor layer. lib/executor.sh provides shell-level execution coordination, while c_src/fork_exec.c contains native process-management logic, likely centered on fork/exec behavior. This separation is the core OS-runtime boundary: built-ins execute in the shell process; external commands execute in child processes.
Paths: lib/executor.sh, c_src/fork_exec.c
Native Interface and Runtime Dependencies
The project combines Bash scripts with C code compiled against Unix/POSIX process APIs. The C layer exposes parser functionality through include/parser.h and implements process launching in c_src/fork_exec.c. Its material runtime dependency is a Unix-like environment supporting shell execution and POSIX process primitives.
Paths: include/parser.h, c_src/parser.c, c_src/fork_exec.c
Primary Control Flow
A command flows from msh.sh into parsing via lib/parser.sh and the C parser. The resulting command classification determines whether it is handled by lib/builtin.sh in-process or sent through lib/executor.sh to the native fork/exec implementation. Utility functions shared across these stages are centralized in lib/utils.sh.
Paths: msh.sh, lib/parser.sh, lib/builtin.sh
Main entry points: msh.sh Shell control loop · parser.c Native parser · fork_exec.c Fork/exec runtime · parser.sh Parser wrapper
Components 11
Bash shell
- Shell control loop · Reads input and routes commands.
msh.sh - Shared utilities
lib/utils.sh - Parser wrapper
lib/parser.sh - Built-in commands · Runs stateful commands in the shell process.
lib/builtin.sh - External executor
lib/executor.sh
Native process layer
- Parser interface
include/parser.h - Native parser
c_src/parser.c - Fork/exec runtime · Launches external commands in child processes.
c_src/fork_exec.c
Other components
- Shell user
- Unix/POSIX runtime · Provides shell execution and process primitives.
- External programs
Connections 12
- Shell user → Shell control loop: enters commands
- Shell control loop → Shared utilities: uses
- Shell control loop → Parser wrapper: parses command
- Parser wrapper → Native parser: invokes native parser
- Native parser → Parser interface: implements
- Shell control loop → Built-in commands: built-in command
- Shell control loop → External executor: external command
- Built-in commands → Shared utilities: uses
- External executor → Fork/exec runtime: delegates launch
- Fork/exec runtime → External programs: forks and execs
- Native parser → Unix/POSIX runtime: runs on
- Fork/exec runtime → Unix/POSIX runtime: uses process APIs