home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / beginners / u4 < prev    next >
Encoding:
Text File  |  1979-01-10  |  6.7 KB  |  286 lines

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