libpc.7 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. .\" $Id$
  2. .TH LIBPC 7 "$Revision$"
  3. .ad
  4. .SH NAME
  5. libpc \- library of external routines for Pascal programs
  6. .SH SYNOPSIS
  7. .ta 11n
  8. const bufsize = ?;
  9. .br
  10. type br1 = 1..bufsize;
  11. .br
  12. br2 = 0..bufsize;
  13. .br
  14. br3 = -1..bufsize;
  15. .br
  16. ok = -1..0;
  17. .br
  18. buf = packed array[br1] of char;
  19. .br
  20. alfa = packed array[1..8] of char;
  21. .br
  22. string = ^packed array[1..?] of char;
  23. .br
  24. filetype = file of ?;
  25. .br
  26. long = ?;
  27. {all routines must be declared extern}
  28. function argc:integer;
  29. .br
  30. function argv(i:integer):string;
  31. .br
  32. function environ(i:integer):string;
  33. .br
  34. procedure argshift;
  35. procedure buff(var f:filetype);
  36. .br
  37. procedure nobuff(var f:filetype);
  38. .br
  39. procedure notext(var f:text);
  40. .br
  41. procedure diag(var f:text);
  42. .br
  43. procedure pcreat(var f:text; s:string);
  44. .br
  45. procedure popen(var f:text; s:string);
  46. .br
  47. procedure pclose(var f:filetype);
  48. procedure trap(err:integer);
  49. .br
  50. procedure encaps(procedure p; procedure q(n:integer));
  51. function perrno:integer;
  52. .br
  53. function uread(fd:integer; var b:buf; len:br1):br3;
  54. .br
  55. function uwrite(fd:integer; var b:buf; len:br1):br3;
  56. function strbuf(var b:buf):string;
  57. .br
  58. function strtobuf(s:string; var b:buf; len:br1):br2;
  59. .br
  60. function strlen(s:string):integer;
  61. .br
  62. function strfetch(s:string; i:integer):char;
  63. .br
  64. procedure strstore(s:string; i:integer; c:char);
  65. function clock:integer;
  66. .SH DESCRIPTION
  67. This library contains some often used external routines for Pascal programs.
  68. The routines can be divided into several categories:
  69. .PP
  70. Argument control:
  71. .RS
  72. .IP argc 10
  73. Gives the number of arguments provided when the program is called.
  74. .PD 0
  75. .IP argv
  76. Selects the specified argument from the argument list and returns a
  77. pointer to it.
  78. This pointer is nil if the index is out of bounds (<0 or >=argc).
  79. .IP environ
  80. Returns a pointer to the i-th environment string (i>=0). Returns nil
  81. if i is beyond the end of the environment list (UNIX version 7).
  82. .IP argshift
  83. Effectively deletes the first argument from the argument list.
  84. Its function is equivalent to \fIshift\fP in the UNIX shell: argv[2] becomes
  85. argv[1], argv[3] becomes argv[2], etc.
  86. It is a useful procedure to skip optional flag arguments.
  87. Note that the matching of arguments and files
  88. is done at the time a file is opened by a call to reset or rewrite.
  89. .PD
  90. .PP
  91. .RE
  92. Additional file handling routines:
  93. .RS
  94. .IP buff 10
  95. Turn on buffering of a file. Not very useful, because all
  96. files are buffered except standard output to a terminal and diagnostic output.
  97. Input files are always buffered.
  98. .PD 0
  99. .IP nobuff
  100. Turn off buffering of an output file. It causes the current contents of the
  101. buffer to be flushed.
  102. .IP notext
  103. Only useful for input files.
  104. End of line characters are not replaced by a space and character codes out of
  105. the ASCII range (0..127) do not cause an error message.
  106. .IP diag
  107. Initialize a file for output on the diagnostic output stream (fd=2).
  108. Output is not buffered.
  109. .IP pcreat
  110. The same as rewrite(f), except that the file name must be provided.
  111. The name must be zero terminated. Only text files are allowed.
  112. .IP popen
  113. The same as reset(f), except that the file name must be provided.
  114. The name must be zero terminated. Only text files are allowed.
  115. .IP pclose
  116. To close files hidden in records or arrays.
  117. All other files are closed automatically.
  118. .PD
  119. .PP
  120. .RE
  121. String handling:
  122. .RS
  123. .IP strbuf 10
  124. Type conversion from character array to string.
  125. It is the responsibility of the user that the string is zero terminated.
  126. .PD 0
  127. .IP strtobuf
  128. Copy string into buffer until the string terminating zero byte
  129. is found or until the buffer if full, whatever comes first.
  130. The zero byte is also copied.
  131. The number of copied characters, excluding the zero byte, is returned. So if
  132. the result is equal to the buffer length, then the end of buffer is reached
  133. before the end of string.
  134. .IP strlen
  135. Returns the string length excluding the terminating zero byte.
  136. .IP strfetch
  137. Fetches the i-th character from a string.
  138. There is no check against the string length.
  139. .IP strstore
  140. Stores a character in a string. There is no check against
  141. string length, so this is a dangerous procedure.
  142. .PD
  143. .PP
  144. .RE
  145. Trap handling:
  146. .RS
  147. These routines allow for user-level handling off almost
  148. all possible error situations.
  149. Trap handlers may be user-defined,
  150. written in Pascal, replacing the
  151. default handler that produces an error message and quits.
  152. Also, traps can be generated by the user.
  153. .IP trap 10
  154. Trap generates the trap passed as argument (0..252).
  155. The trap numbers 128..252 may be used freely. The others are reserved.
  156. .PD 0
  157. .IP encaps
  158. Encapsulate the execution of \fIp\fP with the trap handler \fIq\fP.
  159. Encaps replaces the previous trap handler by \fIq\fP, calls \fIp\fP
  160. and restores
  161. the previous handler when \fIp\fP returns.
  162. If, during the execution of \fIp\fP, a trap occurs,
  163. then \fIq\fP is called with the trap number as parameter.
  164. For the duration of \fIq\fP the previous trap handler is restored, so that
  165. it is possible to only handle some of the errors in \fIq\fP. All the other errors must
  166. then be raised again by a call to \fItrap\fP.
  167. .br
  168. Encapsulations may be nested: a procedure may be encapsulated while executing
  169. an encapsulated routine.
  170. .br
  171. Jumping out of an encapsulated procedure (non-local goto) is dangerous,
  172. because the previous trap handler must be restored.
  173. Therefore, it is only allowed to jump out of procedure \fIp\fP from inside \fIq\fP and
  174. it is only allowed to jump out of one level of encapsulation.
  175. To exit several levels of encapsulation, the use of traps is required.
  176. See pc_prlib(7) for lists of trap numbers
  177. for EM machine errors and Pascal run time system errors.
  178. Note that \fIp\fP may not have parameters.
  179. .PD
  180. .PP
  181. .RE
  182. UNIX system calls:
  183. .RS
  184. The routines of this category require global variables or routines
  185. of the monitor library libmon(7).
  186. .IP uread 10
  187. Equal to the read system call.
  188. Its normal name is blocked by the standard Pascal routine read.
  189. .PD 0
  190. .IP uwrite
  191. As above but for write(2).
  192. .IP perrno
  193. Because external data references are not possible in Pascal,
  194. this routine returns the global variable errno, indicating the result of
  195. the last system call.
  196. .PD
  197. .PP
  198. .RE
  199. Miscellaneous:
  200. .RS
  201. .IP clock 10
  202. Return the number of ticks of user and system time consumed by the program.
  203. .PD
  204. .PP
  205. .RE
  206. The following program presents an example of how these routines can be used.
  207. This program is equivalent to the UNIX command cat(1).
  208. .nf
  209. {$c+}
  210. program cat(input,inp,output);
  211. var inp:text;
  212. s:string;
  213. function argc:integer; extern;
  214. function argv(i:integer):string; extern;
  215. procedure argshift; extern;
  216. function strlen(s:string):integer; extern;
  217. function strfetch(s:string; i:integer):char; extern;
  218. procedure copy(var fi:text);
  219. var c:char;
  220. begin reset(fi);
  221. while not eof(fi) do
  222. begin
  223. while not eoln(fi) do
  224. begin
  225. read(fi,c);
  226. write(c)
  227. end;
  228. readln(fi);
  229. writeln
  230. end
  231. end;
  232. begin {main}
  233. if argc = 1 then
  234. copy(input)
  235. else
  236. repeat
  237. s := argv(1);
  238. if (strlen(s) = 1) and (strfetch(s,1) = '-')
  239. then copy(input)
  240. else copy(inp);
  241. argshift;
  242. until argc <= 1;
  243. end.
  244. .fi
  245. .PP
  246. Another example gives some idea of the way to manage trap handling:
  247. .nf
  248. program bigreal(output);
  249. const EFOVFL=4;
  250. var trapped:boolean;
  251. procedure encaps(procedure p; procedure q(n:integer)); extern;
  252. procedure trap(n:integer); extern;
  253. procedure traphandler(n:integer);
  254. begin if n=EFOVFL then trapped:=true else trap(n) end;
  255. procedure work;
  256. var i,j:real;
  257. begin trapped:=false; i:=1;
  258. while not trapped do
  259. begin j:=i; i:=i*2 end;
  260. writeln('bigreal = ',j);
  261. end;
  262. begin
  263. encaps(work,traphandler);
  264. end.
  265. .fi
  266. .SH FILES
  267. .IP ~em/lib/*/tail_pc 20
  268. .PD
  269. .SH "SEE ALSO"
  270. ack(1), pc_pem(6), pc_prlib(7), libmon(7)
  271. .SH DIAGNOSTICS
  272. Two routines may cause fatal error messages to be generated.
  273. These are:
  274. .IP pcreat 10
  275. Rewrite error (trap 77) if the file cannot be created.
  276. .PD 0
  277. .IP popen
  278. Reset error (trap 76) if the file cannot be opened for reading
  279. .PD
  280. .SH AUTHOR
  281. Johan Stevenson, Vrije Universiteit.
  282. .br
  283. encaps: Ed Keizer, Vrije Universiteit.