Software Tools, Getting Started

Overview

This post is the first in a series revisiting the programs described in the 1981 book by Brian W. Kernighan and P. J. Plauger’s called Software Tools in Pascal. The book is available from the Open Library and physical copies are still (2020) commonly available from used book sellers. The book was an early text on creating portable command line programs.

In this series I present the K & P (i.e. Software Tools in Pascal) programs re-implemented in Oberon-07. I have testing my implementations using Karl Landström’s OBNC compiler and his implementation of the Oakwood Guide’s modules for portable Oberon programs. Karl also provides a few additional modules for working in a POSIX environment (e.g. BSD, macOS, Linux, Windows 10 with Linux subsystem). I have also tested these programs with Mike Spivey’s Oxford Oberon Compiler an aside from the differences file extensions that both compilers use the source code works the same.

NOTE: OBNC compiler is the work of Karl Langström, it is portable across many systems where the C tool chain is available.

NOTE: POSIX defines a standard of compatibility inspired by UNIX, see https://en.wikipedia.org/wiki/POSIX

Getting Started.

Chapter one in K & P is the first chapter that presents code. It introduces some challenges and constraints creating portable Pascal suitable for use across hardware architectures and operating systems. In 1981 this included mainframes, minicomputers as well as the recent evolution of the microcomputer. The programs presented build up from simple to increasingly complex as you move through the book. They provide example documentation and discuss their implementation choices. It is well worth reading the book for those discussions, while specific to the era, mirror the problems program authors face today in spite of the wide spread success of the POXIS model, the consolidation of CPU types and improvements made in development tools in the intervening decades.

Through out K & P you’ll see the bones of many POSIX commands we have today.

Programs from this chapter include:

  1. copyprog, this is like “cat” in a POSIX system
  2. charcount, this is like the “wc” POSIX command using the “-c” option
  3. linecount, this is like the “wc” POSIX command using the “-l” option
  4. wordcount, this is like the “wc” POSIX command using the “-w” option
  5. detab, converts tabs to spaces using tab stops every four characters in a line

All programs in this chapter rely solely on standard input and output. Today’s reader will notice an absence to common concepts in today’s command line programs. First is the lack of interaction with command line parameters, the second is no example take advantage of environment variables. These operating system features were not always available across operating systems of the early 1980s. Finally I’d like to point out a really nice feature included in the book. It is often left out as a topic in programming books. K & P provide example documentation. It’s structure like an early UNIX man page. It very clear and short. This is something I wish all programming texts at least mentioned. Documentation is important to the program author because it clarifies scope of the problem being tackled and to the program user so they understand what they are using.

1.1. File Copying

Here’s how K & P describe “copyprog.pas” (referred to as “copy” in the documentation).

  1. PROGRAM
  2. copy copy input to output
  3. USAGE
  4. copy
  5. FUNCTION
  6. copy copies its input to its output unchanged. It is useful for copying
  7. from a terminal to a file, from file to file, or even from terminal to
  8. terminal. It may be used for displaying the contents of a file, without
  9. interpretation or formatting, by copying from the file to terminal.
  10. EXAMPLE
  11. To echo lines type at your terminal.
  12. copy
  13. hello there, are you listening?
  14. **hello there, are you listening?**
  15. yes, I am.
  16. **yes, I am.**
  17. <ENDFILE>

The source code for “copyprog.pas” is shown on page 9 of K & P. First the authors introduce the copy procedure then a complete the section introducing it in context of the complete Pascal program. After this first example K & P leave implementation of the full program up to the reader.

The body of the Pascal program invokes a procedure called copy which reads from standard input character by character and writes to standard output character by character without modification. Two supporting procedures are introduced, “getc” and “putc”. These are shown in the complete program listing on page 9. They are repeatedly used through out the book. One of the really good aspects of this simple program is relying on the idea of standard input and output. This makes “copyprog.pas” a simple filter and template for writing many of the programs that follow. K & P provide a good explanation for this simple approach. Also note K & P’s rational for working character by character versus line by line.

