Skip to content
Snippets Groups Projects
Forked from CDIS / Computer Sciences / Courses / CS537 / fall24 / public / p3
This fork has diverged from the upstream repository.
GAURAV BATRA's avatar
GAURAV BATRA authored
b91a1d4e
History

Shell Implementation Project

Project Overview

This project is a custom shell implementation designed to replicate the functionality of Unix-like shells with additional features for handling variables, history, file redirection, and batch processing. The shell is implemented in C and adheres to specific requirements outlined in the project description.

Author Information

  • Name: Gaurav Batra
  • CS Login: gbatra
  • Wisc ID: 9087090024
  • Email: gbatra3@wisc.edu

Features Implemented

Parsing

  • Token Parsing: Implements efficient parsing of command-line tokens for processing user input.

Built-in Commands

  • exit: Gracefully exits the shell.
  • cd: Changes the current working directory using chdir.
  • ls: Lists directory contents using scandir with the following specifications:
    • Directories end with a slash (optional).
    • Case-insensitive sorting using strcmp for alphasort.
    • Hidden files (.*) are excluded from the output.

Local Variables (local)

  • Getter and Setter: Manages local shell variables.
  • Token Parsing Updates: Updates parsing methods to handle local variables correctly.

Environment Variables (export)

  • Getter and Setter: Manages environment variables using export.
  • Token Parsing Updates: Updates parsing methods to prioritize environment variables over local variables.

History Management

  • Implements command history using a linked list (LL):
    • Set History Length: Allows setting the number of stored commands.
    • Command History Display: Displays the last n commands.
    • Does not record the execution of history-related commands themselves.
    • Avoids duplicate consecutive commands in the history.

Non Built-in Commands

  • Uses fork and execv to execute all non built-in commands.

File Redirection

  • Implements file redirection for both built-in and non built-in commands.

Script Mode and Batch Mode

  • Script Mode: Executes commands from a script file.
  • Batch Mode: Processes commands from a file passed as an argument without generating a prompt.

Memory Leak Checks

  • Memory leaks are rigorously checked using valgrind.

Miscellaneous Cases

  • Handles $ on the right side of assignments in commands other than echo and export.
  • Ensures repeated commands are recorded only once in the history.
  • Corrects exit conditions and error codes for robust error handling.

Usage

  1. Clone the repository and navigate to the project directory.
  2. Compile the source files using the provided Makefile:
    make
  3. Run the shell:
    ./my_shell

Batch Mode

To run the shell in batch mode, provide the script file as an argument:

./my_shell <script_file>

Development Details

Tools and Libraries

  • Language: C
  • Libraries Used: Standard POSIX libraries
  • Debugging Tools: valgrind

Error Handling

  • Ensures robust handling of invalid commands and erroneous input.
  • Provides meaningful error codes on exit.

Testing

  • Validated all features using unit tests and integration tests.
  • Conducted memory leak checks with valgrind to ensure no memory leaks.

Future Improvements

  • Implement support for more advanced shell scripting features.
  • Add functionality for pipe (|) and process substitution.
  • Enhance performance for handling large scripts and command history.

Repository Information

  • Repository Name: custom_shell_project
  • Contributions: Feel free to fork, contribute, and submit pull requests for enhancements.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Happy Shelling! 🎉