libpc.7 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. .\" $Header$
  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 you must provide the filename yourself.
  111. The name must be zero terminated. Only text files are allowed.
  112. .IP popen
  113. The same as reset(f), except that you must provide the filename yourself.
  114. The name must be zero terminated. Only text files are allowed.
  115. .IP pclose
  116. Gives you the opportunity 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 your own responsibility 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 you to handle almost all
  148. the possible error situations yourself.
  149. You may define your own trap handler, written in Pascal, instead of the
  150. default handler that produces an error message and quits.
  151. You may also generate traps yourself.
  152. .IP trap 10
  153. Trap generates the trap passed as argument (0..252).
  154. The trap numbers 128..252 may be used freely. The others are reserved.
  155. .PD 0
  156. .IP encaps
  157. Encapsulate the execution of \fIp\fP with the trap handler \fIq\fP.
  158. Encaps replaces the previous trap handler by \fIq\fP, calls \fIp\fP
  159. and restores
  160. the previous handler when \fIp\fP returns.
  161. If, during the execution of \fIp\fP, a trap occurs,
  162. then \fIq\fP is called with the trap number as parameter.
  163. For the duration of \fIq\fP the previous trap handler is restored, so that
  164. you may handle only some of the errors in \fIq\fP. All the other errors must
  165. then be raised again by a call to \fItrap\fP.
  166. .br
  167. Encapsulations may be nested: you may encapsulate a procedure while executing
  168. an encapsulated routine.
  169. .br
  170. Jumping out of an encapsulated procedure (non-local goto) is dangerous,
  171. because the previous trap handler must be restored.
  172. Therefore, you may only jump out of procedure \fIp\fP from inside \fIq\fP and
  173. you may only jump out of one level of encapsulation.
  174. If you want to exit several levels of encapsulation, use traps.
  175. See pc_prlib(7) for lists of trap numbers
  176. for EM machine errors and Pascal run time system errors.
  177. Note that \fIp\fP may not have parameters.
  178. .PD
  179. .PP
  180. .RE
  181. UNIX system calls:
  182. .RS
  183. The routines of this category require global variables or routines
  184. of the monitor library libmon(7).
  185. .IP uread 10
  186. Equal to the read system call.
  187. Its normal name is blocked by the standard Pascal routine read.
  188. .PD 0
  189. .IP uwrite
  190. As above but for write(2).
  191. .IP perrno
  192. Because external data references are not possible in Pascal,
  193. this routine returns the global variable errno, indicating the result of
  194. the last system call.
  195. .PD
  196. .PP
  197. .RE
  198. Miscellaneous:
  199. .RS
  200. .IP clock 10
  201. Return the number of ticks of user and system time consumed by the program.
  202. .PD
  203. .PP
  204. .RE
  205. The following program presents an example of how these routines can be used.
  206. This program is equivalent to the UNIX command cat(1).
  207. .nf
  208. {$c+}
  209. program cat(input,inp,output);
  210. var inp:text;
  211. s:string;
  212. function argc:integer; extern;
  213. function argv(i:integer):string; extern;
  214. procedure argshift; extern;
  215. function strlen(s:string):integer; extern;
  216. function strfetch(s:string; i:integer):char; extern;
  217. procedure copy(var fi:text);
  218. var c:char;
  219. begin reset(fi);
  220. while not eof(fi) do
  221. begin
  222. while not eoln(fi) do
  223. begin
  224. read(fi,c);
  225. write(c)
  226. end;
  227. readln(fi);
  228. writeln
  229. end
  230. end;
  231. begin {main}
  232. if argc = 1 then
  233. copy(input)
  234. else
  235. repeat
  236. s := argv(1);
  237. if (strlen(s) = 1) and (strfetch(s,1) = '-')
  238. then copy(input)
  239. else copy(inp);
  240. argshift;
  241. until argc <= 1;
  242. end.
  243. .fi
  244. .PP
  245. Another example gives some idea of the way to manage trap handling:
  246. .nf
  247. program bigreal(output);
  248. const EFOVFL=4;
  249. var trapped:boolean;
  250. procedure encaps(procedure p; procedure q(n:integer)); extern;
  251. procedure trap(n:integer); extern;
  252. procedure traphandler(n:integer);
  253. begin if n=EFOVFL then trapped:=true else trap(n) end;
  254. procedure work;
  255. var i,j:real;
  256. begin trapped:=false; i:=1;
  257. while not trapped do
  258. begin j:=i; i:=i*2 end;
  259. writeln('bigreal = ',j);
  260. end;
  261. begin
  262. encaps(work,traphandler);
  263. end.
  264. .fi
  265. .SH FILES
  266. .IP ~em/lib/*/tail_pc 20
  267. .PD
  268. .SH "SEE ALSO"
  269. ack(1), pc_pem(6), pc_prlib(7), libmon(7)
  270. .SH DIAGNOSTICS
  271. Two routines may cause fatal error messages to be generated.
  272. These are:
  273. .IP pcreat 10
  274. Rewrite error (trap 77) if the file cannot be created.
  275. .PD 0
  276. .IP popen
  277. Reset error (trap 76) if the file cannot be opened for reading
  278. .PD
  279. .SH AUTHOR
  280. Johan Stevenson, Vrije Universiteit.
  281. .br
  282. encaps: Ed Keizer, Vrije Universiteit.