My Oberon-07 version takes a similar approach. The module looks remarkably similar to the Pascal but is shorter because reading and writing characters are provided for by Oberon’s standard modules “In” and “Out”. I have chosen to use a “REPEAT/UNTIL” loop over the “WHILE” loop used by K & P is the attempt to read from standard input needs to happen at least once. Note in my “REPEAT/UNTIL” loop’s terminating condition. The value of In.Done is true on successful read and false otherwise (e.g. you hit an end of the file). That means our loop must terminate on In.Done # TRUE rather than In.Done = TRUE. This appears counter intuitive unless you keep in mind our loop stops when we having nothing more to read, rather than when we can continue to read. It In.Done means the read was successful and does not mean “I’m done and can exit now”. Likewise before writing out the character we read, it is good practice to check the In.Done value. If In.Done is TRUE, I know can safely display the character using Out.Char(c);.

  1. MODULE CopyProg;
  2. IMPORT In, Out;
  3. PROCEDURE copy;
  4. VAR
  5. c : CHAR;
  6. BEGIN
  7. REPEAT
  8. In.Char(c);
  9. IF In.Done THEN
  10. Out.Char(c);
  11. END;
  12. UNTIL In.Done # TRUE;
  13. END copy;
  14. BEGIN
  15. copy();
  16. END CopyProg.

Limitations

This program only works with standard input and output. A more generalized version would work with named files.

1.2 Counting Characters

  1. PROGRAM
  2. charcount count characters in input
  3. USAGE
  4. charcount
  5. FUNCTION
  6. charcount counts the characters in its input and writes the total
  7. as a single line of text to the output. Since each line of text is
  8. internally delimited by a NEWLINE character, the total count is the
  9. number of lines plus the number of characters within each line.
  10. EXAMPLE
  11. charcount
  12. A single line of input.
  13. <ENDFILE>
  14. 24

On page 13 K & P introduces their second program, charcount. It is based on a single procedure that reads from standard input and counts up the number of characters encountered then writes the total number found to standard out followed by a newline. In the text only the procedure is shown, it is assumed you’ll write the outer wrapper of the program yourself as was done with the copyprog program. My Oberon-07 version is very similar to the Pascal. Like in the our first “CopyProg” we will make use of the “In” and “Out” modules. Since we will need to write an INTEGER value we’ll also use “Out.Int()” procedure which is very similar to K & P’s “putdec()”. Aside from the counting this is very simple like our first program.

  1. MODULE CharCount;
  2. IMPORT In, Out;
  3. PROCEDURE CharCount;
  4. VAR
  5. nc : INTEGER;
  6. c : CHAR;
  7. BEGIN
  8. nc := 0;
  9. REPEAT
  10. In.Char(c);
  11. IF In.Done THEN
  12. nc := nc + 1;
  13. END;
  14. UNTIL In.Done # TRUE;
  15. Out.Int(nc, 1);
  16. Out.Ln();
  17. END CharCount;
  18. BEGIN
  19. CharCount();
  20. END CharCount.

Limitations

The primary limitation in counting characters is most readers are interested in visible character count. In our implementation even non-printed characters are counted. Like our first program this only works on standard input and output. Ideally this should be written so it works on any file including standard input and output. If the reader implements that it could become part of a package on statistical analysis of plain text files.

1.3 Counting Lines

  1. PROGRAM
  2. linecount count lines in input
  3. USAGE
  4. linecount
  5. FUNCTION
  6. linecount counts the lines in its input and write the total as a
  7. line of text to the output.
  8. EXAMPLE
  9. linecount
  10. A single line of input.
  11. <ENDFILE>
  12. 1

linecount, from page 15 is very similar to charcount except adding a conditional count in the loop for processing the file. In our Oberon-07 implementation we’ll check if the In.Char(c) call was successful but we’ll add a second condition to see if the character read was a NEWLINE. If it was I increment our counter variable.

  1. MODULE LineCount;
  2. IMPORT In, Out;
  3. PROCEDURE LineCount;
  4. CONST
  5. NEWLINE = 10;
  6. VAR
  7. nl : INTEGER;
  8. c : CHAR;
  9. BEGIN
  10. nl := 0;
  11. REPEAT
  12. In.Char(c);
  13. IF In.Done & (ORD(c) = NEWLINE) THEN
  14. nl := nl + 1;
  15. END;
  16. UNTIL In.Done # TRUE;
  17. Out.Int(nl, 1);
  18. Out.Ln();
  19. END LineCount;
  20. BEGIN
  21. LineCount();
  22. END LineCount.

Limitations

This program assumes that NEWLINE is ASCII value 10. Line delimiters vary between operating systems. If your OS used carriage returns without a NEWLINE then this program would not count lines correctly. The reader could extend the checking to support carriage returns, new lines, and carriage return with new lines and cover most versions of line endings.

1.4 Counting Words

  1. PROGRAM
  2. wordcount count words in input
  3. USAGE
  4. wordcount
  5. FUNCTION
  6. wordcount counts the words in its input and write the total as a
  7. line of text to the output. A "word" is a maximal sequence of characters
  8. not containing a blank or tab or newline.
  9. EXAMPLE
  10. wordcount
  11. A single line of input.
  12. <ENDFILE>
  13. 5
  14. BUGS
  15. The definition of "word" is simplistic.

