<P>
<B>Your very own shell</B>
<P>
In this project you will implement a shell program which prompts the user for commands, parses the command, and executes it with a child process. In this exercise, you will use execv( ). This means that you will have to read the PATH environment  variable and then search each directory in the PATH for the command file name that appears in the command line. 
<P>
An OS shell exports a character-oriented human-computer interface, and uses the system call interface to invoke OS functions. A text-based shell assumes a screen display with a fixed number of lines (say 25) and a fixed number of characters (say 80) per line. The user interacts with the OS by typing a string of characters to the shell and the OS prints the characters back to the shell display.  After a user logs in, the shell first prints a prompt (e.g. mimi$). The user responds by typing in a command (e.g. ls ), terminated by (for example) a return in UNIX. The shell then makes the appropriate system calls to execute the command.
<P>
Shells have their own syntax and semantics. In UNIX a command line has the form
<br>
command  argument_1 argument_2.........
<br>
where the command is what it sounds like it is, the command to be executed, and the arguments are utilized by the command. For example, in ls -al, ls is the command while a and l are arguments which ls utilizes. Each command specifies its own syntax for interpreting an argument. This means that the command determines which arguments are to be grouped (e.g. a and l are grouped above), which arguments must be preceded by a "-" symbol, and whether the position of the  argument in the list of arguments is important. A more complicated example is 
<br>
mimi$ cc -g potrezebie -S main.c inout.c -lmath
<br>
in which all of the arguments following cc are passed to the C compiler c. 
<P>
The command is usually the name of a file that contains an executable program (e.g. ls and cc are the names of files which are stored in /bin on most UNIX machines). When the command is not the name of a file, it is the name of a command that is implemented in the shell, e.g. cd (change directory) is implemented in the shell rather then being the name of a file most of the time.  The vast majority of commands are the names of files which reside in directories on the machine in question. Hence the file's job is to find the file, to prepare the list of parameters (following the syntax of the command) for input to the command, and finally to cause the command to be executed.  There are many shell for UNIX, including the Bourne shell (sh), the C shell (csh) and the Linux shell (bash). All of them have a similar syntax for the commands. 
<P>
The basic approach to designing a shell is to create a new process (or thread) which will execute a new computation. When a user compiles a program, the shell first creates a child process whose job it is to execute the compile command provided by the user. There is a reason behind this design philosophy-if the shell did not put a child process in charge of executing a command such as the compile command, then an error in the command might cause the shell itself to crash, bringing the entire machine to a halt. 
<P>
The file in the command line may be written by the user. It will be executed just as if it was a  command in UNIX. For example, if the user writes a C program contained in a file named main.C, it can be compiled and executed by 
<br>
mimi$ cc main.c<br>
mimi$ a.out <br>
<P>
Here is what happens-the shell finds the cc command in the /bin directory. It creates a child process to execute the cc program and passes it the string main.c. 
A description how to create child processes is included at the end of problem statement, under the heading "how to create children".
The C compiler then compiles main.c and writes the executable into a.out in the current directory. When the shell then executes the second command, it finds a.out in the current directory, loads it and executes it. 
<P>
The steps that a shell must take are as follows: <br>
<br>
1. Print a prompt-when the shell starts up, it looks for the name of the of the machine on which it is executing and prepends it to its own very special character , e.g.
mimi$. The shell can also print the name of the current directory in which it is operating. The name of directory can change as a result of executing the cd command. Once determined, the prompt string prints it to stdout.  For example,
<br>
void printPrompt( ) { <br>
promtString=frammis; <br>
printf("%s",promptString); <br>
} <br>
<br>
2. Get the command line-the shell does a blocking read to read in the command  line. This means that the process executing the shell is blocked until the user types in the command. The user terminates the command with a NEWLINE character. 
The stdio library can be made use of in implementing this command. 
For example,
<br>
void read Command(*char *buffer){ <br>
gets buffer(buffer): <br>
} <br>
<br>
3. Parsing the command.
<P>
The main program in the shell can be declared as <br>
<br>
int main(in argc, char *argv [ ] ) <br>
<P>
This means that a command of the form a.out moxie 100
results in argc being set to 3 (there are three words on the command line) and the argv[ ] array being initialized as follows: <br>
<br>
arg[0]= "a.out"<br>
arg[1]= "moxie" <br>
arg[3]="100" <br>
<P>
Use the struct data structure to store the command line arguments: <br>
<br>
struct command_t{ <br>
char *name; <br>
int argc; <br>
char *argv[MAX_ARGS]; <br>
} <br>
<br>
char *name contains the name of the file which contains the name of the binary program for the command. <P>

