Question & Answer: Hello everyone, I need some help for this question, and how to create a shell in C++……

Hello everyone, I need some help for this question, and how to create a shell in C++.


Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Hello everyone, I need some help for this question, and how to create a shell in C++……
Order Essay

Your shell program must provide services to support the following commands and features:

Note: The commands are shown in uppercase font, but don’t have to be programmed that way. The parameters to the commands are enclosed between the symbols < and >, but the symbols are not actually part of the parameters.

(2 marks) STOP: Terminates execution of the current toyshell session.

(2 marks) SETSHELLNAME <shell_name>: Sets the shell name in the toyshell command prompt to <shell_name>. For example, a sample command prompt is shown below:


In this command prompt, there are three parts. The first part, tsh, is the shell name. The second part, [10], is the number of commands that have been entered in the current session (i.e., since the last time toyshell was run). And the third part, :, is the terminator. If no shell name is defined, toyshell should be the default shell name.

(2 marks) SETTERMINATOR <terminator>: Sets the terminator in the toyshell command prompt to <terminator>. If no terminator is defined, toyshell should use -> as the default terminator.

(4 marks) HISTORY: Lists the commands that have been entered in the current session (i.e., since the last time toyshell was run). The maximum number of commands in the history list should be set to 10 as the default.

(8 marks) ! <n>: Re-executes a command that has been previously executed in the current session. The command will execute the n-th command in the history list. For example, the command ! 6 will cause the 6-th command to be executed again.

(10 marks) NEWNAME <new_name> | <new_name> <old_name>: Manages the alias list. The first option deletes a previously defined alias. The second option defines an alias for another command. For example, the command NEWNAME mymove deletes the alias for mymove, and the command NEWNAME mycopy cp defines mycopy as the alias for the cp command. If an alias for a command already exists, then the new alias replaces the old alias. The maximum number of aliases in the alias list should be set to 10 as the default.

(16 marks) Alias Substitution: When an alias is detected in a command, the old name should be substituted into the command before the command is executed. For example, assume the following aliases are defined as shown below:

aa ls –l

bb grep toyshell

cc grep .cpp

dd aa | bb

Then, for the following sequence of commands at the command prompt:


aa | bb

aa | bb | cc

dd | cc

alias substitutions should result in the following sequence of commands being executed:

ls –l

ls –l | grep toyshell

ls –l | grep toyshell | grep .cpp

ls –l | grep toyshell | grep .cpp

(4 marks) NEWNAMES: Outputs all the aliases that have been defined. Each pair of names should be shown on one line. For example, the possible aliases for a few commands are shown below:

mymove mv

mycopy cp

chkshell ls –l | grep toyshell.cpp

(4 marks) SAVENEWNAMES <file_name>: Stores all currently defined aliases in the file <file_name>.

(6 marks) READNEWNAMES <file_name>: Reads all aliases in the file <file_name> and adds them to the aliases defined in the current session. If a duplicate is found in the file <file_name>, it should be ignored.

(2 marks) <UNIX_command>: Executes the UNIX command <UNIX_command>, corresponding to any valid UNIX command. One approach to implementing this is to use the system function. If the first token on a command line is not a built-in command, assume that it is a UNIX command.

(10 marks) Error handling: Your approach should effectively identify and recover from errors. For example, bad input and/or the inability to execute a command should not cause toyshell to crash.

Note: You must handle all the built-in commands with exactly the same syntax as shown above. Thus, an important part of the toyshell program will be to parse commands entered at the command prompt to break them down into their component parts. Once a command has been parsed, the component parts can be checked to ensure that a valid command has been entered and that it adheres to the required syntax. One approach to effective parsing is to use the C-string strtok function (to be covered in detail in the second lecture and the first lab).

Expert Answer


#include <stdio.h>

#include <signal.h>

#include <sys/types.h>

#include <ctype.h>

//This method looks up the input command against the lookup for

//validity purposes

bool FunctionLookUp(char *argv, char *Lookup[])


bool match = false;

for(int i = 0; i < 10; i++){

if(*Lookup[i] == *argv){

match = true;



return match;


void parse(char *line, char **argv)


while (*line != ‘’) { /* if not the end of line ……. */

while (*line == ‘ ‘ || *line == ‘t’ || *line == ‘n’)

*line++ = ‘’; /* replace white spaces with 0 */

*argv++ = line; /* save the argument position */

while (*line != ‘’ && *line != ‘ ‘ &&

*line != ‘t’ && *line != ‘n’)

line++; /* skip the argument until … */


*argv = ‘’; /* mark the end of argument list */


bool CheckForwardSlash(char temp[], int size)


bool present = false

for(int i = 0; i < size; i++){

if(temp[i] == ‘/’){

present = true;




return present;


void execute(char **argv)


pid_t pid;

int status;

if ((pid = fork()) < 0) { /* fork a child process */

printf(“*** ERROR: forking child process failedn”);



else if (pid == 0) { /* for the child process: */

if (execvp(*argv, argv) < 0) { /* execute the command */

printf(“*** ERROR: exec failedn”);




else { /* for the parent: */

while (wait(&status) != pid) /* wait for completion */





void main(void)


char line[1024]; // The input line

char *argv[3]; // An array of pointers to the tokens (the tokens were parsed

// from the input line)

bool check1, check2 = false;

//A look-up table that contains some of the valid commands

char *LookUp[11] = {“emacs”, “kill”, “bye”, “jobs”, “fg”, “chmod”, “cd”, “help”, “cat”, “cp”};

while (1) { // repeating….

printf(“tish >> “); // the prompt

gets(line); // read in the command line


parse(line, argv); // parse the line

if (strcmp(argv[0], “bye”) == 0) // exit if the user enters bye


//Input Validation//


//If the input is just a white space character, continue with the next iteration of the loop

if (isspace(argv[0]))

continue; //If it is some sort of a white-space character,

//skip the current iteration of the while loop

//Call a function that checks for the validity of the input command

check2 = FunctionLookUp(argv, LookUp);

if(check2 == false){

fprintf(“Invalid Commandn”);



//Test argv[0] for invalid internal commands. Check for Letters and Negative numbers.

if(strcmp(argv[0], “kill”) == 0){

if(isaplha(argv[1]) || atoi(argv[1]) < 0){

fprintf(“Invald PID entered”);

fprintf(“Enter a positive numeric number”);





int size = sizeof(argv[1]) + 1;

char temp[size];


check1 = CheckForwardSlash(temp, size);

//If true is returned by the CheckForwardSlash method, skip the rest of the while loop

if(check1 == true){

printf(“Invalid file formatn”);

printf(“Avoid Forward Slashes in your file namen”);



//Input Validation Ends//


//Signals to catch the Ctrl-C and Ctrl/ combination

signal(SIGINT, SIG_IGN); //The instructions said to ignore the SIGINT signal

signal(SIGTERM, SIG_DFL); //SIGTERM signal must be caught.

execute(argv); //Finally, execute the command



Still stressed from student homework?
Get quality assistance from academic writers!