Assignment 9: Scripting Script

Overview

In this assignment, we will see some of the benefits of embedding a programming language inside an existing (and otherwise fairly ordinary) program, to make the program more extensible.

You will also get to have some fun with Lua, as you write a program that interposes between the user and terminal.

Background: Interposition and embedding

When you interact with a program on the command line (for example, an editor or the lua interpreter), there are three important entities: you, the program providing the terminal window (which is directly talking to the keyboard and the screen), and the command-line program.

Normal Interaction for a Command-Line Tool

Interposition is the idea of sliding code between two things that were previously coupled together. The Unix script program (from the lab) is an example of interposition: script slides between you and the shell, so that it can log your shell session.

Interposing Script to Get Logging

The script command forwards any input from the user along to the command-line program (which is often called the “subprocess”). Output from the program is sent to the user as usual, but also copied to the log.

The script command is usually used to record a log of a terminal session, but what if we wanted to do something more interesting than this simple logging behavior? Unfortunately, as it stands, we would need to modify the C source for script, add our changes, then recompile it, which is cumbersome. In other words, as it stands, the script program is not very extensible.

One way, perhaps a surprising way, to add extensibility to a program is to embed a programming language inside it!

How can embedding a programming language in an application make that application more extensible? The key is that—by including a program evaluator (for that is what a programming language is) as part of an existing application—users of the application can write their own little programs. These little programs can then be evaluated by the main application, all without having to modify or recompile that application!

In this assignment, we will make script more extensible by embedding Lua in it.

Materials

Use the assignment workflow and the link provided on Piazza to get access to this week’s files.

We recommend that you do your work on the CS machine called shadowfax. This machine is similar to knuth: You access it in the same way (ssh <username>@shadowfax.cs.hmc.edu), and all your files are available there. The main difference is that shadowfax has the latest version of Lua installed, whereas knuth does not.

Assignment

We have organized the assignment into separate parts:

  • Part 1: Understand Lua embedding
  • Part 2: Embed Lua in script
  • Part 3: Take advantage of extensibility