Академический Документы
Профессиональный Документы
Культура Документы
v0
P R E L I M I N A R Y S P E C I F I C A T I O N
CPSC 323 Homework #5 The Shell Game: Sister Sue Saw B-Shells ...
REMINDERS: Do not under any circumstances copy another person's code or give
a copy of your code to another person. After discussing the assignment with
another person (such discussions should be noted in your log file), do not
take any written or electronic record away and engage in a full hour of mind-
numbing activity before you work on it again. Sharing with another person ANY
written or electronic document related to the course (e.g., code or test cases)
is a violation of this policy.
(60 points) Bash is a simple shell, a baby brother of the Bourne-again shell
bash, and offers a limited subset of bash's functionality:
- directory manipulation:
cd dirName
cd (equivalent to "cd $HOME"; HOME is environment variable)
pushd dirName (other forms are not implemented)
popd (other forms are not implemented)
Once the command line has been parsed, the exact semantics of Bash are those
of bash, except for the status variable and the items noted below.
https://zoo.cs.yale.edu/classes/cs323/current/f17h5.v0 1/5
11/29/2017 https://zoo.cs.yale.edu/classes/cs323/current/f17h5.v0
Use the submit command to turn in your log file and the source files for
Bash (including a Makefile, but not mainBash.* or parse.*) as assignment 5.
YOU MUST SUBMIT YOUR FILES (INCLUDING THE LOG FILE) AT THE END OF ANY SESSION
WHERE YOU HAVE SPENT AT LEAST ONE HOUR WRITING OR DEBUGGING CODE, AND AT LEAST
ONCE EVERY HOUR DURING LONGER SESSIONS. (All submissions are retained.)
Notes
~~~~~
1. [Matthew & Stones, Chapter 2] contains a more complete description of bash,
including environment variables, the various I/O redirection operators,
pipelines, command operators, command terminators, and subcommands; and
"man bash" and "info bash" contain more information. But bear in mind
that there are many features that Bash does not implement. Moreover, the
behavior of Bash may not match bash in some cases, including (this list will
expand as we learn of discrepancies):
a. bash has both shell variables and environment variables. A command like
% NAME=VALUE
assigns VALUE to the shell variable NAME, and thereafter sequences like
$NAME are expanded to VALUE as commands are parsed; while a command like
% NAME=VALUE printenv NAME
assigns VALUE to the environment variable NAME in the process that is
executing printenv (but NAME is not defined in the parent shell). Bash
only supports the latter construct.
b. bash allows multiple input, output, and error redirections, with the last
encountered taking precedence. In Bash the parse() function issues an
error message instead.
e. bash and Bash report background commands and reaped zombies differently.
g. In Bash the directory stack used by cd, pushd, and popd contains the
absolute pathnames returned by getcwd() or get_current_dir_name(). In
bash these pathnames are "massaged". For example, if you cd to /c/cs323
the top directory name on the stack is /home/classes/cs323, not /c/cs323.
2. An EOF (CTRL-D in column 1) makes Bash exit since getline() returns NULL.
4. Bash uses perror() (see "man perror") to report errors from system calls.
It may ignore error returns from close(), dup(), dup2(), setenv(), wait(),
https://zoo.cs.yale.edu/classes/cs323/current/f17h5.v0 2/5
11/29/2017 https://zoo.cs.yale.edu/classes/cs323/current/f17h5.v0
waitpid(), getcwd(), and get_current_dir_name(), but not from chdir(),
execvp(), fork(), open(), and pipe().
All error messages are written to stderr and are one line long.
5. For simplicity, process() may ignore the possibility of error returns from
malloc() and realloc(). However, all storage that it allocates must still
be reachable after it returns to main().
8. You may find mkstemp() or tmpfile() useful when implementing HERE documents.
Note: Deleting an open file does not close the file descriptor (see "man 2
unlink").
9. Hwk5/mainBash.c contains functions that you may find useful for debugging:
* dumpList() dumps a token list
* dumpTree() dumps a parse tree of CMD structures
If the environment variable DUMP_LIST (DUMP_TREE) exists, then Bash
dumps the token list using dumpList() (the parse tree using dumpTree()) .
Fine Points
~~~~~~~~~~~
1. For a simple command, the status is either the status of the program
executed (*) or the global variable errno (if some system call failed
while setting up to execute the program).
For a pipeline, the status is that of the latest (that is, rightmost) stage
to fail, or 0 if the status of every stage is true. (This is the behavior
of bash with the pipefail option enabled.)
For a subcommand, the status is that of the last simple command, pipeline,
or subcommand to be executed as part of that subcommand.
Note that this status may differ from that reported by bash. The command
% /c/cs323/Hwk5/Tests/exit N
will exit with the status N.
3. The command separators && and || have the same precedence, lower than |, but
higher than ; or &.
5. When a redirection fails, Bash does not execute the command. The status of
the command (or pipeline stage) is the errno of the system call that failed.
Bash reaps zombies periodically (that is, at least once during each call to
process()) to avoid running out of processes. When doing so, it writes the
process id and status to stderr using the format "Completed: %d (%d)\n".
7. To make programming Bash more challenging, Bash may not use waitpid() or any
other system call that specifies the pid of the process whose death it is
awaiting. That is, it may only use wait() and waitpid(-1,...). Unlike the
usual tests, the test of this constraint will deduct 4 points from the total
score if it detects a violation.
8. gdb can follow child processes. See the gdb manual (link on the class web
page) for details.
Limitations
~~~~~~~~~~~
The following features will be worth at most the number of points shown:
* (20 points) pipelines
* (12 points) &&, ||, and &
* (12 points) subcommands
* (12 points) the cd built-in
* (12 points) the status variable $?
* (10 points) HERE documents
* ( 6 points) Reaping zombies
Here "at most" signals a crude upper bound intended to give more flexibility
while developing the test script and to allow interactions among features.
CS-323-11/11/17
https://zoo.cs.yale.edu/classes/cs323/current/f17h5.v0 5/5