.ig
	u4: version 1.2 of 11/22/83
	

	@(#)u4	1.2	(NSC)	11/22/83
..
.SH
IV.  PROGRAMMING
.PP
This section does not attempt to teach any of
the programming languages available,
but offers a few words of advice.
The
.UC GENIX
system 
is a productive programming environment because it 
has available a rich set of tools and facilities such as pipes,
I/O redirection, and 
the capabilities of the shell
that often make it possible to do a job
by pasting together the output of programs and utilities
instead of writing from scratch.
.SH
Tools
.PP
The pipe mechanism lets you fabricate quite complicated operations
out of spare parts that already exist.
For example,
the first draft of the
.UL  spell 
program was (roughly)
.P1
.ta .6i 1.2i
cat ...	\f2collect the files\f3
| tr ...	\f2put each word on a new line\f3
| tr ...	\f2delete punctuation, etc.\f3
| sort	\f2into dictionary order\f3
| uniq	\f2discard duplicates\f3
| comm	\f2print words in text\f3
	\f2  but not in dictionary\f3
.P2
It has been enhanced from this draft, 
but the above short script goes a long way
for such a small effort.
.PP
The editor can be made to do things that would normally
require special programs on other systems.
For example, to list the first and last lines of each of a
set of files, such as a book,
you could laboriously type
.P1
ed
e chap1.1
1p
$p
e chap1.2
1p
$p
.ft R
etc.
.P2
But an easier way is:
.P1
ls chap* >temp
.P2
to get the list of filenames into a file.
Then edit this file to make the necessary
series of editing commands
(using the global commands of
.UL ed ),
and write it into
.UL script .
Now the command
.P1
ed <script
.P2
will produce
the same output as the laborious hand typing.
Alternately
(and more easily),
you can use the loop statement in the shell
to repeat a set of commands over and over again
for a set of arguments.  For the Bourne shell use:
.P1
for i in chap*
do
	ed $i <script
done
.P2
For the C shell, use:
.P1
foreach ; (chap*)
	ed $i <script
end
.P2
This sets the shell variable
.UL i
to each file name in turn and
then executes the command in the loop.
You can type this command at the terminal,
or put it in a file for later execution.
.SH
Programming the Shell
.PP
An option often overlooked by newcomers
is that the shell is itself a programming language,
with variables,
control flow
.UL if-else , (
.UL while ,
.UL foreach \ or
.UL for ,
.UL case \ or
.UL switch ),
subroutines,
and interrupt handling.
By using the
many building-block programs
provided by the
.UC GENIX
operating system,
you can sometimes avoid writing a new program
merely by piecing together some of the building blocks
with shell command files.
.PP
Examples and rules can be found in
.ul
An Introduction to the C Shell
by William Joy.
.SH
Programming in C
.PP
The
.UC GENIX
system
itself
is written in C,
as are most of the utilities that run on it.
C is an easy language to use
once you get started.
C is introduced and fully described in
.ul
The C Programming Language
by
B. W. Kernighan and D. M. Ritchie
(Prentice-Hall, 1978).
Several sections of the manual
describe the system interfaces, that is,
how you do I/O
and similar functions.
Read
.ul
UNIX Programming
and 
The Programmer's Guide to the \s-2GENIX\s0 C Language and Compiler.
.PP
Input and output in C is best handled with the 
standard I/O library,
which provides a set of I/O functions.
To keep a C program in a form compatible with other machines
that have C compilers,
confine the system interactions
in a program to the facilities provided by this library.
.PP
C programs that do not depend on special features of 
the
.UC GENIX
operating system
can be moved to other computers that have C compilers.
The list of such machines includes:
SYS16\s4\uTM\d\s0 Development System,
.UC PDP \ -11,
Honeywell 6000,
IBM 370,
Perkin-Elmer 3200 series,
Data General Nova and Eclipse,
HP 2100,
Harris /7,
VAX 11 series,
SEL 86,
and
Zilog S8000.
Calls to the standard I/O library will work on all of these machines.
.PP
There are a number of supporting programs that go with C.
.UL lint
checks C programs for potential portability problems,
and detects errors such as mismatched argument types
and uninitialized variables.
.PP
For larger programs
(anything whose source is in more than one file)
.UL make
allows you to specify the dependencies among the source files
and the processing steps needed to make a new version;
then it checks the times that the pieces were last changed
and does the minimal amount of recompiling
to create a correct updated version.
.PP
The debugger
.UL ddt
is useful for digging through 
C programs on an assembly language level.
.PP
The C compiler provides a limited instrumentation service,
so you can find out
where programs spend their time and what parts are worth optimizing.
Compile the routines with the
.UL \-p
option;
after the test run, use
.UL prof
to print an execution profile.
The command
.UL time
will give you the approximate gross run-time statistics
of a program.
.SH
Other Languages
.PP
The
.UC GENIX
system optionally comes with a Pascal compiler.
The Pascal Language, compatible with the American National Standards
Institute Pascal,
is easy to learn and use
and offers strong type checking and block structure.
The compiler allows separate modular compilation and produces object
code that is compatible with the object code of C and assembly 
language modules.
.PP
The NS16000 assembler,
.UL as ,
offers convenient directives for programs that must be written in
assembly.  The NS16000 instruction set is described in full in the 
.ul
NS16000 Programmer's Reference Manual.
.PP
If an application requires you to translate
a language into a set of actions or another language,
you are, in effect, building a compiler,
though probably a small one.
In that case,
you should use
the
.UL yacc
compiler-compiler, 
which helps you develop a compiler quickly.
The
.UL lex
lexical analyzer generator does the same job
for the simpler languages that can be expressed as regular expressions.
It can be used by itself,
or as a front end to recognize inputs for a
.UL yacc -based
program.
Both
.UL yacc
and
.UL lex
require some sophistication to use,
but the initial effort of learning them
is repaid many times over in programs
that are easy to change later on.
