Skip to content

Tinyshell: a command-line interface for the OS with features such as command history, redirection, pipes, environment variables, and signals, designed for efficiency.

Notifications You must be signed in to change notification settings

UchihaShaco/Tinyshell

Repository files navigation

Thank you Huong (hbui-vu)

I just wanted to take a moment to thank Huong for being such an amazing partner throughout our project. Your hard work, dedication, and expertise were essential to achieving our goals. I truly appreciated your positive
attitude, collaborative spirit, and your ability to make me laugh with your hilarious reactions whenever we faced a challenge :D.

Tinyshell

Tinyshell is a small shell program that provides a command-line interface to interact with the underlying operating system. It is designed to meet the following requirements:

Requirements

Prompt
The shell should display a prompt when waiting for a new command. The prompt can be customized as desired.

Command History

The shell should have a working history that allows users to recall previously executed commands.

Command Execution

The shell should search for and launch the right executable based on the PATH variable or using a relative or absolute path.

Global Variables

The shell should not use more than one global variable to avoid any unintended side-effects. The purpose of the global variable should be well-defined and documented.

Quoting

The shell should not interpret unclosed quotes or special characters that are not required by the subject such as \ (backslash) or ; (semicolon). It should handle single and double quotes as well.

Redirection

The shell should implement redirections such as < (input), > (output), << (here-doc), and >> (append).

Pipes

The shell should implement pipes (| character) to connect the output of one command to the input of the next command in the pipeline.

Environment Variables

The shell should handle environment variables (e.g., $HOME) which should expand to their values.

Exit Status

The shell should handle $? which should expand to the exit status of the most recently executed foreground pipeline.

Signals

The shell should handle signals such as ctrl-C, ctrl-D, and ctrl-\ which should behave like in bash.

Interactive Mode

In interactive mode, the shell should display a new prompt on a new line when the user presses ctrl-C, exit when the user presses ctrl-D, and do nothing when the user presses ctrl-.

Functions

The shell should use functions from the readline library such as readline, rl_clear_history, rl_on_new_line, rl_replace_line, rl_redisplay, and add_history to implement the command history and line editing features.

Memory Management

The shell should use memory management functions such as malloc and free to allocate and deallocate memory as needed.

File I/O

The shell should use file I/O functions such as access, open, read, and close to read and write files as needed.

Process Management

The shell should use process management functions such as fork, wait, waitpid, wait3, wait4, signal, sigaction, sigemptyset, sigaddset, kill, and exit to create and manage child processes as needed.

Directory Management

The shell should use directory management functions such as opendir, readdir, and closedir to list and manipulate directories as needed.

Error Handling

The shell handles errors by using error handling functions such as strerror and perror to display error messages when necessary.
When an error occurs, the shell will display a descriptive error message indicating the source of the error and any relevant information.
This helps users quickly identify and resolve issues that may arise while using the shell. Additionally, Minishell uses a consistent approach to error handling throughout its implementation, ensuring that errors are handled in a reliable and predictable manner.

Terminal Management

The shell should use terminal management functions such as isatty, ttyname, ttyslot, ioctl, tcsetattr, tcgetattr, tgetent, tgetflag, tgetnum, tgetstr, tgoto, and tputs to manage the terminal as needed.

Built-in Commands

The shell should implement the following built-in commands:

  • echo with option -n
  • cd with only a relative or absolute path
  • pwd with no options
  • export with no options
  • unset with no options
  • env with no options or arguments
  • exit with no options
  • By meeting these requirements, Minishell provides a robust and flexible shell program that can be used to interact with the operating system in a variety of ways.

Implementation

Minishell is implemented in C programming language and uses various libraries and system calls to achieve its functionality. The program is structured using modular programming techniques, where different components of the shell are implemented in separate files to improve readability and maintainability.

The readline library is used to implement the command history and line editing features of the shell. It provides functions such as readline, rl_clear_history, rl_on_new_line, rl_replace_line, rl_redisplay, and add_history. These functions are used to read user input from the terminal, store it in the history, and display it back to the user for editing.

The shell uses the fork system call to create child processes to execute commands. The parent process waits for the child process to complete before proceeding. The wait system call is used to wait for the child process to exit and obtain its exit status. The kill system call is used to send signals to child processes as needed.

The shell uses the execve system call to execute external commands. The PATH environment variable is used to search for the executable file in the directories specified in the variable. The shell also handles redirections such as < (input), > (output), << (here-doc), and >> (append) using the open, close, and dup2 system calls.

The shell handles environment variables by expanding them to their values using the getenv system call. The shell also implements built-in commands such as echo with option -n, cd with only a relative or absolute path, pwd with no options, export with no options, unset with no options, env with no options or arguments, and exit with no options.

Conclusion

Minishell is a small but powerful shell program that provides a command-line interface to interact with the underlying operating system.
It is designed to meet the needs of developers, system administrators, and power users who require a flexible and customizable shell for their daily work.

Minishell provides a rich set of features including command history, command execution, redirection, pipes, environment variables, exit status, signals, interactive mode, functions, memory management, file I/O, process management, directory management, error handling, terminal management, and built-in commands.

The implementation of Minishell is modular and well-structured, making it easy to read, maintain, and extend.
The use of libraries and system calls allows the program to achieve its functionality efficiently and effectively.

In conclusion, Minishell is a valuable tool for anyone who works with the command line on a regular basis. Its flexibility and power make it an excellent choice for developers, system administrators, and power users alike.

About

Tinyshell: a command-line interface for the OS with features such as command history, redirection, pipes, environment variables, and signals, designed for efficiency.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published