Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help thank you so much. Introduction: Part 2 of the assignment is to create a new user program that can read and execute a

Need help thank you so much.

Introduction: Part 2 of the assignment is to create a new user program that can read and execute a given script file. A script file contains commands and/or logic for a Command Line Interface (CLI) for the Operating System to execute. You may already be familiar with the BASH (Bourne-Again Shell) which is a popular CLI for Linux and macOS systems. BASH has many features such as conditional logic, static and dynamic variables, pattern recognition, and the ability to run script files. It is possible to extend the xv6 console to include running script files, but for simplicity, we will just create a new and separate user program to do the work for us, and we will only implement basic functionality into our program. Create a New User Program Create a new C file called script.c and a user program called script by following the steps above for sample_program. Test that your user program was created successfully before moving on. Input Specification Your task is to create a user program that will read a script file with the correct filename extension and execute the commands inside. Your program must accept two arguments from the command line (by using argv and argc) in the following format: script [FILENAME.sh] [Optional Flag: 0/1] The flag argument is either a 0 or a 1. It is optional, so if the flag is omitted, then your program should default to using 0 as the flag. The flag lets the user choose whether or not the command is printed before its output. If the flag is 1, then print the command before running it. If the flag is 0, or omitted, then do not print the command, just run the command. The script file will contain commands for your program to execute. Each command will be on its own line. The script files will not contain conditional logic, variables, globbing patterns, or strings in or . For simplicity, you may assume that each command does not contain more than 10 arguments. An example of a script file, test.sh, is given below: Example: echo Hello World! echo Running Test Script! sample_program echo Making directory: myDir mkdir myDir ls rm myDir echo Exiting Test Script! test.sh We can run this script with the any of the following commands: script test.sh will run the commands without printing the commands themselves, script test.sh 0 will run the commands without printing the commands themselves, script test.sh 1 will print the commands and then run them. The file name must have the extension .sh. If it does not, then your program must print an error message and exit. If the file has the correct extension, then your program will try to open the file. If there is an error opening the file, then your program must print an error message and exit. If too few or too many arguments are passed to the script program, display a usage message and exit. You do not need to perform any error checking on the contents of the script file. Adding New Files to xv6 In xv6, it is not enough to just create a file in xv6s directory to add it to the xv6 file system. We must also make changes in the makefile so that when we compile xv6, our new file is added to the system. Create a new file called test.sh in your xv6 folder. In test.sh, write the commands from the above example. Now open the xv6 Makefile. Add the name of the file, test.sh, under the label fs.img (about line 175, after the UPROGS list) and on the following line, just like the file README. Now, run xv6 and check that your new file, test.sh, has been added by running the ls command. If you make changes to your test.sh file, you must run make clean before running make qemu to force xv6 to rebuild its file system if you want your changes to be visible in xv6. Open, Read, and Close The open and read system calls will be used to open and then read a given script file. They have very similar syntax to the open and read system calls in the POSIX library. After you check the file name, your program will open the file by using the open system call. The function prototype is given below: int open(char* path, int mode); where path is a string containing the filename (or the full path to the file including the filename), and mode is the read/write permissions with which to open the file. You should include the file fcntl.h in your script.c file as it contains the modes: O_RDONLY, O_WRONLY, O_RDWR, and O_CREATE. The open system call will return the positive integer file descriptor, fd, on success and a negative integer on failure. The read system call is used to read characters from a file. The function prototype is given below: int read(int fd, char* buf, int count); where fd is a valid file descriptor, buf, is the starting address to a character buffer array of at least count elements, and count is the maximum number of characters to be read from the given file, fd. The read system call will read at most count characters from fd and place them in the character array given by buf. The read system call returns a positive integer of the number of characters read from the file on success (max is count), returns 0 if end of file is reached, and returns a negative integer on failure. Once you have read all of the input, close the file by using the close system call: int close(int fd); Script User Program To use exec, you will need to pass an argument vector, an array of strings, that contain the command to be run. The script user program will read a script file, and execute each line by populating an argument vector, and then calling fork() and exec(). For example, to run the command: echo Hello World! you must create an argument vector that has three elements: char** myArgv[3] = {echo, Hello, World!}; Then, pass myArgv to the exec system call. You will need to create the algorithm that will read the script file, put each word (separated by spaces) into its own string, and then when a new line character is read, it will put each string into a myArgv array, then fork, and then exec passing myArgv to exec.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions