项目作者: HongyuHe

项目描述 :
:shell: A Unix shell written in C.
高级语言:
项目地址: git://github.com/HongyuHe/shell.git
创建时间: 2020-08-25T21:38:58Z
项目社区:https://github.com/HongyuHe/shell

开源协议:Apache License 2.0

下载


Shell

Maintenance PRs Welcome Build Status

Introduction

This a Unix shell similar to sh, bash and zsh.
This implementation is not based on (or exploit) another shell. For example, system(3).

Implementation Details

  • This shell could run all simple commands.
  • This shell supports the cd and exit following built-in command
  • It runs sequences of 3 commands or more properly.
  • It runs pipes of 2 simple commands properly.
  • This shell can pass all valgrind checks, as well as gcc -Wall -Wextra.
  • It will print an error message on the standard output when an API function fails.
  • This shell can handle interrupts such as Ctrl+C from users properly, but regular commands will still be interrupted when the user enters Ctrl+C.
  • This implementation runs pipes of more than 2 parts consisting of sequences or pipes of simple commands.
  • This shell supports redirections, detached commands and executing commands in a sub-shell.
  • It supports the set and unset built-ins for managing environment variables.
  • It can parse the PS1 correctly and support hostname, user name and current path. ( see PROMPTING in the bash(1) manual page)
  • This shell supports simple job control: Ctrl+Z to suspend a command group, bg to continue a job in the background, and fg to recall a job to the foreground.

Example commands

  1. ## simple commands:
  2. ls
  3. sleep 5 # must not show the prompt too early
  1. ## simple commands, with built-ins:
  2. mkdir t
  3. cd t
  4. /bin/pwd # must show the new path
  5. exit 42 # terminate with code
  1. ## sequences:
  2. echo hello; echo world # must print in this order
  3. exit 0; echo fail # must not print "fail"
  1. ## pipes:
  2. ls | grep t
  3. ls | more # must not show prompt too early
  4. ls | sleep 5 # must not print anything, then wait
  5. sleep 5 | ls # must show listing then wait
  6. ls /usr/lib | grep net | cut -d. -f1 | sort -u
  1. ## redirects:
  2. >dl1 ls /bin; <dl1 wc -l
  3. >dl2 ls /usr/bin; >>dl1 cat dl2 # append
  4. <dl2 wc -l; <dl1 wc -l # show the sum
  5. >dl3 2>&1 find /var/. # errors redirected
  1. ## detached commands:
  2. sleep 5 & # print prompt early
  3. { sleep 1; echo hello }& echo world; sleep 3 # invert output
  1. ## sub-shell:
  2. ( exit 0 ) # top shell does *not* terminate
  3. cd /tmp; /bin/pwd; ( cd /bin ); /bin/pwd # "/tmp" twice
  1. ## environment variables
  2. set hello=world; env | grep hello # prints "hello=world"
  3. (set top=down); env | grep top # does not print "top=down"
  4. # custom PATH handling
  5. mkdir /tmp/hai; touch /tmp/hai/waa; chmod +x /tmp/hai/waa
  6. set PATH=/tmp/hai; waa # OK
  7. unset PATH; waa # execvp() reports failure

Syntax of built-ins

Built-in: cd <path>
: Change the current directory to become the directory specify in the argument. Your shell does not need to support the syntax cd without arguments like Bash does.

Built-in: exit <code>
: Terminate the current shell process using the specified numeric code. Your shell does not need to support the syntax exit without arguments like Bash does.

Built-in (advanced): set <var>=<value>
: Set the specified environment variable. Your shell does not need to support the syntax set without arguments like Bash does.

Built-in (advanced): unset <var> (optional)
: Unset the specified environment variable.

Error handling

This shell might encounter two types of error:

  • When an API function called by the shell fails, for example execvp(2) fails to find an executable program For these errors, this shell will print a usefu error message on its standard error. The helpe function perror(3) is used for this purpose.
  • When a command launched by the shell exits with a non-zero status code, or a built-in command encounters an error. For these errors, this shell will print a useful indicative message but this will not be tested.

Notes

  1. A shell usually supports redirections on all places of a simple command; ls > foo and >foo ls are normally equivalent. However, this shell only supports >foo ls.
  2. Within a ‘pipe’ construction, all parts should be forked, even if they only contain built-in commands. This keeps the implementation easier.
  1. exit 42 # closes the shell
  2. exit 42 | sleep 1 # exit in sub-shell, main shell remains
  3. cd /tmp # changes the directory
  4. cd /tmp | sleep 1 # change directory in sub-shell
  5. # main shell does not

Execution

  1. install dependencies on Ubuntu using the following command:

    1. sudo apt install build-essential python python-pexpect libreadline-dev flex valgrind
  2. Use make check to run tests.