1 .\" Copyright (c) 1979 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)pxin3.n 5.2 (Berkeley) 4/17/91
33 .\" $FreeBSD: src/share/doc/papers/px/pxin3.n,v 1.1.1.1.14.1 2000/11/30 17:13:59 ru Exp $
34 .\" $DragonFly: src/share/doc/papers/px/pxin3.n,v 1.2 2003/06/17 04:36:56 dillon Exp $
44 Each file in the Pascal environment is represented by a pointer
47 structure in the heap.
48 At the location addressed by the pointer is the element
49 in the file's window variable.
50 Behind this window variable is information about the file,
51 at the following offsets:
56 is a pointer to the system FILE block for the file.
57 The standard system I/O library is
58 used that provides block buffered input/output,
59 with 1024 characters normally transferred at each read or write.
63 are all linked together on a single file chain through the
68 pointer gives its associated file variable.
69 These are used to free files at block exit as described in section 3.3
77 file name for the file and the name to be used when printing
78 error diagnostics respectively.
79 Although these names are usually the same,
83 usually have no associated
84 file name so the distinction is necessary.
90 whose representations are:
94 EOF 0x0100 At end-of-file
95 EOLN 0x0200 At end-of-line (text files only)
96 SYNC 0x0400 File window is out of sync
97 TEMP 0x0800 File is temporary
98 FREAD 0x1000 File is open for reading
99 FWRITE 0x2000 File is open for writing
100 FTEXT 0x4000 File is a text file; process EOLN
101 FDEF 0x8000 File structure created, but file not opened
108 bits here reflect the associated built-in function values.
110 specifies that the file has a generated temporary name and that
111 it should therefore be removed when its block exits.
119 respectively have been done on the file so that
120 input or output operations can be done.
122 specifies the file is a text file so that
124 processing should be done,
125 with newline characters turned into blanks, etc.
131 specifies that there is no usable image in the file buffer window.
133 .I "Berkeley Pascal User's Manual,"
134 the interactive environment necessitates having
135 ``input^'' undefined at the beginning
136 of execution so that a program may print a prompt
137 before the user is required to type input.
142 it specifies that the element in the window
143 must be updated before it can be used.
144 This is never done until necessary.
146 Initialization of files
148 All the variables in the Pascal runtime environment are cleared to zero on
150 This is necessary for simple processing of files.
151 If a file is unused, its pointer will be
153 All references to an inactive file are thus references through a
156 If the Pascal system did not clear storage to zero before execution
157 it would not be possible to detect inactive files in this simple way;
158 it would probably be necessary to generate (possibly complicated)
160 each file on block entry.
162 When a file is first mentioned in a
167 a buffer of the form described above is associated with it,
168 and the necessary information about the file is placed in this
170 The file is also linked into the active file chain.
171 This chain is kept sorted by block mark address, the
177 When block exit occurs the interpreter must free the files that are in
179 and their associated buffers.
180 This is simple and efficient because the files in the active file chain are
181 sorted by increasing block mark address.
182 This means that the files for the current block will be at the front
184 For each file that is no longer accessible
185 the interpreter first flushes the files buffer
186 if it is an output file.
187 The interpreter then returns the file buffer and the files structure and window
188 to the free space in the heap and removes the file from the active file chain.
192 Flushing all the file buffers at abnormal termination,
193 or on a call to the procedure
198 each file on the file chain that has the
200 bit set in its flags word.
206 maintains a notion of an active file.
207 Each operation that references a file makes the file
208 it will be using the active file and then does its operation.
209 A subtle point here is that one may do a procedure call to
211 that involves a call to a function that references another file,
212 thereby destroying the active file set up before the
214 Thus the active file is saved at block entry
215 in the block mark and restored at block exit.\*(Dg
217 \*(dg\ It would probably be better to dispense with the notion of
218 active file and use another mechanism that did not involve extra
219 overhead on each procedure and function call.
224 Files in Pascal can be used in two distinct ways:
231 calls, or indirectly as though they were pointers.
232 The second use as pointers must be careful
233 not to destroy the active file in a reference such as
235 write(output, input\(ua)
237 or the system would incorrectly write on the input device.
239 The fundamental operator related to the use of a file is
241 This takes the file variable, as a pointer,
242 insures that the pointer is not
244 and also that a usable image is in the file window,
249 A simple example that demonstrates the use of the file operators
269 Advance the active file to the next input element.
273 A file pointer is on the stack. Insure that the associated file is active
274 and that the file is synced so that there is input available in the window.
278 If the file is a text file, read a block of text
279 and convert it to the internal type of the specified
280 operand. If the file is not a text file then
281 do an unformatted read of the next record.
284 reads upto and including the next end of line character.
290 reads a string name of an enumerated type and converts it
291 to its internal value.
293 takes a pointer to a data structure as shown in figure 3.2.
295 See the description of
297 in the next section for an example.
303 Output the element in the active file window.
307 The argument(s) on the stack are output
316 of longword arguments on the stack.
320 The character on the top of the stack is output
321 without formatting. Formatted characters must be output with
326 The string specified by the pointer on the top of the stack is output
332 All characters including nulls are printed.
336 A linefeed is output to the active file.
337 The line-count for the file is
338 incremented and checked against the line limit.
342 A formfeed is output to the active file.
346 The value on the top of the stack is converted to a pointer
347 to an enumerated type string name.
350 points to an enumerated type structure identical
353 An error is raised if the value is out of range.
354 The form of this structure for the predefined type
356 is shown in figure 3.3.
365 \fBaddl3\fR (lc)+,ap,r6 #r6 points to scalar name list
366 \fBmovl\fR (sp)+,r3 #r3 has data value
367 \fBcmpw\fR r3,(r6)+ #check value out of bounds
369 \fBmovzwl\fR (r6)[r3],r4 #r4 has string index
370 \fBpushab\fR (r6)[r4] #push string pointer
373 \fBmovw\fR $ENAMRNG,_perrno
376 The address of the table is calculated by adding the base address
377 of the interpreter code,
379 to the offset pointed to by
381 The first word of the table gives the number of records and
382 provides a range check of the data to be output.
383 The pointer is then calculated as
388 return(tblbase + tblbase[value]);
395 is subtracted from the integer on the top of the stack.
396 The maximum of the result and the second argument,
398 replaces the value on the top of the stack.
399 This function verifies that variable specified
400 width arguments are non-negative, and meet certain minimum width
405 The minimum of the value on the top of the stack
406 and the sub-opcode replaces the value on the top
410 The uses of files and the file operations are summarized
411 in an example which outputs a real variable (r) with a variable
414 writeln('r =',r:i,' ',true);
416 that generates the code
448 is an abbreviated form of the operator
450 that is used when the file to be made active is
452 A file descriptor, record count, string size, and a pointer
453 to the constant string ``r ='' are pushed
458 is pushed on the stack
459 and the precision size is calculated by taking
460 seven less than the width, but not less than one.
461 This is followed by the width that is reduced by
462 one to leave space for the required leading blank.
463 If the width is too narrow, it
466 A pointer to the format string is pushed followed
467 by a file descriptor and the operator
473 comes from two longs for
475 and a long each for the precision, width, format string pointer,
481 character onto a long on the stack that is then printed out by
483 The internal representation for
485 is pushed as a long onto the stack and is
486 then replaced by a pointer to the string ``true''
492 This string is output by the operator
494 using the format string ``%s''.
497 appends a newline to the file.
499 File activation and status operations
503 The file pointed to by the file pointer on the top
504 of the stack is converted to be the active file.
509 imply standard input and output respectively
510 instead of explicitly pushing their file pointers.
516 library file descriptor associated with the active file
517 is pushed onto the stack.
521 The file pointed to by the file pointer on the top
522 of the stack is checked for end of file. A boolean
525 indicating the end of file condition.
529 The file pointed to by the file pointer on the top
530 of the stack is checked for end of line. A boolean
533 indicating the end of line condition.
534 Note that only text files can check for end of line.
536 File housekeeping operations
540 Four data items are passed on the stack;
541 the size of the data type associated with the file,
542 the maximum size of the file name,
543 a pointer to the file name,
544 and a pointer to the file variable.
545 A file record is created with the specified window size
546 and the file variable set to point to it.
547 The file is marked as defined but not opened.
550 statement association of file names with file variables
551 before their use by a
558 The sub-opcode is placed in the external variable
560 to specify the amount of I/O buffering that is desired.
561 The current options are:
563 0 \- character at a time buffering
564 1 \- line at a time buffering
567 The default value is 1.
573 Four data items are passed on the stack;
574 the size of the data type associated with the file,
575 the maximum size of the name (possibly zero),
576 a pointer to the file name (possibly null),
577 and a pointer to the file variable.
578 If the file has never existed it is created as in
580 If no file name is specified and no previous name exists
581 (for example one created by
583 ) then a system temporary name is created.
585 then opens the file for input, while
587 opens the file for output.
590 The three remaining file operations are
592 that flushes the active file,
594 that takes the pointer to a file name and removes the
597 that flushes all the output files and sets the
598 standard error file to be the active file.