Assignment 4 for Unix Software Development | CSCI 352, Assignments of Computer Science

Material Type: Assignment; Class: Unix Software Development; Subject: Computer Science; University: Western Washington University; Term: Fall 2009;

Typology: Assignments

Pre 2010

Uploaded on 02/25/2010

koofers-user-rwx
koofers-user-rwx 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 352 - UNIX Software Development
Fall 2009 Assignment 4
200 Points
Due: Wednesday, November 4 2009
For this assignment, we will continue work on the mini-shell. Start with
the code base you have for the last assignment. (ASSIGNMENT-3 tagged
sources.)
This assignment’s work consists of the following steps:
Correct any problems you know about from Assignment 3. Specifically,
get the Makefile up to specifications.
Add argument processing for arguments to msh’s main. This is not
argument processing for arguments produced by arg parse, it is for the
arguments passed to your shell when your shell starts. (The param-
eters to msh’s “int main (int mainargc, char **mainargv) ...”. The
name mainargc and mainargv could be any name, I just named them
that to show you that they are the argc and argv that are parameters
to main() for your shell.) This would be the arguments used when
your shell is run. For example, your msh could be run like:
msh filename arg1 arg2 arg3 arg4 arg5
instead of
msh
To add argument processing for main’s arguments, do the following:
If the shell is started with one command line argument (the shell’s
name), all input comes from the standard input. This is consid-
ered an interactive execution. (This is what your msh currently
does. You start your msh by giving the command “msh”.)
If the shell is started with more than one command line argument,
shell commands are read from the file named in the first argument
after the shell’s name. (The first argument is the script file name.
For example, you would start msh running with the command
“msh script arg1 arg2” and msh would read commands from the
file named “script”.) If msh can not open the file, msh should
print an error message and exit with the value of 127.
1
pf3
pf4
pf5

Partial preview of the text

Download Assignment 4 for Unix Software Development | CSCI 352 and more Assignments Computer Science in PDF only on Docsity!

CSCI 352 - UNIX Software Development

Fall 2009 – Assignment 4

200 Points

Due: Wednesday, November 4 2009 For this assignment, we will continue work on the mini-shell. Start with the code base you have for the last assignment. (ASSIGNMENT-3 tagged sources.) This assignment’s work consists of the following steps:

  • Correct any problems you know about from Assignment 3. Specifically, get the Makefile up to specifications.
  • Add argument processing for arguments to msh’s main. This is not argument processing for arguments produced by arg parse, it is for the arguments passed to your shell when your shell starts. (The param- eters to msh’s “int main (int mainargc, char **mainargv) ...”. The name mainargc and mainargv could be any name, I just named them that to show you that they are the argc and argv that are parameters to main() for your shell.) This would be the arguments used when your shell is run. For example, your msh could be run like:

msh filename arg1 arg2 arg3 arg4 arg

instead of

msh

To add argument processing for main’s arguments, do the following:

  • If the shell is started with one command line argument (the shell’s name), all input comes from the standard input. This is consid- ered an interactive execution. (This is what your msh currently does. You start your msh by giving the command “msh”.)
  • If the shell is started with more than one command line argument, shell commands are read from the file named in the first argument after the shell’s name. (The first argument is the script file name. For example, you would start msh running with the command “msh script arg1 arg2” and msh would read commands from the file named “script”.) If msh can not open the file, msh should print an error message and exit with the value of 127.

