STinC++ rewrites the programs in Software Tools in Pascal using C++
Way back at the start of the year, I expressed my excitement at reaching chapter six, in which Kernighan and Plauger start to pull together the various threads of the earlier chapters and write an editor.
Well, it’s been a long, logistically tricky summer preceded by a long, logistically tricky spring, and well, you know. Anyway, I’ve been looking forward to this for three years, I am still excited, and the logistic hoo-har is behind us, at least for now, so I’m going to make a start.
Before I go any further, I want to be absolutely clear that the world does not need another editor. It especially does not need the kind of editor I’m going to write. But I’m going to any way. I shouldn’t, but I’m going to.
Many of the tools we use (and have written for STinC++) are line oriented, think
wc, and so on. They read a line of text, operate on that line, maybe produce a line of output, then do it all again until every line has been read.
Today, we almost never use interactive line oriented programs, other than perhaps in the most extreme circumstances, but they were once commonplace. In chapter six of Software Tools in Pascal, Kernighan and Plauger build a line oriented editor.
The editor we present here is not a "screen editor" - it takes no advantage of the ability of many terminals to add, delete and change characters and lines on a screen with a cursor. Such editors are often convenient, but they are typically bigger, slower, and harder to make portable than ones that make no assumptions about terminals. They also make more demands on the underlying operating system than we are prepared to deal with here.
What I didn’t appreciate until quite recently was what terminal might mean here. Software Tools in Pascal was published in 1981. The book’s readership were probably students using a remote terminal connected to some big mainframe at their university. There’s every chance that terminal was a something recognisable as a computer, like this VT100 terminal
On the other hand, it could have looked like this
This is a Teletype Model 33, which was launched in 1963. The invention of devices like this fundamentally changed the way programmers communicated with their computers. Before terminals like this computers were programmed by wiring up circuits - that’s what they did at Bletchley Park, by toggling the bits into memory, or by punching holes into special cards or tapes. But now, with a Teletype Model 33, you type something in and lo! the computer could respond (almost) immediately by printing onto paper. You could interact with the computer in real time, you could issue commands and run programs and actually see the results.
But you could only do it a line at a time.
The Model 33 was only discontinued as a commercial product in 1981. There were thousands and thousands sold and they must have common well into the 80s, if not longer. There’s every chance, then, Kernighan and Plauger’s readers were trying to work through the book without going deaf in the process.
(The shadows of the Model 33 and the VT100 are still all over our modern computing environments, from "printing" to the screen, to
/dev/tty, to default line lengths, and so on and so on. You can go down a proper rabbit hole.)
Kernighan and Plauger’s editor, then, is a line oriented editor. You enter commands, typically a few keystrokes, and they operate on a group of one or more lines. Typing
p, for example, prints the current line, while
1,3p prints lines 1, 2 and 3. The command
i inserts a new line, while
d deletes the current line. They hook in the text pattern engine from chapter five too, with
s/pat/new/ replacing occurrences of pat with new.
If this feels at all familiar, it’s because they modelled their editor on the Unix editor
ed. In turn,
ex, and the full screen visual interface to
ex is, of course,
vi. We’re all running magnificently powerful Linux and Mac boxes, with their hyper-threading, gigabytes of memory, and 4k multi-monitor graphical displays, but they’ve all still got line editors installed in case you need to connect up to a mechanical terminal via a serial port.
Context searches and substitutions are specified by writing text patterns, following the same rules for building patterns as used by
Line numbers are formed by the following components:
Components may be combined with
Line numbers are separated by commas or semicolons; a semicolon sets the current line to the most recent line number preceding.
Commands may be preceded by an arbitrary number of lines numbers (except for
In alphabetical order, the command and their default line numbers are
Text entered with
The global prefixes cause repeated execution of a command, one for each line that matches (
If the command line argument
Don’t be silly.
Yes it is. I’m going to spend the afternoon programming.
See you soon, I hope.
This whole endeavour relies on Software Tools in Pascal and Software Tools, both by Brian W Kernighan and PJ Plauger. I love these books and commend them to you. They are both still in print, but new copies are, frankly, ridiculously expensive. Happily, there are plenty of second-hand copies around, or you can borrow them from The Internet Archive using the links above.
For this project I’ve been using JetBrain’s CLion, which I liked enough to buy and keep renewing a license for. I use it all the time now.