| 1 | /* Copyright (C) 1995,1996,1997 Free Software Foundation, Inc. |
| 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 |
| 15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 16 | * Boston, MA 02111-1307 USA |
| 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. |
| 40 | * If you do not wish that, delete this exception notice. */ |
| 41 | \f |
| 42 | /* Headers. */ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include "_scm.h" |
| 46 | #include "genio.h" |
| 47 | #include "chars.h" |
| 48 | |
| 49 | #include "markers.h" |
| 50 | #include "filesys.h" |
| 51 | #include "fports.h" |
| 52 | #include "strports.h" |
| 53 | #include "vports.h" |
| 54 | #include "kw.h" |
| 55 | |
| 56 | #include "ports.h" |
| 57 | |
| 58 | #ifdef HAVE_MALLOC_H |
| 59 | #include <malloc.h> |
| 60 | #endif |
| 61 | |
| 62 | #ifdef HAVE_UNISTD_H |
| 63 | #include <unistd.h> |
| 64 | #endif |
| 65 | |
| 66 | #ifdef HAVE_SYS_IOCTL_H |
| 67 | #include <sys/ioctl.h> |
| 68 | #endif |
| 69 | |
| 70 | \f |
| 71 | /* The port kind table --- a dynamically resized array of port types. */ |
| 72 | |
| 73 | |
| 74 | /* scm_ptobs scm_numptob |
| 75 | * implement a dynamicly resized array of ptob records. |
| 76 | * Indexes into this table are used when generating type |
| 77 | * tags for smobjects (if you know a tag you can get an index and conversely). |
| 78 | */ |
| 79 | scm_ptobfuns *scm_ptobs; |
| 80 | int scm_numptob; |
| 81 | |
| 82 | |
| 83 | SCM |
| 84 | scm_markstream (ptr) |
| 85 | SCM ptr; |
| 86 | { |
| 87 | int openp; |
| 88 | openp = SCM_CAR (ptr) & SCM_OPN; |
| 89 | if (openp) |
| 90 | return SCM_STREAM (ptr); |
| 91 | else |
| 92 | return SCM_BOOL_F; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | long |
| 98 | scm_newptob (ptob) |
| 99 | scm_ptobfuns *ptob; |
| 100 | { |
| 101 | char *tmp; |
| 102 | if (255 <= scm_numptob) |
| 103 | goto ptoberr; |
| 104 | SCM_DEFER_INTS; |
| 105 | SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs, (1 + scm_numptob) * sizeof (scm_ptobfuns))); |
| 106 | if (tmp) |
| 107 | { |
| 108 | scm_ptobs = (scm_ptobfuns *) tmp; |
| 109 | scm_ptobs[scm_numptob].mark = ptob->mark; |
| 110 | scm_ptobs[scm_numptob].free = ptob->free; |
| 111 | scm_ptobs[scm_numptob].print = ptob->print; |
| 112 | scm_ptobs[scm_numptob].equalp = ptob->equalp; |
| 113 | scm_ptobs[scm_numptob].fputc = ptob->fputc; |
| 114 | scm_ptobs[scm_numptob].fputs = ptob->fputs; |
| 115 | scm_ptobs[scm_numptob].fwrite = ptob->fwrite; |
| 116 | scm_ptobs[scm_numptob].fflush = ptob->fflush; |
| 117 | scm_ptobs[scm_numptob].fgetc = ptob->fgetc; |
| 118 | scm_ptobs[scm_numptob].fgets = ptob->fgets; |
| 119 | scm_ptobs[scm_numptob].fclose = ptob->fclose; |
| 120 | scm_numptob++; |
| 121 | } |
| 122 | SCM_ALLOW_INTS; |
| 123 | if (!tmp) |
| 124 | ptoberr:scm_wta (SCM_MAKINUM ((long) scm_numptob), (char *) SCM_NALLOC, "newptob"); |
| 125 | return scm_tc7_port + (scm_numptob - 1) * 256; |
| 126 | } |
| 127 | |
| 128 | \f |
| 129 | |
| 130 | SCM_PROC(s_char_ready_p, "char-ready?", 0, 1, 0, scm_char_ready_p); |
| 131 | |
| 132 | SCM |
| 133 | scm_char_ready_p (port) |
| 134 | SCM port; |
| 135 | { |
| 136 | if (SCM_UNBNDP (port)) |
| 137 | port = scm_cur_inp; |
| 138 | else |
| 139 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, |
| 140 | s_char_ready_p); |
| 141 | |
| 142 | if (SCM_CRDYP (port) || !SCM_FPORTP (port)) |
| 143 | return SCM_BOOL_T; |
| 144 | return (scm_input_waiting_p ((FILE *) SCM_STREAM (port), s_char_ready_p) |
| 145 | ? SCM_BOOL_T |
| 146 | : SCM_BOOL_F); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | \f |
| 151 | /* Standard ports --- current input, output, error, and more(!). */ |
| 152 | |
| 153 | SCM_PROC(s_current_input_port, "current-input-port", 0, 0, 0, scm_current_input_port); |
| 154 | |
| 155 | SCM |
| 156 | scm_current_input_port () |
| 157 | { |
| 158 | return scm_cur_inp; |
| 159 | } |
| 160 | |
| 161 | SCM_PROC(s_current_output_port, "current-output-port", 0, 0, 0, scm_current_output_port); |
| 162 | |
| 163 | SCM |
| 164 | scm_current_output_port () |
| 165 | { |
| 166 | return scm_cur_outp; |
| 167 | } |
| 168 | |
| 169 | SCM_PROC(s_current_error_port, "current-error-port", 0, 0, 0, scm_current_error_port); |
| 170 | |
| 171 | SCM |
| 172 | scm_current_error_port () |
| 173 | { |
| 174 | return scm_cur_errp; |
| 175 | } |
| 176 | |
| 177 | SCM_PROC(s_current_load_port, "current-load-port", 0, 0, 0, scm_current_load_port); |
| 178 | |
| 179 | SCM |
| 180 | scm_current_load_port () |
| 181 | { |
| 182 | return scm_cur_loadp; |
| 183 | } |
| 184 | |
| 185 | SCM_PROC(s_set_current_input_port, "set-current-input-port", 1, 0, 0, scm_set_current_input_port); |
| 186 | |
| 187 | SCM |
| 188 | scm_set_current_input_port (port) |
| 189 | SCM port; |
| 190 | { |
| 191 | SCM oinp = scm_cur_inp; |
| 192 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_set_current_input_port); |
| 193 | scm_cur_inp = port; |
| 194 | return oinp; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | SCM_PROC(s_set_current_output_port, "set-current-output-port", 1, 0, 0, scm_set_current_output_port); |
| 199 | |
| 200 | SCM |
| 201 | scm_set_current_output_port (port) |
| 202 | SCM port; |
| 203 | { |
| 204 | SCM ooutp = scm_cur_outp; |
| 205 | port = SCM_COERCE_OUTPORT (port); |
| 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); |
| 213 | |
| 214 | SCM |
| 215 | scm_set_current_error_port (port) |
| 216 | SCM port; |
| 217 | { |
| 218 | SCM oerrp = scm_cur_errp; |
| 219 | port = SCM_COERCE_OUTPORT (port); |
| 220 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_set_current_error_port); |
| 221 | scm_cur_errp = port; |
| 222 | return oerrp; |
| 223 | } |
| 224 | |
| 225 | \f |
| 226 | /* The port table --- a table of all the open ports. */ |
| 227 | |
| 228 | /* Array of open ports, required for reliable MOVE->FDES etc. */ |
| 229 | struct scm_port_table **scm_port_table; |
| 230 | |
| 231 | int scm_port_table_size = 0; /* Number of ports in scm_port_table. */ |
| 232 | int scm_port_table_room = 20; /* Size of the array. */ |
| 233 | |
| 234 | /* Add a port to the table. Call with SCM_DEFER_INTS active. */ |
| 235 | |
| 236 | struct scm_port_table * |
| 237 | scm_add_to_port_table (port) |
| 238 | SCM port; |
| 239 | { |
| 240 | if (scm_port_table_size == scm_port_table_room) |
| 241 | { |
| 242 | scm_port_table = ((struct scm_port_table **) |
| 243 | realloc ((char *) scm_port_table, |
| 244 | (scm_sizet) (sizeof (struct scm_port_table *) |
| 245 | * scm_port_table_room * 2))); |
| 246 | /* !!! error checking */ |
| 247 | scm_port_table_room *= 2; |
| 248 | } |
| 249 | scm_port_table[scm_port_table_size] = ((struct scm_port_table *) |
| 250 | scm_must_malloc (sizeof (struct scm_port_table), |
| 251 | "system port table")); |
| 252 | scm_port_table[scm_port_table_size]->port = port; |
| 253 | scm_port_table[scm_port_table_size]->revealed = 0; |
| 254 | scm_port_table[scm_port_table_size]->stream = 0; |
| 255 | scm_port_table[scm_port_table_size]->file_name = SCM_BOOL_F; |
| 256 | scm_port_table[scm_port_table_size]->line_number = 0; |
| 257 | scm_port_table[scm_port_table_size]->column_number = 0; |
| 258 | return scm_port_table[scm_port_table_size++]; |
| 259 | } |
| 260 | |
| 261 | /* Remove a port from the table. Call with SCM_DEFER_INTS active. */ |
| 262 | |
| 263 | void |
| 264 | scm_remove_from_port_table (port) |
| 265 | SCM port; |
| 266 | { |
| 267 | int i = 0; |
| 268 | while (scm_port_table[i]->port != port) |
| 269 | { |
| 270 | i++; |
| 271 | /* Error if not found: too violent? May occur in GC. */ |
| 272 | if (i >= scm_port_table_size) |
| 273 | scm_wta (port, "Port not in table", "scm_remove_from_port_table"); |
| 274 | } |
| 275 | scm_must_free ((char *)scm_port_table[i]); |
| 276 | scm_mallocated -= sizeof (*scm_port_table[i]); |
| 277 | scm_port_table[i] = scm_port_table[scm_port_table_size - 1]; |
| 278 | SCM_SETPTAB_ENTRY (port, 0); |
| 279 | scm_port_table_size--; |
| 280 | } |
| 281 | |
| 282 | #ifdef GUILE_DEBUG |
| 283 | /* Undocumented functions for debugging. */ |
| 284 | /* Return the number of ports in the table. */ |
| 285 | |
| 286 | SCM_PROC(s_pt_size, "pt-size", 0, 0, 0, scm_pt_size); |
| 287 | SCM |
| 288 | scm_pt_size () |
| 289 | { |
| 290 | return SCM_MAKINUM (scm_port_table_size); |
| 291 | } |
| 292 | |
| 293 | /* Return the ith member of the port table. */ |
| 294 | SCM_PROC(s_pt_member, "pt-member", 1, 0, 0, scm_pt_member); |
| 295 | SCM |
| 296 | scm_pt_member (member) |
| 297 | SCM member; |
| 298 | { |
| 299 | int i; |
| 300 | SCM_ASSERT (SCM_INUMP (member), member, SCM_ARG1, s_pt_member); |
| 301 | i = SCM_INUM (member); |
| 302 | if (i < 0 || i >= scm_port_table_size) |
| 303 | return SCM_BOOL_F; |
| 304 | else |
| 305 | return scm_port_table[i]->port; |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | |
| 310 | \f |
| 311 | /* Revealed counts --- an oddity inherited from SCSH. */ |
| 312 | |
| 313 | /* Find a port in the table and return its revealed count. |
| 314 | Also used by the garbage collector. |
| 315 | */ |
| 316 | |
| 317 | int |
| 318 | scm_revealed_count (port) |
| 319 | SCM port; |
| 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); |
| 329 | |
| 330 | SCM |
| 331 | scm_port_revealed (port) |
| 332 | SCM port; |
| 333 | { |
| 334 | port = SCM_COERCE_OUTPORT (port); |
| 335 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_port_revealed); |
| 336 | return SCM_MAKINUM (scm_revealed_count (port)); |
| 337 | } |
| 338 | |
| 339 | /* Set the revealed count for a port. */ |
| 340 | SCM_PROC(s_set_port_revealed_x, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x); |
| 341 | |
| 342 | SCM |
| 343 | scm_set_port_revealed_x (port, rcount) |
| 344 | SCM port; |
| 345 | SCM rcount; |
| 346 | { |
| 347 | port = SCM_COERCE_OUTPORT (port); |
| 348 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_set_port_revealed_x); |
| 349 | SCM_ASSERT (SCM_INUMP (rcount), rcount, SCM_ARG2, s_set_port_revealed_x); |
| 350 | SCM_DEFER_INTS; |
| 351 | SCM_REVEALED (port) = SCM_INUM (rcount); |
| 352 | SCM_ALLOW_INTS; |
| 353 | return SCM_UNSPECIFIED; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | \f |
| 358 | /* Retrieving a port's mode. */ |
| 359 | |
| 360 | /* Return the flags that characterize a port based on the mode |
| 361 | * string used to open a file for that port. |
| 362 | * |
| 363 | * See PORT FLAGS in scm.h |
| 364 | */ |
| 365 | |
| 366 | long |
| 367 | scm_mode_bits (modes) |
| 368 | char *modes; |
| 369 | { |
| 370 | return (SCM_OPN |
| 371 | | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0) |
| 372 | | ( strchr (modes, 'w') |
| 373 | || strchr (modes, 'a') |
| 374 | || strchr (modes, '+') ? SCM_WRTNG : 0) |
| 375 | | (strchr (modes, '0') ? SCM_BUF0 : 0)); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | /* Return the mode flags from an open port. |
| 380 | * Some modes such as "append" are only used when opening |
| 381 | * a file and are not returned here. */ |
| 382 | |
| 383 | SCM_PROC(s_port_mode, "port-mode", 1, 0, 0, scm_port_mode); |
| 384 | |
| 385 | SCM |
| 386 | scm_port_mode (port) |
| 387 | SCM port; |
| 388 | { |
| 389 | char modes[3]; |
| 390 | modes[0] = '\0'; |
| 391 | |
| 392 | port = SCM_COERCE_OUTPORT (port); |
| 393 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPPORTP (port), port, SCM_ARG1, s_port_mode); |
| 394 | if (SCM_CAR (port) & SCM_RDNG) { |
| 395 | if (SCM_CAR (port) & SCM_WRTNG) |
| 396 | strcpy (modes, "r+"); |
| 397 | else |
| 398 | strcpy (modes, "r"); |
| 399 | } |
| 400 | else if (SCM_CAR (port) & SCM_WRTNG) |
| 401 | strcpy (modes, "w"); |
| 402 | if (SCM_CAR (port) & SCM_BUF0) |
| 403 | strcat (modes, "0"); |
| 404 | return scm_makfromstr (modes, strlen (modes), 0); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | \f |
| 409 | /* Closing ports. */ |
| 410 | |
| 411 | /* scm_close_port |
| 412 | * Call the close operation on a port object. |
| 413 | * see also scm_close. |
| 414 | */ |
| 415 | SCM_PROC(s_close_port, "close-port", 1, 0, 0, scm_close_port); |
| 416 | |
| 417 | SCM |
| 418 | scm_close_port (port) |
| 419 | SCM port; |
| 420 | { |
| 421 | scm_sizet i; |
| 422 | int rv; |
| 423 | |
| 424 | port = SCM_COERCE_OUTPORT (port); |
| 425 | |
| 426 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, |
| 427 | s_close_port); |
| 428 | if (SCM_CLOSEDP (port)) |
| 429 | return SCM_BOOL_F; |
| 430 | i = SCM_PTOBNUM (port); |
| 431 | SCM_DEFER_INTS; |
| 432 | if (scm_ptobs[i].fclose) |
| 433 | { |
| 434 | SCM_SYSCALL (rv = (scm_ptobs[i].fclose) (SCM_STREAM (port))); |
| 435 | /* ports with a closed file descriptor can be reclosed without error. */ |
| 436 | if (rv < 0 && errno != EBADF) |
| 437 | scm_syserror (s_close_port); |
| 438 | } |
| 439 | else |
| 440 | rv = 0; |
| 441 | scm_remove_from_port_table (port); |
| 442 | SCM_SETAND_CAR (port, ~SCM_OPN); |
| 443 | SCM_ALLOW_INTS; |
| 444 | return (rv < 0) ? SCM_BOOL_F : SCM_BOOL_T; |
| 445 | } |
| 446 | |
| 447 | SCM_PROC(s_close_all_ports_except, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except); |
| 448 | |
| 449 | SCM |
| 450 | scm_close_all_ports_except (ports) |
| 451 | SCM ports; |
| 452 | { |
| 453 | int i = 0; |
| 454 | SCM_ASSERT (SCM_NIMP (ports) && SCM_CONSP (ports), ports, SCM_ARG1, s_close_all_ports_except); |
| 455 | SCM_DEFER_INTS; |
| 456 | while (i < scm_port_table_size) |
| 457 | { |
| 458 | SCM thisport = scm_port_table[i]->port; |
| 459 | int found = 0; |
| 460 | SCM ports_ptr = ports; |
| 461 | |
| 462 | while (SCM_NNULLP (ports_ptr)) |
| 463 | { |
| 464 | SCM port = SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr)); |
| 465 | if (i == 0) |
| 466 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPPORTP (port), port, SCM_ARG1, s_close_all_ports_except); |
| 467 | if (port == thisport) |
| 468 | found = 1; |
| 469 | ports_ptr = SCM_CDR (ports_ptr); |
| 470 | } |
| 471 | if (found) |
| 472 | i++; |
| 473 | else |
| 474 | /* i is not to be incremented here. */ |
| 475 | scm_close_port (thisport); |
| 476 | } |
| 477 | SCM_ALLOW_INTS; |
| 478 | return SCM_UNSPECIFIED; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | \f |
| 483 | /* Utter miscellany. Gosh, we should clean this up some time. */ |
| 484 | |
| 485 | SCM_PROC(s_input_port_p, "input-port?", 1, 0, 0, scm_input_port_p); |
| 486 | |
| 487 | SCM |
| 488 | scm_input_port_p (x) |
| 489 | SCM x; |
| 490 | { |
| 491 | if (SCM_IMP (x)) |
| 492 | return SCM_BOOL_F; |
| 493 | return SCM_INPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F; |
| 494 | } |
| 495 | |
| 496 | SCM_PROC(s_output_port_p, "output-port?", 1, 0, 0, scm_output_port_p); |
| 497 | |
| 498 | SCM |
| 499 | scm_output_port_p (x) |
| 500 | SCM x; |
| 501 | { |
| 502 | if (SCM_IMP (x)) |
| 503 | return SCM_BOOL_F; |
| 504 | return SCM_OUTPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | SCM_PROC(s_eof_object_p, "eof-object?", 1, 0, 0, scm_eof_object_p); |
| 509 | |
| 510 | SCM |
| 511 | scm_eof_object_p (x) |
| 512 | SCM x; |
| 513 | { |
| 514 | return SCM_EOF_OBJECT_P (x) ? SCM_BOOL_T : SCM_BOOL_F; |
| 515 | } |
| 516 | |
| 517 | SCM_PROC(s_force_output, "force-output", 0, 1, 0, scm_force_output); |
| 518 | |
| 519 | SCM |
| 520 | scm_force_output (port) |
| 521 | SCM port; |
| 522 | { |
| 523 | if (SCM_UNBNDP (port)) |
| 524 | port = scm_cur_outp; |
| 525 | else |
| 526 | { |
| 527 | port = SCM_COERCE_OUTPORT (port); |
| 528 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_force_output); |
| 529 | } |
| 530 | { |
| 531 | scm_sizet i = SCM_PTOBNUM (port); |
| 532 | SCM_SYSCALL ((scm_ptobs[i].fflush) (SCM_STREAM (port))); |
| 533 | return SCM_UNSPECIFIED; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | SCM_PROC (s_flush_all_ports, "flush-all-ports", 0, 0, 0, scm_flush_all_ports); |
| 538 | SCM |
| 539 | scm_flush_all_ports (void) |
| 540 | { |
| 541 | int i; |
| 542 | |
| 543 | for (i = 0; i < scm_port_table_size; i++) |
| 544 | { |
| 545 | SCM port = scm_port_table[i]->port; |
| 546 | if (SCM_OPOUTPORTP (port)) |
| 547 | { |
| 548 | scm_sizet ptob = SCM_PTOBNUM (port); |
| 549 | (scm_ptobs[ptob].fflush) (SCM_STREAM (port)); |
| 550 | } |
| 551 | } |
| 552 | return SCM_UNSPECIFIED; |
| 553 | } |
| 554 | |
| 555 | SCM_PROC(s_read_char, "read-char", 0, 1, 0, scm_read_char); |
| 556 | |
| 557 | SCM |
| 558 | scm_read_char (port) |
| 559 | SCM port; |
| 560 | { |
| 561 | int c; |
| 562 | if (SCM_UNBNDP (port)) |
| 563 | port = scm_cur_inp; |
| 564 | else |
| 565 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_read_char); |
| 566 | c = scm_getc (port); |
| 567 | if (EOF == c) |
| 568 | return SCM_EOF_VAL; |
| 569 | return SCM_MAKICHR (c); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | SCM_PROC(s_peek_char, "peek-char", 0, 1, 0, scm_peek_char); |
| 574 | |
| 575 | SCM |
| 576 | scm_peek_char (port) |
| 577 | SCM port; |
| 578 | { |
| 579 | int c; |
| 580 | if (SCM_UNBNDP (port)) |
| 581 | port = scm_cur_inp; |
| 582 | else |
| 583 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_peek_char); |
| 584 | c = scm_getc (port); |
| 585 | if (EOF == c) |
| 586 | return SCM_EOF_VAL; |
| 587 | scm_ungetc (c, port); |
| 588 | return SCM_MAKICHR (c); |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * A generic fgets method. We supply this method so that ports which |
| 593 | * can't use fgets(3) (like string ports or soft ports) can still use |
| 594 | * line-based i/o. The generic method calls the port's own fgetc method |
| 595 | * for input. It should be possible to write a more efficient |
| 596 | * method for any given port representation -- this is supplied just |
| 597 | * to ensure that you don't have to. |
| 598 | */ |
| 599 | |
| 600 | char * scm_generic_fgets SCM_P ((SCM port, int *len)); |
| 601 | |
| 602 | char * |
| 603 | scm_generic_fgets (port, len) |
| 604 | SCM port; |
| 605 | int *len; |
| 606 | { |
| 607 | SCM f = SCM_STREAM (port); |
| 608 | scm_sizet p = SCM_PTOBNUM (port); |
| 609 | |
| 610 | char *buf; |
| 611 | int limit = 80; /* current size of buffer */ |
| 612 | int c; |
| 613 | |
| 614 | /* FIXME: It would be nice to be able to check for EOF before anything. */ |
| 615 | |
| 616 | *len = 0; |
| 617 | buf = (char *) malloc (limit * sizeof(char)); |
| 618 | |
| 619 | /* If a char has been pushed onto the port with scm_ungetc, |
| 620 | read that first. */ |
| 621 | if (SCM_CRDYP (port)) |
| 622 | { |
| 623 | buf[*len] = SCM_CGETUN (port); |
| 624 | SCM_CLRDY (port); |
| 625 | if (buf[(*len)++] == '\n') |
| 626 | { |
| 627 | buf[*len] = '\0'; |
| 628 | return buf; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | while (1) { |
| 633 | if (*len >= limit-1) |
| 634 | { |
| 635 | buf = (char *) realloc (buf, sizeof(char) * limit * 2); |
| 636 | limit *= 2; |
| 637 | } |
| 638 | |
| 639 | c = (scm_ptobs[p].fgetc) (f); |
| 640 | if (c != EOF) |
| 641 | buf[(*len)++] = c; |
| 642 | |
| 643 | if (c == EOF || c == '\n') |
| 644 | { |
| 645 | if (*len) |
| 646 | { |
| 647 | buf[*len] = '\0'; |
| 648 | return buf; |
| 649 | } |
| 650 | free (buf); |
| 651 | return NULL; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | SCM_PROC (s_unread_char, "unread-char", 2, 0, 0, scm_unread_char); |
| 657 | |
| 658 | SCM |
| 659 | scm_unread_char (cobj, port) |
| 660 | SCM cobj; |
| 661 | SCM port; |
| 662 | { |
| 663 | int c; |
| 664 | |
| 665 | SCM_ASSERT (SCM_ICHRP (cobj), cobj, SCM_ARG1, s_unread_char); |
| 666 | |
| 667 | if (SCM_UNBNDP (port)) |
| 668 | port = scm_cur_inp; |
| 669 | else |
| 670 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG2, s_unread_char); |
| 671 | |
| 672 | |
| 673 | c = SCM_ICHR (cobj); |
| 674 | |
| 675 | scm_ungetc (c, port); |
| 676 | return cobj; |
| 677 | } |
| 678 | |
| 679 | SCM_PROC (s_port_line, "port-line", 1, 0, 0, scm_port_line); |
| 680 | |
| 681 | SCM |
| 682 | scm_port_line (port) |
| 683 | SCM port; |
| 684 | { |
| 685 | port = SCM_COERCE_OUTPORT (port); |
| 686 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), |
| 687 | port, |
| 688 | SCM_ARG1, |
| 689 | s_port_line); |
| 690 | return SCM_MAKINUM (SCM_LINUM (port)); |
| 691 | } |
| 692 | |
| 693 | SCM_PROC (s_set_port_line_x, "set-port-line!", 2, 0, 0, scm_set_port_line_x); |
| 694 | |
| 695 | SCM |
| 696 | scm_set_port_line_x (port, line) |
| 697 | SCM port; |
| 698 | SCM line; |
| 699 | { |
| 700 | port = SCM_COERCE_OUTPORT (port); |
| 701 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), |
| 702 | port, |
| 703 | SCM_ARG1, |
| 704 | s_set_port_line_x); |
| 705 | SCM_ASSERT (SCM_INUMP (line), line, SCM_ARG2, s_set_port_line_x); |
| 706 | return SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line); |
| 707 | } |
| 708 | |
| 709 | SCM_PROC (s_port_column, "port-column", 1, 0, 0, scm_port_column); |
| 710 | |
| 711 | SCM |
| 712 | scm_port_column (port) |
| 713 | SCM port; |
| 714 | { |
| 715 | port = SCM_COERCE_OUTPORT (port); |
| 716 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), |
| 717 | port, |
| 718 | SCM_ARG1, |
| 719 | s_port_column); |
| 720 | return SCM_MAKINUM (SCM_COL (port)); |
| 721 | } |
| 722 | |
| 723 | SCM_PROC (s_set_port_column_x, "set-port-column!", 2, 0, 0, scm_set_port_column_x); |
| 724 | |
| 725 | SCM |
| 726 | scm_set_port_column_x (port, column) |
| 727 | SCM port; |
| 728 | SCM column; |
| 729 | { |
| 730 | port = SCM_COERCE_OUTPORT (port); |
| 731 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), |
| 732 | port, |
| 733 | SCM_ARG1, |
| 734 | s_set_port_column_x); |
| 735 | SCM_ASSERT (SCM_INUMP (column), column, SCM_ARG2, s_set_port_column_x); |
| 736 | return SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column); |
| 737 | } |
| 738 | |
| 739 | SCM_PROC (s_port_filename, "port-filename", 1, 0, 0, scm_port_filename); |
| 740 | |
| 741 | SCM |
| 742 | scm_port_filename (port) |
| 743 | SCM port; |
| 744 | { |
| 745 | port = SCM_COERCE_OUTPORT (port); |
| 746 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), |
| 747 | port, |
| 748 | SCM_ARG1, |
| 749 | s_port_filename); |
| 750 | return SCM_PTAB_ENTRY (port)->file_name; |
| 751 | } |
| 752 | |
| 753 | SCM_PROC (s_set_port_filename_x, "set-port-filename!", 2, 0, 0, scm_set_port_filename_x); |
| 754 | |
| 755 | SCM |
| 756 | scm_set_port_filename_x (port, filename) |
| 757 | SCM port; |
| 758 | SCM filename; |
| 759 | { |
| 760 | port = SCM_COERCE_OUTPORT (port); |
| 761 | SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port), |
| 762 | port, |
| 763 | SCM_ARG1, |
| 764 | s_set_port_filename_x); |
| 765 | /* We allow the user to set the filename to whatever he likes. */ |
| 766 | return SCM_PTAB_ENTRY (port)->file_name = filename; |
| 767 | } |
| 768 | |
| 769 | #ifndef ttyname |
| 770 | extern char * ttyname(); |
| 771 | #endif |
| 772 | |
| 773 | |
| 774 | void |
| 775 | scm_prinport (exp, port, type) |
| 776 | SCM exp; |
| 777 | SCM port; |
| 778 | char *type; |
| 779 | { |
| 780 | scm_puts ("#<", port); |
| 781 | if (SCM_CLOSEDP (exp)) |
| 782 | scm_puts ("closed: ", port); |
| 783 | else |
| 784 | { |
| 785 | if (SCM_RDNG & SCM_CAR (exp)) |
| 786 | scm_puts ("input: ", port); |
| 787 | if (SCM_WRTNG & SCM_CAR (exp)) |
| 788 | scm_puts ("output: ", port); |
| 789 | } |
| 790 | scm_puts (type, port); |
| 791 | scm_putc (' ', port); |
| 792 | #ifndef MSDOS |
| 793 | #ifndef __EMX__ |
| 794 | #ifndef _DCC |
| 795 | #ifndef AMIGA |
| 796 | #ifndef THINK_C |
| 797 | if (SCM_OPENP (exp) && scm_tc16_fport == SCM_TYP16 (exp) && isatty (fileno ((FILE *)SCM_STREAM (exp)))) |
| 798 | scm_puts (ttyname (fileno ((FILE *)SCM_STREAM (exp))), port); |
| 799 | else |
| 800 | #endif |
| 801 | #endif |
| 802 | #endif |
| 803 | #endif |
| 804 | #endif |
| 805 | if (SCM_OPFPORTP (exp)) |
| 806 | scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp)), 10, port); |
| 807 | else |
| 808 | scm_intprint (SCM_CDR (exp), 16, port); |
| 809 | scm_putc ('>', port); |
| 810 | } |
| 811 | |
| 812 | |
| 813 | void |
| 814 | scm_ports_prehistory () |
| 815 | { |
| 816 | scm_numptob = 0; |
| 817 | scm_ptobs = (scm_ptobfuns *) malloc (sizeof (scm_ptobfuns)); |
| 818 | |
| 819 | /* WARNING: These scm_newptob calls must be done in this order. |
| 820 | * They must agree with the port declarations in tags.h. |
| 821 | */ |
| 822 | /* scm_tc16_fport = */ scm_newptob (&scm_fptob); |
| 823 | /* scm_tc16_pipe = */ scm_newptob (&scm_pipob); |
| 824 | /* scm_tc16_strport = */ scm_newptob (&scm_stptob); |
| 825 | /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob); |
| 826 | } |
| 827 | \f |
| 828 | |
| 829 | \f |
| 830 | /* Void ports. */ |
| 831 | |
| 832 | int scm_tc16_void_port = 0; |
| 833 | |
| 834 | static int |
| 835 | print_void_port (SCM exp, SCM port, scm_print_state *pstate) |
| 836 | { |
| 837 | scm_prinport (exp, port, "void"); |
| 838 | return 1; |
| 839 | } |
| 840 | |
| 841 | static int |
| 842 | putc_void_port (int c, SCM strm) |
| 843 | { |
| 844 | return 0; /* vestigial return value */ |
| 845 | } |
| 846 | |
| 847 | static int |
| 848 | puts_void_port (char *s, SCM strm) |
| 849 | { |
| 850 | return 0; /* vestigial return value */ |
| 851 | } |
| 852 | |
| 853 | static scm_sizet |
| 854 | write_void_port (char *ptr, scm_sizet size, scm_sizet nitems, SCM strm) |
| 855 | { |
| 856 | int len; |
| 857 | len = size * nitems; |
| 858 | return len; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | static int |
| 863 | flush_void_port (SCM strm) |
| 864 | { |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | |
| 869 | static int |
| 870 | getc_void_port (SCM strm) |
| 871 | { |
| 872 | return EOF; |
| 873 | } |
| 874 | |
| 875 | static char * |
| 876 | fgets_void_port (SCM strm, int *len) |
| 877 | { |
| 878 | return NULL; |
| 879 | } |
| 880 | |
| 881 | static int |
| 882 | close_void_port (SCM strm) |
| 883 | { |
| 884 | return 0; /* this is ignored by scm_close_port. */ |
| 885 | } |
| 886 | |
| 887 | |
| 888 | |
| 889 | static int |
| 890 | noop0 (SCM stream) |
| 891 | { |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | |
| 896 | static struct scm_ptobfuns void_port_ptob = |
| 897 | { |
| 898 | 0, |
| 899 | noop0, |
| 900 | print_void_port, |
| 901 | 0, /* equal? */ |
| 902 | putc_void_port, |
| 903 | puts_void_port, |
| 904 | write_void_port, |
| 905 | flush_void_port, |
| 906 | getc_void_port, |
| 907 | fgets_void_port, |
| 908 | close_void_port, |
| 909 | }; |
| 910 | |
| 911 | \f |
| 912 | |
| 913 | |
| 914 | SCM |
| 915 | scm_void_port (mode_str) |
| 916 | char * mode_str; |
| 917 | { |
| 918 | int mode_bits; |
| 919 | SCM answer; |
| 920 | struct scm_port_table * pt; |
| 921 | |
| 922 | SCM_NEWCELL (answer); |
| 923 | SCM_DEFER_INTS; |
| 924 | mode_bits = scm_mode_bits (mode_str); |
| 925 | pt = scm_add_to_port_table (answer); |
| 926 | SCM_SETCAR (answer, scm_tc16_void_port | mode_bits); |
| 927 | SCM_SETPTAB_ENTRY (answer, pt); |
| 928 | SCM_SETSTREAM (answer, SCM_BOOL_F); |
| 929 | SCM_ALLOW_INTS; |
| 930 | return answer; |
| 931 | } |
| 932 | |
| 933 | |
| 934 | SCM_PROC (s_sys_make_void_port, "%make-void-port", 1, 0, 0, scm_sys_make_void_port); |
| 935 | |
| 936 | SCM |
| 937 | scm_sys_make_void_port (mode) |
| 938 | SCM mode; |
| 939 | { |
| 940 | SCM_ASSERT (SCM_NIMP (mode) && SCM_ROSTRINGP (mode), mode, |
| 941 | SCM_ARG1, s_sys_make_void_port); |
| 942 | |
| 943 | SCM_COERCE_SUBSTR (mode); |
| 944 | return scm_void_port (SCM_ROCHARS (mode)); |
| 945 | } |
| 946 | |
| 947 | |
| 948 | |
| 949 | \f |
| 950 | |
| 951 | |
| 952 | void |
| 953 | scm_init_ports () |
| 954 | { |
| 955 | scm_tc16_void_port = scm_newptob (&void_port_ptob); |
| 956 | #include "ports.x" |
| 957 | } |
| 958 | |