Operating System代写:CSE320 SFISH Shell Part1

代写一个类似Shell功能的命令行工具,整个作业分为五个部分,第一部分主要是处理用户的输入,完成相关的交互逻辑。

SFISH Shell

Write a C program named sfish (an acronym for System Fundamentals Interactive SHell) that functions similar to other shells that you are familiar with, like GNU’s Bash. You’re welcome to study the code for bash and other shells, but the code you submit MUST be your own!

Your shell, if completed correctly, will have the:

  • Ability to execute builtins such as set , exit , etc.
  • Ability to execute programs such as ls , grep , etc.
  • Ability to redirect input/output for processes started by the shell.
  • Ability to chain processes together
  • Ability to control and interact with processes running in the foreground or the background

When you start your shell, you should be able to type commands such as these and see their output:

$ ./sfish
sfish> ls
# output from ls
sfish> ls -l
# verbose output from ls
sfish> exit
$

The # comments represent the output of whatever command your shell is executing.

Note that commands like ls are (usually) just executable programs. However, there are shell specific builtin commands discussed below. In general, however, the shell’s job is to launch programs and coordinate their input and output.

You do not need to re-implement any binaries that already exist, such as ls. You simply needto launch these programs appropriately and coordinate their execution.

If you are unsure if something is a builtin or not, you can find out by using the command type in your Linux shell.

$ type cd
cd is a shell builtin
$ type mkdir
mkdir is /bin/mkdir
$ type exit
exit is a shell builtin

Part I: User Input and Builtins

The base code in sfish.c is a simple echo shell that reads user input and prints out the input. When this part is completed, users should be able to type builtin commands which sfish will execute. In this part, you will modify the base code to implement the shell builtin commands.

GNU Readline

You should use the GNU Readline library to help simplify the reading and parsing of user input. GNU Readline is a software library that provides line-editing and history capabilities for interactive programs with a command-line interface, such as Bash. It allows users to move the text cursor, search the command history, control a kill ring (a more flexible version of a copy/paste clipboard) and use tab completion on a text terminal.

You are not required to use GNU Readline. However, it is HIGHLY RECOMMENDED.

You will have to install some of the missing components of the GNU Readline library.
sudo apt install libreadline-dev

Builtin Commands

A builtin command is a command that is executed directly by the shell itself, without the invocation of another program. Builtins are necessary because they add functionality that is impossible or inconvenient to obtain with executable programs. Builtins are functions defined within the shell’s source, but they must be fork(2)-ed and called in a child process in order to redirect their output (implemented later in this assignment).

1
2
3
4
5
6
7
input = readline()
if (builtin_func = get_builtin(input))
fork()
if child:
builtin_func()
else:
wait_for_child()

The pseudocode above outlines how builtins should be executed.

The following features must be implemented:

  • Implement simple command parsing in your shell. It is recommended that you use readline(3) to achieve this. Upon reading a line, determine if it is a “builtin” command and execute it.
  • Implement the following builtins:
    • help
      • Print a list of all builtin’s and their basic usage in a single column. Type help in bash to get an idea.
    • exit
      • Exits the shell by using the exit(3) function.
    • cd
      • Changes the current working directory of the shell by using the chdir(2) system call.
      • cd - should change the working directory to the last directory the user was in.
      • cd with no arguments should go to the user’s home directory which is stored in the HOME environment variable.
      • handle the targets cd . and cd .. properly.
    • pwd
      • Prints the absolute path of the current working directory which can be obtained through the getcwd(3) function.
    • prt
      • Prints the return code of the command that was last executed.
    • chpmt: Change prompt settings
      Before implementing this builtin, modify your shell prompt to print more details such as the username (using the USER environment variable), machine name (using gethostname(2) ), and the current working directory of the shell (relative to the user’s home directory). If the shell is in the user’s home directory, the current working directory should be displayed as ~ .
    • chclr: Change prompt colors
      • Usage: chclr SETTING COLOR BOLD
      • Valid values for SETTING are:
        • user : The user field in the prompt.
        • machine : The context field in the prompt.
      • Valid values for COLOR are:
        • red : ANSI red.blue : ANSI blue.
        • green : ANSI green.
        • yellow : ANSI yellow.
        • cyan : ANSI cyan.
        • magenta : ANSI magenta.
        • black : ANSI black.
        • white : ANSI white.
      • To toggle BOLD use:
        • 1 : Bold Enabled
        • 0 : Bold Disabled