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