DOC PREVIEW
UCSC CMPS 111 - Simple Shell

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

A Simple ShellProf. Darrell Long†Computer Science DepartmentJack Baskin School of EngineeringUniversity of California, Santa CruzDue: October 8, 23:591 PurposeThe primary goal of this assignment is to warm up your programming skills that may have atrophied over the summer,and to gain some familiarity with the system call interface. A secondary goal is to use some of the programming toolsprovided in the UNIX environment. In this assignment you are to implement a UNIX shell program. A shell is simplya program that conveniently allows you to run other programs.2 BasicsYou are provided with the files lex.l and myshell.c that contain some code that calls getline(), a functionprovided by lex.l to read and parse a line of input. The getline() function returns an array of pointers tocharacter strings. Each string is either a word containing the letters, numbers, period (.), and forward slash (/), or acharacter string containing one of the special characters: (, ), <, >, |, &, or ; (which have syntactical meaning to theshell).To compile lex.l, you have to use the flex command. This will produce a file called lex.yy.c. You mustthem compile and link lex.yy.c and myshell.c in order to get a running program. In the link step you also haveto use “-lfl” to get everything to work properly. Use gcc for the compilation and linking.3 DetailsYour shell must support the following:1. The internal shell command “exit” which terminates the shell.Concepts: shell commands, exiting the shellSystem calls: exit()2. A command with no arguments.Example: lsDetails: Your shell must block until the command completes and, if the return code is abnormal, print out amessage to that effect. This holds for all command strings in this assignment.Concepts: Forking a child process, waiting for it to complete, synchronous execution.System calls: fork(), execvp(), exit(), wait()†Although the idea of writing a shell as the first assignment in the operating systems course is an ancient one, dating at least to the venerableProf. Jehan-Franc¸ois Pˆaris, I have borrowed liberally from the assignment designed by Prof. Scott Brandt. Even so, it differs in key aspects whichwill be observed by the discerning student.13. A command with arguments.Example: ls -lDetails: Argument zero is the name of the command other arguments follow in sequence.Concepts: Command-line parameters.4. A command, with or without arguments, whose output is redirected to a file.Example: ls -l > fileDetails: This takes the output of the command and put it in the named file.Concepts: File operations, output redirection.System calls: close() and dup()5. A command, with or without arguments, whose input is redirected from a file.Example: sort < scoresDetails: This takes the named file as input to the command.Concepts: Input redirection, file operations.System calls: close() and dup()6. A command, with or without arguments, whose output is piped to the input of another command.Example: ls -l | moreDetails: This takes the output of the first command and makes it the input to the second command.Concepts: Pipes, synchronous operationSystem calls: pipe(), close() and dup()n.b. You must check and correctly handle all return values. This means that you need to read the manual pages foreach function and system call to figure out what the possible return values are, what errors they indicate, and what youmust do when you get that error.4 DeliverablesA compressed tar file of your project directory, including your design document. You must do “make clean”before creating the tar file. In addition, include a README file to explain anything unusual to the teaching assistant.Your code and other associated files must be in a single directory so they will build properly in the submit directory.Do not submit object files, assembler files, or executables. Every file in the submit directory that could be generatedautomatically bythecompileror assemblerwill result in a 5 point deductionfrom yourprogrammingassignment grade.Your design document should be called DESIGN, and should reside in the project directory with the rest of yourcode. Your design should describe the design of your assignment in enough detail that a knowledgeable programmercould duplicate your work. This includes descriptions of the data structures you use, all non-trivial algorithms andformulas, and a description of each function including its purpose, inputs, outputs, and assumptions it makes about theinputs or outputs.A sample design document will be available on the course web page.2Appendix I: lex.l%{int _numargs = 10;char *_args[10];int _argcount = 0;%}WORD [a-zA-Z0-9\/\.-]+SPECIAL [()><|&;*]%%_argcount = 0; _args[0] = NULL;{WORD}|{SPECIAL} {if(_argcount < _numargs-1) {_args[_argcount++] = (char *)strdup(yytext);_args[_argcount] = NULL;}}\n return (int)_args;[ \t]+.%%char **getline() { return (char **)yylex(); }Appendix II: myshell.c#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <errno.h>extern char **getline();main() {int i;char **args;while(1) {args = getline();for(i = 0; args[i] != NULL; i++) {printf("Argument %d: %s\n", i,


View Full Document

UCSC CMPS 111 - Simple Shell

Download Simple Shell
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Simple Shell and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Simple Shell 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?