Filters, Almost Filters, and Other Command Line Delights
by Bill Buckels
June 21, 1989
Revised March 24, 2000

With the rise to popularity of the C programming language on the MS-DOS
platform in the 1980's, writing efficient utility programs for home
computers was no longer exclusively in the realm of machine language
(assembly language) programmers.

For the "Middle of the Road" programmer, this meant a "gateway" to
quickly constructing efficient utility programs utilizing his or her
existing programming knowledge without needing to be a "low level"
machine language expert "speaking in tongues".

However, one of the things that makes higher level languages like C
bulkier is the "Safety Net" that is provided in buffering contact with
the hardware, and consequently a chunk of code gets tacked onto every C
program using a little more disc space than its "machine language"
equivalent would need. However C is still low level enough to compile
into very small .Exe or .Com programs, and successful speedy coding is
more likely in C than in machine language.

The Command Line

Command Line programs are very easy to write in C.

Command Line programs are run from the DOS Command Line by following the
program name with a space separator and the Command Line "Argument". For
example, if we wish to run the program SPAGHETT.bas and GWBASIC is
lurking nearby, we might enter "GWBASIC SPAGHETT" at the DOS prompt.The
program name "SPAGHETT" is referred to as an Command Line Argument in
this instance, and is passed to the GWBASIC program to be sought after
and loaded and run if found.

Many if not most DOS programs accept command line arguments. For
example, (one that most of us are familiar with), the DOS DIR command
accepts the file name argument as an instruction to display only files
of that name; e.g."DIR *.bat". (Etcetera, Etcetera.)

Filters and Almost Filters

Two other easy types of programs for the C programmer to construct are
Filters and what I call an Almost Filter... Almost Filters are command
line utilities that can rival the BEST FILTER MONEY CAN BUY, except they
take their input from a filename given as a command line argument,
rather than from Standard Input (the DOS console).

General Filtration

A REAL filter is a little program that is used by "SANDWICHING" between
input and output ("Piping"). REAL filters depend on an "EXTERNAL"
program or the Command Interpreter (Command.Com) to "DIRECT" input into
them. When the input goes through the filter, the filter "sifts" each
bit and byte and adds, subtracts, reorganizes, changes, or performs some
kind of a function related to the output.

The "MORE" filter that comes with DOS is an example of of a REAL filter.

"Piping" is done using "|", Ascii CHR$(174), also called the "Pipe Sign"
which can be found above your SLASH character on most DOS keyboards. The
"Pipe Sign" is used as a "go-between" the "TYPE" command and its
"Argument" (usually) the file name and the "MORE" filter and letting the
output default into space (which is the Screen), we may screen the input
file normally, but must interject a keypress between each 24 lines of
text.

However the "MORE" filter is also an Almost Filter and will also accept
a file name as a command line argument.

Somehow, the ease of use of filters vanishes when one tries to explain
without examples. Exemplified, SUPPOSE that you have a text file called
JUNK.txt that you wish to type to the screen a screenfull at a time.

Simply enter "TYPE JUNK.TXT | MORE" at the DOS prompt. "SANDWICHING" the
"MORE" filter between the file and the screen "REDIRECTS" the input
through "MORE" achieving the desired "Filtration".

You may already understand about filters, in which case you could have
skipped the last bit. If you still don't fully understand, you can
review the appropriate section of the DOS manual for additional
information.

A REAL filter will usually default to the keyboard for input and the
screen for output, unless one "REDIRECTs" Input from a File and Output
to a File... or Printer... presumably whatever direction your mind
wanders to.

The peculiar thing is that I almost always, with exception of the "MORE"
filter, "REDIRECT" my filtered Output To a File, and almost always
"operate" on files.

So when I write my "Almost Filters" I use the Standard Form of the
argument (INPUTFILE). An Almost Filter built with very little effort
with "standard parts" is often as well suited to the task as the genuine
article.

Further, I should note that Almost Filters can be easily adapted to a
conventional interactive program application with hardly any recoding.

Commands and Arguments

For example, consider the following Batch Program;

Echo Off
Cls
 rem Test.bat by Bill Buckels 1989
 UNWS %1 | NUMBEY | UCASE > %2

The Test.bat batch program accepts a 7 or 8 bit ASCII file for input,
and outputs an upper case 7 bit ASCII File with each line preceded by
consecutive numbers starting at one. Suppose you have a Document Called
"Program.Doc" written in Wordstar format that you wish to copy over into
a numbered upper case "Clean" 7-bit ASCII file called "Program.Txt" (Not
that you may ever want to, but) you would invoke Test.bat at the DOS
prompt as follows using the filenames as arguments;"TEST PROGRAM.DOC
PROGRAM.TXT". Test.bat passes the filenames to the appropriate Almost
Filters and pipes through the Filters and serves the purpose of tying
the whole thing together.

Used in this manner, Almost Filters and Filters may be called together
in Batch Files like "BUILDING BLOCKS" and be conveniently used to
develop a "QUICK and CLEAN" filtered approach to removing the "SEDIMENT"
from various jobs around the computer.

-Bill Buckels
June 21, 1989
Revised March 24, 2000
