Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of this project is to make your own shell program. In order to construc

ID: 3839948 • Letter: T

Question

The goal of this project is to make your own shell program.

In order to construct your own shell, you have to construct

(a) command line interpreter, and

(b) commands itself, e.g. cat, chmod, etc.

The command line interpreter (as shown in Figure 1) is a basically a program waiting for a user input. Once user type a command with command line argument(s), it needs to parse it, understand it, and pass it to a proper function, which is an implementation of the particular command.

For instance, if you type “cat hello.cpp”, then the command line prompt understands the command “cat” with the argument “hello.cpp”. Then, it calls a function which is an implementation of cat with the string “hello.cpp” as its input argument.

For the project, you can use the system calls and the standard library functions from original C, but not from C++. That is, you can use only those listed in https://en.wikipedia.org/wiki/C_standard_library and the system calls discussed in the class. Still, you can use .cpp file extension to use c++ syntax.

Project

You are required to submit three files, myshell.cpp, mycommands.h, and mycommands.cpp to d2l. myshell.cpp is your implementation of the command line interpreter. mycommands.h and mycommnds.cpp are the implementation of your commands. You are not allowed to use any existing program by exploiting any “exec”-family system calls. However, you are allowed to use any C/C++ standard library function. Below is the list of required implementations and corresponding grading policy.

(a) Command line interpreter

You are required to implement a command line interpreter, which will wait for a user input. Once input is given (i) it should list the command name, e.g. cat, and the arguments if any on the screen. Then, it should pass to the proper function in mycommands.h. (ii) Once the user type “exit”, then the interpreter ends.

clear

clear is a standard Unix computer operating system command which is used to clear the screen.

(b) Commands

You must interpret the command line interpreter before starting this section.

Below is the list of commands to implement. The bottom line is that the way how implementations work should be identical to the way they are in bash shell.

Command: cat

cat filename will print out all of the contents of the file on the screen if the file exists. Otherwise, it will print “cat: file-name-here: No such file or directory” on the screen.

cat file1 file2 file3 … > outfile.txt

This means that the contents of multiple files will be written into outfile.txt. If outfile.txt does not exist, it should be created. If does exists, it will overwrite.

Command: cp

cp file1 file2 is a command to create a new file namely file2, which is identical to file1. If file2 already exists, it will overwrite.

cp file1 file2 file3 ... directory

This command copies the files (file1 file2 file3 ...) into directory

Command: ls

Command ls will print out the information of all files on the current working directory. Type “ls” to see what have to be printed.

ls -l

This command prints one file/directory name with more detailed information per each line. Type “ls -l” to see what have to be printed.

Command: grep

Command grep looks for the text in files. It will list out the lines containing the text with filename if more than one file examined.

Example: grep "hi there" file1 file2 …

$ grep "apple" *.txt

This command will look for the text “apple” from every file in the working directory whose extension is .txt.

Explanation / Answer

mycommands.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char input(){

char *commandall[] = {
"cd",
"cat",
"cp",
"ls",
"grep"
};
}
int sizecommand() {
return sizeof(commandall) / sizeof(char *);
}

myshell.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/wait.h>
#include "mycommands.hpp"
#include "mycommands.cpp"


static char* args[512];
pid_t pid;
int command_pipe[2];


static int command(int input, int first, int last)
{

   int pipettes[2];
   pipe( pipettes );
   pid = fork();


   if (pid == 0) {
       if (first == 1 && last == 0 && input == 0) {
           dup2( pipettes[WRITE], STDOUT_FILENO );
       } else if (first == 0 && last == 0 && input != 0) {
           dup2(input, STDIN_FILENO);
           dup2(pipettes[WRITE], STDOUT_FILENO);
       } else {
           dup2( input, STDIN_FILENO );
       }

       if (execvp( args[0], args) == -1)
           _exit(EXIT_FAILURE);
   }

   if (input != 0)
       close(input);

   close(pipettes[WRITE]);

   if (last == 1)
       close(pipettes[READ]);

   return pipettes[READ];
}

static void cleanup(int n)
{
   int i;
   for (i = 0; i < n; ++i)
       wait(NULL);
}


static char line[1024];
static int n = 0;

int main()
{
   printf("SIMPLE SHELL: Type 'exit' or send EOF to exit. ");
   while (1) {

       printf("CMD---> ");
       fflush(NULL);
       if (!fgets(line, 1024, stdin))
           return 0;

       int input = 0;
       int first = 1;

       char* cmd = line;
       printf("%s",cmd);
       int a = sizecommand();
       char algo(char commandall);
       printf("%s",commandall[0]);
       bool flag = true;
        for (int i = 0; i < a; i++) {
            if( commandall[i] == line)
            {
                flag = false;
                char* next = strchr(cmd, '|');
                while (next != NULL) {

                    *next = '';
                    input = run(cmd, input, first, 0);

                    cmd = next + 1;
                    next = strchr(cmd, '|');
                    first = 0;
                }
                input = run(cmd, input, first, 1);
                cleanup(n);
                n = 0;
            }
            else{
                if(flag == true){
                    printf("No such file or directory");
                }

        }
   }
   }
   return 0;
}

static int run(char* cmd, int input, int first, int last)
{
   split(cmd);
   if (args[0] != NULL) {
       if (strcmp(args[0], "exit") == 0)
           exit(0);
       n += 1;
       return command(input, first, last);
   }

   return 0;
}

static char* skipwhite(char* s)
{
   while (isspace(*s)) ++s;
   return s;
}

static void split(char* cmd)
{
   cmd = skipwhite(cmd);
   char* next = strchr(cmd, ' ');
   int i = 0;

   while(next != NULL) {
       next[0] = '';
       args[i] = cmd;
       ++i;
       cmd = skipwhite(next + 1);
       next = strchr(cmd, ' ');
   }

   if (cmd[0] != '') {
       args[i] = cmd;
       next = strchr(cmd, ' ');
       next[0] = '';
       ++i;
   }

   args[i] = NULL;
}

mycommands.h


#ifndef MYCOMMANDS_HPP
#define MYCOMMANDS_HPP

#define READ 0
#define WRITE 1
static int command(int input, int first, int last);
static void cleanup(int n);
static int run(char* cmd, int input, int first, int last);
static void split(char* cmd);
static char* skipwhite(char* s);
static void split(char* cmd);
int sizecommand();

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote