1 .\" $Id: libpc.7,v 2.9 1994/06/24 14:02:03 ceriel Exp $
2 .TH LIBPC 7 "$Revision: 2.9 $"
5 libpc \- library of external routines for Pascal programs
10 type br1 = 1..bufsize;
18 buf = packed array[br1] of char;
20 alfa = packed array[1..8] of char;
22 string = ^packed array[1..?] of char;
28 {all routines must be declared extern}
30 function argc:integer;
32 function argv(i:integer):string;
34 function environ(i:integer):string;
38 procedure buff(var f:filetype);
40 procedure nobuff(var f:filetype);
42 procedure notext(var f:text);
44 procedure diag(var f:text);
46 procedure pcreat(var f:text; s:string);
48 procedure popen(var f:text; s:string);
50 procedure pclose(var f:filetype);
52 procedure trap(err:integer);
54 procedure encaps(procedure p; procedure q(n:integer));
56 function perrno:integer;
58 function uread(fd:integer; var b:buf; len:br1):br3;
60 function uwrite(fd:integer; var b:buf; len:br1):br3;
62 function strbuf(var b:buf):string;
64 function strtobuf(s:string; var b:buf; len:br1):br2;
66 function strlen(s:string):integer;
68 function strfetch(s:string; i:integer):char;
70 procedure strstore(s:string; i:integer; c:char);
72 function clock:integer;
74 This library contains some often used external routines for Pascal programs.
75 The routines can be divided into several categories:
80 Gives the number of arguments provided when the program is called.
83 Selects the specified argument from the argument list and returns a
85 This pointer is nil if the index is out of bounds (<0 or >=argc).
87 Returns a pointer to the i-th environment string (i>=0). Returns nil
88 if i is beyond the end of the environment list (UNIX version 7).
90 Effectively deletes the first argument from the argument list.
91 Its function is equivalent to \fIshift\fP in the UNIX shell: argv[2] becomes
92 argv[1], argv[3] becomes argv[2], etc.
93 It is a useful procedure to skip optional flag arguments.
94 Note that the matching of arguments and files
95 is done at the time a file is opened by a call to reset or rewrite.
99 Additional file handling routines:
102 Turn on buffering of a file. Not very useful, because all
103 files are buffered except standard output to a terminal and diagnostic output.
104 Input files are always buffered.
107 Turn off buffering of an output file. It causes the current contents of the
108 buffer to be flushed.
110 Only useful for input files.
111 End of line characters are not replaced by a space and character codes out of
112 the ASCII range (0..127) do not cause an error message.
114 Initialize a file for output on the diagnostic output stream (fd=2).
115 Output is not buffered.
117 The same as rewrite(f), except that the file name must be provided.
118 The name must be zero terminated. Only text files are allowed.
120 The same as reset(f), except that the file name must be provided.
121 The name must be zero terminated. Only text files are allowed.
123 To close files hidden in records or arrays.
124 All other files are closed automatically.
131 Type conversion from character array to string.
132 It is the responsibility of the user that the string is zero terminated.
135 Copy string into buffer until the string terminating zero byte
136 is found or until the buffer if full, whatever comes first.
137 The zero byte is also copied.
138 The number of copied characters, excluding the zero byte, is returned. So if
139 the result is equal to the buffer length, then the end of buffer is reached
140 before the end of string.
142 Returns the string length excluding the terminating zero byte.
144 Fetches the i-th character from a string.
145 There is no check against the string length.
147 Stores a character in a string. There is no check against
148 string length, so this is a dangerous procedure.
154 These routines allow for user-level handling off almost
155 all possible error situations.
156 Trap handlers may be user-defined,
157 written in Pascal, replacing the
158 default handler that produces an error message and quits.
159 Also, traps can be generated by the user.
161 Trap generates the trap passed as argument (0..252).
162 The trap numbers 128..252 may be used freely. The others are reserved.
165 Encapsulate the execution of \fIp\fP with the trap handler \fIq\fP.
166 Encaps replaces the previous trap handler by \fIq\fP, calls \fIp\fP
168 the previous handler when \fIp\fP returns.
169 If, during the execution of \fIp\fP, a trap occurs,
170 then \fIq\fP is called with the trap number as parameter.
171 For the duration of \fIq\fP the previous trap handler is restored, so that
172 it is possible to only handle some of the errors in \fIq\fP. All the other errors must
173 then be raised again by a call to \fItrap\fP.
175 Encapsulations may be nested: a procedure may be encapsulated while executing
176 an encapsulated routine.
178 Jumping out of an encapsulated procedure (non-local goto) is dangerous,
179 because the previous trap handler must be restored.
180 Therefore, it is only allowed to jump out of procedure \fIp\fP from inside \fIq\fP and
181 it is only allowed to jump out of one level of encapsulation.
182 To exit several levels of encapsulation, the use of traps is required.
183 See pc_prlib(7) for lists of trap numbers
184 for EM machine errors and Pascal run time system errors.
185 Note that \fIp\fP may not have parameters.
191 The routines of this category require global variables or routines
192 of the monitor library libmon(7).
194 Equal to the read system call.
195 Its normal name is blocked by the standard Pascal routine read.
198 As above but for write(2).
200 Because external data references are not possible in Pascal,
201 this routine returns the global variable errno, indicating the result of
202 the last system call.
209 Return the number of ticks of user and system time consumed by the program.
213 The following program presents an example of how these routines can be used.
214 This program is equivalent to the UNIX command cat(1).
217 program cat(input,inp,output);
221 function argc:integer; extern;
222 function argv(i:integer):string; extern;
223 procedure argshift; extern;
224 function strlen(s:string):integer; extern;
225 function strfetch(s:string; i:integer):char; extern;
227 procedure copy(var fi:text);
232 while not eoln(fi) do
248 if (strlen(s) = 1) and (strfetch(s,1) = '-')
256 Another example gives some idea of the way to manage trap handling:
259 program bigreal(output);
263 procedure encaps(procedure p; procedure q(n:integer)); extern;
264 procedure trap(n:integer); extern;
266 procedure traphandler(n:integer);
267 begin if n=EFOVFL then trapped:=true else trap(n) end;
271 begin trapped:=false; i:=1;
273 begin j:=i; i:=i*2 end;
274 writeln('bigreal = ',j);
278 encaps(work,traphandler);
282 .IP ~em/lib/*/tail_pc 20
285 ack(1), pc_pem(6), pc_prlib(7), libmon(7)
287 Two routines may cause fatal error messages to be generated.
290 Rewrite error (trap 77) if the file cannot be created.
293 Reset error (trap 76) if the file cannot be opened for reading
296 Johan Stevenson, Vrije Universiteit.
298 encaps: Ed Keizer, Vrije Universiteit.