Here is a function which parses a command line and returns the result in struct command_t *cmd argument: <br>
<br>
#include  <string.h> <br>
<br>
int parseCommand(char *cLine, struct command_t *cmd) { <br>
int argc; <br>
char **char **c1Ptr; <br>
/*Initialization*/ <br>
c1Ptr = &cLine; /*the command line*/ <br>
argc=0; <br>
cmd->argv[argc] = (char *) malloc(MAX_ARG_LEN); <br>
/*fill argv[ ]*/ <br>
while((cmd->argv[argc] = strsep((c1Ptr, WHITESPACE)) ! NULL { <br>
cmd->argv[++argc] = (char *) malloc(MAX_ARG_LEN); <br>
}<br>
<br>
/*Set the command name and argc*/ <br>
cmd->argc=argc-1; <br>
cmd->name + (char *) malloc(sizeof(cmd->argv[0])); <br>
strcopy(cmd->name, cmd->argv[0]); <br>
return 1; <br>
}<br>
<P>
4. Finding the file. The shell provides environment variables for the user. They are defined in the .login file, and can be modified by the set command. The PATH environment variable is a list of pathnames which specify where the shell should look for command files. It the .login file has the line
 set path=(.:/bin:/usr/bin), then the shell first looks in the current directory ( as indicated by the "." , then in bin and finally in /usr/bin.  
<P>
Before reading command lines, the shell needs to parse the the PATH variable, accomplished by <br>
<br>
int parsePath( char *dirs[ ]) { <br>
/* This function reads the PATH variable for this environment, and builds an array, dirs[ ], of the directories in PATH*/ <br>
<br>
char *pathEnvVar;<br>
char *thePath;<br>
<br>
for(i-0; i < MAX_ARGS; i ++) <br>
dirs[ ]=NULL;<br>
pathEnvVar = (char*)getenv("PATH"); <br>
thePath = (char *) malloc(strlen(pathEnvVar) + 1; <br>
strcpy(thePath, pathEnvVar); <br>
<br>
/*Loop to parse thePath. Look for a ":" delimiter between each path name.*/
}
<P>
If the command name begins with a "/" then it is an absolute pathname that can be used to launch execution. Otherwise, it is necessary to search each directory in the list provided by the PATH environment variable to find the relative pathname. Each time that a command is read it is necessary to see if there is an executable file corresponding to the command in one of the directories specified by the Path variable.  The following lookup( ) function serves this purpose.  <br>
<br>
char *lookupPath(char **argv, char **dir) { <br>
/*This function searches the directories identified by dir to see if argv[0] (the file name) is there. It allocates a new string, places the full path name in it and then returns the string*/ <br>
<br>
char *result; <br>
char pName[MAX_PATH_LEN]; <br>
<br>
//Check to see if file name is already an absolute path name 
if(*argv[0]==== '/') { <br>
....<br>
}<br>
<br>
//Look in PATH directories <br>
//Use access ( ) to see if the file is in a dir. <br>
for(i=0; i < MAX_PATHS;  i+ +) { <br>
... <br>
} <br>

<B>Skeleton for the solution</B>
<br>
<P>
Here is a header file, minishell.h, for the shell program.
<br>
<br>
#define LINE_LEN     80<br>
#define MAX_ARGS  64<br>
#define MAX_ARG_LEN  16 <br>
#define MAX_PATHS 64 <br>
#define MAX_PATH-LEN   96 <br>
#define WHITESPACE " .,\t\n" <br>
<br>
#ifndef  NULL <br>
#define NULL ...... <br>
#endif <br>
<br>
struct command_t { <br>
char *name; <br>
int argc <br>
char *argv[MAX_ARGS]; <br>
}; <br>
<br>

<B>Here is a skeleton for the solution:</B>
<br>
<br>
/*The shell first finds an executable in the PATH, and then loads and executes it *using execv. Because it uses "." as a separator, it cannot handle file names like *"minshell.h" */ <br>
<br>
#include ...<br>
#include "minishell.h"<br>
<br>
char *lookupPath(char **, char **); <br>
int parseCommand(char *, struct command_t*);<br>
int parsePath(char **);<br>
void printPrompt( );<br>
void readCommand(char *);<br>
...<br>
void readCommand(char *);<br>
...<br>
int main( ) { <br>
...<br>
<br>
/* Shell initialization* /<br>
...<br>
parsePath(pathv);/*Get directory paths from PATH*/<br>
<br>
while(TRUE) { <br>
printPrompt( ); <br>
<br>
/*read the command line and parse it*/ <br>
readCommand(commandLine); <br>
...<br>
parseCommand(commandLine, &command);<br>
...<br>
/*Get the full pathname for the file*/ <br>
command.name = lookupPath(command.argv, pathv); <br>
if(command.name == NULL { <br>
/* report error*/<br>
continue;<br>
/* create child and execute the command*/ <br>
...<br>
/*wait for the child to terminate*/ <br>
... <br>
}<br>
/*shell termination*/<br>
...<br>
}<br>
<br> 
 <B>How to create children, according to UNIX</B>
<P>
When a (parent) process calls fork ( ), a new child process is created with its own copies of the parent's program text, data and stack segments, as well as access to all open file descriptors. The fork function returns a value of zero to the child and the PID of the child to the parent.  The child and the parent then execute concurrently, each in their own address spaces. No part of their address spaces are shared; this means that the parent and child cannot communicate via shared variables, among other things. In UNIX, two processes can only reference open files.  In the following code, the child starts executing the same code as the parent,
right after the fork, i.e. it starts at the print statement. 
<br>
theChild = fork( );<br>
printf(" I am being treated like an orphan, my PID is %"d\n" , theChild");<br>
...<br>
<P>
If the parent wants the child to execute code which is different from the parent,
this can be done via:
<br>
childPID = fork( );<br>
if(theChild == 0) {<br>
code for the child;<br>
exit(0);<br>
}<br>
/*the parent executes here*/<br>
<P>
In this code, both parent and child test the PID variable. The child sees zero, and executes its code, while the parent sees the value of the PID variable, and executes his code. <br>
<br>
<B>But how do we get the command actually executed?</B>
<P>
Use execve( ), which dynamically calls the loader to redefine a process's text, stack and data.
<br>
int execve (char *pth, char *argv[ ] , char * envp[ ]);<br>
<P>
This causes the binary program stored in the file at path to replace the text, data, and stack currently being exectued by the process. After execve( ) is called, the program which called it is no longer being executed by the process. The OS does not return from the execve( ) call. After the new program is loaded, the stack is cleared, variables are initialized, and the process begins execution at the the program's entry point. The new process is passed the argument list, argv and the process uses a new set of environment variables, envp. 
<P>
So the way to create a new child to execute the next command is 
<br>
if((pid = fork( ) = = 0) { <br>
execvp(comand.name, command.argv); <br>