1 07/10/84  Multics Pascal File I/O
  2 
  3 Multics allows Pascal programs to read and write storage system files,
  4 both sequentially and via direct access (indexed) I/O. This feature is
  5 an extension to the ISO standard.
  6 
  7 
  8 Predefined files:
  9 Pascal provides three predefined file variables named "input",
 10 "output", and "error".  A program that uses any of these must include
 11 its name in the parameter list of the program header.  These file
 12 variables are not, however, to be declared in the "var" section of the
 13 main program.  Default attachments are:
 14 
 15           Pascal file name    I/O switch name     Attachment
 16           ----------------    ---------------     ----------
 17           input               pascal_input_       syn_ user_input
 18           output              pascal_output_      syn_ user_output
 19           error               pascal_error_       syn_ error_output
 20 
 21 
 22 All three files are, by default, open at the beginning of program
 23 execution: reset(input), rewrite(output), rewrite(error).
 24 
 25 The "error" file is a Multics SOL extension to standard Pascal.
 26 Its use requires compilation with the -full_extensions (-full) or
 27 -sol control argument.
 28 
 29 
 30 User-declared file variables:
 31 All files other than the predefined "input", "output", and "error"
 32 correspond to I/O switches of the same name.  There are three types of
 33 user-declared Pascal file variables: permanent, static, and local.
 34 
 35 Permanent files are named in the parameter list of the program header
 36 and are also declared in the "var" section of the main program.
 37 Permanent files have no default attachment and are not opened by
 38 default.
 39 
 40 
 41 Static files are not named in the parameter list of the program header
 42 but are declared in the "var" section of the main program.  A static
 43 file has a default attach description that refers to a temporary
 44 segment allocated in the process directory at the first invocation of
 45 the program.  This segment remains until the end of the process or
 46 "termination" of the program.  Static files are not opened by default.
 47 
 48 Local files are declared in the internal procedures of the program. A
 49 local file has a default attach description that refers to a temporary
 50 segment that is allocated in the process directory when the procedure
 51 is invoked and freed when the procedure exits (via return, release, or
 52 nonlocal goto). Local files are not opened by default.
 53 
 54 
 55 Files that are not attached by default must be attached explicitly with
 56 the "fconnect" statement.  Files that are not opened by default must be
 57 opened explicitly with the "reset" statement (for input) or the
 58 "rewrite" statement (for output). See "Attaching a file from within a
 59 program" below.
 60 
 61 
 62 Redirecting I/O of a Pascal program:
 63 To divert input or output to a Multics file, use the io_call (io)
 64 command (type "help io") using the syntax:
 65 
 66           io attach file_variable_name vfile_ pathname
 67 
 68 where file_variable_name is the lowercase name of a Pascal file
 69 variable on which the program performs I/O (for example, the
 70 predefined file variables "input", "output", and "error") and pathname
 71 is the pathname of a Multics file.
 72 
 73 
 74 For example, to allow a program to take input (on the "input" file
 75 variable) from a segment named test_data, type the following command
 76 line before running the program--
 77 
 78           io attach pascal_input_ vfile_ test_data
 79 
 80 To restore terminal input, type--
 81 
 82           io detach pascal_input_
 83 
 84 If "input" is not explicitly attached, running a Pascal program that
 85 uses the "input" file variable causes it to be attached to the
 86 terminal.  When the program completes, "input" remains attached to the
 87 terminal and must be detached via "io detach pascal_input_" before it
 88 can be re-attached elsewhere.
 89 
 90 
 91 To divert program output on "output" to a segment named test_results,
 92 issue the following command line before running the program--
 93 
 94           io attach pascal_output_ vfile_ test_results
 95 
 96 To restore terminal output, type the command line--
 97 
 98           io detach pascal_output_
 99 
100 
101 Attaching a file from within a program:
102 The Pascal "fconnect" statement attaches files within a Pascal
103 program.  This statement is a nonstandard Multics SOL Pascal extension
104 and requires the program to be compiled with the -full_extensions
105 (-full) control argument.  The syntax of this statement is:
106 
107           fconnect (file_name, attach_description);
108 
109 where file_name is the name of the file to be connected (for example,
110 "input") and attach_description is a Multics attach description (for
111 example, "vfile_ test_data").  The "fconnect" statement closes the
112 file if it was open, detaches the file if it was attached, and
113 attaches the file using attach_description.
114 
115 
116 Opening a file:
117 Once a file is attached, it must be opened using the "reset" statement
118 (for input) or the "rewrite" statement (for output). If a file is not
119 attached when either of these statements is executed, the default
120 attach description (if any; for example, "syn_ user_input" for "input")
121 is used to attach the file. If there is no default attach description,
122 an error occurs. If the file is already opened, it is closed and
123 re-opened with the same attachment.
124 
125 
126 Closing a file:
127 A file is closed automatically when the procedure in which it is
128 declared exits (via "return", "end", or nonlocal "goto"). To close a
129 file at any time before that, use the nonstandard "fclose" statement.
130 This is a Multics extension to standard Pascal. Its syntax is:
131 
132           fclose (file_name);
133 
134 On close (implicit or explicit), the file is detached if it has been
135 attached in the program by the fconnect procedure.  If it was attached
136 before execution of the program, it is not detached.
137 
138 
139 Sequential I/O:
140 Sequential input and output to files is performed via the standard
141 Pascal I/O statements.  A Multics SOL extension adds the following
142 statement:
143 
144           flush (file_name);
145 
146 which prints the contents of the output buffer for file_name, which
147 contains the results of any previous "write" operations, without
148 outputting a newline character.
149 
150 
151 Random access I/O:
152 The following nonstandard I/O statements are offered as Multics SOL
153 extensions to standard Pascal:
154 
155 fupdate (file_name);
156    opens a switch for direct update.
157 fput (file_name, integer_expression);
158    transfers an item into the file buffer.  Execution of this
159    statement causes the item in the file file_name to be output to the
160    file to which file_name is attached, as a data record with the
161    character representation of integer_expression as the key.
162 fget (file_name, integer_expression);
163    transfers an item from the file buffer.  The specified record is
164    input into the file file_name.
165 
166 
167 Use of these statements requires compilation with the -full_extensions
168 (-full) control argument.  Files used by these statements must be
169 attached.
170 
171 
172 Example of random I/O:
173 The following program writes 100 records to the direct access file
174 named >udd>Foo>Bar>squares. Each record has key "N" where N is an
175 integer from 1 to 100, and has data value N**2. Finally, the program
176 reads the record whose key is "50" and prints out its value.
177 
178 
179    program square (output, squarefile);
180       var
181          squarefile: file of integer;
182          i: integer;
183       begin
184          fconnect (squarefile, 'vfile_ >udd>Foo>Bar>squares');
185          fupdate (squarefile);
186          for i := 1 to 100 do
187             begin
188                squarefile^ := i**2;
189                fput (squarefile, i)
190             end;
191          fget (squarefile, 50);
192          writeln (squarefile^);
193       END.