home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / datafile / usd / 01_begin / u4_txt < prev    next >
Encoding:
Text File  |  1996-10-12  |  7.2 KB  |  199 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. IV.  PROGRAMMING
  8.  
  9.      There  will be no attempt made to teach any of the pro-
  10. gramming languages available but a few words of  advice  are
  11. in order.  One of the reasons why the system is a productive
  12. programming environment is that there is already a rich  set
  13. of  tools available, and facilities like pipes, I/O redirec-
  14. tion, and the capabilities of the shell often make it possi-
  15. ble  to  do  a job by pasting together programs that already
  16. exist instead of writing from scratch.
  17.  
  18. The Shell
  19.  
  20.      The pipe mechanism lets you fabricate quite complicated
  21. operations out of spare parts that already exist.  For exam-
  22. ple, the first draft of the spell-program was (roughly)  cat
  23. ...     collect the files | tr ...    put each word on a new
  24. line | tr ...    delete punctuation, etc.  |  sort      into
  25. dictionary    order   |   uniq      discard   duplicates   |
  26. comm      print words in text         but not in  dictionary
  27. More  pieces  have  been added subsequently, but this goes a
  28. long way for such a small effort.
  29.  
  30.      The editor can be made to do things that would normally
  31. require  special programs on other systems.  For example, to
  32. list the first and last lines of each of  a  set  of  files,
  33. such  as  a book, you could laboriously type ed e chap1.1 1p
  34. $p e chap1.2 1p $p etc.  But you can do the  job  much  more
  35. easily.   One  way is to type ls chap* >temp to get the list
  36. of filenames into a file.  Then edit this file to  make  the
  37. necessary  series of editing commands (using the global com-
  38. mands of ed), and write it into script.  Now the command  ed
  39. <script  will  produce the same output as the laborious hand
  40. typing.  Alternately (and more easily), you can use the fact
  41. that  the  shell will perform loops, repeating a set of com-
  42. mands over and over again for a set of arguments: for  i  in
  43. chap*  do       ed $i <script done This sets the shell vari-
  44. able i-to each file name in turn,  then  does  the  command.
  45. You  can  type  this command at the terminal, or put it in a
  46. file for later execution.
  47.  
  48. Programming the Shell
  49.  
  50.      An option often overlooked by  newcomers  is  that  the
  51. shell is itself a programming language, with variables, con-
  52. trol flow  if-else,  while,  for,  case),  subroutines,  and
  53. interrupt  handling.   Since  there  are many building-block
  54. programs, you can sometimes  avoid  writing  a  new  program
  55. merely  by piecing together some of the building blocks with
  56. shell command files.
  57.  
  58.      We will not go into  any  details  here;  examples  and
  59. rules  can  be  found  in  An  Introduction  to the by S. R.
  60. Bourne.
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                              -2-
  71.  
  72.  
  73. Programming in C
  74.  
  75.      If you are undertaking anything substantial, C  is  the
  76. only  reasonable  choice of programming language: everything
  77. in the system is tuned to it.  The system itself is  written
  78. in  C,  as  are  most of the programs that run on it.  It is
  79. also a easy language to use once  you  get  started.   C  is
  80. introduced and fully described in The C Programming Language
  81. by B. W. Kernighan and D. M. Ritchie (Prentice-Hall,  1978).
  82. Several  sections  of  the manual describe the system inter-
  83. faces, that is, how you do I/O and similar functions.   Read
  84. UNIX Programming for more complicated things.
  85.  
  86.      Most  input  and  output  in C is best handled with the
  87. standard I/O library, which provides a set of I/O  functions
  88. that  exist  in compatible form on most machines that have C
  89. compilers.  In general, it's wisest to  confine  the  system
  90. interactions in a program to the facilities provided by this
  91. library.
  92.  
  93.      C programs that don't depend too much on  special  fea-
  94. tures  of  (such  as  pipes) can be moved to other computers
  95. that have C compilers.  The  list  of  such  machines  grows
  96. daily;  in addition to the original it currently includes at
  97. least Honeywell 6000, IBM 370  and  PC  families,  Interdata
  98. 8/32,  Data  General  Nova  and Eclipse, HP 2100, Harris /7,
  99. Motorola 68000 family (including machines like Sun Microsys-
  100. tems  and Apple Macintosh), VAX 11 family, SEL 86, and Zilog
  101. Z80.  Calls to the standard I/O library will work on all  of
  102. these machines.
  103.  
  104.      There  are a number of supporting programs that go with
  105. C.  lint-checks C programs for potential  portability  prob-
  106. lems,  and  detects errors such as mismatched argument types
  107. and uninitialized variables.
  108.  
  109.      For larger programs (anything whose source is  on  more
  110. than  one  file) make-allows you to specify the dependencies
  111. among the source files and the processing  steps  needed  to
  112. make a new version; it then checks the times that the pieces
  113. were last changed and does the minimal amount of recompiling
  114. to create a consistent updated version.
  115.  
  116.      The debugger adb-is useful for digging through the dead
  117. bodies of C programs, but is rather hard  to  learn  to  use
  118. effectively.   The  most  effective  debugging tool is still
  119. careful  thought,  coupled  with  judiciously  placed  print
  120. statements.-
  121.  
  122. -----------
  123. -  The  "dbx"  debugger,  supplied  starting  with
  124. 4.2BSD, has extensive  facilities  for  high-level
  125. debugging  of C programs and is much easier to use
  126. than "adb".
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                              -3-
  137.  
  138.  
  139.      The C compiler provides a limited instrumentation  ser-
  140. vice,  so  you  can find out where programs spend their time
  141. and what parts are worth optimizing.  Compile  the  routines
  142. with the -p-option; after the test run, use prof-to print an
  143. execution profile.  The command time-will give you the gross
  144. run-time  statistics  of  a  program, but they are not super
  145. accurate or reproducible.
  146.  
  147. Other Languages
  148.  
  149.      If you have to use Fortran, there  are  two  possibili-
  150. ties.  You might consider Ratfor, which gives you the decent
  151. control structures and free-form input that characterize  C,
  152. yet  lets  you  write  code  that is still portable to other
  153. environments.  Bear in mind that Fortran  tends  to  produce
  154. large  and  relatively  slow-running programs.  Furthermore,
  155. supporting software like adb, prof, etc., are all  virtually
  156. useless  with Fortran programs.  There may also be a Fortran
  157. 77 compiler on your system.  If so, this is a viable  alter-
  158. native  to Ratfor, and has the non-trivial advantage that it
  159. is compatible with C and related programs.  (The Ratfor pro-
  160. cessor and C tools can be used with Fortran 77 too.)
  161.  
  162.      If  your  application  requires you to translate a lan-
  163. guage into a set of actions or another language, you are  in
  164. effect building a compiler, though probably a small one.  In
  165. that case, you should be using the  yacc- compiler-compiler,
  166. which helps you develop a compiler quickly.  The lex-lexical
  167. analyzer generator does the same job for  the  simpler  lan-
  168. guages that can be expressed as regular expressions.  It can
  169. be used by itself, or as a front end to recognize inputs for
  170. a  yacc-based  program.   Both  yacc- and  lex- require some
  171. sophistication to use, but the initial  effort  of  learning
  172. them can be repaid many times over in programs that are easy
  173. to change later on.
  174.  
  175.      Most systems also make available other languages,  such
  176. as  Algol 68, APL, Basic, Lisp, Pascal, and Snobol.  Whether
  177. these are useful depends largely on the  local  environment:
  178. if someone cares about the language and has worked on it, it
  179. may be in good shape.  If not, the odds are strong  that  it
  180. will be more trouble than it's worth.
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.