Author: Shaswath S
Language: C (Standard 11)
Build System: CMake
Shas-Shell is a lightweight, custom-built command-line interpreter (shell) designed to emulate the core functionalities of standard UNIX shells (such as Bash or Zsh). Developed in C, this project serves as a practical exploration of operating system concepts, specifically process management, signal handling, memory allocation, and the execution lifecycle of system commands.
The shell provides an interactive interface for users to execute standard system programs and utilizes a set of built-in commands for session management. It features a modular architecture that separates tokenization, parsing, and execution logic, ensuring maintainability and extensibility.
The project is structured into distinct modules, each handling a specific phase of the shell's Read-Eval-Print Loop (REPL).
Note:
exec.cserves as themain.centry point for this project.
This module initializes the shell and enters an infinite loop that drives the user session. Its primary responsibilities include:
- Prompt Generation: Dynamically renders a color-coded prompt displaying the shell name (
shas-shell) and the current working directory. - Input Acquisition: Utilizes
getline()to capture user input fromstdin. - Environment Variable Expansion: Implements logic to detect and expand environment variables (tokens beginning with
$) before command execution. - History Management: Dynamically allocates memory to store a history of commands entered during the current session.
This module is responsible for lexical analysis. It takes the raw input string and splits it into discrete tokens based on whitespace delimiters. It employs dynamic memory reallocation to handle command lines of varying lengths and argument counts.
The parser accepts the array of tokens produced by the tokeniser and organizes them into a structured command entity (defined in command.h). This separation allows the shell to distinguish between the executable command and its subsequent arguments.
This module handles the execution of non-built-in system commands. It utilizes the UNIX system calls fork() to create child processes and execvp() to replace the child process image with the requested program. The parent process waits for the child's termination using waitpid(), ensuring synchronous execution.
This module contains the implementation logic for commands that must be executed within the shell's own process space (e.g., changing the directory) rather than spawning a new process.
The Shas-Shell includes a suite of internal commands developed to manage the shell environment. These are prioritized over system executables.
cd <directory>: Changes the current working directory. Supports absolute and relative paths. If no argument is provided, it defaults to the user'sHOMEdirectory.history: Displays a numbered list of all commands executed in the current session, allowing users to review their workflow.help: Prints a manual detailing the available built-in commands and their usage syntax.exit: Gracefully terminates the shell session and frees allocated memory.
The shell is fully capable of executing standard terminal commands and binaries found in the system's PATH.
- Examples:
ls -la,pwd,grep,mkdir,top, etc. - Mechanism: Leverages
execvp, allowing users to run any installed UNIX utility with arguments.
The prompt provides immediate context to the user by displaying the current working directory in blue and the shell name in green.
- Format: shas-shell:current_directory>
The shell supports the expansion of environment variables.
- Usage:
$VAR_NAME - Example:
echo $USERorcd $HOMEwill be expanded to their respective values before execution.
- GCC Compiler (supporting C11 standard)
- CMake (Version 3.26 or higher)
- Linux/UNIX environment
-
Clone the Repository: git clone cd Shas-Shell
-
Build with CMake: cmake . make
Once compiled, the executable Shas_Shell will be created.
Run command: ./Shas_Shell
- Memory Management: The shell extensively uses
mallocandreallocto handle dynamic input sizes. It includes cleanup routines to free tokens and command structures after every execution cycle to prevent memory leaks. - Error Handling: The shell provides feedback for common errors, such as "directory not found" for
cdor "cmd not found" ifexecvpfails to locate a binary.
Refer to the Test_Cases.pdf to see the UNIX terminal commands running.
Future iterations of the Shas-Shell aim to include:
- I/O Redirection (
>,<) and Piping (|) - Background process execution (
&) - Signal handling (e.g., catching
SIGINT/ Ctrl+C)