Question

In: Computer Science

Building a Command Line Interpreter (myshell) 1.Goal of this programming assignment The primary goal of this...

Building a Command Line Interpreter (myshell)

1.Goal of this programming assignment

The primary goal of this assignment is to understand and gain some familiarity with

the system call interface in Unix Environment. In this assignment you will be

implementing your own command line interpreter like a Unix shell program.

YOU ARE NOT IMPLEMENTING COMMANDS!

YOUR program should just

FORK off programs and EXECUTE them.

2.

Requirements

(1)

Programming language: You have to use either C or C++ to develop your program.

(2)

Running Environment: Your program should be compiled at

CSEGRIDand be able tobe tested without errors.

3.

The implementation details

(1)

Backgrounds: The command line interpreter, called a shell, is an application program

that gets commands from a keyboard and uses the system call interfaces to invoke OS

functions.

(2)

Your shell should support followings:

(a)

Support exit or stop

your shell command

a)

Example(s): exit

(b)

Support commands with no arguments

.

a)

Examples: “ls”, “pwd”, “whoami” and “hostname”

(c)

Support commands with arguments:

a)

Examples: “ls –l”, “ls –fx”, “ps –aux” ,“date –u”, “cd directory”, etc.

(d)

A command, with or without arguments, whose output is redirected to a file

a)

Examples: ls > foo, ls -Fx > foob

(e)

A command, with or without arguments, whose input is redirected from a file

a)

Examples: sort < testfile

(f)

Supporting a sequence of programs that communicate through a pipe.

a)

Examples: ls | wc

(g)

Display error messages if the typed command has errors or can’t be found

2

4.

Hint

Your shell should isolate itself from program failures by creating a child process to

execute each command specified by the user. For example, the “tar cvf cli5678 program1”

command is executed by a child of the process executing the shell, myshell.

% tar cvf cli5678 program1

You might consider the following functions to build myshell:

(1)

Printing a prompt – when myshell is begun, it display its own string as its own prompt.

For example, “myshell> ”.

(2)

Getting command line –

To get a command line, the shell performs a blocking read

operation so that the process that executes the shell will be blocked until the user types

a command line in response to the prompt. When the user enters the command, the

comm

and line string should be returned to the myshell.

(3)

Parsing the command: Take the user command and divide it into command and

arguments as well as checking errors.

(4)

Finding the file – The shell provides a set of “environment variables” for each user

such as “PATH”. The PATH environment variable is an ordered list of absolute

pathnames that specifies where the shell should search for command files.

(5)

Create a child process and launch the command –

Once the command and arguments

are prepared, create a child process to execute the command with its arguments and

wait until the command is completed.

Someone help to solve this Operating Systems problem?

Solutions

Expert Solution

PROGRAM:

CODE:

int main(int argc, char **argv)
{
lsh_loop();

return EXIT_SUCCESS;
}

void lsh_loop(void)
{
char *lines;
char **args;
int status;

do {
printf("> ");
lines = lsh_read_lines();
args = lsh_split_lines(lines);
status = lsh_execute(args);

free(lines);
free(args);
} while (status);
}


#define LSH_RL_BUFF_SIZE 1024
char *lsh_read_lines(void)
{
int Buff_Size = LSH_RL_BUFF_SIZE;
int pos = 0;
char *buff = malloc(sizeof(char) * Buff_Size);
int c;

if (!buff) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}

while (1) {
c = getchar();
if (c == EOF || c == '\n') {
buff[pos] = '\0';
return buff;
} else {
buff[pos] = c;
}
pos++;
if (pos >= Buff_Size) {
Buff_Size += LSH_RL_BUFF_SIZE;
buff = realloc(buff, Buff_Size);
if (!buff) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}

char *read_lines_lsh(void)
{
char *lines = NULL;
ssize_t Buff_Size = 0;
getlines(&lines, &Buff_Size, stdin);
return lines;
}

#define BUFF_SIZE_TOKEN_LSH 64
#define DELIM_TOKEN_LSH " \t\r\n\a"
char **lsh_split_lines(char *lines)
{
int Buff_Size = BUFF_SIZE_TOKEN_LSH, pos = 0;
char **tokens = malloc(Buff_Size * sizeof(char*));
char *token;

if (!tokens) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}

token = strtok(lines, DELIM_TOKEN_LSH);
while (token != NULL) {
tokens[pos] = token;
pos++;

if (pos >= Buff_Size) {
Buff_Size += BUFF_SIZE_TOKEN_LSH;
tokens = realloc(tokens, Buff_Size * sizeof(char*));
if (!tokens) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}

token = strtok(NULL, DELIM_TOKEN_LSH);
}
tokens[pos] = NULL;
return tokens;
}

