Answered step by step
Verified Expert Solution
Question
1 Approved Answer
GREAT SKELETON CODE TO USE: /* strtokeg - skeleton shell using strtok to parse command line usage: strtokeg reads in a line of keyboard input
GREAT SKELETON CODE TO USE:
/* strtokeg - skeleton shell using strtok to parse command line usage: strtokeg reads in a line of keyboard input at a time, parsing it into tokens that are separated by white spaces (set by #define SEPARATORS). can use redirected input if the first token is a recognized internal command, then that command is executed. otherwise the tokens are printed on the display. internal commands: clear - clears the screen quit - exits from the program ******************************************************************** version: 1.0 date: December 2003 author: Ian G Graham School of Information Technology Griffith University, Gold Coast ian.graham@griffith.edu.au copyright (c) Ian G Graham, 2003. All rights reserved. This code can be used for teaching purposes, but no warranty, explicit or implicit, is provided. *******************************************************************/ #includeCreate a shell program Use the Unix environment you installed in the previous homework to write a C or C++ program called mysh replaces the command shell in Unix. After started, it prints a prompt "#" and reads a command line terminated by newline. This line should be parsed out into a command and all its arguments. In other words, tokenize it. You may assume that the only supported delimiter is the whitespace character (ASCII character number 32). You do not need to handle "special" characters. Do not worry about handling quotation marks, backslashes, and tab characters. This means your shell will be unable support arguments with spaces in them. For example, your shell will not support file paths with spaces in them. You may set a reasonable maximum on the number of command line arguments, but your shell should handle input lines of any length. Implement built-in commands Your shell should be able to interpret the following commands # changedir directory Short for change directory. It is used to change the current directory (which must be an internal variable) to the specified one. If the specified directory does not exist, it should keep the old directory and write an error message. #whereami Prints the current directory. #lastcommands [-c] Without the parameter, prints out the recently typed commands (with their arguments). # quit terminates the mysh shell # run program [parameters] The argument "program" is the program to execute. If the argument starts with a "/" (such as /usr/bin/xterm, the shell should interpret it as a full path. Otherwise, the program will be interpreted as a relative path starting from the current directory. The program will be executed with the optional "parameters". It uses forkl) + exec() to start the program with the corresponding parameters, and waits until the program terminates (use the waitpid() call). For instance run /usr/bin/xterm -bg green would bring up a terminal with a green background. The prompt would not return until the terminal is closed. Display an error message if the specified program cannot be found or cannot be executed. # background program [parameters] It is similar to the run command, but it immediately prints the PID of the program it started, and returns the prompt. # exterminate PID Immediately terminate the program with the specific PID (presumably started from this command line interpreter). Use the kill() function call to send a SIGKILL signal to the program. Display success or failure. To help you, you might want to read some of the readings associated with this homework at the class webpage. I have also linked from the webpage a code segment which might help you in reading the commands. Create a shell program Use the Unix environment you installed in the previous homework to write a C or C++ program called mysh replaces the command shell in Unix. After started, it prints a prompt "#" and reads a command line terminated by newline. This line should be parsed out into a command and all its arguments. In other words, tokenize it. You may assume that the only supported delimiter is the whitespace character (ASCII character number 32). You do not need to handle "special" characters. Do not worry about handling quotation marks, backslashes, and tab characters. This means your shell will be unable support arguments with spaces in them. For example, your shell will not support file paths with spaces in them. You may set a reasonable maximum on the number of command line arguments, but your shell should handle input lines of any length. Implement built-in commands Your shell should be able to interpret the following commands # changedir directory Short for change directory. It is used to change the current directory (which must be an internal variable) to the specified one. If the specified directory does not exist, it should keep the old directory and write an error message. #whereami Prints the current directory. #lastcommands [-c] Without the parameter, prints out the recently typed commands (with their arguments). # quit terminates the mysh shell # run program [parameters] The argument "program" is the program to execute. If the argument starts with a "/" (such as /usr/bin/xterm, the shell should interpret it as a full path. Otherwise, the program will be interpreted as a relative path starting from the current directory. The program will be executed with the optional "parameters". It uses forkl) + exec() to start the program with the corresponding parameters, and waits until the program terminates (use the waitpid() call). For instance run /usr/bin/xterm -bg green would bring up a terminal with a green background. The prompt would not return until the terminal is closed. Display an error message if the specified program cannot be found or cannot be executed. # background program [parameters] It is similar to the run command, but it immediately prints the PID of the program it started, and returns the prompt. # exterminate PID Immediately terminate the program with the specific PID (presumably started from this command line interpreter). Use the kill() function call to send a SIGKILL signal to the program. Display success or failure. To help you, you might want to read some of the readings associated with this homework at the class webpage. I have also linked from the webpage a code segment which might help you in reading the commands#include #include #define MAX_BUFFER 1024 // max line buffer #define MAX_ARGS 64 // max # args #define SEPARATORS " \t " // token sparators int main (int argc, char ** argv) { char buf[MAX_BUFFER]; // line buffer char * args[MAX_ARGS]; // pointers to arg strings char ** arg; // working pointer thru args char * prompt = "==>" ; // shell prompt /* keep reading input until "quit" command or eof of redirected input */ while (!feof(stdin)) { /* get command line from input */ fputs (prompt, stdout); // write prompt if (fgets (buf, MAX_BUFFER, stdin )) { // read a line /* tokenize the input into args array */ arg = args; *arg++ = strtok(buf,SEPARATORS); // tokenize input while ((*arg++ = strtok(NULL,SEPARATORS))); // last entry will be NULL if (args[0]) { // if there's anything there /* check for internal/external command */ if (!strcmp(args[0],"clear")) { // "clear" command system("clear"); continue; } if (!strcmp(args[0],"quit")) // "quit" command break; // break out of 'while' loop /* else pass command onto OS (or in this instance, print them out) */ arg = args; while (*arg) fprintf(stdout,"%s ",*arg++); fputs (" ", stdout); } } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started