1 .\" $NetBSD
: pxin3
.n
,v
1.2 1998/01/09 06:41:56 perry Exp $
3 .\" Copyright (c
) 1979 The Regents of the University of California
.
4 .\" All rights reserved
.
6 .\" Redistribution
and use
in source
and binary forms
, with or without
7 .\" modification
, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice
, this list of conditions
and the following disclaimer
.
11 .\" 2. Redistributions
in binary form must reproduce the above copyright
12 .\" notice
, this list of conditions
and the following disclaimer
in the
13 .\" documentation
and/or other materials provided with the distribution
.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from
this software
16 .\" without specific prior written permission
.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS
'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES
, INCLUDING
, BUT NOT LIMITED TO
, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED
. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT
, INDIRECT
, INCIDENTAL
, SPECIAL
, EXEMPLARY
, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING
, BUT NOT LIMITED TO
, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES
; LOSS OF USE
, DATA
, OR PROFITS
; OR BUSINESS INTERRUPTION
)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY
, WHETHER IN CONTRACT
, STRICT
26 .\" LIABILITY
, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE
) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE
, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @
(#)pxin3.n 5.2 (Berkeley) 4/17/91
41 Each file
in the Pascal environment is represented by a pointer
44 structure
in the heap
.
45 At the location addressed by the pointer is the element
46 in the file
's window variable.
47 Behind this window variable is information about the file,
48 at the following offsets:
53 is a pointer to the system FILE block for the file.
54 The standard system I/O library is
55 used that provides block buffered input/output,
56 with 1024 characters normally transferred at each read or write.
60 are all linked together on a single file chain through the
65 pointer gives its associated file variable.
66 These are used to free files at block exit as described in section 3.3
74 file name for the file and the name to be used when printing
75 error diagnostics respectively.
76 Although these names are usually the same,
80 usually have no associated
81 file name so the distinction is necessary.
87 whose representations are:
91 EOF 0x0100 At end-of-file
92 EOLN 0x0200 At end-of-line (text files only)
93 SYNC 0x0400 File window is out of sync
94 TEMP 0x0800 File is temporary
95 FREAD 0x1000 File is open for reading
96 FWRITE 0x2000 File is open for writing
97 FTEXT 0x4000 File is a text file; process EOLN
98 FDEF 0x8000 File structure created, but file not opened
105 bits here reflect the associated built-in function values.
107 specifies that the file has a generated temporary name and that
108 it should therefore be removed when its block exits.
116 respectively have been done on the file so that
117 input or output operations can be done.
119 specifies the file is a text file so that
121 processing should be done,
122 with newline characters turned into blanks, etc.
128 specifies that there is no usable image in the file buffer window.
130 .I "Berkeley Pascal User's Manual
,"
131 the interactive environment necessitates having
132 ``input^'' undefined at the beginning
133 of execution so that a program may print a prompt
134 before the user is required to type input.
139 it specifies that the element in the window
140 must be updated before it can be used.
141 This is never done until necessary.
143 Initialization of files
145 All the variables in the Pascal runtime environment are cleared to zero on
147 This is necessary for simple processing of files.
148 If a file is unused, its pointer will be
150 All references to an inactive file are thus references through a
153 If the Pascal system did not clear storage to zero before execution
154 it would not be possible to detect inactive files in this simple way;
155 it would probably be necessary to generate (possibly complicated)
157 each file on block entry.
159 When a file is first mentioned in a
164 a buffer of the form described above is associated with it,
165 and the necessary information about the file is placed in this
167 The file is also linked into the active file chain.
168 This chain is kept sorted by block mark address, the
174 When block exit occurs the interpreter must free the files that are in
176 and their associated buffers.
177 This is simple and efficient because the files in the active file chain are
178 sorted by increasing block mark address.
179 This means that the files for the current block will be at the front
181 For each file that is no longer accessible
182 the interpreter first flushes the files buffer
183 if it is an output file.
184 The interpreter then returns the file buffer and the files structure and window
185 to the free space in the heap and removes the file from the active file chain.
189 Flushing all the file buffers at abnormal termination,
190 or on a call to the procedure
195 each file on the file chain that has the
197 bit set in its flags word.
203 maintains a notion of an active file.
204 Each operation that references a file makes the file
205 it will be using the active file and then does its operation.
206 A subtle point here is that one may do a procedure call to
208 that involves a call to a function that references another file,
209 thereby destroying the active file set up before the
211 Thus the active file is saved at block entry
212 in the block mark and restored at block exit.\*(Dg
214 \*(dg\ It would probably be better to dispense with the notion of
215 active file and use another mechanism that did not involve extra
216 overhead on each procedure and function call.
221 Files in Pascal can be used in two distinct ways:
228 calls, or indirectly as though they were pointers.
229 The second use as pointers must be careful
230 not to destroy the active file in a reference such as
232 write(output, input\(ua)
234 or the system would incorrectly write on the input device.
236 The fundamental operator related to the use of a file is
238 This takes the file variable, as a pointer,
239 insures that the pointer is not
241 and also that a usable image is in the file window,
246 A simple example that demonstrates the use of the file operators
266 Advance the active file to the next input element.
270 A file pointer is on the stack. Insure that the associated file is active
271 and that the file is synced so that there is input available in the window.
275 If the file is a text file, read a block of text
276 and convert it to the internal type of the specified
277 operand. If the file is not a text file then
278 do an unformatted read of the next record.
281 reads upto and including the next end of line character.
287 reads a string name of an enumerated type and converts it
288 to its internal value.
290 takes a pointer to a data structure as shown in figure 3.2.
292 See the description of
294 in the next section for an example.
300 Output the element in the active file window.
304 The argument(s) on the stack are output
313 of longword arguments on the stack.
317 The character on the top of the stack is output
318 without formatting. Formatted characters must be output with
323 The string specified by the pointer on the top of the stack is output
329 All characters including nulls are printed.
333 A linefeed is output to the active file.
334 The line-count for the file is
335 incremented and checked against the line limit.
339 A formfeed is output to the active file.
343 The value on the top of the stack is converted to a pointer
344 to an enumerated type string name.
347 points to an enumerated type structure identical
350 An error is raised if the value is out of range.
351 The form of this structure for the predefined type
353 is shown in figure 3.3.
362 \fBaddl3\fR (lc)+,ap,r6 #r6 points to scalar name list
363 \fBmovl\fR (sp)+,r3 #r3 has data value
364 \fBcmpw\fR r3,(r6)+ #check value out of bounds
366 \fBmovzwl\fR (r6)[r3],r4 #r4 has string index
367 \fBpushab\fR (r6)[r4] #push string pointer
370 \fBmovw\fR $ENAMRNG,_perrno
373 The address of the table is calculated by adding the base address
374 of the interpreter code,
376 to the offset pointed to by
378 The first word of the table gives the number of records and
379 provides a range check of the data to be output.
380 The pointer is then calculated as
385 return(tblbase + tblbase[value]);
392 is subtracted from the integer on the top of the stack.
393 The maximum of the result and the second argument,
395 replaces the value on the top of the stack.
396 This function verifies that variable specified
397 width arguments are non-negative, and meet certain minimum width
402 The minimum of the value on the top of the stack
403 and the sub-opcode replaces the value on the top
407 The uses of files and the file operations are summarized
408 in an example which outputs a real variable (r) with a variable
411 writeln('r =',r:i,' ',true);
413 that generates the code
445 is an abbreviated form of the operator
447 that is used when the file to be made active is
449 A file descriptor, record count, string size, and a pointer
450 to the constant string ``r ='' are pushed
455 is pushed on the stack
456 and the precision size is calculated by taking
457 seven less than the width, but not less than one.
458 This is followed by the width that is reduced by
459 one to leave space for the required leading blank.
460 If the width is too narrow, it
463 A pointer to the format string is pushed followed
464 by a file descriptor and the operator
470 comes from two longs for
472 and a long each for the precision, width, format string pointer,
478 character onto a long on the stack that is then printed out by
480 The internal representation for
482 is pushed as a long onto the stack and is
483 then replaced by a pointer to the string ``true''
489 This string is output by the operator
491 using the format string ``%s''.
494 appends a newline to the file.
496 File activation and status operations
500 The file pointed to by the file pointer on the top
501 of the stack is converted to be the active file.
506 imply standard input and output respectively
507 instead of explicitly pushing their file pointers.
513 library file descriptor associated with the active file
514 is pushed onto the stack.
518 The file pointed to by the file pointer on the top
519 of the stack is checked for end of file. A boolean
522 indicating the end of file condition.
526 The file pointed to by the file pointer on the top
527 of the stack is checked for end of line. A boolean
530 indicating the end of line condition.
531 Note that only text files can check for end of line.
533 File housekeeping operations
537 Four data items are passed on the stack;
538 the size of the data type associated with the file,
539 the maximum size of the file name,
540 a pointer to the file name,
541 and a pointer to the file variable.
542 A file record is created with the specified window size
543 and the file variable set to point to it.
544 The file is marked as defined but not opened.
547 statement association of file names with file variables
548 before their use by a
555 The sub-opcode is placed in the external variable
557 to specify the amount of I/O buffering that is desired.
558 The current options are:
560 0 \- character at a time buffering
561 1 \- line at a time buffering
564 The default value is 1.
570 Four data items are passed on the stack;
571 the size of the data type associated with the file,
572 the maximum size of the name (possibly zero),
573 a pointer to the file name (possibly null),
574 and a pointer to the file variable.
575 If the file has never existed it is created as in
577 If no file name is specified and no previous name exists
578 (for example one created by
580 ) then a system temporary name is created.
582 then opens the file for input, while
584 opens the file for output.
587 The three remaining file operations are
589 that flushes the active file,
591 that takes the pointer to a file name and removes the
594 that flushes all the output files and sets the
595 standard error file to be the active file.