Program A (xssh2) o Features ( Basic capabilities ) - Evaluate (execute) commands located in standard places (execvp) - Evaluate scripts + Environment + Variables - Concurrent command execution - Redirect stdin and stdout o Usage xssh2 [-x] [-d DebugLevel] [Filename] -x The command line after variable substitution should be displayed before the command is evaluated. -d DebugLevel Output debug messages 0: don't output (default) 1: ... Filename Input is from the file instead of stdin o Informal Semantics of Simple Cases Non-builtin commands A Unix executable that can be found in a directory listed in the PATH environment variable or is an absolute pathname Built-in Commands (W1, W2, ... stand for Word1, Word2, ...) chdir Change the current directory to $HOME chdir W1 Change the current directory to word W1 echo W1 W2 ... Display the words W1, W2, ... followed by a newline. Multiple spaces/tabs should be reduced to a single space. export Display the environment variables export W1 W2 ... Export the variables W1, W2, ... to the environment quit Quit the shell with the exit status set to that of the previous command executed quit W Quit the shell with an exit status of W set Display the contents of the variable table set W1 Set the variable W1 to the empty string set W1 W2 ... Set the variable W1 to the concatenation of words W2, W3, ... sleep W1 Pause for W1 seconds wait Wait for ANY child process to terminate wait W1 Wait for process W1 to terminate o Complex Constructs (C stands for Command) C ; Sequential execution; C terminates before proceding C & Parallel execution; exec C in the background and continue processing commands C , Sequential background execution; exec C in the background, but any background command following C can not start until C terminates o Redirection (C stands for Command and F stands for File) C < F1 > F2 Redirect stdin to the file F1 and stdout to the file F2. Both redirections are optional; e.g., "C > F" means redirect stdout to F but do not redirect stdin. << Other Features >> o A newline character is replaced with ';'. o A sequence of complex commands on one line has equal precedence; e.g., C1 ; C2 ; C3 ; Run C1, C2, and C3 sequentially in the foreground C1 & C2 & C3 & Run C1, C2, and C3 concurrently in the background C1 , C2 , C3 , Run C1, C2, and C3 sequentially in the background C1 ; C2 & C3 ; Run C1 in the foreground, C2 in the background and C3 in the foreground concurrent with C2; i.e., C3 doesn't need to wait for C2 to terminate. C1 & C2 , C3 , C4 ; Run C1, C2 and C3 in the background, but C3 can not start running until C2 has terminated. Run C4 in the foreground after starting C1 in the background. In each case, the command can contain stdin/stdout redirection. o Multiple spaces/tabs are reduced to a single space during the substitution and line scanning phase. o The command line prompt should be the three character sequence '>> ' (i.e., >, >, space). o A child process inherits all of its parent's environment variables. o '$' as the first character of a word signifies variable substitution; e.g., $XY$Z means "the value of the variable XY$Z". o All undefined variables have a value of the null string. o The '#' character signifies the beginning of a comment. All other characters following and including the '#' should be ignored during interpretation. o Blank lines should be ignored. << Special Variables >> $$ PID of this shell $? Decimal value returned by last command executed $! PID of last background command << Implementation Notes >> o You are not allowed to use the library function system(3). << Implementation Sketch >> o -x command line flag Already recognized in xsshA.c Need to implement do_subst() to populate xword[] o -d DebugLevel Modify DEBUG macro to handle DebugLevel o "xssh2 Filename" Read input from file until EOF Temporarily redirect stdin to read from Filename [dup2] o Read-Eval Loop Read line from stdin until EOF or quit command { word[] = Parse into words; xword[] = Do variable substitution; if (builtin command) Eval builtin command; else Eval non-builtin command; } o Non-builtin commands Lift code from HW3 Need exit status of commands [waitpid] o chdir Remember current working directory is $HOME Need $HOME from environment [getenv,putenv] Change kernel's understanding of current working directory is $HOME [chdir] o chdir W1 Same as "chdir", but need to know current directory. [getcwd] o echo W1 W2 ... Lift code from HW1 o export Walk through environ(5) and display (see lecture notes) [environ] o export W1 W2 ... Update environment [setenv] o quit Keep track of exit status of each command Lift code from HW3 o quit W Set exit status Lift code from HW3 o set Walk through symbol table displaying contents [map] o set W1 Set variable W1 to "" [map] o set W1 W2 ... Set variable W1 to concatenation of W2 ... [map,strcat] o sleep W1 Release CPU for W1 seconds [sleep] o wait Wait for any child to terminate [waitpid] [ waitpid(-1, ...) ] How to set exit status ??? o wait W1 Same as "wait" except wait for specific PID o C ; Sequential execution [fork,execvp] Wait on child to terminate [waitpid] o C & Run C in background but don't wait on child [fork,execvp] o C , Run C in background but if next command is another sequential background command, wait on C to terminate before execing Scan ahead and continue to process commands that can be run o C < F1 > F2 Redirect stdin/stdout before execing [dup2] o List of Commands Parse xword[] into commands (yword[]???)