Software Tools source code
Autor: Andre Adrian
Version: 31.may.2011
Introduction
The book "Software Tools" was written in 1976 by Brian W. Kernighan and
P. J. Plauger. For a C programmer it is wise to read all books that
were co-authored by Kernighan. In "Software Tools" the authors used the
programming language RATFOR. RATFOR was implemented as a FORTRAN
pre-processor. For easy digestion of the pearls of programming wisdom
in this book, the source code was ported (transcribed) from RATFOR to
ANSI-C. Beginners in the area of programming shall start with the C-tutorial,
another document by Kernighan, which is now available for ANSI-C.
Computer science is still a new department at an university. The first
computer science historians begin now to preserve the early heritage of
the field. One of these private preservation actions is to bring again
to life the source code from the book "Software tools".
Contents
detab - Removing Tabs
The program detab is presented in chapter 1.5, Removing Tabs, starting
at page 18.
#include <stdio.h>
enum {
MAXLINE = 100,
YES = 1,
NO = 0,
TAB = 9,
NEWLINE = 10,
BLANK = 32,
};
/* tabpos -- return YES if col is a tab stop */
int tabpos(int col, int tabs[])
{
if (col >= MAXLINE)
return YES;
else
return tabs[col];
}
/* settab -- set initial tab stops */
void settab(int tabs[])
{
int i;
for (i = 0; i < MAXLINE; i = i + 1)
if (1 == i % 8)
tabs[i] = YES;
else
tabs[i] = NO;
return;
}
/* detab -- convert tabs to equivalent number of blanks */
int main()
{
int c;
int col, tabs[MAXLINE];
settab(tabs);
/* set initial tab stops */
col = 1;
while ((c = getchar()) != EOF)
if (TAB == c)
do {
putchar(BLANK);
col
= col + 1;
}
while (tabpos(col,
tabs) != YES);
else if (NEWLINE == c) {
putchar(NEWLINE);
col = 1;
} else {
putchar(c);
col = col + 1;
}
return 0;
}
entab - Putting Tabs
Back
The program entab is presented in chapter 2.1, Putting Tabs Back,
starting
at page 35.
#include <stdio.h>
enum {
MAXLINE = 100,
YES = 1,
NO = 0,
TAB = 9,
NEWLINE = 10,
BLANK = 32,
};
/* tabpos -- return YES if col is a tab stop */
int tabpos(int col, int tabs[])
{
if (col >= MAXLINE)
return YES;
else
return tabs[col];
}
/* settab -- set initial tab stops */
void settab(int tabs[])
{
int i;
for (i = 0; i < MAXLINE; i = i + 1)
if (1 == i % 8)
tabs[i] = YES;
else
tabs[i] = NO;
return;
}
/* entab -- replace blanks by tabs and blanks */
int main()
{
int c;
int col, i, newcol, tabs[MAXLINE];
settab(tabs);
col = 1;
for (;;) {
newcol = col;
while (BLANK == (c = getchar()))
{ /* collect blanks */
newcol = newcol + 1;
if (YES ==
tabpos(newcol, tabs)) {
putchar(TAB);
col
= newcol;
}
}
for (; col < newcol; col = col + 1)
putchar(BLANK); /* output
leftover blanks */
if (EOF == c)
break;
putchar(c);
if (NEWLINE == c)
col = 1;
else
col = col + 1;
}
return 0;
}
About the author
The author is a computer science engineer of the second generation. He
did not learn the programming language C on a PDP11 with the AT&T
compiler cc in the 1970s, but in the 1980s on an Atari ST with the
Lattice C compiler. Interest in the history of computer science is only
a hobby, for his living the author maintains and creates state of the
art software. Today, many people call it progress if things are done
different. For the author, progress is to do things better. Doing
things better is much harder then doing things different. To make
things better, it helps to look into the past and learn from the
engineers that worked in the field before yourself.