Homework 3, DASH (or whatever you wish).
Short Description:
Create a program that will act as a simple shell.
Goals
When you finish this homework, you should have:
- Written a program that uses fork, exec, dup, wait, ...
- You will probably get better with an array of pointers as well :), but that is a side effect.
Formal Description
Write a simple command line interpreter. Call it whatever you wish, but for my examples, I will use dash.
For 1/4 credit your program should
- prompt
- read the command line given
- execute the command, including passing arguments
- wait for the command to exit
- Return to step 1.
An example of this might be:
dash -> ls -alrt /
total 24
drwxr-xr-x. 1 root root 0 Jan 23 2024 srv
lrwxrwxrwx. 1 root root 8 Jan 23 2024 sbin -> usr/sbin
drwxr-xr-x. 1 root root 0 Jan 23 2024 mnt
drwxr-xr-x. 1 root root 0 Jan 23 2024 media
...
dash ->
You should support the built in command quit to exit the dash shell.
For 1/4 credit you should support I/O redirection to a file.
- If the command contains a >, redirect the output to the value immediately following the >.
- This file should be created with rw permissions for the user.
- It should be truncated if it has data in it.
- If the command contains a <, redirect the input from the value immediately following the <
dash -> cat < dash.cpp > newDash.cpp -n
dash -> more newDash.cpp
1 #include <iostream>
2 #include <sys/wait.h>
...
dash->
This should read from dash.cpp and write to newDash.cpp, but it will number the lines in the new file.
For 1/4 credit support pipes between processes
- 1/8 for two commands: cmd1 args | cmd2 args
- 1/8 for more than two commands: cmd1 args | cmd 2 args | ... | cmdn args
dash -> ls -a | sort -d | grep cpp
dash.cpp
two.cpp
dash->
For the final 1/4 credit make sure that both i/o redirection and pipes work.
- But i only on the first command
- And o only on the last command.
dash -> grep -o '[[:alpha:]]*' < dash.cpp | sort -n | uniq > words
dash -> cat words
ParseCmd
arg
args
auto
back
...
dash ->
Notes:
- I only wrote part of this so you are on your own for part.
- You may use vectors or any other item from the standard library.
- I needed to pass an array of characters by reference:
-
void ParseCmd(string cmd, char ** & args,...
- But no memory leaks!
- I would build a struct to hold a command to be executed
- The command
- A vector of the arguments, or perhaps a char** of the arguments
- A flag indicating if output is redirected
- A file name to redirect output to
- A flag indicating if input is redirected
- A file name to redirect input from
- I think I would build a vector of these then, indicating that they should be connected with a pipe.
- Oh and you probably need to catch the cd command and do that in the shell.
- Look at
chdir
, but again only in the shell.
- If you change the directory in a subshell, it will not change the directory in the parent shell, which is what you want.
Required Files
A single c++ source code file that implements the program.
Submission
Submit the assignment to the D2L folder Homework 3 by the due date.