Fall 2014
Runs on Linux/macOS
Compile with make
, run with ./shell
Exit with exit
, quit
, or Ctrl-D at the start of a line
- Execute multiple commands per line, separated by
;
- Trim excess white space
- Ignore consecutive semicolons rather than returning a syntax error
- Basic file redirection:
<
,>
,>>
- Basic piping:
|
- Checked for memory leaks: Valgrind
- Tilde expansion:
~
is interchangeable with user's $HOME directory - Color prompt:
- Username, hostname, current working directory
- Better redirection and piping:
-
cmd < in > out
-
cmd > out1 flag1 flag2
-
cmd1 | cmd2 | cmd3
-
cmd1 | cmd2 > out1
-
<<
,<<<
, redirection forSTDERR
and other file descriptors
-
- Handle
EOF
(Ctrl-D) - Handle
SIGINT
(Ctrl-C)- Re-print the prompt
- Command history & navigation
- Directory history & navigation (
cd -
) - Wildcard
*
- Tab completion: for files and commands
- Logic operators:
&&
,||
,!
- Background processes:
&
- Fix: Segfault when exiting with EOF after using
~
expansion, redirection, or pipes - Allow redirect and pipe symbols to be adjacent to commands or flags (not separated by space)
- Allow tilde expansion to work in conjunction with redirection e.g.
ls ~ > out
shell.h
- static void sigHandler(int signo)
- Allows
SIGINT
to interrupt the shell's commands rather than the shell itself
- Allows
- void redirect()
- Handles the redirection (calls to
dup2
) of file descriptors
- Handles the redirection (calls to
- void safe_exec()
- Executes the command in
argv
withexecvp
and safely frees all allocated memory before exiting
- Executes the command in
- void executePipe(int pipeIndex)
- A new child executes the first command from
argv
withsafe_exec
, writing into the pipe - The parent executes the rest of the command with
executeMisc
while reading out of the pipe
- A new child executes the first command from
- void executeMisc()
- Searches for any pipes
|
in global varargv
and callsexecutePipe
if it finds one. If not, it redirects ifparseInput
found a redirect symbol and finally callssafe_exec
- Searches for any pipes
- void execute()
- Handles
exit
(orquit
) andcd
commands, otherwise forking and the new child callsexecuteMisc
- Handles
- char ** parseInput(char *input, char *delim)
- Returns a dynamically allocated char **argv of input arguments separated by
delim
and trimmed of whitespace and semicolons - Redirect symbols and the first argument afterwards are not added to argv. They set booleans
redir_out
and/orredir_in
to true and the file (if valid) is stored in a global variable.
- Returns a dynamically allocated char **argv of input arguments separated by
- void shell()
- Continuously loops through the shell procedures: reading and parsing user input and then executing the commands
- If the return value of
fgets
is null, the user has sent an EOF at the start of a line and the shell exits normally