BSD 3 development
[unix-history] / usr / doc / beginners / u4
CommitLineData
8340f87c
BJ
1.SH
2IV. PROGRAMMING
3.PP
4There will be no attempt made to teach any of
5the programming languages available
6but a few words of advice are in order.
7One of the reasons why the
8.UC UNIX
9system is a productive programming environment
10is that there is already a rich set of tools available,
11and facilities like pipes, I/O redirection,
12and the capabilities of the shell
13often make it possible to do a job
14by pasting together programs that already exist
15instead of writing from scratch.
16.SH
17The Shell
18.PP
19The pipe mechanism lets you fabricate quite complicated operations
20out of spare parts that already exist.
21For example,
22the first draft of the
23.UL spell
24program was (roughly)
25.P1
26.ta .6i 1.2i
27cat ... \f2collect the files\f3
28| tr ... \f2put each word on a new line\f3
29| tr ... \f2delete punctuation, etc.\f3
30| sort \f2into dictionary order\f3
31| uniq \f2discard duplicates\f3
32| comm \f2print words in text\f3
33 \f2 but not in dictionary\f3
34.P2
35More pieces have been added subsequently,
36but this goes a long way
37for such a small effort.
38.PP
39The editor can be made to do things that would normally
40require special programs on other systems.
41For example, to list the first and last lines of each of a
42set of files, such as a book,
43you could laboriously type
44.P1
45ed
46e chap1.1
471p
48$p
49e chap1.2
501p
51$p
52.ft R
53etc.
54.P2
55But you can do the job much more easily.
56One way is to type
57.P1
58ls chap* >temp
59.P2
60to get the list of filenames into a file.
61Then edit this file to make the necessary
62series of editing commands
63(using the global commands of
64.UL ed ),
65and write it into
66.UL script .
67Now the command
68.P1
69ed <script
70.P2
71will produce
72the same output as the laborious hand typing.
73Alternately
74(and more easily),
75you can use the fact that the shell will perform loops,
76repeating a set of commands over and over again
77for a set of arguments:
78.P1
79for i in chap*
80do
81 ed $i <script
82done
83.P2
84This sets the shell variable
85.UL i
86to each file name in turn,
87then does the command.
88You can type this command at the terminal,
89or put it in a file for later execution.
90.SH
91Programming the Shell
92.PP
93An option often overlooked by newcomers
94is that the shell is itself a programming language,
95with variables,
96control flow
97.UL if-else , (
98.UL while ,
99.UL for ,
100.UL case ),
101subroutines,
102and interrupt handling.
103Since
104there are
105many building-block programs,
106you can sometimes avoid writing a new program
107merely by piecing together some of the building blocks
108with shell command files.
109.PP
110We will not go into any details here;
111examples and rules can be found in
112.ul
113An Introduction to the
114.ul
115.UC UNIX
116.IT Shell ,
117by S. R. Bourne.
118.SH
119Programming in C
120.PP
121If you are undertaking anything substantial,
122C is the only reasonable choice of programming language:
123everything in
124the
125.UC UNIX
126system
127is tuned to it.
128The
129system
130itself
131is written in C,
132as are most of the programs that run on it.
133It is also a easy language to use
134once you get started.
135C is introduced and fully described in
136.ul
137The C Programming Language
138by
139B. W. Kernighan and D. M. Ritchie
140(Prentice-Hall, 1978).
141Several sections of the manual
142describe the system interfaces, that is,
143how you do I/O
144and similar functions.
145Read
146.ul
147UNIX Programming
148for more complicated things.
149.PP
150Most input and output in C is best handled with the
151standard I/O library,
152which provides a set of I/O functions
153that exist in compatible form on most machines
154that have C compilers.
155In general, it's wisest to confine the system interactions
156in a program to the facilities provided by this library.
157.PP
158C programs that don't depend too much on special features of
159.UC UNIX
160(such as pipes)
161can be moved to other computers that have C compilers.
162The list of such machines grows daily;
163in addition to the original
164.UC PDP -11,
165it currently includes
166at least
167Honeywell 6000,
168IBM 370,
169Interdata 8/32,
170Data General Nova and Eclipse,
171HP 2100,
172Harris /7,
173VAX 11/780,
174SEL 86,
175and
176Zilog Z80.
177Calls to the standard I/O library will work on all of these machines.
178.PP
179There are a number of supporting programs that go with C.
180.UL lint
181checks C programs for potential portability problems,
182and detects errors such as mismatched argument types
183and uninitialized variables.
184.PP
185For larger programs
186(anything whose source is on more than one file)
187.UL make
188allows you to specify the dependencies among the source files
189and the processing steps needed to make a new version;
190it then checks the times that the pieces were last changed
191and does the minimal amount of recompiling
192to create a consistent updated version.
193.PP
194The debugger
195.UL adb
196is useful for digging through the dead bodies
197of C programs,
198but is rather hard to learn to use effectively.
199The most effective debugging tool is still
200careful thought, coupled with judiciously placed
201print statements.
202.PP
203The C compiler provides a limited instrumentation service,
204so you can find out
205where programs spend their time and what parts are worth optimizing.
206Compile the routines with the
207.UL \-p
208option;
209after the test run, use
210.UL prof
211to print an execution profile.
212The command
213.UL time
214will give you the gross run-time statistics
215of a program, but they are not super accurate or reproducible.
216.SH
217Other Languages
218.PP
219If you
220.ul
221have
222to use Fortran,
223there are two possibilities.
224You might consider
225Ratfor,
226which gives you the decent control structures
227and free-form input that characterize C,
228yet lets you write code that
229is still portable to other environments.
230Bear in mind that
231.UC UNIX
232Fortran
233tends to produce large and relatively slow-running
234programs.
235Furthermore, supporting software like
236.UL adb ,
237.UL prof ,
238etc., are all virtually useless with Fortran programs.
239There may also be a Fortran 77 compiler on your system.
240If so,
241this is a viable alternative to
242Ratfor,
243and has the non-trivial advantage that it is compatible with C
244and related programs.
245(The Ratfor processor
246and C tools
247can be used with Fortran 77 too.)
248.PP
249If your application requires you to translate
250a language into a set of actions or another language,
251you are in effect building a compiler,
252though probably a small one.
253In that case,
254you should be using
255the
256.UL yacc
257compiler-compiler,
258which helps you develop a compiler quickly.
259The
260.UL lex
261lexical analyzer generator does the same job
262for the simpler languages that can be expressed as regular expressions.
263It can be used by itself,
264or as a front end to recognize inputs for a
265.UL yacc -based
266program.
267Both
268.UL yacc
269and
270.UL lex
271require some sophistication to use,
272but the initial effort of learning them
273can be repaid many times over in programs
274that are easy to change later on.
275.PP
276Most
277.UC UNIX
278systems also make available other languages,
279such as
280Algol 68, APL, Basic, Lisp, Pascal, and Snobol.
281Whether these are useful depends largely on the local environment:
282if someone cares about the language and has worked on it,
283it may be in good shape.
284If not, the odds are strong that it
285will be more trouble than it's worth.