Page 17 brings us to the wordcount program. Counting words can be very nuanced but here K & P have chosen a simple definition which most of the time is “good enough” for languages like English. A word is defined simply as an run of characters separated by a space, tab or newline characters. In practice most documents will work with this minimal definition. It also makes the code straight forward. This is a good example of taking the simple road if you can. It keeps this program short and sweet.

If you follow along in the K & P book note their rational and choices in arriving at there solutions. There solutions will often balance readability and clarity over machine efficiency. While the code has progressed from “if then” to “if then else if” logical sequence, the solution’s modeled remains clear. This means the person reading the source code can easily verify if the approach chosen was too simple to meet their needs or it was “good enough”.

My Oberon-07 implementation is again very simple. Like in previous programs I still have an outer check to see if the read worked (i.e. “In.Done = TRUE”), otherwise the conditional logic is the same as the Pascal implementation.

  1. MODULE WordCount;
  2. IMPORT In, Out;
  3. PROCEDURE WordCount;
  4. CONST
  5. NEWLINE = 10;
  6. BLANK = 32;
  7. TAB = 9;
  8. VAR
  9. nw : INTEGER;
  10. c : CHAR;
  11. inword : BOOLEAN;
  12. BEGIN
  13. nw := 0;
  14. inword := FALSE;
  15. REPEAT
  16. In.Char(c);
  17. IF In.Done THEN
  18. IF ((ORD(c) = BLANK) OR (ORD(c) = NEWLINE) OR (ORD(c) = TAB)) THEN
  19. inword := FALSE;
  20. ELSIF (inword = FALSE) THEN
  21. inword := TRUE;
  22. nw := nw + 1;
  23. END;
  24. END;
  25. UNTIL In.Done # TRUE;
  26. Out.Int(nw, 1);
  27. Out.Ln();
  28. END WordCount;
  29. BEGIN
  30. WordCount();
  31. END WordCount.

1.5 Removing Tabs

  1. PROGRAM
  2. detab convert tabs into blanks
  3. USAGE
  4. detab
  5. FUNCTION
  6. detab copies its input to its output, expanding the horizontal
  7. tabs to blanks along the way, so that the output is visually
  8. the same as the input, but contains no tab characters. Tab stops
  9. are assumed to be set every four columns (i.e. 1, 5, 9, ...), so
  10. each tab character is replaced by from one to four blanks.
  11. EXAMPLE
  12. Usaing "->" as a visible tab:
  13. detab
  14. ->col 1->2->34->rest
  15. col 1 2 34 rest
  16. BUGS
  17. detab is naive about backspaces, vertical motions, and
  18. non-printing characters.

The source code for “detab” can be found on page 24 in the last section of chapter 1. detab removes tabs and replaces them with spaces. Rather than a simple “tab” replaced with four spaces detab preserves a concept found on typewriters called “tab stops”. In 1981 typewrites were still widely used though word processing software would become common. Supporting the “tab stop” model means the program works with what office workers would expect from older tools like the typewriter or even the computer’s teletype machine. I think this shows an important aspect of writing programs. Write the program for people, support existing common concepts they will likely know.

K & P implementation includes separate source files for setting tab stops and checking a tab stop. The Pascal K & P wrote for didn’t support separate source files or program modules. Recent Pascal versions did support the concept of modularization (e.g. UCSD Pascal). Since and significant goal of K & P was portability they needed to come up with a solution that worked on the “standard” Pascal compilers available on minicomputers and mainframes and not write their solution to a specific Pascal system like UCSD Pascal (see Appendix, “IMPLEMENTATION PRIMITIVES page 315). Modularization facilitates code reuse and like information hiding is an import software technique. Unfortunately the preprocessor approach doesn’t support information hiding.

To facilitate code reuse the K & P book includes a preprocessor as part of the Pascal development tools (see page 71 for implementation). The preprocessor written in Pascal was based on the early versions of the “C” preprocessor they had available in the early UNIX systems. Not terribly Pascal like but it worked and allowed the two files to be shared between this program and one in the next chapter.

Oberon-07 of course benefits from all of Wirth’s language improvements that came after Pascal. Oberon-07 supports modules and as such there is no need for a preprocessor. Because of Oberon-07’s module support I’ve implemented the Oberon version using two files rather than three. My main program file is “Detab.Mod”, the supporting library module is “Tabs.Mod”. “Tabs” is where I define our tab stop data structure as well as the procedures that operating on that data structure.

