BSD 3 development
[unix-history] / usr / doc / lisp / ch5.n
CommitLineData
f9211498
JF
1.Lc Input/Output 5
2.\".ch 5
3The following functions are used to read and write to and from external devices
4and programs (through pipes).
5All I/O goes through the lisp data type called the port.
6A port may be open for either reading or writing but not both
7simultaneously.
8There are only a limited number of ports (20) and they will not be reclaimed
9unless you
10.i close
11them.
12All ports are reclaimed by a
13.i resetio
14call but this drastic step won't be necessary if the program closes
15what it uses.
16If you don't supply a port argument to a function which requires one
17or if you supply a bad port argument (such as nil)
18then
19.Fr
20will use the default port according to this scheme.
21If you are reading then the default port is the value of the symbol
22.i piport
23and if you are writing it is the value of the symbol
24.i poport .
25Furthermore if the value of piport or poport is not a valid port then
26the standard input or standard output will be used, respectively.
27The standard input and standard output are usually the keyboard and
28terminal display unless your job is running in the background and its
29input or output is connected to a pipe.
30.Lf close "'p_port"
31.Re
32t
33.Se
34the specified port is drained and closed, releasing the port.
35.No
36The standard defaults are not used in this case since you probably never
37want to close the standard output or standard input.
38.Lf drain "['p_port]"
39.Re
40nil
41.Se
42If this is an output port then
43the characters in the output buffer are all sent to the device.
44If this is an input port then all pending characters are flushed.
45The default port for this function is the default output port.
46.Lf flatsize "'g_form 'x_max"
47.Re
48the minumum of x_max and
49the number of characters required to print g_form using patom (that is,
50no escapes).
51.No
52flatsize only knows about lists, symbols, fixnums and flonums.
53.\".pg
54.Lf infile "'s_filename"
55.Re
56a port ready to read s_filename.
57.Se
58this tries to open s_filename and if it cannot or if there are no
59ports available it gives an error message.
60.No
61to allow your program to continue on a file not found error you can
62use something like:
63.br
64(cond ((null (setq myport (car (errset (infile name) nil))))
65.br
66\ \ \ \ \ \ \ \ (patom '"couldn't open the file")))
67.br
68which will set myport to the port to read from if the file exists
69or will print a message if it couldn't open it and also set myport to nil.
70.Lf load "'s_filename"
71.Re
72nil
73.Se
74the given filename or one similar to it is loaded.
75The sequence of places searched is this:
76.in +12n
77.np
78s_filename.l in the current directory
79.np
80/usr/lib/lisp/s_filename.l
81.np
82s_filename in the current directory
83.np
84/usr/lib/lisp/s_filename
85.No
86load required a port to open the file s_filename.
87It then lambda binds the symbol piport to this port and reads and
88evaluates the form.
89.Lf makereadtable "['s_flag]"
90.Wh
91if s_flag is not present it is assumed to be nil.
92.Re
93a readtable equal to the original readtable if s_flag is non nil, or else
94equal to the current readtable.
95See chapter XX for a description of readtables and their uses.
96.Lf nwritn "['p_port]"
97.Re
98the number of characters in the buffer
99of the given port but not yet written out to the file or device.
100The buffer is flushed
101automatically after the buffer (of 512 characters) is filled
102or when
103.i terpr
104is called.
105.Lf outfile "'s_filename"
106.Re
107a port or nil
108.Se
109this opens a port to write s_filename.
110The file opened is truncated by the outfile if it existed beforehand.
111If there are no free ports, outfile returns nil.
112.\".pg
113.Lf patom "'g_exp ['p_port]"
114.Re
115g_exp
116.Se
117g_exp is printed to the given port or the default port.
118If g_exp is a symbol then the print name is printed without
119any escape characters around special characters in the print name.
120If g_exp is a list then patom has the same effect as print.
121.Lf pntlen "'xfs_arg"
122.Wh
123xfs_arg is a fixnum, flonum or symbol.
124.Re
125the number of characters needed to print xfs_arg.
126.Lf portp "'g_arg"
127.Re
128t iff g_arg is a port.
129.Lf princ "'g_arg ['p_port]"
130.Eq
131patom.
132.Lf print "'g_arg ['p_port]"
133.Re
134nil
135.Se
136prints g_arg on the port p_port or the default port.
137.Im
138we must decide soon whether we will print a cr-lf too.
139.Lf ratom "['p_port]"
140.Re
141the next atom read from the given or default port.
142.Lf read "['p_port]"
143.Re
144the next lisp exppression read from the given or default port.
145.Lf readc "['p_port]"
146.Re
147the next character read from the given or default port.
148.Lf readlist "'l_arg"
149.Re
150the lisp expression read from the list of characters in l_arg.
151.Im
152check this one out.
153.\".pg
154.Lf resetio
155.Re
156nil
157.Se
158all ports except the standard intput, output and error
159are closed.
160.Lf setsyntax "'s_symbol 'x_code"
161.Re
162t
163.Se
164this sets the code for s_symbol to x_code in the current readtable.
165See the section on the reader, \(sc X.Y, for details.
166.Lf terpr "['p_port]"
167.Re
168nil
169.Se
170a terminate line character sequence
171is sent to the given port or the default port.
172This will also flush the buffer.
173.Lf terpri "['p_port]"
174.Eq
175terpr.
176.Lf tyi "['p_port]"
177.Re
178the fixnum representation of the next character read.
179.No
180Upon end of file, -1 is returned.
181If you are reading from a file then succeeding
182.b tyi 's
183will return -1 without waiting.
184.Lf tyipeek "['p_port]"
185.Re
186the fixnum representation of the next character to be read.
187.No
188This does not actually read the character, it just peeks at it.
189.Lf tyo "'x_char ['p_port]"
190.Re
191x_char.
192.Se
193the fixnum representation of a character, x_code, is printed as a
194character on the given output port or the default output port.
195.Lf zapline
196.Re
197nil
198.Se
199all characters up to and including the line termination character
200are read and discarded from the last port used
201for input.
202.No
203this is used as the macro function for the semicolon character when
204it acts as a comment character.