Jez Higgins

Freelance software grandad
software created
extended or repaired

Follow me on Mastodon
Applications, Libraries, Code
Talks & Presentations

Hire me

Older posts are available in the archive or through tags.


The Forest Road Reader, No 2.68 : STinC++ - Chapter Six Kick Off

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.

Line editing

Many of the tools we use (and have written for STinC++) are line oriented, think grep, 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

DEC VT100 Terminal

On the other hand, it could have looked like this

Teletype Model 33

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.)

The Editor

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, ed inspired/provoked 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.



edit edit text files


edit [file]


edit is an interactive text editor that reads command lines from its input and writes display information, upon command, to its output. It works by reading text files on command into an internal "buffer" (which may be quite large), displaying and modifying the buffer contents by other commands, then writing all or part of the buffer to text files, also on command. The buffer is organisated as a sequence of lines, numbered from 1; lines are implicitly renumbered as text is added or deleted.

Context searches and substitutions are specified by writing text patterns, following the same rules for building patterns as used by find. Substitutions specify replacement text following the same rules as used by the program change.

Line numbers are formed by the following components:

n a decimal number

. the current line ("dot")

$ the last line

/pattern/ a forward context search

\pattern\ a backward context search

Components may be combined with + or -, as in, for example

.+1 sum of . and 1

$-5 five lines before $

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 e, f and q, which require none be present). The last one or two are used as needed. If two line numbers are needed and only one is specified, it is used for both. If no line numbers are specified, a default rule is applied:

(.) use the current line

(.+1) use the next line

(.,.) use the current line for both line numbers

(1,$) use all lines

In alphabetical order, the command and their default line numbers are

(.) a append text after line (text follows)

(.,.) c change text (text follows)

(.,.) dp delete text

e file edit file after discarding all previous text, remember file name

f file print file name, remember file name

(.) i insert text before line (text follows)

(.,.) m line3 p move text to after line3

(.,.) p print text

q quit

(.) r file read file, appending after line

(.,.) s/pat/new/gp substitute new for occurrence of pat (g implies for each occurrence across line)

(1,$) w file write file (leaves current state unaltered)

(.) =p print line number

(.,+1) newline print one line

The trailing p, which is optional, causes the last affected line to be printed. Dot is set to the last affected line, except for f, w, and =, for which it is unchanged.

Text entered with a, c and i is terminated with a line containing just a ..

The global prefixes cause repeated execution of a command, one for each line that matches (g) or does not match (x) a specified text pattern:

(1,$) g/pattern/command (1,$) x/pattern/command

command can be anything but a, c, i or q, and may be preceded by line numbers as usual. Dot is set to the matched line before command is done.

If the command line argument file is present, then the editor behaves as if its input began with the command e file. The first filename used is remembered, so that a subsequent e, f, r, or w command can be written with no filename to refer to the remembered filename. A filename given with e or f replaces any remembered filename.


Don’t be silly.

That’s a lot

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.

Tagged code, and software-tools-in-c++

Jez Higgins

Freelance software grandad
software created
extended or repaired

Follow me on Mastodon
Applications, Libraries, Code
Talks & Presentations

Hire me

Older posts are available in the archive or through tags.