BSD 4_2 development
[unix-history] / usr / lisp / ch5.n
CommitLineData
450a98ab
C
1." $Header: ch5.n 1.3 83/07/23 12:40:05 layer Exp $
2.Lc Input/Output 5
3.pp
4The following functions are used to read from and write to external devices
5(e.g. files)
6and programs (through pipes).
7All I/O goes through the lisp data type called the port.
8A port may be open for either reading or writing, but usually not both
9simultaneously (see
10.i fileopen
11).
12There are only a limited number of ports (20) and they will not be reclaimed
13unless they are
14.i close d.
15All ports are reclaimed by a
16.i resetio
17call,
18but this drastic step won't be necessary if the program closes
19what it uses.
20.pp
21If a port argument is not supplied to a function which requires one
22or if a bad port argument (such as nil) is given,
23then
24.Fr
25will use the default port according to this scheme:
26If input is being done then the default port is the value
27of the symbol
28.b piport
29and if output is being done then the default port is the value
30of the symbol
31.b poport .
32Furthermore,
33if the value of piport or poport is not a valid port,
34then the standard input or standard output will be used, respectively.
35.pp
36The standard input and standard output are usually the keyboard and
37terminal display unless your job is running in the background and its
38input or output is connected to a pipe.
39All output which goes to the standard output will also go to the
40port
41.b ptport
42if it is a valid port.
43Output destined for the standard output will not reach
44the standard output if the symbol
45.b ^w
46is non nil (although it will still go to
47.b ptport
48if
49.b ptport
50is a valid port).
51.pp
52Some of the functions listed below reference files directly.
53.Fr
54has borrowed a convenient shorthand notation from
55.i /bin/csh ,
56concerning naming files.
57If a file name begins with ~ (tilde),
58and the symbol
59.b tilde-expansion
60
61is bound to something other than nil,
62then
63.Fr
64expands the file name.
65It takes the string of characters between the leading tilde, and
66the first slash as a user-name.
67Then, that initial segment of the filename is replaced by the home
68directory of the user. The null username is taken to be the current
69user.
70.pp
71Having gone to the effort of searching the password file,
72.Fr
73remembers the user directory, in case it gets asked to do so again.
74Tilde-expansion is performed in the following functions:
75\fIcfasl, chdir, fasl, ffasl, fileopen, infile, load, outfile,
76probef, sys:access, sys:unlink\fP.
77.Lf cfasl "'st_file 'st_entry 'st_funcname ['st_disc ['st_library]]"
78.Re
79t
80.Se
81This is used to load in a foreign function (see \(sc8.4).
82The object file st_file is loaded into the lisp system.
83St_entry should be an entry point in the file just loaded.
84The function binding of the symbol s_funcname will be set to point
85to st_entry, so that when the lisp function s_funcname is called,
86st_entry will be run.
87st_disc is the discipline to be given to s_funcname.
88st_disc defaults to "subroutine" if it is not given or if it is given as nil.
89If st_library is non-null, then after st_file is loaded, the libraries
90given in st_library will be searched to resolve external references.
91The form of st_library should be something like "-lS -lm".
92The C library (" -lc " ) is always searched so when loading in a C
93file you probably won't need to specify a library.
94For Fortran files, you should specify "-lF77" and if you are doing
95any I/O, the library entry should be "-lI77 -lF77".
96For Pascal files "-lpc" is required.
97.No
98This function may be used to load the output of the assembler, C compiler,
99Fortran compiler, and Pascal compiler but NOT the lisp compiler (use
100.i fasl
101for that).
102If a file has more than one entry point, then use
103.i getaddress
104to locate and setup other foreign functions.
105.br
106It is an error to load in a file which has a global entry point of the same
107name as a global entry point in the running lisp.
108As soon as you load in a file with
109.i cfasl ,
110its global entry points become part of the
111lisp's entry points.
112Thus you cannot
113.i cfasl
114in the same file twice unless you
115use
116.i removeaddress
117to change certain global entry points to local entry points.
118.Lf close "'p_port"
119.Re
120t
121.Se
122the specified port is drained and closed, releasing the port.
123.No
124The standard defaults are not used in this case since you probably never
125want to close the standard output or standard input.
126.Lf cprintf "'st_format 'xfst_val ['p_port]"
127.Re
128xfst_val
129.Se
130The UNIX formatted output function printf is called with arguments st_format
131and xfst_val.
132If xfst_val is a symbol then its print name is passed to printf.
133The format string may contain characters which are just printed literally
134and it may contain special formatting commands preceded by a percent
135sign.
136The complete set of formatting characters is described in the UNIX manual.
137Some useful ones are %d for printing a fixnum in decimal, %f or %e for printing
138a flonum, and %s for printing a character string (or print name of a symbol).
139.Ex
140\fI(cprintf "Pi equals %f" 3.14159)\fP prints `Pi equals 3.14159'
141.Lf drain "['p_port]"
142.Re
143nil
144.Se
145If this is an output port then
146the characters in the output buffer are all sent to the device.
147If this is an input port then all pending characters are flushed.
148The default port for this function is the default output port.
149.Lf ex "[s_filename]"
150.Lx vi "[s_filename]"
151.Lx exl "[s_filename]"
152.Lx vil "[s_filename]"
153.Re t
154.Se
155The lisp system starts up an editor on the file named as the argument.
156It will try appending .l to the file if it can't find it.
157The functions \fIexl\fP and \fIvil\fP will load the file after
158you finish editing it. These functions will also remember the name
159of the file so that on subsequent invocations, you don't need to
160provide the argument.
161.No
162These functions do not evaluate their argument.
163.Lf fasl "'st_name ['st_mapf ['g_warn]]"
164.Wh
165st_mapf and g_warn default to nil.
166.Re
167t if the function succeeded, nil otherwise.
168.Se
169this function is designed to load in an object file generated by
170the lisp compiler Liszt.
171File names for object files usually end in `.o', so
172.i fasl
173will append `.o' to st_name (if it is not already present).
174If st_mapf is non nil, then it is the name of the map file to
175create.
176.i Fasl
177writes in the map file the names and addresses of the functions
178it loads and defines.
179Normally the map file is created (i.e. truncated if it
180exists), but if \fI(sstatus\ appendmap\ t)\fP is done then the map file
181will be appended.
182If g_warn is non nil and if a function is loaded from the file which
183is already defined, then a warning message will be printed.
184.No
185.i fasl
186only looks in the current directory for the file to load.
187The function
188.i load
189looks through a user-supplied search path and will call
190.i fasl
191if it finds a file with the same root name and a `.o' extension.
192In most cases the user
193would be better off using the function
194.i load
195rather than calling
196.i fasl
197directly.
198.Lf ffasl "'st_file 'st_entry 'st_funcname ['st_discipline ['st_library]]"
199.Re
200the binary object created.
201.Se
202the Fortran object file st_file is loaded into the lisp system.
203St_entry should be an entry point in the file just loaded.
204A binary object will be created and its entry field will be set to point
205to st_entry.
206The discipline field of the binary will be set to st_discipline or
207"subroutine" by default.
208If st_library is present and non-null, then after st_file is loaded, the libraries
209given in st_library will be searched to resolve external references.
210The form of st_library should be something like "-lS -ltermcap".
211In any case, the standard Fortran libraries will be
212searched also to resolve external references.
213.No
214in F77 on Unix, the entry point for the fortran function foo
215is named `_foo_'.
216.Lf filepos "'p_port ['x_pos]"
217.Re
218the current position in the file if x_pos is not
219given or else x_pos if x_pos is given.
220.Se
221If x_pos is given, the next byte to be read or written to the
222port will be at
223position x_pos.
224.Lf filestat 'st_filename
225.Re
226a vector containing various numbers which the UNIX operating
227system assigns to files. if the file doesn't exist, an error is
228invoked. Use \fIprobef\fP to determine if the file exists.
229.No
230The individual entries can be accesed by mnemonic functions
231of the form filestat:\fIfield\fP, where field may be any of
232atime, ctime, dev, gid, ino, mode,mtime, nlink, rdev, size,
233type, uid. See the UNIX programmers manual for a more detailed
234description of these quantities.
235.Lf flatc "'g_form ['x_max]"
236.Re
237the number of characters required to print g_form using \fIpatom\fP.
238If x_max is given, and if \fIflatc\fP determines that it will return a value
239greater than x_max, then it gives up and returns the current value it
240has computed.
241This is useful if you just want to see if an expression is larger than
242a certain size.
243.Lf flatsize "'g_form ['x_max]"
244.Re
245the number of characters required to print g_form using \fIprint\fP.
246The meaning of x_max is the same as for flatc.
247.No
248Currently this just
249.i explode 's
250g_form and checks its length.
251.Lf fileopen "'st_filename 'st_mode"
252.Re
253a port for reading or writing (depending on st_mode) the file st_name.
254.Se
255the given file is opened (or created if opened for writing and it
256doesn't yet exist).
257.No
258this function call provides a direct
259interface to the operating system's fopen function.
260The mode may be more than just "r" for read, "w" for write or "a" for
261append. The modes "r+", "w+" and "a+" permit both reading and writing
262on a port provided that
263.i fseek
264is done between changes in direction.
265See the UNIX manual description of fopen for more details.
266This routine does not look through a search path for a given file.
267.Lf fseek "'p_port 'x_offset 'x_flag"
268.Re
269the position in the file after the function is performed.
270.Se
271this function positions the read/write pointer before a certain byte
272in the file.
273If x_flag is 0 then the pointer is set to x_offset bytes from the
274beginning of the file.
275If x_flag is 1 then the pointer is set to x_offset bytes from the
276current location in the file.
277If x_flag is 2 then the pointer is set to x_offset bytes from the
278end of the file.
279.Lf infile "'s_filename"
280.Re
281a port ready to read s_filename.
282.Se
283this tries to open s_filename and if it cannot or if there are no
284ports available it gives an error message.
285.No
286to allow your program to continue on a file-not-found error,
287you can use something like:
288.br
289\fI(cond ((null (setq myport (car (errset (infile name) nil))))
290.br
291\ \ \ \ \ \ \ \ \ \ \ \ (patom '"couldn't open the file")))\fP
292.br
293which will set myport to the port to read from if the file exists
294or will print a message if it couldn't open it and also set myport to nil.
295To simply determine if a file exists, there is a function named
296.i probef .
297.Lf load "'s_filename ['st_map ['g_warn]]"
298.Re
299t
300.No
301The function of
302.i load
303has changed since previous releases of
304.Fr
305and the following description should be read carefully.
306.Se
307.i load
308now serves the function of both
309.i fasl
310and the old
311.i load .
312.i Load
313will search a user defined search path for a lisp source or object file
314with the filename s_filename (with the extension .l or .o added as
315appropriate).
316The search path which
317.i load
318uses is the value of \fI(status\ load-search-path)\fP.
319The default is (|.|\ /usr/lib/lisp) which means look in the current
320directory first and then /usr/lib/lisp.
321The file which
322.i load
323looks for depends on the last two characters of s_filename.
324If s_filename ends with ".l" then
325.i load
326will only look for a file name
327s_filename and will assume that this is a
328.Fr
329source file.
330If s_filename ends with ".o" then
331.i load
332will only look for a file named s_filename and will assume that this is
333a
334.Fr
335object file to be
336.i fasl ed
337in.
338Otherwise,
339.i load
340will first look for s_filename.o, then s_filename.l and finally
341s_filename itself.
342If it finds s_filename.o it will assume that this is an object file,
343otherwise it will assume that it is a source file.
344An object file is loaded using
345.i fasl
346and a source file is loaded by reading and evaluating each form in the
347file.
348The optional arguments st_map and g_warn are passed to
349.i fasl
350should
351.i fasl
352be called.
353.No
354\fIload\fP requires a port to open the file s_filename.
355It then lambda binds the symbol piport to this port and reads and
356evaluates the forms.
357.Lf makereadtable "['s_flag]"
358.Wh
359if s_flag is not present it is assumed to be nil.
360.Re
361a readtable equal to the original readtable if s_flag is non-null, or else
362equal to the current readtable.
363See chapter 7 for a description of readtables and their uses.
364.Lf msg "[l_option ...] ['g_msg ...]"
365.No
366This function is intended for printing short messages.
367Any of the arguments or options
368presented can be used any number of times, in any
369order. The messages themselves (g_msg) are evaluated, and then
370they are transmitted to
371.i patom .
372Typically, they are strings, which evaluate to themselves.
373The options are interpreted specially:
374.Eb
375\fImsg Option Summary\fP
376
377\fI(P\ p_portname)\fP causes subsequent output to go to the port p_portname
378 (port should be opened previously)
379
380\fIB\fP print a single blank.
381
382\fI(B\ 'n_b)\fP\ \ evaluate n_b and print that many blanks.
383
384\fIN\fP print a single by calling \fIterpr\fP.
385
386\fI(N\ 'n_n)\fP\ \ evaluate n_n and transmit
387 that many newlines to the stream.
388
389\fID\fP \fIdrain\fP the current port.
390.Ee
391.Lf nwritn "['p_port]"
392.Re
393the number of characters in the buffer
394of the given port but not yet written out to the file or device.
395The buffer is flushed
396automatically when filled,
397or when
398.i terpr
399is called.
400.Lf outfile "'s_filename ['st_type]"
401.Re
402a port or nil
403.Se
404this opens a port to write s_filename.
405If st_type is given and if it is a symbol or string whose name
406begins with `a', then the file will be opened in append mode,
407that is the current contents will not be lost and the next data
408will be written at the end of the file.
409Otherwise,
410the file opened is truncated by \fIoutfile\fP if it existed beforehand.
411If there are no free ports, outfile returns nil.
412If one cannot write on s_filename, an error is signalled.
413.\".pg
414.Lf patom "'g_exp ['p_port]"
415.Re
416g_exp
417.Se
418g_exp is printed to the given port or the default port.
419If g_exp is a symbol or string, the print name is printed without
420any escape characters around special characters in the print name.
421If g_exp is a list then \fIpatom\fP has the same effect as \fIprint\fP.
422.Lf pntlen "'xfs_arg"
423.Re
424the number of characters needed to print xfs_arg.
425.Lf portp "'g_arg"
426.Re
427t iff g_arg is a port.
428.Lf pp "[l_option] s_name1 ..."
429.Re
430t
431.Se
432If s_name\fIi\fP has a function binding, it is pretty-printed,
433otherwise if s_name\fIi\fP has a value then that is pretty-printed.
434Normally the output of the pretty-printer goes to the standard
435output port poport.
436The options allow you to redirect it.
437.Eb
438\fIPP Option Summary\fP
439
440\fI(F\ s_filename)\fP direct future printing to s_filename
441
442\fI(P\ p_portname)\fP causes output to go to the port p_portname
443 (port should be opened previously)
444
445\fI(E\ g_expression)\fP evaluate g_expression and don't print
446.Ee
447.Lf princ "'g_arg ['p_port]"
448.Eq
449patom.
450.Lf print "'g_arg ['p_port]"
451.Re
452nil
453.Se
454prints g_arg on the port p_port or the default port.
455.Lf probef "'st_file"
456.Re
457t iff the file st_file exists.
458.No
459Just because it exists doesn't mean you can read it.
460.Lf pp-form "'g_form ['p_port]"
461.Re
462t
463.Se
464g_form is pretty-printed to the port p_port (or poport if
465p_port is not given).
466This is the function which \fIpp\fP uses.
467\fIpp-form\fP does not look for
468function definitions or values of variables, it just prints out the form
469it is given.
470.No
471This is useful as a top-level-printer, c.f.
472.i top-level
473in Chapter 6.
474.Lf ratom "['p_port ['g_eof]]"
475.Re
476the next atom read from the given or default port.
477On end of file, g_eof (default nil) is returned.
478.Lf read "['p_port ['g_eof]]"
479.Re
480the next lisp expression read from the given or default port.
481On end of file, g_eof (default nil) is returned.
482.No
483An error will occur if the reader is given an ill formed expression.
484The most common error is too many right parentheses (note that this is
485not considered an error in Maclisp).
486.Lf readc "['p_port ['g_eof]]"
487.Re
488the next character read from the given or default port.
489On end of file, g_eof (default nil) is returned.
490.Lf readlist "'l_arg"
491.Re
492the lisp expression read from the list of characters in l_arg.
493.Lf removeaddress "'s_name1 ['s_name2 ...]"
494.Re
495nil
496.Se
497the entries for the s_name\fIi\fP in the Lisp symbol table are removed.
498This is useful if you wish to
499.i cfasl
500or
501.i ffasl
502in a file twice, since it is illegal for a symbol in the file you
503are loading to already exist in the lisp symbol table.
504.Lf resetio
505.Re
506nil
507.Se
508all ports except the standard input, output and error
509are closed.
510.Lf setsyntax "'s_symbol 's_synclass ['ls_func]"
511.Re
512t
513.Se
514this sets the code for s_symbol to sx_code in the current readtable.
515If s_synclass is
516.i macro
517or
518.i splicing
519then ls_func is the associated function.
520See Chapter 7 on the reader for more details.
521.Lf sload "'s_file"
522.Se
523the file s_file (in the current directory) is opened for reading and
524each form is read, printed and evaluated.
525If the form is recognizable as a function definition, only its name
526will be printed, otherwise the whole form is printed.
527.No
528This function is useful when a file refuses to load because
529of a syntax error and you would like to narrow down
530where the error is.
531.Lf tab "'x_col ['p_port]"
532.Se
533enough spaces are printed to put the cursor on column x_col.
534If the cursor is beyond x_col to start with, a
535.i terpr
536is done first.
537.Lf terpr "['p_port]"
538.Re
539nil
540.Se
541a terminate line character sequence
542is sent to the given port or the default port.
543This will also drain the port.
544.Lf terpri "['p_port]"
545.Eq
546terpr.
547.Lf tilde-expand 'st_filename
548.Re
549a symbol whose pname is the tilde-expansion of the argument,
550(as discussed at the beginning of this chapter).
551If the argument does not begin with a tilde, the argument itself is
552returned.
553.Lf tyi "['p_port]"
554.Re
555the fixnum representation of the next character read.
556On end of file, -1 is returned.
557.Lf tyipeek "['p_port]"
558.Re
559the fixnum representation of the next character to be read.
560.No
561This does not actually read the character, it just peeks at it.
562.Lf tyo "'x_char ['p_port]"
563.Re
564x_char.
565.Se
566the character whose fixnum representation is
567x_code, is printed as a
568on the given output port or the default output port.
569.Lf untyi "'x_char ['p_port]"
570.Se
571x_char is put back in the input buffer so a subsequent
572.i tyi
573or
574.i read
575will read it first.
576.No
577a maximum of one character may be put back.
578.Lf username-to-dir 'st_name
579.Re
580the home directory of the given user.
581The result is stored, to avoid unnecessarily searching the
582password file.
583.Lf zapline
584.Re
585nil
586.Se
587all characters up to and including the line termination character
588are read and discarded from the last port used
589for input.
590.No
591this is used as the macro function for the semicolon character when
592it acts as a comment character.