Commit | Line | Data |
---|---|---|
1e598865 | 1 | /* Copyright (C) 1995,1996,1997 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
0f2d19dd JB |
41 | \f |
42 | #include <stdio.h> | |
43 | #include "_scm.h" | |
20e6290e JB |
44 | #include "genio.h" |
45 | #include "chars.h" | |
0f2d19dd | 46 | |
20e6290e | 47 | #include "markers.h" |
5d09ff4e | 48 | #include "filesys.h" |
20e6290e JB |
49 | #include "fports.h" |
50 | #include "strports.h" | |
51 | #include "vports.h" | |
89ea5b7c | 52 | #include "kw.h" |
20e6290e JB |
53 | |
54 | #include "ports.h" | |
0f2d19dd JB |
55 | |
56 | #ifdef HAVE_MALLOC_H | |
95b88819 | 57 | #include <malloc.h> |
0f2d19dd JB |
58 | #endif |
59 | ||
60 | #ifdef HAVE_UNISTD_H | |
61 | #include <unistd.h> | |
62 | #endif | |
63 | ||
95b88819 GH |
64 | #ifdef HAVE_SYS_IOCTL_H |
65 | #include <sys/ioctl.h> | |
66 | #endif | |
0f2d19dd JB |
67 | \f |
68 | ||
69 | ||
70 | /* scm_ptobs scm_numptob | |
71 | * implement a dynamicly resized array of ptob records. | |
72 | * Indexes into this table are used when generating type | |
73 | * tags for smobjects (if you know a tag you can get an index and conversely). | |
74 | */ | |
75 | scm_ptobfuns *scm_ptobs; | |
76 | scm_sizet scm_numptob; | |
77 | ||
1cc91f1b | 78 | |
0f2d19dd JB |
79 | SCM |
80 | scm_markstream (ptr) | |
81 | SCM ptr; | |
0f2d19dd JB |
82 | { |
83 | int openp; | |
84 | if (SCM_GC8MARKP (ptr)) | |
85 | return SCM_BOOL_F; | |
86 | openp = SCM_CAR (ptr) & SCM_OPN; | |
87 | SCM_SETGC8MARK (ptr); | |
88 | if (openp) | |
89 | return SCM_STREAM (ptr); | |
90 | else | |
91 | return SCM_BOOL_F; | |
92 | } | |
93 | ||
94 | ||
1cc91f1b | 95 | |
0f2d19dd JB |
96 | long |
97 | scm_newptob (ptob) | |
98 | scm_ptobfuns *ptob; | |
0f2d19dd JB |
99 | { |
100 | char *tmp; | |
101 | if (255 <= scm_numptob) | |
102 | goto ptoberr; | |
103 | SCM_DEFER_INTS; | |
104 | SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs, (1 + scm_numptob) * sizeof (scm_ptobfuns))); | |
105 | if (tmp) | |
106 | { | |
107 | scm_ptobs = (scm_ptobfuns *) tmp; | |
108 | scm_ptobs[scm_numptob].mark = ptob->mark; | |
109 | scm_ptobs[scm_numptob].free = ptob->free; | |
110 | scm_ptobs[scm_numptob].print = ptob->print; | |
111 | scm_ptobs[scm_numptob].equalp = ptob->equalp; | |
112 | scm_ptobs[scm_numptob].fputc = ptob->fputc; | |
113 | scm_ptobs[scm_numptob].fputs = ptob->fputs; | |
114 | scm_ptobs[scm_numptob].fwrite = ptob->fwrite; | |
115 | scm_ptobs[scm_numptob].fflush = ptob->fflush; | |
116 | scm_ptobs[scm_numptob].fgetc = ptob->fgetc; | |
117 | scm_ptobs[scm_numptob].fclose = ptob->fclose; | |
118 | scm_numptob++; | |
119 | } | |
120 | SCM_ALLOW_INTS; | |
121 | if (!tmp) | |
122 | ptoberr:scm_wta (SCM_MAKINUM ((long) scm_numptob), (char *) SCM_NALLOC, "newptob"); | |
123 | return scm_tc7_port + (scm_numptob - 1) * 256; | |
124 | } | |
125 | ||
126 | \f | |
127 | /* internal SCM call */ | |
1cc91f1b | 128 | |
0f2d19dd JB |
129 | void |
130 | scm_fflush (port) | |
131 | SCM port; | |
0f2d19dd JB |
132 | { |
133 | scm_sizet i = SCM_PTOBNUM (port); | |
134 | (scm_ptobs[i].fflush) (SCM_STREAM (port)); | |
135 | } | |
136 | ||
137 | \f | |
138 | ||
44493941 | 139 | SCM_PROC(s_char_ready_p, "char-ready?", 0, 1, 0, scm_char_ready_p); |
1cc91f1b | 140 | |
0f2d19dd JB |
141 | SCM |
142 | scm_char_ready_p (port) | |
143 | SCM port; | |
0f2d19dd JB |
144 | { |
145 | if (SCM_UNBNDP (port)) | |
146 | port = scm_cur_inp; | |
147 | else | |
148 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_char_ready_p); | |
149 | if (SCM_CRDYP (port) || !SCM_FPORTP (port)) | |
150 | return SCM_BOOL_T; | |
5d09ff4e MD |
151 | return (scm_input_waiting_p ((FILE *) SCM_STREAM (port), s_char_ready_p) |
152 | ? SCM_BOOL_T | |
153 | : SCM_BOOL_F); | |
0f2d19dd | 154 | } |
0f2d19dd JB |
155 | |
156 | ||
157 | \f | |
158 | ||
159 | ||
160 | /* {Standard Ports} | |
161 | */ | |
162 | SCM_PROC(s_current_input_port, "current-input-port", 0, 0, 0, scm_current_input_port); | |
1cc91f1b | 163 | |
0f2d19dd JB |
164 | SCM |
165 | scm_current_input_port () | |
0f2d19dd JB |
166 | { |
167 | return scm_cur_inp; | |
168 | } | |
169 | ||
170 | SCM_PROC(s_current_output_port, "current-output-port", 0, 0, 0, scm_current_output_port); | |
1cc91f1b | 171 | |
0f2d19dd JB |
172 | SCM |
173 | scm_current_output_port () | |
0f2d19dd JB |
174 | { |
175 | return scm_cur_outp; | |
176 | } | |
177 | ||
178 | SCM_PROC(s_current_error_port, "current-error-port", 0, 0, 0, scm_current_error_port); | |
1cc91f1b | 179 | |
0f2d19dd JB |
180 | SCM |
181 | scm_current_error_port () | |
0f2d19dd JB |
182 | { |
183 | return scm_cur_errp; | |
184 | } | |
185 | ||
186 | SCM_PROC(s_set_current_input_port, "set-current-input-port", 1, 0, 0, scm_set_current_input_port); | |
1cc91f1b | 187 | |
0f2d19dd JB |
188 | SCM |
189 | scm_set_current_input_port (port) | |
190 | SCM port; | |
0f2d19dd JB |
191 | { |
192 | SCM oinp = scm_cur_inp; | |
193 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_set_current_input_port); | |
194 | scm_cur_inp = port; | |
195 | return oinp; | |
196 | } | |
197 | ||
198 | ||
199 | SCM_PROC(s_set_current_output_port, "set-current-output-port", 1, 0, 0, scm_set_current_output_port); | |
1cc91f1b | 200 | |
0f2d19dd JB |
201 | SCM |
202 | scm_set_current_output_port (port) | |
203 | SCM port; | |
0f2d19dd JB |
204 | { |
205 | SCM ooutp = scm_cur_outp; | |
206 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_set_current_output_port); | |
207 | scm_cur_outp = port; | |
208 | return ooutp; | |
209 | } | |
210 | ||
211 | ||
212 | SCM_PROC(s_set_current_error_port, "set-current-error-port", 1, 0, 0, scm_set_current_error_port); | |
1cc91f1b | 213 | |
0f2d19dd JB |
214 | SCM |
215 | scm_set_current_error_port (port) | |
216 | SCM port; | |
0f2d19dd JB |
217 | { |
218 | SCM oerrp = scm_cur_errp; | |
219 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_set_current_error_port); | |
220 | scm_cur_errp = port; | |
221 | return oerrp; | |
222 | } | |
223 | ||
224 | \f | |
225 | ||
226 | /* {Ports - in general} | |
227 | * | |
228 | */ | |
229 | ||
230 | /* Array of open ports, required for reliable MOVE->FDES etc. */ | |
231 | struct scm_port_table **scm_port_table; | |
232 | ||
233 | int scm_port_table_size = 0; /* Number of ports in scm_port_table. */ | |
234 | int scm_port_table_room = 20; /* Size of the array. */ | |
235 | ||
236 | /* Add a port to the table. Call with SCM_DEFER_INTS active. */ | |
1cc91f1b | 237 | |
0f2d19dd JB |
238 | struct scm_port_table * |
239 | scm_add_to_port_table (port) | |
240 | SCM port; | |
0f2d19dd JB |
241 | { |
242 | if (scm_port_table_size == scm_port_table_room) | |
243 | { | |
244 | scm_port_table = ((struct scm_port_table **) | |
245 | realloc ((char *) scm_port_table, | |
246 | (long) (sizeof (struct scm_port_table) | |
247 | * scm_port_table_room * 2))); | |
248 | /* !!! error checking */ | |
249 | scm_port_table_room *= 2; | |
250 | } | |
251 | scm_port_table[scm_port_table_size] = ((struct scm_port_table *) | |
252 | scm_must_malloc (sizeof (struct scm_port_table), | |
253 | "system port table")); | |
254 | scm_port_table[scm_port_table_size]->port = port; | |
255 | scm_port_table[scm_port_table_size]->revealed = 0; | |
256 | scm_port_table[scm_port_table_size]->stream = 0; | |
ebf7394e | 257 | scm_port_table[scm_port_table_size]->file_name = SCM_BOOL_F; |
0f2d19dd JB |
258 | scm_port_table[scm_port_table_size]->line_number = 1; |
259 | scm_port_table[scm_port_table_size]->column_number = 0; | |
260 | scm_port_table[scm_port_table_size]->representation = scm_regular_port; | |
261 | return scm_port_table[scm_port_table_size++]; | |
262 | } | |
263 | ||
264 | /* Remove a port from the table. Call with SCM_DEFER_INTS active. */ | |
1cc91f1b | 265 | |
0f2d19dd JB |
266 | void |
267 | scm_remove_from_port_table (port) | |
268 | SCM port; | |
0f2d19dd JB |
269 | { |
270 | int i = 0; | |
271 | while (scm_port_table[i]->port != port) | |
272 | { | |
273 | i++; | |
274 | /* Error if not found: too violent? May occur in GC. */ | |
275 | if (i >= scm_port_table_size) | |
276 | scm_wta (port, "Port not in table", "scm_remove_from_port_table"); | |
277 | } | |
278 | scm_must_free ((char *)scm_port_table[i]); | |
279 | scm_mallocated -= sizeof (*scm_port_table[i]); | |
280 | scm_port_table[i] = scm_port_table[scm_port_table_size - 1]; | |
281 | SCM_SETPTAB_ENTRY (port, 0); | |
282 | scm_port_table_size--; | |
283 | } | |
284 | ||
fea6b4ea | 285 | #ifdef GUILE_DEBUG |
0f2d19dd JB |
286 | /* Undocumented functions for debugging. */ |
287 | /* Return the number of ports in the table. */ | |
1cc91f1b | 288 | |
1146b6cd | 289 | SCM_PROC(s_pt_size, "pt-size", 0, 0, 0, scm_pt_size); |
0f2d19dd JB |
290 | SCM |
291 | scm_pt_size () | |
0f2d19dd JB |
292 | { |
293 | return SCM_MAKINUM (scm_port_table_size); | |
294 | } | |
295 | ||
296 | /* Return the ith member of the port table. */ | |
1146b6cd | 297 | SCM_PROC(s_pt_member, "pt-member", 1, 0, 0, scm_pt_member); |
0f2d19dd JB |
298 | SCM |
299 | scm_pt_member (member) | |
300 | SCM member; | |
0f2d19dd JB |
301 | { |
302 | int i; | |
303 | SCM_ASSERT (SCM_INUMP (member), member, SCM_ARG1, s_pt_member); | |
304 | i = SCM_INUM (member); | |
305 | if (i < 0 || i >= scm_port_table_size) | |
306 | return SCM_BOOL_F; | |
307 | else | |
308 | return scm_port_table[i]->port; | |
309 | } | |
310 | #endif | |
311 | ||
312 | ||
8b13c6b3 GH |
313 | /* Find a port in the table and return its revealed count. |
314 | Also used by the garbage collector. | |
0f2d19dd | 315 | */ |
1cc91f1b | 316 | |
0f2d19dd JB |
317 | int |
318 | scm_revealed_count (port) | |
319 | SCM port; | |
0f2d19dd JB |
320 | { |
321 | return SCM_REVEALED(port); | |
322 | } | |
323 | ||
324 | ||
325 | ||
326 | /* Return the revealed count for a port. */ | |
327 | ||
328 | SCM_PROC(s_port_revealed, "port-revealed", 1, 0, 0, scm_port_revealed); | |
1cc91f1b | 329 | |
0f2d19dd JB |
330 | SCM |
331 | scm_port_revealed (port) | |
332 | SCM port; | |
0f2d19dd | 333 | { |
0f2d19dd | 334 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_port_revealed); |
8b13c6b3 | 335 | return SCM_MAKINUM (scm_revealed_count (port)); |
0f2d19dd JB |
336 | } |
337 | ||
338 | /* Set the revealed count for a port. */ | |
339 | SCM_PROC(s_set_port_revealed_x, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x); | |
1cc91f1b | 340 | |
0f2d19dd JB |
341 | SCM |
342 | scm_set_port_revealed_x (port, rcount) | |
343 | SCM port; | |
344 | SCM rcount; | |
0f2d19dd JB |
345 | { |
346 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_set_port_revealed_x); | |
347 | SCM_ASSERT (SCM_INUMP (rcount), rcount, SCM_ARG2, s_set_port_revealed_x); | |
348 | SCM_DEFER_INTS; | |
349 | SCM_REVEALED (port) = SCM_INUM (rcount); | |
350 | SCM_ALLOW_INTS; | |
8b13c6b3 | 351 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
352 | } |
353 | ||
354 | /* scm_close_port | |
355 | * Call the close operation on a port object. | |
356 | */ | |
357 | SCM_PROC(s_close_port, "close-port", 1, 0, 0, scm_close_port); | |
1cc91f1b | 358 | |
0f2d19dd JB |
359 | SCM |
360 | scm_close_port (port) | |
361 | SCM port; | |
0f2d19dd JB |
362 | { |
363 | scm_sizet i; | |
364 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_close_port); | |
365 | if (SCM_CLOSEDP (port)) | |
366 | return SCM_UNSPECIFIED; | |
367 | i = SCM_PTOBNUM (port); | |
368 | SCM_DEFER_INTS; | |
369 | if (scm_ptobs[i].fclose) | |
370 | SCM_SYSCALL ((scm_ptobs[i].fclose) (SCM_STREAM (port))); | |
371 | scm_remove_from_port_table (port); | |
898a256f | 372 | SCM_SETAND_CAR (port, ~SCM_OPN); |
0f2d19dd JB |
373 | SCM_ALLOW_INTS; |
374 | return SCM_UNSPECIFIED; | |
375 | } | |
376 | ||
377 | SCM_PROC(s_close_all_ports_except, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except); | |
1cc91f1b | 378 | |
0f2d19dd JB |
379 | SCM |
380 | scm_close_all_ports_except (ports) | |
381 | SCM ports; | |
0f2d19dd JB |
382 | { |
383 | int i = 0; | |
384 | SCM_ASSERT (SCM_NIMP (ports) && SCM_CONSP (ports), ports, SCM_ARG1, s_close_all_ports_except); | |
385 | SCM_DEFER_INTS; | |
386 | while (i < scm_port_table_size) | |
387 | { | |
388 | SCM thisport = scm_port_table[i]->port; | |
389 | int found = 0; | |
390 | SCM ports_ptr = ports; | |
391 | ||
392 | while (SCM_NNULLP (ports_ptr)) | |
393 | { | |
394 | SCM port = SCM_CAR (ports_ptr); | |
395 | if (i == 0) | |
396 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPPORTP (port), port, SCM_ARG1, s_close_all_ports_except); | |
397 | if (port == thisport) | |
398 | found = 1; | |
399 | ports_ptr = SCM_CDR (ports_ptr); | |
400 | } | |
401 | if (found) | |
402 | i++; | |
403 | else | |
404 | /* i is not to be incremented here. */ | |
405 | scm_close_port (thisport); | |
406 | } | |
407 | SCM_ALLOW_INTS; | |
408 | return SCM_UNSPECIFIED; | |
409 | } | |
410 | ||
411 | SCM_PROC(s_input_port_p, "input-port?", 1, 0, 0, scm_input_port_p); | |
1cc91f1b | 412 | |
0f2d19dd JB |
413 | SCM |
414 | scm_input_port_p (x) | |
415 | SCM x; | |
0f2d19dd JB |
416 | { |
417 | if (SCM_IMP (x)) | |
418 | return SCM_BOOL_F; | |
419 | return SCM_INPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F; | |
420 | } | |
421 | ||
422 | SCM_PROC(s_output_port_p, "output-port?", 1, 0, 0, scm_output_port_p); | |
1cc91f1b | 423 | |
0f2d19dd JB |
424 | SCM |
425 | scm_output_port_p (x) | |
426 | SCM x; | |
0f2d19dd JB |
427 | { |
428 | if (SCM_IMP (x)) | |
429 | return SCM_BOOL_F; | |
430 | return SCM_OUTPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F; | |
431 | } | |
432 | ||
433 | ||
434 | SCM_PROC(s_eof_object_p, "eof-object?", 1, 0, 0, scm_eof_object_p); | |
1cc91f1b | 435 | |
0f2d19dd JB |
436 | SCM |
437 | scm_eof_object_p (x) | |
438 | SCM x; | |
0f2d19dd JB |
439 | { |
440 | return (SCM_EOF_VAL == x) ? SCM_BOOL_T : SCM_BOOL_F; | |
441 | } | |
442 | ||
443 | SCM_PROC(s_force_output, "force-output", 0, 1, 0, scm_force_output); | |
1cc91f1b | 444 | |
0f2d19dd JB |
445 | SCM |
446 | scm_force_output (port) | |
447 | SCM port; | |
0f2d19dd JB |
448 | { |
449 | if (SCM_UNBNDP (port)) | |
450 | port = scm_cur_outp; | |
451 | else | |
452 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_force_output); | |
453 | { | |
454 | scm_sizet i = SCM_PTOBNUM (port); | |
455 | SCM_SYSCALL ((scm_ptobs[i].fflush) (SCM_STREAM (port))); | |
456 | return SCM_UNSPECIFIED; | |
457 | } | |
458 | } | |
459 | ||
89ea5b7c GH |
460 | SCM_PROC (s_flush_all_ports, "flush-all-ports", 0, 1, 0, scm_flush_all_ports); |
461 | SCM | |
462 | scm_flush_all_ports (void) | |
463 | { | |
464 | int i; | |
465 | ||
466 | for (i = 0; i < scm_port_table_size; i++) | |
467 | { | |
468 | SCM port = scm_port_table[i]->port; | |
469 | if (SCM_OPOUTPORTP (port)) | |
470 | { | |
471 | scm_sizet ptob = SCM_PTOBNUM (port); | |
472 | (scm_ptobs[ptob].fflush) (SCM_STREAM (port)); | |
473 | } | |
474 | } | |
475 | return SCM_UNSPECIFIED; | |
476 | } | |
0f2d19dd JB |
477 | |
478 | SCM_PROC(s_read_char, "read-char", 0, 1, 0, scm_read_char); | |
1cc91f1b | 479 | |
0f2d19dd JB |
480 | SCM |
481 | scm_read_char (port) | |
482 | SCM port; | |
0f2d19dd JB |
483 | { |
484 | int c; | |
485 | if (SCM_UNBNDP (port)) | |
334341aa | 486 | port = scm_cur_inp; |
0f2d19dd JB |
487 | else |
488 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_read_char); | |
489 | c = scm_gen_getc (port); | |
490 | if (EOF == c) | |
491 | return SCM_EOF_VAL; | |
492 | return SCM_MAKICHR (c); | |
493 | } | |
494 | ||
495 | ||
496 | SCM_PROC(s_peek_char, "peek-char", 0, 1, 0, scm_peek_char); | |
1cc91f1b | 497 | |
0f2d19dd JB |
498 | SCM |
499 | scm_peek_char (port) | |
500 | SCM port; | |
0f2d19dd JB |
501 | { |
502 | int c; | |
503 | if (SCM_UNBNDP (port)) | |
504 | port = scm_cur_inp; | |
505 | else | |
506 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_peek_char); | |
507 | c = scm_gen_getc (port); | |
508 | if (EOF == c) | |
509 | return SCM_EOF_VAL; | |
510 | scm_gen_ungetc (c, port); | |
511 | return SCM_MAKICHR (c); | |
512 | } | |
513 | ||
514 | SCM_PROC (s_unread_char, "unread-char", 2, 0, 0, scm_unread_char); | |
1cc91f1b | 515 | |
0f2d19dd JB |
516 | SCM |
517 | scm_unread_char (cobj, port) | |
518 | SCM cobj; | |
519 | SCM port; | |
0f2d19dd JB |
520 | { |
521 | int c; | |
522 | ||
523 | SCM_ASSERT (SCM_ICHRP (cobj), cobj, SCM_ARG1, s_unread_char); | |
524 | ||
525 | if (SCM_UNBNDP (port)) | |
526 | port = scm_cur_inp; | |
527 | else | |
528 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG2, s_unread_char); | |
529 | ||
530 | ||
531 | c = SCM_ICHR (cobj); | |
532 | ||
533 | scm_gen_ungetc (c, port); | |
534 | return cobj; | |
535 | } | |
536 | ||
537 | ||
538 | ||
d14af9f2 | 539 | SCM_PROC (s_port_line, "port-line", 0, 1, 0, scm_port_line); |
1cc91f1b | 540 | |
0f2d19dd | 541 | SCM |
d14af9f2 | 542 | scm_port_line (port) |
0f2d19dd | 543 | SCM port; |
0f2d19dd JB |
544 | { |
545 | SCM p; | |
546 | p = ((port == SCM_UNDEFINED) | |
547 | ? scm_cur_inp | |
548 | : port); | |
549 | if (!(SCM_NIMP (p) && SCM_PORTP (p))) | |
550 | return SCM_BOOL_F; | |
551 | else | |
552 | return SCM_MAKINUM (SCM_LINUM (p)); | |
553 | } | |
554 | ||
d043d8c2 MD |
555 | SCM_PROC (s_set_port_line_x, "set-port-line!", 1, 1, 0, scm_set_port_line_x); |
556 | ||
557 | SCM | |
558 | scm_set_port_line_x (port, line) | |
559 | SCM port; | |
560 | SCM line; | |
561 | { | |
562 | if (line == SCM_UNDEFINED) | |
563 | { | |
564 | line = port; | |
565 | port = scm_cur_inp; | |
566 | } | |
567 | else | |
568 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), | |
569 | port, | |
570 | SCM_ARG1, | |
571 | s_set_port_line_x); | |
572 | return SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line); | |
573 | } | |
574 | ||
d14af9f2 | 575 | SCM_PROC (s_port_column, "port-column", 0, 1, 0, scm_port_column); |
1cc91f1b | 576 | |
0f2d19dd | 577 | SCM |
d14af9f2 | 578 | scm_port_column (port) |
0f2d19dd | 579 | SCM port; |
0f2d19dd JB |
580 | { |
581 | SCM p; | |
582 | p = ((port == SCM_UNDEFINED) | |
583 | ? scm_cur_inp | |
584 | : port); | |
585 | if (!(SCM_NIMP (p) && SCM_PORTP (p))) | |
586 | return SCM_BOOL_F; | |
587 | else | |
588 | return SCM_MAKINUM (SCM_COL (p)); | |
589 | } | |
590 | ||
d043d8c2 MD |
591 | SCM_PROC (s_set_port_column_x, "set-port-column!", 1, 1, 0, scm_set_port_column_x); |
592 | ||
593 | SCM | |
594 | scm_set_port_column_x (port, column) | |
595 | SCM port; | |
596 | SCM column; | |
597 | { | |
598 | if (column == SCM_UNDEFINED) | |
599 | { | |
600 | column = port; | |
601 | port = scm_cur_inp; | |
602 | } | |
603 | else | |
604 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), | |
605 | port, | |
606 | SCM_ARG1, | |
607 | s_set_port_column_x); | |
608 | return SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column); | |
609 | } | |
610 | ||
d14af9f2 | 611 | SCM_PROC (s_port_filename, "port-filename", 0, 1, 0, scm_port_filename); |
1cc91f1b | 612 | |
0f2d19dd | 613 | SCM |
d14af9f2 | 614 | scm_port_filename (port) |
0f2d19dd | 615 | SCM port; |
0f2d19dd JB |
616 | { |
617 | SCM p; | |
618 | p = ((port == SCM_UNDEFINED) | |
619 | ? scm_cur_inp | |
620 | : port); | |
621 | if (!(SCM_NIMP (p) && SCM_PORTP (p))) | |
622 | return SCM_BOOL_F; | |
623 | else | |
624 | return SCM_PTAB_ENTRY (p)->file_name; | |
625 | } | |
626 | ||
d14af9f2 | 627 | SCM_PROC (s_set_port_filename_x, "set-port-filename!", 1, 1, 0, scm_set_port_filename_x); |
1cc91f1b | 628 | |
d14af9f2 MD |
629 | SCM |
630 | scm_set_port_filename_x (port, filename) | |
631 | SCM port; | |
632 | SCM filename; | |
d14af9f2 MD |
633 | { |
634 | if (filename == SCM_UNDEFINED) | |
635 | { | |
636 | filename = port; | |
637 | port = scm_cur_inp; | |
638 | } | |
639 | else | |
640 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), | |
641 | port, | |
642 | SCM_ARG1, | |
643 | s_set_port_filename_x); | |
644 | return SCM_PTAB_ENTRY (port)->file_name = filename; | |
645 | } | |
646 | ||
0f2d19dd JB |
647 | #ifndef ttyname |
648 | extern char * ttyname(); | |
649 | #endif | |
650 | ||
1cc91f1b | 651 | |
0f2d19dd JB |
652 | void |
653 | scm_prinport (exp, port, type) | |
654 | SCM exp; | |
655 | SCM port; | |
656 | char *type; | |
0f2d19dd JB |
657 | { |
658 | scm_gen_puts (scm_regular_string, "#<", port); | |
659 | if (SCM_CLOSEDP (exp)) | |
660 | scm_gen_puts (scm_regular_string, "closed: ", port); | |
661 | else | |
662 | { | |
663 | if (SCM_RDNG & SCM_CAR (exp)) | |
664 | scm_gen_puts (scm_regular_string, "input: ", port); | |
665 | if (SCM_WRTNG & SCM_CAR (exp)) | |
666 | scm_gen_puts (scm_regular_string, "output: ", port); | |
667 | } | |
668 | scm_gen_puts (scm_regular_string, type, port); | |
669 | scm_gen_putc (' ', port); | |
670 | #ifndef MSDOS | |
671 | #ifndef __EMX__ | |
672 | #ifndef _DCC | |
673 | #ifndef AMIGA | |
674 | #ifndef THINK_C | |
675 | if (SCM_OPENP (exp) && scm_tc16_fport == SCM_TYP16 (exp) && isatty (fileno ((FILE *)SCM_STREAM (exp)))) | |
676 | scm_gen_puts (scm_regular_string, ttyname (fileno ((FILE *)SCM_STREAM (exp))), port); | |
677 | else | |
678 | #endif | |
679 | #endif | |
680 | #endif | |
681 | #endif | |
682 | #endif | |
683 | if (SCM_OPFPORTP (exp)) | |
684 | scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp)), 10, port); | |
685 | else | |
686 | scm_intprint (SCM_CDR (exp), 16, port); | |
687 | scm_gen_putc ('>', port); | |
688 | } | |
689 | ||
1cc91f1b | 690 | |
0f2d19dd JB |
691 | void |
692 | scm_ports_prehistory () | |
0f2d19dd JB |
693 | { |
694 | scm_numptob = 0; | |
695 | scm_ptobs = (scm_ptobfuns *) malloc (sizeof (scm_ptobfuns)); | |
696 | ||
697 | /* WARNING: These scm_newptob calls must be done in this order. | |
698 | * They must agree with the port declarations in tags.h. | |
699 | */ | |
700 | /* scm_tc16_fport = */ scm_newptob (&scm_fptob); | |
701 | /* scm_tc16_pipe = */ scm_newptob (&scm_pipob); | |
702 | /* scm_tc16_strport = */ scm_newptob (&scm_stptob); | |
703 | /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob); | |
704 | } | |
705 | \f | |
706 | ||
707 | \f | |
708 | /* {Void Ports} | |
709 | */ | |
710 | ||
711 | int scm_tc16_void_port = 0; | |
712 | ||
713 | static int | |
38cb0e9c | 714 | print_void_port (SCM exp, SCM port, scm_print_state *pstate) |
0f2d19dd JB |
715 | { |
716 | scm_prinport (exp, port, "void"); | |
717 | return 1; | |
718 | } | |
719 | ||
720 | static int | |
38cb0e9c | 721 | putc_void_port (int c, SCM strm) |
0f2d19dd JB |
722 | { |
723 | return 0; /* vestigial return value */ | |
724 | } | |
725 | ||
726 | static int | |
38cb0e9c | 727 | puts_void_port (char *s, SCM strm) |
0f2d19dd JB |
728 | { |
729 | return 0; /* vestigial return value */ | |
730 | } | |
731 | ||
732 | static scm_sizet | |
38cb0e9c | 733 | write_void_port (char *ptr, scm_sizet size, scm_sizet nitems, SCM strm) |
0f2d19dd JB |
734 | { |
735 | int len; | |
736 | len = size * nitems; | |
737 | return len; | |
738 | } | |
739 | ||
1cc91f1b | 740 | |
0f2d19dd | 741 | static int |
38cb0e9c | 742 | flush_void_port (SCM strm) |
0f2d19dd JB |
743 | { |
744 | return 0; | |
745 | } | |
746 | ||
1cc91f1b | 747 | |
0f2d19dd | 748 | static int |
38cb0e9c | 749 | getc_void_port (SCM strm) |
0f2d19dd JB |
750 | { |
751 | return EOF; | |
752 | } | |
753 | ||
1cc91f1b | 754 | |
0f2d19dd | 755 | static int |
38cb0e9c | 756 | close_void_port (SCM strm) |
0f2d19dd JB |
757 | { |
758 | return 0; /* this is ignored by scm_close_port. */ | |
759 | } | |
760 | ||
761 | ||
1cc91f1b | 762 | |
0f2d19dd | 763 | static int |
38cb0e9c | 764 | noop0 (SCM stream) |
0f2d19dd JB |
765 | { |
766 | return 0; | |
767 | } | |
768 | ||
769 | ||
770 | static struct scm_ptobfuns void_port_ptob = | |
771 | { | |
772 | scm_mark0, | |
773 | noop0, | |
774 | print_void_port, | |
775 | 0, /* equal? */ | |
776 | putc_void_port, | |
777 | puts_void_port, | |
778 | write_void_port, | |
779 | flush_void_port, | |
780 | getc_void_port, | |
781 | close_void_port, | |
782 | }; | |
783 | ||
784 | \f | |
785 | ||
1cc91f1b | 786 | |
0f2d19dd JB |
787 | SCM |
788 | scm_void_port (mode_str) | |
789 | char * mode_str; | |
0f2d19dd JB |
790 | { |
791 | int mode_bits; | |
792 | SCM answer; | |
793 | struct scm_port_table * pt; | |
794 | ||
795 | SCM_NEWCELL (answer); | |
796 | SCM_DEFER_INTS; | |
797 | mode_bits = scm_mode_bits (mode_str); | |
798 | pt = scm_add_to_port_table (answer); | |
898a256f | 799 | SCM_SETCAR (answer, scm_tc16_void_port | mode_bits); |
0f2d19dd JB |
800 | SCM_SETPTAB_ENTRY (answer, pt); |
801 | SCM_SETSTREAM (answer, SCM_BOOL_F); | |
802 | SCM_ALLOW_INTS; | |
803 | return answer; | |
804 | } | |
805 | ||
806 | ||
807 | SCM_PROC (s_sys_make_void_port, "%make-void-port", 1, 0, 0, scm_sys_make_void_port); | |
1cc91f1b | 808 | |
0f2d19dd JB |
809 | SCM |
810 | scm_sys_make_void_port (mode) | |
811 | SCM mode; | |
0f2d19dd | 812 | { |
89958ad0 | 813 | SCM_ASSERT (SCM_NIMP (mode) && SCM_ROSTRINGP (mode), mode, |
0f2d19dd JB |
814 | SCM_ARG1, s_sys_make_void_port); |
815 | ||
89958ad0 | 816 | SCM_COERCE_SUBSTR (mode); |
0f2d19dd JB |
817 | return scm_void_port (SCM_ROCHARS (mode)); |
818 | } | |
819 | ||
820 | ||
821 | ||
822 | \f | |
823 | ||
1cc91f1b | 824 | |
0f2d19dd JB |
825 | void |
826 | scm_init_ports () | |
0f2d19dd JB |
827 | { |
828 | scm_tc16_void_port = scm_newptob (&void_port_ptob); | |
829 | #include "ports.x" | |
830 | } | |
831 |