BSD 3 development
[unix-history] / .ref-BSD-2 / doc / px / pxin3.n
CommitLineData
1662094b
BJ
1.if !\n(xx .so tmac.p
2.nr H1 2
3.if n .ND
4.NH
5Input/output
6.NH 2
7The files structure
8.PP
9Each file in the Pascal environment is represented by a pointer
10to a
11.I files
12structure in the heap.
13At the location addressed by the pointer is the element
14in the file's window variable.
15Behind this window variable is information about the file,
16at the following offsets:
17.TS
18center;
19n l l.
20\-14 FBUF Pointer to i/o buffer
21\-12 FCHAIN Chain to next file
22\-10 FLEV Pointer to associated block mark
23\-8 PFNAME Name of file for error messages
24\-6 FNAME Name of associated file
25\-4 FUNIT Unit number packed with flags
26\-2 FSIZE Size of elements in the file
270 File window element
28.TE
29.PP
30Here
31.SM FBUF
32is a pointer to the input or output buffer for the file.
33The standard system routines
34.I getc
35and
36.I putc
37are used and provide block buffered input/output,
38with 512 characters normally transferred at each read or write.
39.PP
40The files in the
41Pascal environment,
42with the exception of
43.I input
44and
45.I output
46are all linked together on a single file chain through the
47.SM FCHAIN
48links.
49For each file the
50.SM FLEV
51pointer gives its associated block mark.
52These are used to free files at block exit as described in section 3.3
53below.
54.PP
55The
56NAME
57and
58PFNAME
59give the associated
60file name for the file and the name to be used when printing
61error diagnostics respectively.
62Although these names are usually the same,
63.I input
64and
65.I output
66usually have no associated
67file name so the distinction is necessary.
68.PP
69The
70FUNIT
71word contains the
72unit number on which the file is open as well as a set of flags.
73These flags and their representations are:
74.TS
75center;
76l l l.
77EOF 00400 At end-of-file
78EOLN 01000 At end-of-line
79SYNC 02000 File window is out of sync
80TEMP 04000 File is temporary
81FREAD 02000 File is open for reading
82FWRITE 04000 File is open for writing
83FTEXT 08000 File is a text file; process EOLN
84.TE
85.PP
86The
87EOF
88and
89EOLN
90bits here reflect the associated built-in function values.
91TEMP
92indicates that the file has a generated temporary name and that
93it should therefore be removed when its block exits.
94FREAD
95and
96FWRITE
97indicate that
98.I reset
99and
100.I rewrite
101respectively have been performed on the file so that
102input or output operations should be attempted.
103FTEXT
104indicates the file is a text file so that
105EOLN
106processing should be done,
107with newline characters turned into blanks, etc.
108.PP
109The
110SYNC
111bit,
112when true,
113indicates that there is no usable image in the file buffer window.
114As discussed in the
115.I "Berkeley Pascal User's Manual,"
116it is necessary,
117because of the interactive environment,
118to have ``input^'' essentially undefined at the beginning
119of execution so that a program may print a prompt
120before the user is required to type input.
121The
122SYNC
123bit implements this.
124When it is set,
125it indicates that before the element in the window
126can be used the Pascal system must actually put something there.
127This is never done until necessary.
128.NH 2
129Initialization of files
130.PP
131All the variables in the Pascal runtime environment are cleared to zero on
132block entry.
133This is necessary for simple processing of files.
134If a file is unused, its pointer will be
135.B nil.
136All references to an inactive file are thus references through a
137.B nil
138pointer.
139If the Pascal system did not clear storage to zero before execution
140it would not be possible to detect inactive files in this simple way;
141it would probably be necessary to generate (possibly complicated)
142code to initialize
143each file on block entry.
144.PP
145When a file is first mentioned in a
146.I reset
147or
148.I rewrite
149call,
150a buffer of the form described above is associated with it,
151and the necessary information about the file is placed in this
152buffer.
153The file is also linked into the active file chain.
154This chain is kept sorted by block mark address, the
155FLEV
156entries.
157.NH 2
158Block exit
159.PP
160When block exit occurs the interpreter must free the files which are in
161use in the block
162and their associated buffers.
163This is simple and efficient because the files in the active file chain are
164sorted by increasing block mark address.
165This means that the files for the current block will be at the front
166of the chain.
167For each file which is no longer accessible
168the interpreter first flushes the files buffer
169if it is an output file.
170The interpreter then returns the file buffer and the files structure and window
171to the free space in the heap and removes the file from the active file chain.
172.NH 2
173Flushing
174.PP
175Flushing all the file buffers at abnormal termination,
176or on a call to the procedure
177.I flush
178or
179.I message
180is quite easy.
181The Pascal system simply flushes the file
182.I output
183and each file on the file chain which has the
184FWRITE
185bit set in its flags word.
186.NH 2
187The active file
188.PP
189For the purposes of input-output,
190.I px
191maintains a notion of an active file.
192Each operation which references a file makes the file
193it will be using the active file and then performs its operation.
194A subtle point here is that one may do a procedure call to
195.I write
196which involves a call to a function which references another file,
197thereby destroying the active file set up before the
198.I write.
199For this reason,
200the active file is saved at block entry
201in the block mark and restored at block exit.\*(Dg
202.FS
203\*(dgIt would probably be better to dispense with the notion of
204active file and use another mechanism which did not involve extra
205overhead on each procedure and function call.
206.FE
207.NH 2
208File operations
209.PP
210Files in Pascal can be used in two distinct ways:
211as the object of
212.I read,
213.I write,
214.I get,
215and
216.I put
217calls, or indirectly as though they were pointers.
218It should be noted that the second use as pointers must be careful
219not to destroy the active file in a reference such as
220.LS
221write(output, input\(ua)
222.LE
223or the system would end up writing on the input device.
224.PP
225The fundamental operator related to the use of a file is
226FNIL.
227This takes the file variable, as a pointer,
228insures that the pointer is not
229.B nil,
230and also that a usable image is in the file window,
231by forcing the
232SYNC
233bit to be cleared.
234.PP
235The rest of the uses of files and the file operations may be summarized
236by a simple example:
237.LS
238write('*')
239.LE
240which generates the code
241.LS
242\s-2UNITOUT\s0
243\s-2CONC\s0 '*'
244\s-2WRITC\s0
245.LE
246Here the operator
247.SM UNITOUT
248is an abbreviated form of the operator
249.SM UNIT
250which sets the active file,
251when the file to be made active is
252.I output.
253Thus to write a character to the file
254.I output
255it is only necessary to make
256.I output
257the active file,
258to place the character to be output
259on the stack,
260and to do a
261.SM WRITC
262write character operation.
263.PP
264Files other than
265.I output
266differ from this example only in that the operator
267.op UNIT
268is used preceded by an evaluation of the file variable expression.
269Thus
270.LS
271writeln(f)
272.LE
273produces
274.LS
275\s-2RV\s0 \fIf\fP
276\s-2UNIT\s0
277\s-2WRITELN\s0
278.LE
279.PP
280Write widths are easily handled here by packing information
281about the presence or absence of width specifications and their
282types into the sub-operation code and pushing the values
283of the write widths onto the top of the stack.
284.PP
285One other operation worth mentioning is
286.SM DEFNAME
287which is used to implement the program statement association of
288file names.
289.SM DEFNAME
290simply allocates the
291.I files
292(section 3.1) area for the given file as though
293it had been the object of a
294.I reset
295or
296.I rewrite
297call, initializing the
298FNAME
299field, but omitting the system interactions associated with
300and actual
301.I reset
302or
303.I rewrite.