13.5.1. Program Notes for adj
This small text formatter is a nifty program for those of us
who use text editors. It allows you to set the maximum line width
and justify paragraphs and thus can be used to format mail
messages or simple letters.
The adj shell script does all the option setting,
although it could have been done by reading ARGV
in the BEGIN action. Using the shell to establish command-line
parameters is probably easier
for those who are already familiar with the shell.
The lack of comments in the adj.awk script makes this
script more difficult to read than some of the others.
The BEGIN procedure assigns three regular expressions to
variables: blankline, startblank,
startwords. This is a good technique
(one that you'll see used in lex specifications)
because regular expressions can be difficult to read
and the name of the variable makes it clear what it matches.
Remember that modern awks lets you supply a regular expression as
a string, in a variable.
There are three main procedures, which can be named by the variable
they match. The first is blankline, a procedure
which handles collected
text once a blank line is encountered. The second is startblank,
which handles lines that begin with whitespace (spaces or tabs). The third
is startwords, which handles a line of text.
The basic procedure is to read a line of text and determine how many
of the words in that line will fit, given the line width, outputting
those that will fit and saving those that will not in the variable
outline. When the next input line is read, the contents of
outline must be output before that line is output.
The adjust() function does the work of justifying the text
based on a command-line option specifying the format type.
All types except "l" (left-adjusted, right-ragged) need
to be filled. Therefore, the first thing this function does
is figure out how much "fill" is needed by subtracting
the length of the current line from the specified line length.
It makes excellent use of the sprintf() function
to actually do the positioning of the text. For instance,
to center text, the value of fill (plus 1) is divided by
2 to determine the amount of padding needed on each side of the
line. This amount is passed through the fmt variable
as the argument to sprintf():
fmt = "%" (fill+1)/2 "s%s"
line = sprintf( fmt, " ", line )
Thus, the space will be used to pad a field that is
the length of half the amount of fill needed.
If text is right-justified, the value of fill itself is used
to pad the field. Finally, if the format type is "b" (block),
then the function fillout is called to determine
where to add spaces that will fill out the line.
In looking over the design of the program,
you can see, once again, how the use of functions helps
to clarify what a program is doing.
It helps to think of the main procedure
as controlling the flow of input through the program
while procedures handle
the operations performed on the input.
Separating the "operations" from the flow control makes
the program readable and more easily maintained.
In passing, we're not sure why FS, the field separator, is set
to newline
in the BEGIN procedure. This means that the field and record
separators are the same (i.e., $0 and $1 are the same).
The split() function is
called to break the line into fields using tabs or spaces
as the delimiter.
nf = split( $0, word, "[ \t]+" )
It would seem that the field separator could have been set
to the same regular expression, as follows:
FS = "[ \t]+"
It would be more efficient to use the default field parsing.
Finally, using the match() function to find punctuation is inefficient;
it would have been better to use index().