关键词 > 131A/java代写

COMPUTER SCIENCE 131A OPERATING SYSTEMS PROGRAMMING ASSIGNMENT 1

发布时间:2022-01-21

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


COMPUTER SCIENCE  131A

OPERATING SYSTEMS

PROGRAMMING ASSIGNMENT  1

UNIX SHELL


Description

You will write a simplified version of a Unix shell, a command-line interpreter. A shell is a      program that allows users to interact with a computer’s operating system via a set of typed       commands. Using the shell, one can open and read files, search files, navigate directories, and

more.

This simple shell will consist oftwo components that must be implemented:

1.   The command line interface that parses and executes user input.

2.   The object oriented decomposition of an abstract command represented using the pipes andfilters design pattern.

Command Line Interface

The command line interface will be a read-eval-print-loop (REPL). The shell must support the following commands typed into the REPL:

Command                 Description

pwd

Returns current working directory.

ls

Lists the files in the current working directory

cd <dir>

Changes current working directory to <dir>.

cat <file>

Reads <file>’s contents into pipe or stdout.

head/tail

Returns up to the first/last 10 lines from piped input.

grep <query>

Returns all lines from piped input that contain <query>.

wc

Counts the number of lines, words, and characters in the piped input.

uniq

Removes any line from the piped input that is the same as the previous line.

cc <r>

Applies a caesar cipher to the piped input with the provided rotation <r>.

exit

Terminates the REPL.

Commands can also be combined to form compound commands using the following operators. When a command is part of a compound command, it is also known as a subcommand.

Operator                              Description

[cmd1] | [cmd2]

| chains commands together such that the output of [cmd1] becomes the input of [cmd2]. This is referred to as a pipe.

[cmd1] > <file>

> redirects the output of <cmd1> into <file>.

Terminology:

-    Parameters are the arguments listed to the right ofthe subcommands, which define their behavior.

-     Input are contents delivered to a command by another command, using a pipe.

-     Output are contents emitted by a command, also using a pipe.

For example, cat requires the <file> parameter, but it does not take any piped input. It does      produce output. Any parameters that these commands take are labeled with angle brackets < > and are required.

Implementation Details

Pipes and Filters Paradigm

Apipeline is a set ofprocesses chained together so that the output of each process is passed into  the input ofthe next. In this Unix shell, user commands can be expressed as a pipeline, which is  a convenient abstraction used for the purpose of evaluation. These pipelines are created by using the pipe operator, defined from left to right.

subcommand1 | subcommand2 | subcommand3

In order to evaluate a compound command, this program will first separate the command into   subcommands represented as Filters. Next, the Filters must be connected with input and output Pipes. Pipes allow Filters to pass lines of data between them. When the filters are executed, they take their input from their input Pipe (if one exists) and write their output to     their output Pipe (if one exists).

Every pipeline must either end in a RedirectFilter or a PrintFilter. Ifthe pipeline command does not specify a redirection file, then a PrintFilter should be appended to a pipeline that should print its output to stdout (i.e. the Eclipse console).

Subcommand Categories

The first category is commands that cannot have input. These subcommands can only appear at the beginning of a compound command:

pwd, ls, cat, cd

Next are commands that must have input. Unlike the previous subcommands these can not appear at the beginning of a compound command, and must be piped into:

head, tail, grep, wc, uniq, cc

Finally, cd also cannot have output. It can only appear at the end of a compound command. That is, it cannot pipe into anything.

cd

This also applies to the redirection operator >.

Read Eval Print Loop

The REPL runs in the Eclipse Console by nesting a Scanner in a while loop. The user can        terminate the REPL by typing the command “exit” which is already implemented for you. The    shell will exist in a particular directory in your file system. This is known as the current working directory. The shell starts in your Eclipse project directory.

Subcommand Details

The current working directory of the shell can be retrieved using the get() method of the        filter package’s CurrentWorkingDirectory class. You can update the shell’s current working directory to be a new String using CurrentWorkingDirectory.setTo().    See the file for more details. The cd subcommand must support the special directories “ .” (the    current directory) and “ ..” (the parent directory). It must allow for chaining of directory              movement. For example cd dir1/dir2/dir3.

The wc subcommand counts the number of lines, words and characters in the piped input, and    outputs them as a string separated by spaces. A word is a sequence of characters not separated by a space. For example, an input with two lines “I am”, “input.” has 3 words “I”,  “am”,     and “input.”, 2 lines, and 10 characters. This should be output as “2 3 10”.

The cc <r> subcommand takes a required parameter <r> and rotates every alphabetic            character about the English alphabet by <r> places . Ifthis operation shifts a character beyond   the range of the alphabet, it must wrap back around. Any uppercase and lowercase letters should remain uppercase and lowercase after a caesar cipher translation. Characters that are not letters

