bash

Useful Links

How I will run your code

Save your programs in a file called proj.sh in folders called proj1 and proj2 respectively for phases 1 and 2 of your project.

I will test your code in the same environment as the lab machines in MI 302, using the command
/bin/bash --norc proj.sh

Phase 2 Assignment

For phase 2 of your project, you will write a program that imitates some of the features of the popular linux utility make. You should submit your program in a folder called proj2, and be sure that it runs as described above.

Specification

Your program will read from a file called spec.txt (like a simplified makefile). This file will consist of a series of rules. Each rule is structured like

target : prereq1 prereq2 ...
  command1
  command2
  ...

You will notice a few components of the rule:

The meaning of the rule is that the target should be re-generated whenever every one of its prerequisites exists, and either (1) the target does not exist, or (2) at least one of the prerequisites is newer (in terms of file modification time) than the target. Re-generation just involves executing each of the commands in sequence.

The spec.txt file can also contain comment lines, which are indicated by starting the line with a # character. Note that comments must be whole lines; they cannot start in the middle of a rule, for instance.

Your program will read the spec.txt file and repeatedly re-generate targets according to the rules above, until no more targets can be created or re-generated. Note that target names are not specified as they normally are to make; instead, your program will essentially attempt to make every possible target, repeatedly.

Tips

I suggest you write your program in the following steps:

  1. Initially, don't allow comments, ignore prerequisites, and just read the file and write out the targets and the command for each target.
  2. Next, make your program read spec.txt once, and run the commands to generate every target once.
  3. Now read the prerequisites and only make the targets for which every prerequisite exists, in a single pass through spec.txt.
  4. Make is so your program only generates targets when all the prerequisites exist and at least one is newer than the target, as specified above.
  5. Now put all this in a loop, with a flag to detect when no targets were re-generated on a given pass and the program should terminate.
  6. Finally, add in support for comment lines.

Style Requirements

For full credit, your program should:

Example

Suppose we are in a directory that initially only contains the file spec.txt, consisting of:

a :
  echo "this will go into a" > a

b.txt : index.php a
  cp a b.txt
  head index.php >> b.txt

c : c.cpp
  g++ -o c c.cpp

index.php :
  wget "http://www.usna.edu/Users/cs/roche/courses/f11si413/project/index.php"

Running your program in this directory should first cause the files a and index.php to be generated, since those targets do not exist, but all of their prerequisites do (vacuously, since they have no prerequisites). They are generated, in any order, by running the echo and wget commands as specified.

After this, the b.txt file should be generated, since it does not exist, and now both of its prerequisites do. This will cause the cp and head commands to be run, as specified, and in order.

And then your program should terminate, since there is nothing else to do. Notice that the target c never gets generated, since its single prerequisite does not exist.