[BACK]Return to u4 CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / share / doc / usd / 01.begin

File: [cvs.NetBSD.org] / src / share / doc / usd / 01.begin / u4 (download)

Revision 1.1, Thu Nov 29 12:35:06 2007 UTC (12 years, 11 months ago) by mjf
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, mjf-devfs2-base, mjf-devfs2, matt-premerge-20091211, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-armv6-nbase, matt-armv6-base, localcount-20160914, keiichi-mipv6-nbase, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, is-mlppp-base, is-mlppp, hpcarm-cleanup-nbase, hpcarm-cleanup-base, cube-autoconf-base, cube-autoconf, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, HEAD
Branch point for: matt-armv6

Commit Caldera licensed documentation from 4.4BSD. This was taken from the
OpenBSD tree.

No objections on netbsd-docs.

.\"	$NetBSD: u4,v 1.1 2007/11/29 12:35:06 mjf Exp $
.\"
.\" Copyright (C) Caldera International Inc.  2001-2002.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code and documentation must retain the above
.\"    copyright notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"	This product includes software developed or owned by Caldera
.\"	International, Inc.
.\" 4. Neither the name of Caldera International, Inc. nor the names of other
.\"    contributors may be used to endorse or promote products derived from
.\"    this software without specific prior written permission.
.\"
.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.\"	@(#)u4	8.1 (Berkeley) 6/8/93
.\"
.SH
IV.  PROGRAMMING
.PP
There will be no attempt made to teach any of
the programming languages available
but a few words of advice are in order.
One of the reasons why the
.UC UNIX
system is a productive programming environment
is that there is already a rich set of tools available,
and facilities like pipes, I/O redirection,
and the capabilities of the shell
often make it possible to do a job
by pasting together programs that already exist
instead of writing from scratch.
.SH
The Shell
.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
More pieces have been added subsequently,
but this 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 you can do the job much more easily.
One way is to type
.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 fact that the shell will perform loops,
repeating a set of commands over and over again
for a set of arguments:
.P1
for i in chap*
do
	ed $i <script
done
.P2
This sets the shell variable
.UL i
to each file name in turn,
then does the command.
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 for ,
.UL case ),
subroutines,
and interrupt handling.
Since
there are
many building-block programs,
you can sometimes avoid writing a new program
merely by piecing together some of the building blocks
with shell command files.
.PP
We will not go into any details here;
examples and rules can be found in
.ul
An Introduction to the
.ul
.UC UNIX
.IT Shell ,
by S. R. Bourne.
.SH
Programming in C
.PP
If you are undertaking anything substantial,
C is the only reasonable choice of programming language:
everything in
the
.UC UNIX
system
is tuned to it.
The
system
itself
is written in C,
as are most of the programs that run on it.
It is also 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
for more complicated things.
.PP
Most input and output in C is best handled with the 
standard I/O library,
which provides a set of I/O functions
that exist in compatible form on most machines
that have C compilers.
In general, it's wisest to confine the system interactions
in a program to the facilities provided by this library.
.PP
C programs that don't depend too much on special features of 
.UC UNIX
(such as pipes)
can be moved to other computers that have C compilers.
The list of such machines grows daily;
in addition to the original
.UC PDP -11,
it currently includes
at least
Honeywell 6000,
IBM 370 and PC families,
Interdata 8/32,
Data General Nova and Eclipse,
HP 2100,
Harris /7,
Motorola 68000 family (including machines like Sun Microsystems and
Apple Macintosh),
VAX 11 family,
SEL 86,
and
Zilog Z80.
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 on 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;
it then checks the times that the pieces were last changed
and does the minimal amount of recompiling
to create a consistent updated version.
.PP
The debugger
.UL gdb
is useful for digging through the dead bodies
of C programs,
but is rather hard to learn to use effectively.
The most effective debugging tool is still
careful thought, coupled with judiciously placed
print statements.
.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 \-pg
option;
after the test run, use
.UL gprof
to print an execution profile.
The command
.UL time
will give you the gross run-time statistics
of a program, but they are not super accurate or reproducible.
.SH
Other Languages
.PP
If you 
.ul
have
to use Fortran,
there are two possibilities.
You might consider
Ratfor,
which gives you the decent control structures
and free-form input that characterize C,
yet lets you write code that
is still portable to other environments.
Bear in mind that
.UC UNIX
Fortran
tends to produce large and relatively slow-running
programs.
Furthermore, supporting software like
.UL gdb ,
.UL prof ,
etc., are all virtually useless with Fortran programs.
There may also be a Fortran 77 compiler on your system.
If so,
this is a viable alternative to 
Ratfor,
and has the non-trivial advantage that it is compatible with C
and related programs.
(The Ratfor processor
and C tools
can be used with Fortran 77 too.)
.PP
If your 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 be using
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
can be repaid many times over in programs
that are easy to change later on.
.PP
Most
.UC UNIX
systems also make available other languages,
such as
Algol 68, APL, Basic, Lisp, Pascal, and Snobol.
Whether these are useful depends largely on the local environment:
if someone cares about the language and has worked on it,
it may be in good shape.
If not, the odds are strong that it
will be more trouble than it's worth.