should remain unchanged. For example, cc 3 when applied to the input “Aby123?” should output “Deb123?”. Notice how “y” rotates 3 places: it goes to “z”, then “a”, then “b”.

Error Handling

The shell should not crash when an error is encountered, and instead display an appropriate error  message. Each error message can be found in the Message enum and must be called from there. The shell must detect the following errors and display the appropriate message:

Message                                      Trigger

COMMAND_NOT_FOUND

Executing an unlisted command.

FILE_NOT_FOUND

Reading from a file that does not exist

DIRECTORY_NOT_FOUND

Changing current directory to one that does not exist

REQUIRES_PARAMETER

Failing to pass a parameter to a command that requires one

REQUIRES_INPUT

Failing to pipe into a command that requires input

CANNOT_HAVE_INPUT

Piping into a command that does not take input

CANNOT_HAVE_OUTPUT

Piping from a command that does produce output

Note: Given the descriptions of commands and errors, you may assume that your commands will not be provided too many parameters. That is, a command that receives no parameters (like pwd) will not be provided with any and a command that receives 1 parameter (like grep) will not be    provided with more than one.

For this reason, you can parse a command with a parameter as: [command] [space] [parameter].

Design Strategy

SequentialCommandBuilder

You are not required to implement this class, but it is recommended you do. This class has three methods which provide an outline of how you could go about converting a command String into a List oflinked SequentialFilters:

createFiltersFromSubCommand: String  List<SequentialFilter>

The goal of this method would be to take a String command and produce a List of SequentialFilters.

Hint: Your REPL should use this method to evaluate commands.

constructFilterFromSubCommand: String  SequentialFilter

The goal ofthis method would be to take a String subcommand and return an instance ofthe appropriate SequentialFilter. For example, “cat file.txt” would       return an instance of CatFilter.

Hint: You can use the constructors ofyour Filters to your advantage.

linkFilters: List<SequentialFilter>  boolean

The goal ofthis method would be to take a List of SequentialFilter objects corresponding to subcommands and link them together. To understand what linking   means, make sure to read SequentialFilter.

Hint: Some errors can be caught in constructFilterFromSubCommand while     others can be caught in this method. Correctly deciding which errors should be caught at this stage will lead to an elegant solution.

Note on Exceptions

As noted above, your REPL cannot crash ifthe user types in an incorrect command. However,     you can throw an exception in one class and catch it in another, allowing your REPL to continue. Throwing an exception in one class A and catching it in class B provides an easy way to pass a     message from an instance ofA to an instance ofB.

Implementing Error Handling

One could try to detect every type of user error in SequentialCommandBuilder and/or       SequentialREPL. However, this will involve quite detailed code most likely addressing each command separately for both syntax and piping errors. A syntax error is an error unrelated to       piping, say something like failing to pass in a required parameter or passing in a nonexistent file. Exceptions would allow us to detect these types of errors in the Filters which could throw an  exception to the class that creates them. The creating class would then catch the exception and     display the appropriate error message.

Grading

There are no hidden unit tests that will be used for grading this assignment. You should use the  tests to make sure you have the correct behavior and are printing the appropriate Strings for   each of the error cases. For example, you will see that the tests make the assumption mentioned in the note on Error Handling on page 4.

You need to use CurrentWorkingDirectory.FILE_SEPERATOR anytime where you need to build file paths. Do not hardcode the file path separator to be “/” or “\”.

You are only allowed to modify the files in the sequential package. You cannot modify SequentialFilter and Pipe.

Make sure you import and export the Eclipse project properly. The submitted project should have a structure that looks exactly like the skeleton. An improperly structured project will receive a 0.  The project needs to be renamed FirstnameLastnamePA1 (make sure to use Eclipse’s Refactor >  Rename). The zip file you submit should be named FirstnameLastnamePA1.zip.

Notes on Tests

●   To run the tests run AllSequentialTests.java. Do not run each ofthe other 4 test files individually.

●   The files will create files to pass into your shell and your shell will output files as part of   the tests. To save all the files that are created over the course of the tests, set                        AllSequentialTests.DEBUGGING_MODE to true. Your code should still work if it is set to false.

●   You may see AllSequentialTests.java throw an Exception when you have   AllSequentialTests.DEBUGGING_MODE set to false. This is most likely due   to the fact that you have left a Java reader/writer object open on a test data file which did not allow the tests to properly delete the file. To fix this issue, check that you open and    close your reader/writer objects properly in the related Filters.

Remarks

This is an individual assignment. Any sign of collaboration will result in a 0 score for the assignment. Please check the academic integrity policies in the syllabus of the course.

Late policy: Please check the syllabus for the late submission policies of the course.