Question
Use C Write a unix shell 1. Create a read-evaluate-print loop, print mellshell> at the beginning of the line, and then read the user's input
Use C Write a unix shell
1. Create a read-evaluate-print loop, print mellshell> at the beginning of the line, and then read the user's input
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:
/bin/echo -n Your working directory :
/bin/pwd
/bin/echo
5. 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.
6. 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 as much as possible 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
char **command line tokenize (char *cmdline){
return result;
}
//change tokens to command
struct Command commandparse (char **tks) {
struct Command testa= {.args = tks, .outputFile = NULL};
return testa;
}
//judge one single command
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