Note: Again, these are not command lines processed by msh, this is how msh is started. You will need to use mainargc and mainargv as passed into the main function of your msh program.

  • If, while reading a script file, the shell encounters an end of file, exit with the value of 0.
  • Do not print a prompt when reading from a script file. An inter- active run still must print a prompt to standard error.
  • Lines from either standard input or from the file are processed exactly the same way. They are sent to “processline()” for pro- cessing. (In fact, the only change in the main loop should be which file is read by fgets(). The best way would be to have only one fgets(). Remember, variables can vary and you can have stream variables. (Type FILE *.))
  • Add processing to “expand()” that recognizes the pattern $n, where n is an integer greater than or equal to 0. “expand()” should replace the $n by the the shell’s command line argument n+1. (Yes, n may be a multi-digit integer.) When run with multiple command line arguments, $0 should be replaced by the script name. If n is larger than the number of command line arguments, replace the $n with the empty string. (The empty string really means that the $n pattern in the command is just removed from the command.) When the shell is run without any arguments, that is during an interactive execution, $0 should be replaced with the name of the shell. All other $n values are replaced by the empty string.
  • “expand()” should also replace $# by the base 10 ascii repre- sentation of the number of arguments. That is, the number of arguments indexed from $0 to $n-1 where n is the value of $#. For example, if your msh was run using the command: msh script_name a b c d $0 should be replaced by “script name”, $1 by “a” and so forth. Also $# should be replaced by 5. (Note: the argc parameter to msh’s main() function would be 6.)
  • Implement a new built-in command with the syntax: shift [n] that shifts all arguments starting with $1 by n values. That is, parameter i+n now becomes parameter i and for i = 1 to $#-n.

Argument 2 is b. Argument 3 is c. Argument 4 is d. Number of arguments is 4. Argument 1 is d. Argument 2 is e. Argument 3 is f. Argument 4 is. Number of arguments is 5. Argument 1 is c. Number of arguments is 7. Argument 1 is a.

Note: Good implementations of shift and unshift will not copy data in the mainargv variable or change the mainargv variable. Note: While the above script has the $n variables near the end of the line, they may appear anywhere in the line and there may more than one replacement needed.

  • Add the following other special expansion processing to “expand()”.
    • Expand $? by replacing it with the base 10 ascii equivalent of the exit value of the last command. If the command did not exit normally, use 127 for the exit value. (This should already done by the “exit(127)” after the execvp().) Note, if the last command was a successful built-in command, $? is replaced by 0. If the last command was an unsuccessful built-in command, $? is replaced by 1.
    • Add a limited wildcard expansion capability. The wildcard char- acter for your shell is ’’. In the simple version, the ’’ appears by itself. White space (or the beginning or end of the line) must be on either side of the ’’. In this case, replace the ’’ by names of all the files in the current working directory who’s name do not start with a period. In the more complex version, if the ’’ appears with a leading white space and trailing non-white space, that is something like “ xyz ”, replace the ’’ and the following characters with all file names in the current directory that end with the context characters. Note: a double quote ends the con- text characters. Finally, the sequence ’*’ should put only the ’’ in the expanded string. (Again, do not use file names that

start with a period.) The file names placed into the expanded string must be separated by a single space character between file names. You should not have leading or trailing spaces. Note: If the trailing characters include a slash (/), generate an error message and stop the processing of the current line. Also note: If no file names are matched, the pattern must be put in the ex- panded string. A final note: A double quote before a star should be treated as having a leading space. Finally, don’t use a regu- lar expression matching library to implement wildcard expansion. You are to use opendir(3) and friends.

  • Add a new built-in command “sstat file [file ...]” that prints the in- formation from stat(2) system call for each file on the command line. Your output for each file must be as follows: - the file name - the user name (not the uid) - the group name (not the gid) - the permission list including file type as printed by ls(1). - the number of links - the size in bytes - the modification time (use localtime(3) and asctime(3))

These fields should be separated by a single space between the fields. Each file should be listed on a separate line. If there is no associated user name or group name, print the uid or gid in numerical form. (Hint: man strmode)

  • Add no other features.
  • If you add a new file to your repository, don’t forget the RCS id in your new file and to follow my style guide.
  • Tag your files ASSIGNMENT-4.
  • Again, I will grade your assignment by checking out your sources, running make and then testing your shell.
  • Turn a printed copy of your sources and a printed copy of your own tests.