| 1 | /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
| 2 | * 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
| 3 | * 2014 Free Software Foundation, Inc. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation; either version 3 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | \f |
| 23 | #define _LARGEFILE64_SOURCE /* ask for stat64 etc */ |
| 24 | #define _GNU_SOURCE /* ask for LONG_LONG_MAX/LONG_LONG_MIN */ |
| 25 | |
| 26 | #ifdef HAVE_CONFIG_H |
| 27 | # include <config.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | #ifdef HAVE_STRING_H |
| 34 | #include <string.h> |
| 35 | #endif |
| 36 | #include <unistd.h> |
| 37 | #ifdef HAVE_IO_H |
| 38 | #include <io.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 41 | #include <sys/stat.h> |
| 42 | #endif |
| 43 | #include <poll.h> |
| 44 | #include <errno.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <sys/select.h> |
| 48 | |
| 49 | #include <full-write.h> |
| 50 | |
| 51 | #include "libguile/_scm.h" |
| 52 | #include "libguile/strings.h" |
| 53 | #include "libguile/validate.h" |
| 54 | #include "libguile/gc.h" |
| 55 | #include "libguile/posix.h" |
| 56 | #include "libguile/dynwind.h" |
| 57 | #include "libguile/hashtab.h" |
| 58 | |
| 59 | #include "libguile/fports.h" |
| 60 | #include "libguile/ports-internal.h" |
| 61 | |
| 62 | #if SIZEOF_OFF_T == SIZEOF_INT |
| 63 | #define OFF_T_MAX INT_MAX |
| 64 | #define OFF_T_MIN INT_MIN |
| 65 | #elif SIZEOF_OFF_T == SIZEOF_LONG |
| 66 | #define OFF_T_MAX LONG_MAX |
| 67 | #define OFF_T_MIN LONG_MIN |
| 68 | #elif SIZEOF_OFF_T == SIZEOF_LONG_LONG |
| 69 | #define OFF_T_MAX LONG_LONG_MAX |
| 70 | #define OFF_T_MIN LONG_LONG_MIN |
| 71 | #else |
| 72 | #error Oops, unknown OFF_T size |
| 73 | #endif |
| 74 | |
| 75 | scm_t_bits scm_tc16_fport; |
| 76 | |
| 77 | |
| 78 | /* default buffer size, used if the O/S won't supply a value. */ |
| 79 | static const size_t default_buffer_size = 1024; |
| 80 | |
| 81 | /* Create FPORT buffers with specified sizes (or -1 to use default size |
| 82 | or 0 for no buffer.) */ |
| 83 | static void |
| 84 | scm_fport_buffer_add (SCM port, long read_size, long write_size) |
| 85 | #define FUNC_NAME "scm_fport_buffer_add" |
| 86 | { |
| 87 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
| 88 | |
| 89 | if (read_size == -1 || write_size == -1) |
| 90 | { |
| 91 | size_t default_size; |
| 92 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 93 | struct stat st; |
| 94 | scm_t_fport *fp = SCM_FSTREAM (port); |
| 95 | |
| 96 | default_size = (fstat (fp->fdes, &st) == -1) ? default_buffer_size |
| 97 | : st.st_blksize; |
| 98 | #else |
| 99 | default_size = default_buffer_size; |
| 100 | #endif |
| 101 | if (read_size == -1) |
| 102 | read_size = default_size; |
| 103 | if (write_size == -1) |
| 104 | write_size = default_size; |
| 105 | } |
| 106 | |
| 107 | if (SCM_INPUT_PORT_P (port) && read_size > 0) |
| 108 | { |
| 109 | pt->read_buf = scm_gc_malloc_pointerless (read_size, "port buffer"); |
| 110 | pt->read_pos = pt->read_end = pt->read_buf; |
| 111 | pt->read_buf_size = read_size; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf; |
| 116 | pt->read_buf_size = 1; |
| 117 | } |
| 118 | |
| 119 | if (SCM_OUTPUT_PORT_P (port) && write_size > 0) |
| 120 | { |
| 121 | pt->write_buf = scm_gc_malloc_pointerless (write_size, "port buffer"); |
| 122 | pt->write_pos = pt->write_buf; |
| 123 | pt->write_buf_size = write_size; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | pt->write_buf = pt->write_pos = &pt->shortbuf; |
| 128 | pt->write_buf_size = 1; |
| 129 | } |
| 130 | |
| 131 | pt->write_end = pt->write_buf + pt->write_buf_size; |
| 132 | if (read_size > 0 || write_size > 0) |
| 133 | SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) & ~SCM_BUF0); |
| 134 | else |
| 135 | SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) | SCM_BUF0); |
| 136 | } |
| 137 | #undef FUNC_NAME |
| 138 | |
| 139 | SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0, |
| 140 | (SCM port, SCM mode, SCM size), |
| 141 | "Set the buffering mode for @var{port}. @var{mode} can be:\n" |
| 142 | "@table @code\n" |
| 143 | "@item _IONBF\n" |
| 144 | "non-buffered\n" |
| 145 | "@item _IOLBF\n" |
| 146 | "line buffered\n" |
| 147 | "@item _IOFBF\n" |
| 148 | "block buffered, using a newly allocated buffer of @var{size} bytes.\n" |
| 149 | "If @var{size} is omitted, a default size will be used.\n" |
| 150 | "@end table\n\n" |
| 151 | "Only certain types of ports are supported, most importantly\n" |
| 152 | "file ports.") |
| 153 | #define FUNC_NAME s_scm_setvbuf |
| 154 | { |
| 155 | int cmode; |
| 156 | long csize; |
| 157 | size_t ndrained; |
| 158 | char *drained; |
| 159 | scm_t_port *pt; |
| 160 | scm_t_port_internal *pti; |
| 161 | |
| 162 | port = SCM_COERCE_OUTPORT (port); |
| 163 | |
| 164 | SCM_VALIDATE_OPENPORT (1, port); |
| 165 | pti = SCM_PORT_GET_INTERNAL (port); |
| 166 | |
| 167 | if (pti->setvbuf == NULL) |
| 168 | scm_wrong_type_arg_msg (FUNC_NAME, 1, port, |
| 169 | "port that supports 'setvbuf'"); |
| 170 | |
| 171 | cmode = scm_to_int (mode); |
| 172 | if (cmode != _IONBF && cmode != _IOFBF && cmode != _IOLBF) |
| 173 | scm_out_of_range (FUNC_NAME, mode); |
| 174 | |
| 175 | if (cmode == _IOLBF) |
| 176 | { |
| 177 | SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) | SCM_BUFLINE); |
| 178 | cmode = _IOFBF; |
| 179 | } |
| 180 | else |
| 181 | SCM_SET_CELL_WORD_0 (port, |
| 182 | SCM_CELL_WORD_0 (port) & ~(scm_t_bits) SCM_BUFLINE); |
| 183 | |
| 184 | if (SCM_UNBNDP (size)) |
| 185 | { |
| 186 | if (cmode == _IOFBF) |
| 187 | csize = -1; |
| 188 | else |
| 189 | csize = 0; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | csize = scm_to_int (size); |
| 194 | if (csize < 0 || (cmode == _IONBF && csize > 0)) |
| 195 | scm_out_of_range (FUNC_NAME, size); |
| 196 | } |
| 197 | |
| 198 | pt = SCM_PTAB_ENTRY (port); |
| 199 | |
| 200 | if (SCM_INPUT_PORT_P (port)) |
| 201 | { |
| 202 | /* Drain pending input from PORT. Don't use `scm_drain_input' since |
| 203 | it returns a string, whereas we want binary input here. */ |
| 204 | ndrained = pt->read_end - pt->read_pos; |
| 205 | if (pt->read_buf == pt->putback_buf) |
| 206 | ndrained += pt->saved_read_end - pt->saved_read_pos; |
| 207 | |
| 208 | if (ndrained > 0) |
| 209 | { |
| 210 | drained = scm_gc_malloc_pointerless (ndrained, "file port"); |
| 211 | scm_take_from_input_buffers (port, drained, ndrained); |
| 212 | } |
| 213 | } |
| 214 | else |
| 215 | ndrained = 0; |
| 216 | |
| 217 | if (SCM_OUTPUT_PORT_P (port)) |
| 218 | scm_flush (port); |
| 219 | |
| 220 | if (pt->read_buf == pt->putback_buf) |
| 221 | { |
| 222 | pt->read_buf = pt->saved_read_buf; |
| 223 | pt->read_pos = pt->saved_read_pos; |
| 224 | pt->read_end = pt->saved_read_end; |
| 225 | pt->read_buf_size = pt->saved_read_buf_size; |
| 226 | } |
| 227 | |
| 228 | pti->setvbuf (port, csize, csize); |
| 229 | |
| 230 | if (ndrained > 0) |
| 231 | /* Put DRAINED back to PORT. */ |
| 232 | scm_unget_bytes ((unsigned char *) drained, ndrained, port); |
| 233 | |
| 234 | return SCM_UNSPECIFIED; |
| 235 | } |
| 236 | #undef FUNC_NAME |
| 237 | |
| 238 | /* Move ports with the specified file descriptor to new descriptors, |
| 239 | * resetting the revealed count to 0. |
| 240 | */ |
| 241 | static void |
| 242 | scm_i_evict_port (void *closure, SCM port) |
| 243 | { |
| 244 | int fd = * (int*) closure; |
| 245 | |
| 246 | if (SCM_FPORTP (port)) |
| 247 | { |
| 248 | scm_t_port *p; |
| 249 | scm_t_fport *fp; |
| 250 | |
| 251 | /* XXX: In some cases, we can encounter a port with no associated ptab |
| 252 | entry. */ |
| 253 | p = SCM_PTAB_ENTRY (port); |
| 254 | fp = (p != NULL) ? (scm_t_fport *) p->stream : NULL; |
| 255 | |
| 256 | if ((fp != NULL) && (fp->fdes == fd)) |
| 257 | { |
| 258 | fp->fdes = dup (fd); |
| 259 | if (fp->fdes == -1) |
| 260 | scm_syserror ("scm_evict_ports"); |
| 261 | scm_set_port_revealed_x (port, scm_from_int (0)); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | scm_evict_ports (int fd) |
| 268 | { |
| 269 | scm_c_port_for_each (scm_i_evict_port, (void *) &fd); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | SCM_DEFINE (scm_file_port_p, "file-port?", 1, 0, 0, |
| 274 | (SCM obj), |
| 275 | "Determine whether @var{obj} is a port that is related to a file.") |
| 276 | #define FUNC_NAME s_scm_file_port_p |
| 277 | { |
| 278 | return scm_from_bool (SCM_FPORTP (obj)); |
| 279 | } |
| 280 | #undef FUNC_NAME |
| 281 | |
| 282 | |
| 283 | static SCM sys_file_port_name_canonicalization; |
| 284 | SCM_SYMBOL (sym_relative, "relative"); |
| 285 | SCM_SYMBOL (sym_absolute, "absolute"); |
| 286 | |
| 287 | static SCM |
| 288 | fport_canonicalize_filename (SCM filename) |
| 289 | { |
| 290 | SCM mode = scm_fluid_ref (sys_file_port_name_canonicalization); |
| 291 | |
| 292 | if (!scm_is_string (filename)) |
| 293 | { |
| 294 | return filename; |
| 295 | } |
| 296 | else if (scm_is_eq (mode, sym_relative)) |
| 297 | { |
| 298 | SCM path, rel; |
| 299 | |
| 300 | path = scm_variable_ref (scm_c_module_lookup (scm_the_root_module (), |
| 301 | "%load-path")); |
| 302 | rel = scm_i_relativize_path (filename, path); |
| 303 | |
| 304 | return scm_is_true (rel) ? rel : filename; |
| 305 | } |
| 306 | else if (scm_is_eq (mode, sym_absolute)) |
| 307 | { |
| 308 | char *str, *canon; |
| 309 | |
| 310 | str = scm_to_locale_string (filename); |
| 311 | canon = canonicalize_file_name (str); |
| 312 | free (str); |
| 313 | |
| 314 | return canon ? scm_take_locale_string (canon) : filename; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | return filename; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* scm_open_file_with_encoding |
| 323 | Return a new port open on a given file. |
| 324 | |
| 325 | The mode string must match the pattern: [rwa+]** which |
| 326 | is interpreted in the usual unix way. |
| 327 | |
| 328 | Unless binary mode is requested, the character encoding of the new |
| 329 | port is determined as follows: First, if GUESS_ENCODING is true, |
| 330 | 'file-encoding' is used to guess the encoding of the file. If |
| 331 | GUESS_ENCODING is false or if 'file-encoding' fails, ENCODING is used |
| 332 | unless it is also false. As a last resort, the default port encoding |
| 333 | is used. It is an error to pass a non-false GUESS_ENCODING or |
| 334 | ENCODING if binary mode is requested. |
| 335 | |
| 336 | Return the new port. */ |
| 337 | SCM |
| 338 | scm_open_file_with_encoding (SCM filename, SCM mode, |
| 339 | SCM guess_encoding, SCM encoding) |
| 340 | #define FUNC_NAME "open-file" |
| 341 | { |
| 342 | SCM port; |
| 343 | int fdes, flags = 0, binary = 0; |
| 344 | unsigned int retries; |
| 345 | char *file, *md, *ptr; |
| 346 | |
| 347 | if (SCM_UNLIKELY (!(scm_is_false (encoding) || scm_is_string (encoding)))) |
| 348 | scm_wrong_type_arg_msg (FUNC_NAME, 0, encoding, |
| 349 | "encoding to be string or false"); |
| 350 | |
| 351 | scm_dynwind_begin (0); |
| 352 | |
| 353 | file = scm_to_locale_string (filename); |
| 354 | scm_dynwind_free (file); |
| 355 | |
| 356 | md = scm_to_locale_string (mode); |
| 357 | scm_dynwind_free (md); |
| 358 | |
| 359 | switch (*md) |
| 360 | { |
| 361 | case 'r': |
| 362 | flags |= O_RDONLY; |
| 363 | break; |
| 364 | case 'w': |
| 365 | flags |= O_WRONLY | O_CREAT | O_TRUNC; |
| 366 | break; |
| 367 | case 'a': |
| 368 | flags |= O_WRONLY | O_CREAT | O_APPEND; |
| 369 | break; |
| 370 | default: |
| 371 | scm_out_of_range (FUNC_NAME, mode); |
| 372 | } |
| 373 | ptr = md + 1; |
| 374 | while (*ptr != '\0') |
| 375 | { |
| 376 | switch (*ptr) |
| 377 | { |
| 378 | case '+': |
| 379 | flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; |
| 380 | break; |
| 381 | case 'b': |
| 382 | binary = 1; |
| 383 | #if defined (O_BINARY) |
| 384 | flags |= O_BINARY; |
| 385 | #endif |
| 386 | break; |
| 387 | case '0': /* unbuffered: handled later. */ |
| 388 | case 'l': /* line buffered: handled during output. */ |
| 389 | break; |
| 390 | default: |
| 391 | scm_out_of_range (FUNC_NAME, mode); |
| 392 | } |
| 393 | ptr++; |
| 394 | } |
| 395 | |
| 396 | for (retries = 0, fdes = -1; |
| 397 | fdes < 0 && retries < 2; |
| 398 | retries++) |
| 399 | { |
| 400 | SCM_SYSCALL (fdes = open_or_open64 (file, flags, 0666)); |
| 401 | if (fdes == -1) |
| 402 | { |
| 403 | int en = errno; |
| 404 | |
| 405 | if (en == EMFILE && retries == 0) |
| 406 | /* Run the GC in case it collects open file ports that are no |
| 407 | longer referenced. */ |
| 408 | scm_i_gc (FUNC_NAME); |
| 409 | else |
| 410 | SCM_SYSERROR_MSG ("~A: ~S", |
| 411 | scm_cons (scm_strerror (scm_from_int (en)), |
| 412 | scm_cons (filename, SCM_EOL)), en); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* Create a port from this file descriptor. The port's encoding is initially |
| 417 | %default-port-encoding. */ |
| 418 | port = scm_i_fdes_to_port (fdes, scm_i_mode_bits (mode), |
| 419 | fport_canonicalize_filename (filename)); |
| 420 | |
| 421 | if (binary) |
| 422 | { |
| 423 | if (scm_is_true (encoding)) |
| 424 | scm_misc_error (FUNC_NAME, |
| 425 | "Encoding specified on a binary port", |
| 426 | scm_list_1 (encoding)); |
| 427 | if (scm_is_true (guess_encoding)) |
| 428 | scm_misc_error (FUNC_NAME, |
| 429 | "Request to guess encoding on a binary port", |
| 430 | SCM_EOL); |
| 431 | |
| 432 | /* Use the binary-friendly ISO-8859-1 encoding. */ |
| 433 | scm_i_set_port_encoding_x (port, NULL); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | char *enc = NULL; |
| 438 | |
| 439 | if (scm_is_true (guess_encoding)) |
| 440 | { |
| 441 | if (SCM_INPUT_PORT_P (port)) |
| 442 | enc = scm_i_scan_for_encoding (port); |
| 443 | else |
| 444 | scm_misc_error (FUNC_NAME, |
| 445 | "Request to guess encoding on an output-only port", |
| 446 | SCM_EOL); |
| 447 | } |
| 448 | |
| 449 | if (!enc && scm_is_true (encoding)) |
| 450 | { |
| 451 | char *buf = scm_to_latin1_string (encoding); |
| 452 | enc = scm_gc_strdup (buf, "encoding"); |
| 453 | free (buf); |
| 454 | } |
| 455 | |
| 456 | if (enc) |
| 457 | scm_i_set_port_encoding_x (port, enc); |
| 458 | } |
| 459 | |
| 460 | scm_dynwind_end (); |
| 461 | |
| 462 | return port; |
| 463 | } |
| 464 | #undef FUNC_NAME |
| 465 | |
| 466 | SCM |
| 467 | scm_open_file (SCM filename, SCM mode) |
| 468 | { |
| 469 | return scm_open_file_with_encoding (filename, mode, SCM_BOOL_F, SCM_BOOL_F); |
| 470 | } |
| 471 | |
| 472 | /* We can't define these using SCM_KEYWORD, because keywords have not |
| 473 | yet been initialized when scm_init_fports is called. */ |
| 474 | static SCM k_guess_encoding = SCM_UNDEFINED; |
| 475 | static SCM k_encoding = SCM_UNDEFINED; |
| 476 | |
| 477 | SCM_DEFINE (scm_i_open_file, "open-file", 2, 0, 1, |
| 478 | (SCM filename, SCM mode, SCM keyword_args), |
| 479 | "Open the file whose name is @var{filename}, and return a port\n" |
| 480 | "representing that file. The attributes of the port are\n" |
| 481 | "determined by the @var{mode} string. The way in which this is\n" |
| 482 | "interpreted is similar to C stdio. The first character must be\n" |
| 483 | "one of the following:\n" |
| 484 | "@table @samp\n" |
| 485 | "@item r\n" |
| 486 | "Open an existing file for input.\n" |
| 487 | "@item w\n" |
| 488 | "Open a file for output, creating it if it doesn't already exist\n" |
| 489 | "or removing its contents if it does.\n" |
| 490 | "@item a\n" |
| 491 | "Open a file for output, creating it if it doesn't already\n" |
| 492 | "exist. All writes to the port will go to the end of the file.\n" |
| 493 | "The \"append mode\" can be turned off while the port is in use\n" |
| 494 | "@pxref{Ports and File Descriptors, fcntl}\n" |
| 495 | "@end table\n" |
| 496 | "The following additional characters can be appended:\n" |
| 497 | "@table @samp\n" |
| 498 | "@item b\n" |
| 499 | "Open the underlying file in binary mode, if supported by the system.\n" |
| 500 | "Also, open the file using the binary-compatible character encoding\n" |
| 501 | "\"ISO-8859-1\", ignoring the default port encoding.\n" |
| 502 | "@item +\n" |
| 503 | "Open the port for both input and output. E.g., @code{r+}: open\n" |
| 504 | "an existing file for both input and output.\n" |
| 505 | "@item 0\n" |
| 506 | "Create an \"unbuffered\" port. In this case input and output\n" |
| 507 | "operations are passed directly to the underlying port\n" |
| 508 | "implementation without additional buffering. This is likely to\n" |
| 509 | "slow down I/O operations. The buffering mode can be changed\n" |
| 510 | "while a port is in use @pxref{Ports and File Descriptors,\n" |
| 511 | "setvbuf}\n" |
| 512 | "@item l\n" |
| 513 | "Add line-buffering to the port. The port output buffer will be\n" |
| 514 | "automatically flushed whenever a newline character is written.\n" |
| 515 | "@end table\n" |
| 516 | "In theory we could create read/write ports which were buffered\n" |
| 517 | "in one direction only. However this isn't included in the\n" |
| 518 | "current interfaces. If a file cannot be opened with the access\n" |
| 519 | "requested, @code{open-file} throws an exception.") |
| 520 | #define FUNC_NAME s_scm_i_open_file |
| 521 | { |
| 522 | SCM encoding = SCM_BOOL_F; |
| 523 | SCM guess_encoding = SCM_BOOL_F; |
| 524 | |
| 525 | scm_c_bind_keyword_arguments (FUNC_NAME, keyword_args, 0, |
| 526 | k_guess_encoding, &guess_encoding, |
| 527 | k_encoding, &encoding, |
| 528 | SCM_UNDEFINED); |
| 529 | |
| 530 | return scm_open_file_with_encoding (filename, mode, |
| 531 | guess_encoding, encoding); |
| 532 | } |
| 533 | #undef FUNC_NAME |
| 534 | |
| 535 | \f |
| 536 | /* Building Guile ports from a file descriptor. */ |
| 537 | |
| 538 | /* Build a Scheme port from an open file descriptor `fdes'. |
| 539 | MODE indicates whether FILE is open for reading or writing; it uses |
| 540 | the same notation as open-file's second argument. |
| 541 | NAME is a string to be used as the port's filename. |
| 542 | */ |
| 543 | SCM |
| 544 | scm_i_fdes_to_port (int fdes, long mode_bits, SCM name) |
| 545 | #define FUNC_NAME "scm_fdes_to_port" |
| 546 | { |
| 547 | SCM port; |
| 548 | scm_t_port *pt; |
| 549 | scm_t_port_internal *pti; |
| 550 | |
| 551 | /* Test that fdes is valid. */ |
| 552 | #ifdef F_GETFL |
| 553 | int flags = fcntl (fdes, F_GETFL, 0); |
| 554 | if (flags == -1) |
| 555 | SCM_SYSERROR; |
| 556 | flags &= O_ACCMODE; |
| 557 | if (flags != O_RDWR |
| 558 | && ((flags != O_WRONLY && (mode_bits & SCM_WRTNG)) |
| 559 | || (flags != O_RDONLY && (mode_bits & SCM_RDNG)))) |
| 560 | { |
| 561 | SCM_MISC_ERROR ("requested file mode not available on fdes", SCM_EOL); |
| 562 | } |
| 563 | #else |
| 564 | /* If we don't have F_GETFL, as on mingw, at least we can test that |
| 565 | it is a valid file descriptor. */ |
| 566 | struct stat st; |
| 567 | if (fstat (fdes, &st) != 0) |
| 568 | SCM_SYSERROR; |
| 569 | #endif |
| 570 | |
| 571 | scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex); |
| 572 | |
| 573 | port = scm_new_port_table_entry (scm_tc16_fport); |
| 574 | SCM_SET_CELL_TYPE(port, scm_tc16_fport | mode_bits); |
| 575 | pt = SCM_PTAB_ENTRY (port); |
| 576 | |
| 577 | /* File ports support 'setvbuf'. */ |
| 578 | pti = SCM_PORT_GET_INTERNAL (port); |
| 579 | pti->setvbuf = scm_fport_buffer_add; |
| 580 | |
| 581 | { |
| 582 | scm_t_fport *fp |
| 583 | = (scm_t_fport *) scm_gc_malloc_pointerless (sizeof (scm_t_fport), |
| 584 | "file port"); |
| 585 | |
| 586 | fp->fdes = fdes; |
| 587 | pt->rw_random = SCM_FDES_RANDOM_P (fdes); |
| 588 | SCM_SETSTREAM (port, fp); |
| 589 | if (mode_bits & SCM_BUF0) |
| 590 | scm_fport_buffer_add (port, 0, 0); |
| 591 | else |
| 592 | scm_fport_buffer_add (port, -1, -1); |
| 593 | } |
| 594 | SCM_SET_FILENAME (port, name); |
| 595 | scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex); |
| 596 | return port; |
| 597 | } |
| 598 | #undef FUNC_NAME |
| 599 | |
| 600 | SCM |
| 601 | scm_fdes_to_port (int fdes, char *mode, SCM name) |
| 602 | { |
| 603 | return scm_i_fdes_to_port (fdes, scm_mode_bits (mode), name); |
| 604 | } |
| 605 | |
| 606 | /* Return a lower bound on the number of bytes available for input. */ |
| 607 | static int |
| 608 | fport_input_waiting (SCM port) |
| 609 | { |
| 610 | int fdes = SCM_FSTREAM (port)->fdes; |
| 611 | |
| 612 | struct pollfd pollfd = { fdes, POLLIN, 0 }; |
| 613 | |
| 614 | if (poll (&pollfd, 1, 0) < 0) |
| 615 | scm_syserror ("fport_input_waiting"); |
| 616 | |
| 617 | return pollfd.revents & POLLIN ? 1 : 0; |
| 618 | } |
| 619 | |
| 620 | \f |
| 621 | static int |
| 622 | fport_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) |
| 623 | { |
| 624 | scm_puts ("#<", port); |
| 625 | scm_print_port_mode (exp, port); |
| 626 | if (SCM_OPFPORTP (exp)) |
| 627 | { |
| 628 | int fdes; |
| 629 | SCM name = SCM_FILENAME (exp); |
| 630 | if (scm_is_string (name) || scm_is_symbol (name)) |
| 631 | scm_display (name, port); |
| 632 | else |
| 633 | scm_puts (SCM_PTOBNAME (SCM_PTOBNUM (exp)), port); |
| 634 | scm_putc (' ', port); |
| 635 | fdes = (SCM_FSTREAM (exp))->fdes; |
| 636 | |
| 637 | #if (defined HAVE_TTYNAME) && (defined HAVE_POSIX) |
| 638 | if (isatty (fdes)) |
| 639 | scm_display (scm_ttyname (exp), port); |
| 640 | else |
| 641 | #endif /* HAVE_TTYNAME */ |
| 642 | scm_intprint (fdes, 10, port); |
| 643 | } |
| 644 | else |
| 645 | { |
| 646 | scm_puts (SCM_PTOBNAME (SCM_PTOBNUM (exp)), port); |
| 647 | scm_putc (' ', port); |
| 648 | scm_uintprint ((scm_t_bits) SCM_PTAB_ENTRY (exp), 16, port); |
| 649 | } |
| 650 | scm_putc ('>', port); |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | static void fport_flush (SCM port); |
| 655 | |
| 656 | /* fill a port's read-buffer with a single read. returns the first |
| 657 | char or EOF if end of file. */ |
| 658 | static scm_t_wchar |
| 659 | fport_fill_input (SCM port) |
| 660 | { |
| 661 | long count; |
| 662 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
| 663 | scm_t_fport *fp = SCM_FSTREAM (port); |
| 664 | |
| 665 | SCM_SYSCALL (count = read (fp->fdes, pt->read_buf, pt->read_buf_size)); |
| 666 | if (count == -1) |
| 667 | scm_syserror ("fport_fill_input"); |
| 668 | if (count == 0) |
| 669 | return (scm_t_wchar) EOF; |
| 670 | else |
| 671 | { |
| 672 | pt->read_pos = pt->read_buf; |
| 673 | pt->read_end = pt->read_buf + count; |
| 674 | return *pt->read_buf; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | static scm_t_off |
| 679 | fport_seek (SCM port, scm_t_off offset, int whence) |
| 680 | { |
| 681 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
| 682 | scm_t_fport *fp = SCM_FSTREAM (port); |
| 683 | off_t_or_off64_t rv; |
| 684 | off_t_or_off64_t result; |
| 685 | |
| 686 | if (pt->rw_active == SCM_PORT_WRITE) |
| 687 | { |
| 688 | if (offset != 0 || whence != SEEK_CUR) |
| 689 | { |
| 690 | fport_flush (port); |
| 691 | result = rv = lseek_or_lseek64 (fp->fdes, offset, whence); |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | /* read current position without disturbing the buffer. */ |
| 696 | rv = lseek_or_lseek64 (fp->fdes, offset, whence); |
| 697 | result = rv + (pt->write_pos - pt->write_buf); |
| 698 | } |
| 699 | } |
| 700 | else if (pt->rw_active == SCM_PORT_READ) |
| 701 | { |
| 702 | if (offset != 0 || whence != SEEK_CUR) |
| 703 | { |
| 704 | /* could expand to avoid a second seek. */ |
| 705 | scm_end_input (port); |
| 706 | result = rv = lseek_or_lseek64 (fp->fdes, offset, whence); |
| 707 | } |
| 708 | else |
| 709 | { |
| 710 | /* read current position without disturbing the buffer |
| 711 | (particularly the unread-char buffer). */ |
| 712 | rv = lseek_or_lseek64 (fp->fdes, offset, whence); |
| 713 | result = rv - (pt->read_end - pt->read_pos); |
| 714 | |
| 715 | if (pt->read_buf == pt->putback_buf) |
| 716 | result -= pt->saved_read_end - pt->saved_read_pos; |
| 717 | } |
| 718 | } |
| 719 | else /* SCM_PORT_NEITHER */ |
| 720 | { |
| 721 | result = rv = lseek_or_lseek64 (fp->fdes, offset, whence); |
| 722 | } |
| 723 | |
| 724 | if (rv == -1) |
| 725 | scm_syserror ("fport_seek"); |
| 726 | |
| 727 | return result; |
| 728 | } |
| 729 | |
| 730 | static void |
| 731 | fport_truncate (SCM port, scm_t_off length) |
| 732 | { |
| 733 | scm_t_fport *fp = SCM_FSTREAM (port); |
| 734 | |
| 735 | if (ftruncate (fp->fdes, length) == -1) |
| 736 | scm_syserror ("ftruncate"); |
| 737 | } |
| 738 | |
| 739 | static void |
| 740 | fport_write (SCM port, const void *data, size_t size) |
| 741 | #define FUNC_NAME "fport_write" |
| 742 | { |
| 743 | /* this procedure tries to minimize the number of writes/flushes. */ |
| 744 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
| 745 | |
| 746 | if (pt->write_buf == &pt->shortbuf |
| 747 | || (pt->write_pos == pt->write_buf && size >= pt->write_buf_size)) |
| 748 | { |
| 749 | /* Unbuffered port, or port with empty buffer and data won't fit in |
| 750 | buffer. */ |
| 751 | if (full_write (SCM_FPORT_FDES (port), data, size) < size) |
| 752 | SCM_SYSERROR; |
| 753 | |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | { |
| 758 | scm_t_off space = pt->write_end - pt->write_pos; |
| 759 | |
| 760 | if (size <= space) |
| 761 | { |
| 762 | /* data fits in buffer. */ |
| 763 | memcpy (pt->write_pos, data, size); |
| 764 | pt->write_pos += size; |
| 765 | if (pt->write_pos == pt->write_end) |
| 766 | { |
| 767 | fport_flush (port); |
| 768 | /* we can skip the line-buffering check if nothing's buffered. */ |
| 769 | return; |
| 770 | } |
| 771 | } |
| 772 | else |
| 773 | { |
| 774 | memcpy (pt->write_pos, data, space); |
| 775 | pt->write_pos = pt->write_end; |
| 776 | fport_flush (port); |
| 777 | { |
| 778 | const void *ptr = ((const char *) data) + space; |
| 779 | size_t remaining = size - space; |
| 780 | |
| 781 | if (size >= pt->write_buf_size) |
| 782 | { |
| 783 | if (full_write (SCM_FPORT_FDES (port), ptr, remaining) |
| 784 | < remaining) |
| 785 | SCM_SYSERROR; |
| 786 | return; |
| 787 | } |
| 788 | else |
| 789 | { |
| 790 | memcpy (pt->write_pos, ptr, remaining); |
| 791 | pt->write_pos += remaining; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | /* handle line buffering. */ |
| 797 | if ((SCM_CELL_WORD_0 (port) & SCM_BUFLINE) && memchr (data, '\n', size)) |
| 798 | fport_flush (port); |
| 799 | } |
| 800 | } |
| 801 | #undef FUNC_NAME |
| 802 | |
| 803 | static void |
| 804 | fport_flush (SCM port) |
| 805 | { |
| 806 | size_t written; |
| 807 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
| 808 | scm_t_fport *fp = SCM_FSTREAM (port); |
| 809 | size_t count = pt->write_pos - pt->write_buf; |
| 810 | |
| 811 | written = full_write (fp->fdes, pt->write_buf, count); |
| 812 | if (written < count) |
| 813 | scm_syserror ("scm_flush"); |
| 814 | |
| 815 | pt->write_pos = pt->write_buf; |
| 816 | pt->rw_active = SCM_PORT_NEITHER; |
| 817 | } |
| 818 | |
| 819 | /* clear the read buffer and adjust the file position for unread bytes. */ |
| 820 | static void |
| 821 | fport_end_input (SCM port, int offset) |
| 822 | { |
| 823 | scm_t_fport *fp = SCM_FSTREAM (port); |
| 824 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
| 825 | |
| 826 | offset += pt->read_end - pt->read_pos; |
| 827 | |
| 828 | if (offset > 0) |
| 829 | { |
| 830 | pt->read_pos = pt->read_end; |
| 831 | /* will throw error if unread-char used at beginning of file |
| 832 | then attempting to write. seems correct. */ |
| 833 | if (lseek (fp->fdes, -offset, SEEK_CUR) == -1) |
| 834 | scm_syserror ("fport_end_input"); |
| 835 | } |
| 836 | pt->rw_active = SCM_PORT_NEITHER; |
| 837 | } |
| 838 | |
| 839 | static int |
| 840 | fport_close (SCM port) |
| 841 | { |
| 842 | scm_t_fport *fp = SCM_FSTREAM (port); |
| 843 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
| 844 | int rv; |
| 845 | |
| 846 | fport_flush (port); |
| 847 | SCM_SYSCALL (rv = close (fp->fdes)); |
| 848 | if (rv == -1 && errno != EBADF) |
| 849 | { |
| 850 | if (scm_gc_running_p) |
| 851 | /* silently ignore the error. scm_error would abort if we |
| 852 | called it now. */ |
| 853 | ; |
| 854 | else |
| 855 | scm_syserror ("fport_close"); |
| 856 | } |
| 857 | if (pt->read_buf == pt->putback_buf) |
| 858 | pt->read_buf = pt->saved_read_buf; |
| 859 | if (pt->read_buf != &pt->shortbuf) |
| 860 | scm_gc_free (pt->read_buf, pt->read_buf_size, "port buffer"); |
| 861 | if (pt->write_buf != &pt->shortbuf) |
| 862 | scm_gc_free (pt->write_buf, pt->write_buf_size, "port buffer"); |
| 863 | scm_gc_free (fp, sizeof (*fp), "file port"); |
| 864 | return rv; |
| 865 | } |
| 866 | |
| 867 | static size_t |
| 868 | fport_free (SCM port) |
| 869 | { |
| 870 | fport_close (port); |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static scm_t_bits |
| 875 | scm_make_fptob () |
| 876 | { |
| 877 | scm_t_bits tc = scm_make_port_type ("file", fport_fill_input, fport_write); |
| 878 | |
| 879 | scm_set_port_free (tc, fport_free); |
| 880 | scm_set_port_print (tc, fport_print); |
| 881 | scm_set_port_flush (tc, fport_flush); |
| 882 | scm_set_port_end_input (tc, fport_end_input); |
| 883 | scm_set_port_close (tc, fport_close); |
| 884 | scm_set_port_seek (tc, fport_seek); |
| 885 | scm_set_port_truncate (tc, fport_truncate); |
| 886 | scm_set_port_input_waiting (tc, fport_input_waiting); |
| 887 | |
| 888 | return tc; |
| 889 | } |
| 890 | |
| 891 | /* We can't initialize the keywords from 'scm_init_fports', because |
| 892 | keywords haven't yet been initialized at that point. */ |
| 893 | void |
| 894 | scm_init_fports_keywords () |
| 895 | { |
| 896 | k_guess_encoding = scm_from_latin1_keyword ("guess-encoding"); |
| 897 | k_encoding = scm_from_latin1_keyword ("encoding"); |
| 898 | } |
| 899 | |
| 900 | void |
| 901 | scm_init_fports () |
| 902 | { |
| 903 | scm_tc16_fport = scm_make_fptob (); |
| 904 | |
| 905 | scm_c_define ("_IOFBF", scm_from_int (_IOFBF)); |
| 906 | scm_c_define ("_IOLBF", scm_from_int (_IOLBF)); |
| 907 | scm_c_define ("_IONBF", scm_from_int (_IONBF)); |
| 908 | |
| 909 | sys_file_port_name_canonicalization = scm_make_fluid (); |
| 910 | scm_c_define ("%file-port-name-canonicalization", |
| 911 | sys_file_port_name_canonicalization); |
| 912 | |
| 913 | #include "libguile/fports.x" |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | Local Variables: |
| 918 | c-file-style: "gnu" |
| 919 | End: |
| 920 | */ |