Network Programming Project 1 - NPShell
NP TA
Deadline: Wednesday, 2024/3/27 23:55
1 Introduction
In this project, you are asked to design a shell named npshell. The npshell should support the
following features:
1. Execution of commands 2. Ordinary Pipe
3. Numbered Pipe
4. File Redirection
More details will be defined in Section 3.
2 Scenario of using npshell 2.1 Structure of Working Directory
Example:
working_dir
|-- bin
| |-- cat
| |--ls
| |-- noop
| |-- number
| |-- removetag
| |-- removetag0 # Same as removetag, but outputs error messages to STDERR
|-- test.html
|-- npshell
2.2 Scenario
The following is a scenario of using the npshell.
bash$ ./npshell
% printenv PATH
bin:.
% setenv PATH bin
% printenv PATH
bin
% ls
# execute your npshell
# initial PATH is bin/ and ./
# set PATH to bin/ only
# Executables may be added to or removed from bin/
# A program that does nothing
# Add a number to each line of input
# Remove HTML tags and output to STDOUT
bin npshell test.html
% ls bin
cat ls noop number removetag removetag0
% cat test.html > test1.txt
% cat test1.txt
<!test.html>
<TITLE>Test</TITLE>
<BODY>This is a <b>test</b> program
for ras.
</BODY>
% removetag test.html
Test
This is a test program
for ras.
% removetag test.html > test2.txt
% cat test2.txt
Test
This is a test program
for ras.
% removetag0 test.html
Error: illegal tag "!test.html"
Test
This is a test program
for ras.
% removetag0 test.html > test2.txt
Error: illegal tag "!test.html"
% cat test2.txt
Test
This is a test program
for ras.
% removetag test.html | number
2 Test
3 This is a test program
4 for ras.
% removetag test.html |1 # pipe STDOUT to the first command of next line
% number # STDIN is from the previous pipe (removetag)
2 Test
3 This is a test program
4 for ras.
% removetag test.html |2 # pipe STDOUT to the first command of next next line 2
% ls
bin npshell test1.txt test2.txt test.html
% number # STDIN is from the previous pipe (removetag)
2 Test
3 This is a test program
4 for ras.
% removetag test.html |2 # pipe STDOUT to the first command of next next line
% removetag test.html |1 # pipe STDOUT to the first command of next line
# (merge with the previous one)
% number # STDIN is from the previous pipe (both two removetag)
2 Test
3 This is a test program
4 for ras.
7 Test
8 This is a test program
9 for ras.
% removetag test.html |2
% removetag test.html |1
% number |1
% number
2 2 Test
3 3 This is a test program
4 4 for ras.
% removetag test.html | number |1
% number
2 2 Test
3 3 This is a test program
4 4 for ras.
% removetag test.html |2 removetag test.html |1 # number pipe may occur in middle
% number |1 number
2 2 Test
3 3 This is a test program
4 4 for ras.
7 7 Test
7 Test
8 This is a test program
9 for ras.
8 This is a test program
9 for ras. 10
% ls |2
% ls
bin npshell test1.txt test2.txt test.html
% number > test3.txt
% cat test3.txt
1 bin
2 npshell
3 test1.txt
4 test2.txt
5 test.html
% removetag0 test.html |1
Error: illegal tag "!test.html" # output error message to STDERR
% number
2 Test
3 This is a test program
4 for ras.
% removetag0 test.html !1
# pipe both STDOUT and STDERR
# to the first command of the next line
% number
1 Error: illegal tag "!test.html"
3 Test
4 This is a test program
5 for ras.
% date
Unknown command: [date].
# TA manually moves the executable "date" into $working_dir/bin/
% date
Wed Mar 13 10:44:39 PM CST 2024
% exit
bash$
3 Specification
3.1 NPShell Behavior
1. Use ”% ” as the command line prompt. Notice that there is one space character after %. 2. The npshell parses the inputs and executes commands.
3. The npshell terminates after receiving the exit command or EOF.
4. There will NOT exist the test case that commands need to read from STDIN.
3.2 Input
1. The length of a single-line input will not exceed 15000 characters.
2. The length of each command will not exceed 256 characters.
3. There must be one or more spaces between commands, arguments, pipe symbol (|), and redi- rection symbol (>), but no spaces between pipe and numbers for numbered-pipe.
Examples:
% ls -l | cat
% ls > hello.txt
% cat hello.txt |4 # no space between "|" and "4"
% cat hello.txt !4 # no space between "!" and "4"
4. Only English alphabets (uppercase and lowercase), digits, space, newline, ”.”, ”-”, ”:”, ”>”, ”|”, and ”!” may appear in test cases.
3.3 Built-in Commands
1. Format
• setenv [var] [value]
Change or add an environment variable.
If var does not exist in the environment, add var to the environment with the value value.
If var already exists in the environment, change the value of var to value.
Examples:
% setenv PATH bin # set PATH to bin
% setenv PATH bin:npbin # set PATH to bin:npbin
• printenv [var]
Print the value of an environment variable.
If var does not exist in the environment, show nothing.
Examples:
% printenv LANG
en_US.UTF-8
% printenv VAR1
% setenv VAR1 test
% printenv VAR1
test
• exit
Terminate npshell.
# show nothing if the variable does not exist
2. Built-in commands will appear solely in a line.
3. Built-in commands will not pipe to other commands, and no commands will pipe to built-in commands.
3.4 Unknown Command