int launch_LSH(char **args)
{
pid_t pid, wpid;
int status;

pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
perror("lsh");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
perror("lsh");
} else {
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}

return 1;
}

int lsh_cd(char **args);
int lsh_help(char **args);
int lsh_exit(char **args);

char *bullitin_str[] = {
"cd",
"help",
"exit"
};

int (*builtin_func[]) (char **) = {
&lsh_cd,
&lsh_help,
&lsh_exit
};

int lsh_num_builtins() {
return sizeof(bullitin_str) / sizeof(char *);
}

int lsh_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "lsh: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
perror("lsh");
}
}
return 1;
}

int lsh_help(char **args)
{
int i;
printf("Stephen Brennan's LSH\n");
printf("Type program names and arguments, and hit enter.\n");
printf("The following are built in:\n");

for (i = 0; i < lsh_num_builtins(); i++) {
printf(" %s\n", bullitin_str[i]);
}

printf("Use the man command for information on other programs.\n");
return 1;
}

int lsh_exit(char **args)
{
return 0;
}

int lsh_execute(char **args)
{
int i;

if (args[0] == NULL) {
return 1;
}

for (i = 0; i < lsh_num_builtins(); i++) {
if (strcmp(args[0], bullitin_str[i]) == 0) {
return (*builtin_func[i])(args);
}
}

return launch_LSH(args);
}


#include <sys/wait.h>
waitpid() and associated macros
#include <unistd.h>
chdir()
fork()
exec()
pid_t
#include <stdlib.h>
malloc()
realloc()
free()
exit()
execvp()
EXIT_SUCCESS, EXIT_FAILURE
#include <stdio.h>
fprintf()
printf()
stderr
getchar()
perror()
#include <string.h>
strcmp()
strtok()


Related Solutions

1. INTRODUCTION The goal of this programming assignment is for students to write a Python program...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION  Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the...
Here are the tasks for this assignment: 1. Write a bash command line to determine if...
Here are the tasks for this assignment: 1. Write a bash command line to determine if the server 'syccuxfs01.pcc.edu' is reachable from the syccuxas01.pcc.edu server. 2. As you have read, TCP/IP messages are passed from one device to another until the message reaches its destination. Write a bash command line that will verify that no more than two (2) network devices are used to pass messages from the syccuxas01.pcc.edu server to the www.pcc.edu server. 3. Write a bash command line...
What is the primary goal in using a scripting language for programming work?
What is the primary goal in using a scripting language for programming work?
C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line...
C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line arguments to be passed to the programs. You only need to support one pipe command at a time. For example, when you type ls | wc the shell should write the output of ls to a temporary file by redirecting standard output when running ls, and run wc, redirecting standard input so it reads from the temporary file written to by ls.
Turtle Command Interpreter You will write a program that reads a sequence of codes, converts them...
Turtle Command Interpreter You will write a program that reads a sequence of codes, converts them into Turtle commands, and then executes them. The codes are in a Python list, passed as an argument to your function. turtleRunner(t, x) t is a Turtle x is a list of Turtle commands Code List (f, d)    Move forward by d pixels u Lift pen up d Put pen down (l, d) Turn left by d degrees (r, d) Turn right by...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
Python programming: Instructions: The python program should respond to user input on a command line Below...
Python programming: Instructions: The python program should respond to user input on a command line Below are the four options - if the user input is A, the program will quit -if the user input is B, the program will display the number of times the prompt has been displayed -if the user input is C, will display whatever is stored. -if the user inputs something else, it will store that user input as a string and append it to...
1. What command is used to change the current working directory at the command line?
1. What command is used to change the current working directory at the command line?
What is the primary goal of capitalism? What is the primary goal of socialism?
What is the primary goal of capitalism? What is the primary goal of socialism? What do you think are the pros and cons of each?
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT