Question
Use C Write a unix shell The program we write must be able to achieve the following 8 requirements, and only five functions in Programming
Use C Write a unix shell
The program we write must be able to achieve the following 8 requirements, and only five functions in Programming requirements can be used to achieve
1. Create a read-evaluate-print loop, print mellshell> at the beginning of the line, then read the user's input, and execute this step in a loop
2. Parsing built-in commands Commands consist of space-separated ASCII words Use strtok() to implement some method of splitting commands so you can recover the words
exit, call exit(0) to make the shell exit
cd : cd takes only one argument. mellshell should call chdir() with user-supplied arguments
path: The path command takes zero or more arguments, each separated by a space
ex: mellshell> path /etc /bin/etc
3. Implement a scripting system: If mellshell is invoked with an argument, assume its argument is a filename, and try to get commands from that file one at a time.
4. If the input file is invalid, or there are multiple parameters, the program should output an error message and call exit(1) to exit, which is the only error that causes mellshell to exit
ex:
testScript.mes:
/bin/echo -n Your working directory :
/bin/pwd
console:
mellshell> testScript.mes
Your working directory :/bin/echo
123456
5.Implement built-in command "path"
In the variable default_shell_path, two paths can be stored, and the user needs to be able to modify the second path through the "path" command.
When implementing external program execution, it is assumed that the 0th parameter in default_shell_path in the given program is the path of the executable file, but this is too complicated for user input, we need to create a set of user-specified directories, use to search for external programs. When the shell receives a command it doesn't recognize, it looks for that program in its PATH. Users can set PATH with the path command. Each argument to path corresponds to an entry in the shell's PATH. The path command completely overwrites the existing PATH -- it does not append entries. If PATH is empty because the user executed a path command without arguments, mellshell cannot execute any external programs unless the full path to the program is provided.
6.Handle errors
Whenever an error occurs, my shell should print an error message on stderr
char msg[30] = "Error ";
int written = write(STDERR_FILENO, emsg, strlen(emsg));
if(written != strlen(msg)){
exit(2);
}
Program-implemented built-in commands should error when:
exit: Any arguments are provided.
cd: wrong number of arguments, or chdir() call failed.
Path: never fails.
7. Redirect
If the user types ls -al /tmp > output, nothing is output to the screen. Instead, the program's output and errors should be rerouted to the file output
Multiple redirections in commands such as ls > file1 > file2 are wrong.
A redirection without a corresponding command is an error, eg > file1.
It is an error to redirect without a corresponding file, e.g. ls >
There will always be spaces around redirects, e.g. ls>file1 requests that the command execute the file named ls>file1, not a redirect.
8. Concurrent commands
There may be no spaces around the & operator. For example cmd1 arg1&cmd2 > file2 is a valid command line and requests the execution of two commands. Also, some or all of the sides of the ampersand may be blank. For example &&&& is valid.
ex: mellshell>cmd1 & cmd2 & cmd3 args1
If the command line has multiple concurrent commands, all of which are external, the current specification applies.
If the command line has multiple builtin concurrent commands, the shell should execute them sequentially from left to right.
There will be no command line that mixes concurrent external/internal builtins. mellshell should not crash if this happens
Programming requirements:
Use the following five functions to achieve
command parse
command line tokenize
eval
builtinexec
cmdexternal
main
#include
#include
#include
char shell_paths[MAX_ENTRIES_IN_SHELLPATH][MAX_CHARS_PER_CMDLINE];
static char prompt[] = "mellshell> ";
static char *default_shell_path[2] = {"/bin", NULL};
struct Command {
char **args;
char *outputFile;
};
int main(int argc, char **argv) {
}
// using strtok() turns a command line into an array of arguments.change string to tokens
//allocate a char** and fill it using strtok()
char **command line tokenize (char *cmdline){
return result;
}
//change tokens to command
//Command represents a command to execute
//This is the best format for storing information about commands,you are free to change
//this function change tokens to command
struct Command commandparse (char **tks) {
struct Command testa= {.args = tks, .outputFile = NULL};
return testa;
}
//judge one single command
//builtinexec and cmdexternal passed to this function
//The correct type should be found and appropriate action taken.
void eval(struct Command *cmd) {
return;
}
// * if cmd is built-in command return 1, if cmd not a built-in command, do nothing return 0
void builtinexec(struct Command *cmd){
return;
}
//Execute external command use fork() and exec() and consider redirecting
void cmdexternal(struct Command *cmd){
return;
}
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