Let’s look at the first part, “Detab.Mod”. This is the module that forms the program and it features an module level “BEGIN/END” block. In that block I call “Detab();” which implements the program’s functionality. I import “In”, “Out” as before but I also import “Tabs” which I will show next. Like my previous examples I validate the read was successful before proceeding with the logic presented in the original Pascal and deciding what to write to standard output.

  1. MODULE Detab;
  2. IMPORT In, Out, Tabs;
  3. CONST
  4. NEWLINE = 10;
  5. TAB = 9;
  6. BLANK = 32;
  7. PROCEDURE Detab;
  8. VAR
  9. c : CHAR;
  10. col : INTEGER;
  11. tabstops : Tabs.TabType;
  12. BEGIN
  13. Tabs.SetTabs(tabstops); (* set initial tab stops *)
  14. col := 1;
  15. REPEAT
  16. In.Char(c);
  17. IF In.Done THEN
  18. IF (ORD(c) = TAB) THEN
  19. REPEAT
  20. Out.Char(CHR(BLANK));
  21. col := col + 1;
  22. UNTIL Tabs.TabPos(col, tabstops);
  23. ELSIF (ORD(c) = NEWLINE) THEN
  24. Out.Char(c);
  25. col := 1;
  26. ELSE
  27. Out.Char(c);
  28. col := col + 1;
  29. END;
  30. END;
  31. UNTIL In.Done # TRUE;
  32. END Detab;
  33. BEGIN
  34. Detab();
  35. END Detab.

Our second module is “Tabs.Mod”. It provides the supporting procedures and definition of the our “TabType” data structure. For us this is the first time we write a module which “exports” procedures and type definitions. If you are new to Oberon, expected constants, variables and procedures names have a trailing “*“. Otherwise the Oberon compiler will assume a local use only. This is a very powerful information hiding capability and what allows you to evolve a modules’ internal implementation independently of the programs that rely on it.

  1. MODULE Tabs;
  2. CONST
  3. MAXLINE = 1000; (* or whatever *)
  4. TYPE
  5. TabType* = ARRAY MAXLINE OF BOOLEAN;
  6. (* TabPos -- return TRUE if col is a tab stop *)
  7. PROCEDURE TabPos*(col : INTEGER; VAR tabstops : TabType) : BOOLEAN;
  8. VAR res : BOOLEAN;
  9. BEGIN
  10. res := FALSE; (* Initialize our internal default return value *)
  11. IF (col >= MAXLINE) THEN
  12. res := TRUE;
  13. ELSE
  14. res := tabstops[col];
  15. END;
  16. RETURN res
  17. END TabPos;
  18. (* SetTabs -- set initial tab stops *)
  19. PROCEDURE SetTabs*(VAR tabstops: TabType);
  20. CONST
  21. TABSPACE = 4; (* 4 spaces per tab *)
  22. VAR
  23. i : INTEGER;
  24. BEGIN
  25. (* NOTE: Arrays in Oberon start at zero, we want to
  26. stop at the last cell *)
  27. FOR i := 0 TO (MAXLINE - 1) DO
  28. tabstops[i] := ((i MOD TABSPACE) = 0);
  29. END;
  30. END SetTabs;
  31. END Tabs.

NOTE: This module is used by “Detab.Mod” and “Entab.Mod” and provides for common type definitions and code reuse. We exported TabType, TabPos and SetTabs. Everything else is private to this module.

In closing

This post briefly highlighted ports of the programs presented in Chapter 1 of “Software Tools in Pascal”. Below are links to my source files of the my implementations inspired by the K & P book. Included in each Oberon module source after the module definition is transcribed text of the program documentation as well as transcribed text of the K & P Pascal implementations. Each file should compiler without modification using the OBNC compiler. By default the OBNC compiler will use the module’s name as the name of the executable version. I I have used mixed case module names, if you prefer lower case executable names use the “-o” option with the OBNC compiler.

  1. obnc -o copy CopyProg.Mod
  2. obnc -o charcount CharCount.Mod
  3. obnc -o linecount LineCount.Mod
  4. obnc -o wordcount WordCount.Mod
  5. obnc -o detab Detab.Mod

If you happen to be using The Oxford Oberon Compiler you need to rename the files ending in “.Mod” to “.m” and you can compiler with the following command.

  1. obc -07 -o copyprog CopyProg.m
  2. obc -07 -o charcount CharCount.m
  3. obc -07 -o linecount LineCount.m
  4. obc -07 -o wordcount WordCount.m
  5. obc -07 -o detab Tabs.m Detab.m

Note the line for compiling “Detab” with obc, your local modules need to become before the module calling them.

Next