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. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)pxin3.n 5.2 (Berkeley) 4/17/91
29 .\" $FreeBSD: src/share/doc/papers/px/pxin3.n,v 1.1.1.1.14.1 2000/11/30 17:13:59 ru Exp $
30 .\" $DragonFly: src/share/doc/papers/px/pxin3.n,v 1.2 2003/06/17 04:36:56 dillon Exp $
40 Each file in the Pascal environment is represented by a pointer
43 structure in the heap.
44 At the location addressed by the pointer is the element
45 in the file's window variable.
46 Behind this window variable is information about the file,
47 at the following offsets:
52 is a pointer to the system FILE block for the file.
53 The standard system I/O library is
54 used that provides block buffered input/output,
55 with 1024 characters normally transferred at each read or write.
59 are all linked together on a single file chain through the
64 pointer gives its associated file variable.
65 These are used to free files at block exit as described in section 3.3
73 file name for the file and the name to be used when printing
74 error diagnostics respectively.
75 Although these names are usually the same,
79 usually have no associated
80 file name so the distinction is necessary.
86 whose representations are:
90 EOF 0x0100 At end-of-file
91 EOLN 0x0200 At end-of-line (text files only)
92 SYNC 0x0400 File window is out of sync
93 TEMP 0x0800 File is temporary
94 FREAD 0x1000 File is open for reading
95 FWRITE 0x2000 File is open for writing
96 FTEXT 0x4000 File is a text file; process EOLN
97 FDEF 0x8000 File structure created, but file not opened
104 bits here reflect the associated built-in function values.
106 specifies that the file has a generated temporary name and that
107 it should therefore be removed when its block exits.
115 respectively have been done on the file so that
116 input or output operations can be done.
118 specifies the file is a text file so that
120 processing should be done,
121 with newline characters turned into blanks, etc.
127 specifies that there is no usable image in the file buffer window.
129 .I "Berkeley Pascal User's Manual,"
130 the interactive environment necessitates having
131 ``input^'' undefined at the beginning
132 of execution so that a program may print a prompt
133 before the user is required to type input.
138 it specifies that the element in the window
139 must be updated before it can be used.
140 This is never done until necessary.
142 Initialization of files
144 All the variables in the Pascal runtime environment are cleared to zero on
146 This is necessary for simple processing of files.
147 If a file is unused, its pointer will be
149 All references to an inactive file are thus references through a
152 If the Pascal system did not clear storage to zero before execution
153 it would not be possible to detect inactive files in this simple way;
154 it would probably be necessary to generate (possibly complicated)
156 each file on block entry.
158 When a file is first mentioned in a
163 a buffer of the form described above is associated with it,
164 and the necessary information about the file is placed in this
166 The file is also linked into the active file chain.
167 This chain is kept sorted by block mark address, the
173 When block exit occurs the interpreter must free the files that are in
175 and their associated buffers.
176 This is simple and efficient because the files in the active file chain are
177 sorted by increasing block mark address.
178 This means that the files for the current block will be at the front
180 For each file that is no longer accessible
181 the interpreter first flushes the files buffer
182 if it is an output file.
183 The interpreter then returns the file buffer and the files structure and window
184 to the free space in the heap and removes the file from the active file chain.
188 Flushing all the file buffers at abnormal termination,
189 or on a call to the procedure
194 each file on the file chain that has the
196 bit set in its flags word.
202 maintains a notion of an active file.
203 Each operation that references a file makes the file
204 it will be using the active file and then does its operation.
205 A subtle point here is that one may do a procedure call to
207 that involves a call to a function that references another file,
208 thereby destroying the active file set up before the
210 Thus the active file is saved at block entry
211 in the block mark and restored at block exit.\*(Dg
213 \*(dg\ It would probably be better to dispense with the notion of
214 active file and use another mechanism that did not involve extra
215 overhead on each procedure and function call.
220 Files in Pascal can be used in two distinct ways:
227 calls, or indirectly as though they were pointers.
228 The second use as pointers must be careful
229 not to destroy the active file in a reference such as
231 write(output, input\(ua)
233 or the system would incorrectly write on the input device.
235 The fundamental operator related to the use of a file is
237 This takes the file variable, as a pointer,
238 insures that the pointer is not
240 and also that a usable image is in the file window,
245 A simple example that demonstrates the use of the file operators
265 Advance the active file to the next input element.
269 A file pointer is on the stack. Insure that the associated file is active
270 and that the file is synced so that there is input available in the window.
274 If the file is a text file, read a block of text
275 and convert it to the internal type of the specified
276 operand. If the file is not a text file then
277 do an unformatted read of the next record.
280 reads upto and including the next end of line character.
286 reads a string name of an enumerated type and converts it
287 to its internal value.
289 takes a pointer to a data structure as shown in figure 3.2.
291 See the description of
293 in the next section for an example.
299 Output the element in the active file window.
303 The argument(s) on the stack are output
312 of longword arguments on the stack.
316 The character on the top of the stack is output
317 without formatting. Formatted characters must be output with
322 The string specified by the pointer on the top of the stack is output
328 All characters including nulls are printed.
332 A linefeed is output to the active file.
333 The line-count for the file is
334 incremented and checked against the line limit.
338 A formfeed is output to the active file.
342 The value on the top of the stack is converted to a pointer
343 to an enumerated type string name.
346 points to an enumerated type structure identical
349 An error is raised if the value is out of range.
350 The form of this structure for the predefined type
352 is shown in figure 3.3.
361 \fBaddl3\fR (lc)+,ap,r6 #r6 points to scalar name list
362 \fBmovl\fR (sp)+,r3 #r3 has data value
363 \fBcmpw\fR r3,(r6)+ #check value out of bounds
365 \fBmovzwl\fR (r6)[r3],r4 #r4 has string index
366 \fBpushab\fR (r6)[r4] #push string pointer
369 \fBmovw\fR $ENAMRNG,_perrno
372 The address of the table is calculated by adding the base address
373 of the interpreter code,
375 to the offset pointed to by
377 The first word of the table gives the number of records and
378 provides a range check of the data to be output.
379 The pointer is then calculated as
384 return(tblbase + tblbase[value]);
391 is subtracted from the integer on the top of the stack.
392 The maximum of the result and the second argument,
394 replaces the value on the top of the stack.
395 This function verifies that variable specified
396 width arguments are non-negative, and meet certain minimum width
401 The minimum of the value on the top of the stack
402 and the sub-opcode replaces the value on the top
406 The uses of files and the file operations are summarized
407 in an example which outputs a real variable (r) with a variable
410 writeln('r =',r:i,' ',true);
412 that generates the code
444 is an abbreviated form of the operator
446 that is used when the file to be made active is
448 A file descriptor, record count, string size, and a pointer
449 to the constant string ``r ='' are pushed
454 is pushed on the stack
455 and the precision size is calculated by taking
456 seven less than the width, but not less than one.
457 This is followed by the width that is reduced by
458 one to leave space for the required leading blank.
459 If the width is too narrow, it
462 A pointer to the format string is pushed followed
463 by a file descriptor and the operator
469 comes from two longs for
471 and a long each for the precision, width, format string pointer,
477 character onto a long on the stack that is then printed out by
479 The internal representation for
481 is pushed as a long onto the stack and is
482 then replaced by a pointer to the string ``true''
488 This string is output by the operator
490 using the format string ``%s''.
493 appends a newline to the file.
495 File activation and status operations
499 The file pointed to by the file pointer on the top
500 of the stack is converted to be the active file.
505 imply standard input and output respectively
506 instead of explicitly pushing their file pointers.
512 library file descriptor associated with the active file
513 is pushed onto the stack.
517 The file pointed to by the file pointer on the top
518 of the stack is checked for end of file. A boolean
521 indicating the end of file condition.
525 The file pointed to by the file pointer on the top
526 of the stack is checked for end of line. A boolean
529 indicating the end of line condition.
530 Note that only text files can check for end of line.
532 File housekeeping operations
536 Four data items are passed on the stack;
537 the size of the data type associated with the file,
538 the maximum size of the file name,
539 a pointer to the file name,
540 and a pointer to the file variable.
541 A file record is created with the specified window size
542 and the file variable set to point to it.
543 The file is marked as defined but not opened.
546 statement association of file names with file variables
547 before their use by a
554 The sub-opcode is placed in the external variable
556 to specify the amount of I/O buffering that is desired.
557 The current options are:
559 0 \- character at a time buffering
560 1 \- line at a time buffering
563 The default value is 1.
569 Four data items are passed on the stack;
570 the size of the data type associated with the file,
571 the maximum size of the name (possibly zero),
572 a pointer to the file name (possibly null),
573 and a pointer to the file variable.
574 If the file has never existed it is created as in
576 If no file name is specified and no previous name exists
577 (for example one created by
579 ) then a system temporary name is created.
581 then opens the file for input, while
583 opens the file for output.
586 The three remaining file operations are
588 that flushes the active file,
590 that takes the pointer to a file name and removes the
593 that flushes all the output files and sets the
594 standard error file to be the active file.