2020-04-10 01:11:40 -04:00
|
|
|
#ifndef RUBY_IO_H /*-*-C++-*-vi:se ft=cpp:*/
|
2007-06-09 23:06:15 -04:00
|
|
|
#define RUBY_IO_H 1
|
2020-04-10 01:11:40 -04:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @author $Author$
|
|
|
|
* @date Fri Nov 12 16:47:09 JST 1993
|
|
|
|
* @copyright Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
* @copyright This file is a part of the programming language Ruby.
|
|
|
|
* Permission is hereby granted, to either redistribute and/or
|
|
|
|
* modify this file, provided that the conditions mentioned in the
|
|
|
|
* file COPYING are met. Consult the file for details.
|
|
|
|
*/
|
2020-05-08 05:31:09 -04:00
|
|
|
#include "ruby/internal/config.h"
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
#include "ruby/encoding.h"
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2004-11-19 11:59:11 -05:00
|
|
|
#if defined(HAVE_STDIO_EXT_H)
|
|
|
|
#include <stdio_ext.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-21 15:39:09 -05:00
|
|
|
#include <errno.h>
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** @cond INTERNAL_MACRO */
|
2011-05-03 21:07:03 -04:00
|
|
|
#if defined(HAVE_POLL)
|
2013-03-28 10:10:21 -04:00
|
|
|
# ifdef _AIX
|
|
|
|
# define reqevents events
|
|
|
|
# define rtnevents revents
|
|
|
|
# endif
|
2011-05-03 21:07:03 -04:00
|
|
|
# include <poll.h>
|
2013-03-28 10:10:21 -04:00
|
|
|
# ifdef _AIX
|
|
|
|
# undef reqevents
|
|
|
|
# undef rtnevents
|
2013-03-28 21:24:14 -04:00
|
|
|
# undef events
|
|
|
|
# undef revents
|
2013-03-28 10:10:21 -04:00
|
|
|
# endif
|
2011-05-03 21:07:03 -04:00
|
|
|
# define RB_WAITFD_IN POLLIN
|
2022-01-18 22:16:56 -05:00
|
|
|
# if defined(POLLPRI)
|
2021-12-15 01:49:59 -05:00
|
|
|
# define RB_WAITFD_PRI POLLPRI
|
2022-01-18 22:16:56 -05:00
|
|
|
# else
|
|
|
|
# define RB_WAITFD_PRI 0
|
2021-12-15 01:49:59 -05:00
|
|
|
# endif
|
2011-05-03 21:07:03 -04:00
|
|
|
# define RB_WAITFD_OUT POLLOUT
|
|
|
|
#else
|
|
|
|
# define RB_WAITFD_IN 0x001
|
|
|
|
# define RB_WAITFD_PRI 0x002
|
|
|
|
# define RB_WAITFD_OUT 0x004
|
|
|
|
#endif
|
2021-07-07 23:48:32 -04:00
|
|
|
/** @endcond */
|
2011-05-03 20:59:57 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
#include "ruby/internal/attr/const.h"
|
|
|
|
#include "ruby/internal/attr/pure.h"
|
|
|
|
#include "ruby/internal/attr/noreturn.h"
|
2020-05-08 05:31:09 -04:00
|
|
|
#include "ruby/internal/dllexport.h"
|
2021-07-07 23:48:32 -04:00
|
|
|
#include "ruby/internal/value.h"
|
|
|
|
#include "ruby/backward/2/attributes.h" /* PACKED_STRUCT_UNALIGNED */
|
|
|
|
|
2022-06-18 02:03:51 -04:00
|
|
|
// IO#wait, IO#wait_readable, IO#wait_writable, IO#wait_priority are defined by this implementation.
|
|
|
|
#define RUBY_IO_WAIT_METHODS
|
|
|
|
|
2022-10-07 05:51:27 -04:00
|
|
|
// Used as the default timeout argument to `rb_io_wait` to use the `IO#timeout` value.
|
2022-10-07 21:02:34 -04:00
|
|
|
#define RUBY_IO_TIMEOUT_DEFAULT Qnil
|
2022-10-07 05:51:27 -04:00
|
|
|
|
2020-05-04 02:52:56 -04:00
|
|
|
RBIMPL_SYMBOL_EXPORT_BEGIN()
|
2010-07-21 17:38:25 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
struct stat;
|
|
|
|
struct timeval;
|
|
|
|
|
2022-10-07 04:48:38 -04:00
|
|
|
/**
|
|
|
|
* Indicates that a timeout has occurred while performing an IO operation.
|
|
|
|
*/
|
|
|
|
RUBY_EXTERN VALUE rb_eIOTimeoutError;
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Type of events that an IO can wait.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* This is visible from extension libraries because `io/wait` wants it.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
RUBY_IO_READABLE = RB_WAITFD_IN, /**< `IO::READABLE` */
|
|
|
|
RUBY_IO_WRITABLE = RB_WAITFD_OUT, /**< `IO::WRITABLE` */
|
|
|
|
RUBY_IO_PRIORITY = RB_WAITFD_PRI, /**< `IO::PRIORITY` */
|
|
|
|
} rb_io_event_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IO buffers. This is an implementation detail of ::rb_io_t::wbuf and
|
|
|
|
* ::rb_io_t::rbuf. People don't manipulate it directly.
|
|
|
|
*/
|
2014-07-24 03:03:38 -04:00
|
|
|
PACKED_STRUCT_UNALIGNED(struct rb_io_buffer_t {
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** Pointer to the underlying memory region, of at least `capa` bytes. */
|
2010-11-18 08:58:47 -05:00
|
|
|
char *ptr; /* off + len <= capa */
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** Offset inside of `ptr`. */
|
2010-11-18 08:58:47 -05:00
|
|
|
int off;
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** Length of the buffer. */
|
2010-11-18 08:58:47 -05:00
|
|
|
int len;
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** Designed capacity of the buffer. */
|
2010-11-18 08:58:47 -05:00
|
|
|
int capa;
|
2014-07-21 12:56:13 -04:00
|
|
|
});
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** @alias{rb_io_buffer_t} */
|
2014-07-21 12:56:13 -04:00
|
|
|
typedef struct rb_io_buffer_t rb_io_buffer_t;
|
2010-11-18 08:58:47 -05:00
|
|
|
|
2022-07-22 03:49:08 -04:00
|
|
|
/** Decomposed encoding flags (e.g. `"enc:enc2""`). */
|
|
|
|
/*
|
|
|
|
* enc enc2 read action write action
|
|
|
|
* NULL NULL force_encoding(default_external) write the byte sequence of str
|
|
|
|
* e1 NULL force_encoding(e1) convert str.encoding to e1
|
|
|
|
* e1 e2 convert from e2 to e1 convert str.encoding to e2
|
|
|
|
*/
|
|
|
|
struct rb_io_enc_t {
|
|
|
|
/** Internal encoding. */
|
|
|
|
rb_encoding *enc;
|
|
|
|
/** External encoding. */
|
|
|
|
rb_encoding *enc2;
|
|
|
|
/**
|
|
|
|
* Flags.
|
|
|
|
*
|
|
|
|
* @see enum ::ruby_econv_flag_type
|
|
|
|
*/
|
|
|
|
int ecflags;
|
|
|
|
/**
|
|
|
|
* Flags as Ruby hash.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* This is set. But used from nowhere maybe?
|
|
|
|
*/
|
|
|
|
VALUE ecopts;
|
|
|
|
};
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** Ruby's IO, metadata and buffers. */
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
typedef struct rb_io_t {
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** The IO's Ruby level counterpart. */
|
2020-08-20 08:53:08 -04:00
|
|
|
VALUE self;
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** stdio ptr for read/write, if available. */
|
|
|
|
FILE *stdio_file;
|
|
|
|
|
|
|
|
/** file descriptor. */
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/** mode flags: FMODE_XXXs */
|
|
|
|
int mode;
|
|
|
|
|
|
|
|
/** child's pid (for pipes) */
|
|
|
|
rb_pid_t pid;
|
2008-08-16 13:06:35 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** number of lines read */
|
|
|
|
int lineno;
|
2008-08-16 13:06:35 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** pathname for file */
|
|
|
|
VALUE pathv;
|
|
|
|
|
|
|
|
/** finalize proc */
|
|
|
|
void (*finalize)(struct rb_io_t*,int);
|
|
|
|
|
|
|
|
/** Write buffer. */
|
|
|
|
rb_io_buffer_t wbuf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (Byte) read buffer. Note also that there is a field called
|
|
|
|
* ::rb_io_t::cbuf, which also concerns read IO.
|
|
|
|
*/
|
|
|
|
rb_io_buffer_t rbuf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Duplex IO object, if set.
|
|
|
|
*
|
|
|
|
* @see rb_io_set_write_io()
|
|
|
|
*/
|
2007-11-19 22:16:53 -05:00
|
|
|
VALUE tied_io_for_writing;
|
2008-08-18 04:00:55 -04:00
|
|
|
|
2022-07-22 03:49:08 -04:00
|
|
|
struct rb_io_enc_t encs; /**< Decomposed encoding flags. */
|
2008-08-16 13:06:35 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** Encoding converter used when reading from this IO. */
|
2008-08-16 13:06:35 -04:00
|
|
|
rb_econv_t *readconv;
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* rb_io_ungetc() destination. This buffer is read before checking
|
|
|
|
* ::rb_io_t::rbuf
|
|
|
|
*/
|
2010-11-18 08:58:47 -05:00
|
|
|
rb_io_buffer_t cbuf;
|
2008-08-18 08:06:42 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** Encoding converter used when writing to this IO. */
|
2008-08-18 08:06:42 -04:00
|
|
|
rb_econv_t *writeconv;
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is, when set, an instance of ::rb_cString which holds the "common"
|
|
|
|
* encoding. Write conversion can convert strings twice... In case
|
|
|
|
* conversion from encoding X to encoding Y does not exist, Ruby finds an
|
|
|
|
* encoding Z that bridges the two, so that X to Z to Y conversion happens.
|
|
|
|
*/
|
2008-09-09 11:02:42 -04:00
|
|
|
VALUE writeconv_asciicompat;
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** Whether ::rb_io_t::writeconv is already set up. */
|
2014-07-20 00:18:58 -04:00
|
|
|
int writeconv_initialized;
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Value of ::rb_io_t::rb_io_enc_t::ecflags stored right before
|
|
|
|
* initialising ::rb_io_t::writeconv.
|
|
|
|
*/
|
2008-09-04 06:22:11 -04:00
|
|
|
int writeconv_pre_ecflags;
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Value of ::rb_io_t::rb_io_enc_t::ecopts stored right before initialising
|
|
|
|
* ::rb_io_t::writeconv.
|
|
|
|
*/
|
2008-09-03 14:18:10 -04:00
|
|
|
VALUE writeconv_pre_ecopts;
|
2008-08-18 08:06:42 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* This is a Ruby level mutex. It avoids multiple threads to write to an
|
|
|
|
* IO at once; helps for instance rb_io_puts() to ensure newlines right
|
|
|
|
* next to its arguments.
|
|
|
|
*
|
|
|
|
* This of course doesn't help inter-process IO interleaves, though.
|
|
|
|
*/
|
2008-11-07 15:47:02 -05:00
|
|
|
VALUE write_lock;
|
2022-10-07 04:48:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The timeout associated with this IO when performing blocking operations.
|
|
|
|
*/
|
|
|
|
VALUE timeout;
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
} rb_io_t;
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** @alias{rb_io_enc_t} */
|
2016-06-09 19:01:39 -04:00
|
|
|
typedef struct rb_io_enc_t rb_io_enc_t;
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* @deprecated This macro once was a thing in the old days, but makes no sense
|
|
|
|
* any longer today. Exists here for backwards compatibility
|
|
|
|
* only. You can safely forget about it.
|
|
|
|
*/
|
2007-02-24 20:16:58 -05:00
|
|
|
#define HAVE_RB_IO_T 1
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* @name Possible flags for ::rb_io_t::mode
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** The IO is opened for reading. */
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_READABLE 0x00000001
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** The IO is opened for writing. */
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_WRITABLE 0x00000002
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/** The IO is opened for both read/write. */
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE)
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IO is in "binary mode". This is not what everything rb_io_binmode()
|
|
|
|
* concerns. This low-level flag is to stop CR <-> CRLF conversions that would
|
|
|
|
* happen in the underlying operating system.
|
|
|
|
*
|
|
|
|
* Setting this one and #FMODE_TEXTMODE at the same time is a contradiction.
|
|
|
|
* Setting this one and #ECONV_NEWLINE_DECORATOR_MASK at the same time is also
|
|
|
|
* a contradiction.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_BINMODE 0x00000004
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IO is in "sync mode". All output is immediately flushed to the
|
|
|
|
* underlying operating system then. Can be set via rb_io_synchronized(), but
|
|
|
|
* there is no way except calling `IO#sync=` to reset.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_SYNC 0x00000008
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IO is a TTY. What is a TTY and what isn't depends on the underlying
|
|
|
|
* operating system's `isatty(3)` output. You cannot change this.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_TTY 0x00000010
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ruby eventually detects that the IO is bidirectional. For instance a TTY
|
|
|
|
* has such property. There are several other things known to be duplexed.
|
|
|
|
* Additionally you (extension library authors) can also implement your own
|
|
|
|
* bidirectional IO subclasses. One of such example is `Socket`.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_DUPLEX 0x00000020
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IO is opened for appending. This mode always writes at the end of the
|
|
|
|
* IO. Ruby manages this flag for record but basically the logic behind this
|
|
|
|
* mode is at the underlying operating system. We almost do nothing.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_APPEND 0x00000040
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IO is opened for creating. This makes sense only when the destination
|
|
|
|
* file does not exist at the time the IO object was created. This is the
|
|
|
|
* default mode for writing, but you can pass `"r+"` to `IO.open` etc., to
|
|
|
|
* reroute this creation.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_CREATE 0x00000080
|
|
|
|
/* #define FMODE_NOREVLOOKUP 0x00000100 */
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This flag amends the effect of #FMODE_CREATE, so that if there already is a
|
|
|
|
* file at the given path the operation fails. Using this you can be sure that
|
|
|
|
* the file you get is a fresh new one.
|
|
|
|
*/
|
2018-08-09 04:49:09 -04:00
|
|
|
#define FMODE_EXCL 0x00000400
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This flag amends the effect of #FMODE_CREATE, so that if there already is a
|
|
|
|
* file at the given path it gets truncated.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_TRUNC 0x00000800
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The IO is in "text mode". On systems where such mode make sense, this flag
|
|
|
|
* changes the way the IO handles the contents. On POSIX systems it is
|
|
|
|
* basically a no-op, but with this flag set you can optionally let Ruby
|
|
|
|
* manually convert newlines, unlike when in binary mode:
|
|
|
|
*
|
|
|
|
* ```ruby
|
|
|
|
* IO.open("/p/a/t/h", "wt", crlf_newline: true) # "wb" is NG.
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Setting this one and #FMODE_BINMODE at the same time is a contradiction.
|
|
|
|
*/
|
2008-08-23 04:41:02 -04:00
|
|
|
#define FMODE_TEXTMODE 0x00001000
|
|
|
|
/* #define FMODE_PREP 0x00010000 */
|
2021-07-07 23:48:32 -04:00
|
|
|
/* #define FMODE_SIGNAL_ON_EPIPE 0x00020000 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This flag amends the encoding of the IO so that the BOM of the contents of
|
|
|
|
* the IO takes effect.
|
|
|
|
*/
|
2009-08-21 02:17:06 -04:00
|
|
|
#define FMODE_SETENC_BY_BOM 0x00100000
|
2015-07-01 21:58:14 -04:00
|
|
|
/* #define FMODE_UNIX 0x00200000 */
|
|
|
|
/* #define FMODE_INET 0x00400000 */
|
|
|
|
/* #define FMODE_INET6 0x00800000 */
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries the underlying IO pointer.
|
|
|
|
*
|
|
|
|
* @param[in] obj An IO object.
|
|
|
|
* @param[out] fp A variable of type ::rb_io_t.
|
|
|
|
* @exception rb_eFrozenError `obj` is frozen.
|
|
|
|
* @exception rb_eIOError `obj` is closed.
|
|
|
|
* @post `fp` holds `obj`'s underlying IO.
|
|
|
|
*/
|
2020-08-19 21:48:29 -04:00
|
|
|
#define RB_IO_POINTER(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is an old name of #RB_IO_POINTER. Not sure if we want to deprecate
|
|
|
|
* this macro. There still are tons of usages out there in the wild.
|
|
|
|
*/
|
2020-08-19 21:48:29 -04:00
|
|
|
#define GetOpenFile RB_IO_POINTER
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Fills an IO object. This makes the best sense when called from inside of an
|
|
|
|
* `#initialize` method of a 3rd party extension library that inherits
|
|
|
|
* ::rb_cIO.
|
|
|
|
*
|
|
|
|
* If the passed IO is already opened for something it first closes that and
|
|
|
|
* opens a new one instead.
|
|
|
|
*
|
|
|
|
* @param[out] obj An IO object to fill in.
|
|
|
|
* @param[out] fp A variable of type ::rb_io_t.
|
|
|
|
* @exception rb_eTypeError `obj` is not ::RUBY_T_FILE.
|
|
|
|
* @post `fp` holds `obj`'s underlying IO.
|
|
|
|
*/
|
2020-08-19 21:48:29 -04:00
|
|
|
#define RB_IO_OPEN(obj, fp) do {\
|
2014-10-09 22:34:24 -04:00
|
|
|
(fp) = rb_io_make_open_file(obj);\
|
2010-11-18 08:58:47 -05:00
|
|
|
} while (0)
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is an old name of #RB_IO_OPEN. Not sure if we want to deprecate this
|
|
|
|
* macro. There still are usages out there in the wild.
|
|
|
|
*/
|
2020-08-19 21:48:29 -04:00
|
|
|
#define MakeOpenFile RB_IO_OPEN
|
2010-11-18 08:58:47 -05:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* This is an implementation detail of #RB_IO_OPEN. People don't use it
|
|
|
|
* directly.
|
|
|
|
*
|
|
|
|
* @param[out] obj An IO object to fill in.
|
|
|
|
* @exception rb_eTypeError `obj` is not ::RUBY_T_FILE.
|
|
|
|
* @return `obj`'s backend IO.
|
|
|
|
* @post `obj` is initialised.
|
|
|
|
*/
|
2014-10-09 22:34:24 -04:00
|
|
|
rb_io_t *rb_io_make_open_file(VALUE obj);
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Finds or creates a stdio's file structure from a Ruby's one. This can be
|
|
|
|
* handy if you want to call an external API that accepts `FILE *`.
|
|
|
|
*
|
|
|
|
* @note Note however, that `FILE`s can have their own buffer. Mixing Ruby's
|
|
|
|
* and stdio's file are basically dangerous. Use with care.
|
|
|
|
*
|
|
|
|
* @param[in,out] fptr Target IO.
|
|
|
|
* @return A stdio's file, created if absent.
|
|
|
|
* @post `fptr` has its corresponding stdio's file.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* We had rich support for `FILE` before! In the days of 1.8.x ::rb_io_t was
|
|
|
|
* like this:
|
|
|
|
*
|
|
|
|
* ```CXX
|
|
|
|
* typedef struct rb_io_t {
|
|
|
|
* FILE *f; // stdio ptr for read/write
|
|
|
|
* FILE *f2; // additional ptr for rw pipes
|
|
|
|
* int mode; // mode flags
|
|
|
|
* int pid; // child's pid (for pipes)
|
|
|
|
* int lineno; // number of lines read
|
|
|
|
* char *path; // pathname for file
|
|
|
|
* void (*finalize) _((struct rb_io_t*,int)); // finalize proc
|
|
|
|
* } rb_io_t;
|
|
|
|
*```
|
|
|
|
*
|
|
|
|
* But we eventually abandoned this layout. It was too difficult. We could
|
|
|
|
* not have fine-grained control over the `f` field.
|
|
|
|
*
|
|
|
|
* - `FILE` tends to be an opaque struct. It does not interface well with
|
|
|
|
* `select(2)` etc. This makes IO multiplexing quite hard. Using stdio,
|
|
|
|
* there is arguably no portable way to know if `fwrite(3)` blocks.
|
|
|
|
*
|
|
|
|
* - Nonblocking mode, which is another core concept that enables IO
|
|
|
|
* multiplexing, does not interface with stdio routines at all.
|
|
|
|
*
|
|
|
|
* - Detection of duplexed IO is also hard for the same reason.
|
|
|
|
*
|
|
|
|
* - `feof(3)` is not portable.
|
|
|
|
* https://mail.python.org/pipermail/python-dev/2001-January/011390.html
|
|
|
|
*
|
|
|
|
* - Solaris was a thing back then. They could not have more than 256 `FILE`
|
|
|
|
* structures at a time. Their file descriptors ware stored in an
|
|
|
|
* `unsigned char`.
|
|
|
|
*
|
|
|
|
* - It is next to impossible to avoid SEGV, especially when a thread tries to
|
|
|
|
* `ungetc(3)`-ing from a `FILE` which is `fread(3)`-ed by another one.
|
|
|
|
*
|
|
|
|
* In short, it is a bad idea to let someone else manage IO buffers, especially
|
|
|
|
* someone you cannot control. This still applies to extension libraries
|
|
|
|
* methinks. Ruby doesn't prevent you from shooting yourself in the foot, but
|
|
|
|
* consider yourself warned here.
|
|
|
|
*/
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
FILE *rb_io_stdio_file(rb_io_t *fptr);
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Identical to rb_io_stdio_file(), except it takes file descriptors instead of
|
|
|
|
* Ruby's IO. It can also be seen as a compatibility layer to wrap
|
|
|
|
* `fdopen(3)`. Nowadays all supporting systems, including Windows, have
|
|
|
|
* `fdopen`. Why not use them.
|
|
|
|
*
|
|
|
|
* @param[in] fd A file descriptor.
|
|
|
|
* @param[in] modestr C string, something like `"r+"`.
|
|
|
|
* @exception rb_eSystemCallError `fdopen` failed for some reason.
|
|
|
|
* @return A stdio's file associated with `fd`.
|
|
|
|
* @note Interpretation of `modestr` depends on the underlying operating
|
|
|
|
* system. On glibc you might be able to pass e.g. `"rm"`, but
|
|
|
|
* that's an extension to POSIX.
|
|
|
|
*/
|
|
|
|
FILE *rb_fdopen(int fd, const char *modestr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps a file mode string (that rb_file_open() takes) into a mixture of
|
|
|
|
* `FMODE_` flags. This for instance returns
|
|
|
|
* `FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE | FMODE_EXCL` for `"wx"`.
|
|
|
|
*
|
|
|
|
* @note You cannot pass this return value to OS provided `open(2)` etc.
|
|
|
|
*
|
|
|
|
* @param[in] modestr File mode, in C's string.
|
|
|
|
* @exception rb_eArgError `modestr` is broken.
|
|
|
|
* @return A set of flags.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* rb_io_modestr_fmode() is not a pure function because it raises.
|
|
|
|
*/
|
2008-09-05 07:30:35 -04:00
|
|
|
int rb_io_modestr_fmode(const char *modestr);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identical to rb_io_modestr_fmode(), except it returns a mixture of `O_`
|
2021-11-02 05:29:53 -04:00
|
|
|
* flags. This for instance returns `O_WRONLY | O_TRUNC | O_CREAT | O_EXCL` for
|
2021-07-07 23:48:32 -04:00
|
|
|
* `"wx"`.
|
|
|
|
*
|
|
|
|
* @param[in] modestr File mode, in C's string.
|
|
|
|
* @exception rb_eArgError `modestr` is broken.
|
|
|
|
* @return A set of flags.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* rb_io_modestr_oflags() is not a pure function because it raises.
|
|
|
|
*/
|
2008-09-05 07:30:35 -04:00
|
|
|
int rb_io_modestr_oflags(const char *modestr);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
RBIMPL_ATTR_CONST()
|
|
|
|
/**
|
|
|
|
* Converts an oflags (that rb_io_modestr_oflags() returns) to a fmode (that
|
|
|
|
* rb_io_mode_flags() returns). This is a purely functional operation.
|
|
|
|
*
|
|
|
|
* @param[in] oflags A set of `O_` flags.
|
|
|
|
* @return Corresponding set of `FMODE_` flags.
|
|
|
|
*/
|
|
|
|
int rb_io_oflags_fmode(int oflags);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that an IO is opened for writing.
|
|
|
|
*
|
|
|
|
* @param[in] fptr An IO you want to write to.
|
|
|
|
* @exception rb_eIOError `fptr` is not for writing.
|
|
|
|
* @post Upon successful return `fptr` is ready for writing.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* The parameter must have been `const rb_io_t *`.
|
|
|
|
*/
|
|
|
|
void rb_io_check_writable(rb_io_t *fptr);
|
|
|
|
|
|
|
|
/** @alias{rb_io_check_byte_readable} */
|
|
|
|
void rb_io_check_readable(rb_io_t *fptr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that an IO is opened for character-based reading. A character can
|
|
|
|
* be wider than a byte. Because of this we have to buffer reads from
|
|
|
|
* descriptors. This fiction checks if that is possible.
|
|
|
|
*
|
|
|
|
* @param[in] fptr An IO you want to read characters from.
|
|
|
|
* @exception rb_eIOError `fptr` is not for reading.
|
|
|
|
* @post Upon successful return `fptr` is ready for reading characters.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* Unlike rb_io_check_writable() the parameter cannot be `const rb_io_t *`.
|
|
|
|
* Behind the scene this operation flushes its write buffers. This is because
|
|
|
|
* of OpenSSL. They mandate this way.
|
|
|
|
*
|
|
|
|
* @see "Can I use OpenSSL's SSL library with non-blocking I/O?"
|
|
|
|
* https://www.openssl.org/docs/faq.html
|
|
|
|
*/
|
2011-01-14 19:42:14 -05:00
|
|
|
void rb_io_check_char_readable(rb_io_t *fptr);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that an IO is opened for byte-based reading. Byte-based and
|
|
|
|
* character-based reading operations cannot be mixed at a time.
|
|
|
|
*
|
|
|
|
* @param[in] fptr An IO you want to read characters from.
|
|
|
|
* @exception rb_eIOError `fptr` is not for reading.
|
|
|
|
* @post Upon successful return `fptr` is ready for reading bytes.
|
|
|
|
*/
|
2011-01-14 19:42:14 -05:00
|
|
|
void rb_io_check_byte_readable(rb_io_t *fptr);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys the given IO. Any pending operations are flushed.
|
|
|
|
*
|
|
|
|
* @note It makes no sense to call this function from anywhere outside of your
|
|
|
|
* class' ::rb_data_type_struct::dfree.
|
|
|
|
*
|
|
|
|
* @param[out] fptr IO to close.
|
|
|
|
* @post `fptr` is no longer a valid pointer.
|
|
|
|
*/
|
|
|
|
int rb_io_fptr_finalize(rb_io_t *fptr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets #FMODE_SYNC.
|
|
|
|
*
|
|
|
|
* @note There is no way for C extensions to undo this operation.
|
|
|
|
*
|
|
|
|
* @param[out] fptr IO to set the flag.
|
|
|
|
* @exception rb_eIOError `fptr` is not opened.
|
|
|
|
* @post `fptr` is in sync mode.
|
|
|
|
*/
|
|
|
|
void rb_io_synchronized(rb_io_t *fptr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that the passed IO is initialised.
|
|
|
|
*
|
|
|
|
* @param[in] fptr IO that you expect be initialised.
|
|
|
|
* @exception rb_eIOError `fptr` is not initialised.
|
|
|
|
* @post `fptr` is initialised.
|
|
|
|
*/
|
|
|
|
void rb_io_check_initialized(rb_io_t *fptr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This badly named function asserts that the passed IO is _open_.
|
|
|
|
*
|
|
|
|
* @param[in] fptr An IO
|
|
|
|
* @exception rb_eIOError `fptr` is closed.
|
|
|
|
* @post `fptr` is open.
|
|
|
|
*/
|
|
|
|
void rb_io_check_closed(rb_io_t *fptr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identical to rb_io_check_io(), except it raises exceptions on conversion
|
|
|
|
* failures.
|
|
|
|
*
|
|
|
|
* @param[in] io Target object.
|
|
|
|
* @exception rb_eTypeError No implicit conversion to IO.
|
|
|
|
* @return Return value of `obj.to_io`.
|
|
|
|
* @see rb_str_to_str
|
|
|
|
* @see rb_ary_to_ary
|
|
|
|
*/
|
2011-01-14 19:42:14 -05:00
|
|
|
VALUE rb_io_get_io(VALUE io);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try converting an object to its IO representation using its `to_io` method,
|
|
|
|
* if any. If there is no such thing, returns ::RUBY_Qnil.
|
|
|
|
*
|
|
|
|
* @param[in] io Arbitrary ruby object to convert.
|
|
|
|
* @exception rb_eTypeError `obj.to_io` returned something non-IO.
|
|
|
|
* @retval RUBY_Qnil No conversion from `obj` to IO defined.
|
|
|
|
* @retval otherwise Converted IO representation of `obj`.
|
|
|
|
* @see rb_check_array_type
|
|
|
|
* @see rb_check_string_type
|
|
|
|
* @see rb_check_hash_type
|
|
|
|
*/
|
2012-07-27 05:25:54 -04:00
|
|
|
VALUE rb_io_check_io(VALUE io);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries the tied IO for writing. An IO can be duplexed. Fine. The thing
|
|
|
|
* is, that characteristics could sometimes be achieved by the underlying
|
|
|
|
* operating system (for instance a socket's duplexity is by nature) but
|
|
|
|
* sometimes by us. Notable example is a bidirectional pipe. Suppose you
|
|
|
|
* have:
|
|
|
|
*
|
|
|
|
* ```ruby
|
|
|
|
* fp = IO.popen("-", "r+")
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This pipe is duplexed (the `"r+"`). You can both read from/write to it.
|
|
|
|
* However your operating system may or may not implement bidirectional pipes.
|
|
|
|
* FreeBSD is one of such operating systems known to have one; OTOH Linux is
|
|
|
|
* known to lack such things. So to achieve maximum portability, Ruby's
|
|
|
|
* bidirectional pipes are done purely in user land. A pipe in ruby can have
|
|
|
|
* multiple file descriptors; one for reading and the other for writing. This
|
|
|
|
* API is to obtain the IO port which corresponds to the passed one, for
|
|
|
|
* writing.
|
|
|
|
*
|
|
|
|
* @param[in] io An IO.
|
|
|
|
* @return Its tied IO for writing, if any, or `io` itself otherwise.
|
|
|
|
*/
|
2011-01-14 19:42:14 -05:00
|
|
|
VALUE rb_io_get_write_io(VALUE io);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns the tied IO for writing. See rb_io_get_write_io() for what a "tied
|
|
|
|
* IO for writing" is.
|
|
|
|
*
|
|
|
|
* @param[out] io An IO.
|
|
|
|
* @param[in] w Another IO.
|
|
|
|
* @retval RUBY_Qnil There was no tied IO for writing for `io`.
|
|
|
|
* @retval otherwise The IO formerly tied to `io`.
|
|
|
|
* @post `io` ties `w` for writing.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @shyouhei doesn't think there is any needs of this function for 3rd party
|
|
|
|
* extension libraries.
|
|
|
|
*/
|
2011-01-14 19:42:14 -05:00
|
|
|
VALUE rb_io_set_write_io(VALUE io, VALUE w);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
2022-04-21 01:54:00 -04:00
|
|
|
* Instructs the OS to put its internal file structure into "nonblocking mode".
|
|
|
|
* This is an in-Kernel concept. Reading from/writing to that file using C
|
|
|
|
* function calls would return -1 with errno set. However when it comes to a
|
|
|
|
* ruby program, we hide that error behind our `IO#read` method. Ruby level
|
|
|
|
* `IO#read` blocks regardless of this flag. If you want to avoid blocking,
|
|
|
|
* you should consider using methods like `IO#readpartial`.
|
|
|
|
*
|
|
|
|
* ```ruby
|
|
|
|
* require 'io/nonblock'
|
|
|
|
* STDIN.nonblock = true
|
|
|
|
* STDIN.gets # blocks.
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* As of writing there is a room of this API in Fiber schedulers. A Fiber
|
|
|
|
* scheduler could be written in a way its behaviour depends on this property.
|
|
|
|
* You need an in-depth understanding of how schedulers work to properly
|
|
|
|
* leverage this, though.
|
2021-07-07 23:48:32 -04:00
|
|
|
*
|
|
|
|
* @note Note however that nonblocking-ness propagates across process
|
|
|
|
* boundaries. You must really carefully watch your step when turning
|
|
|
|
* for instance `stderr` into nonblock mode (it tends to be shared
|
|
|
|
* across many processes). Also it is a complete disaster to mix a
|
|
|
|
* nonblocking file and stdio, and `stderr` tends to be under control of
|
|
|
|
* stdio in other processes.
|
|
|
|
*
|
|
|
|
* @param[out] fptr An IO that is to ne nonblocking.
|
|
|
|
* @post Descriptor that `fptr` describes is under nonblocking mode.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* There is `O_NONBLOCK` but not `FMODE_NONBLOCK`. You cannot atomically
|
|
|
|
* create a nonblocking file descriptor using our API.
|
|
|
|
*/
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
void rb_io_set_nonblock(rb_io_t *fptr);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
2021-07-02 06:41:16 -04:00
|
|
|
/**
|
|
|
|
* Returns an integer representing the numeric file descriptor for
|
|
|
|
* <em>io</em>.
|
|
|
|
*
|
|
|
|
* @param[in] io An IO.
|
|
|
|
* @retval int A file descriptor.
|
|
|
|
*/
|
|
|
|
int rb_io_descriptor(VALUE io);
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* This function breaks down the option hash that `IO#initialize` takes into
|
|
|
|
* components. This is an implementation detail of rb_io_extract_modeenc()
|
|
|
|
* today. People prefer that API instead.
|
|
|
|
*
|
|
|
|
* @param[in] opt The hash to decompose.
|
|
|
|
* @param[out] enc_p Return value buffer.
|
|
|
|
* @param[out] enc2_p Return value buffer.
|
|
|
|
* @param[out] fmode_p Return value buffer.
|
|
|
|
* @exception rb_eTypeError `opt` is broken.
|
|
|
|
* @exception rb_eArgError Specified encoding does not exist.
|
|
|
|
* @retval 1 Components got extracted.
|
|
|
|
* @retval 0 Otherwise.
|
|
|
|
* @post `enc_p` is the specified internal encoding.
|
|
|
|
* @post `enc2_p` is the specified external encoding.
|
|
|
|
* @post `fmode_p` is the specified set of `FMODE_` modes.
|
|
|
|
*/
|
2010-07-21 23:02:40 -04:00
|
|
|
int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function can be seen as an extended version of
|
|
|
|
* rb_io_extract_encoding_option() that not only concerns the option hash but
|
|
|
|
* also mode string and so on. This should be mixed with rb_scan_args() like:
|
|
|
|
*
|
|
|
|
* ```CXX
|
|
|
|
* // This method mimics File.new
|
|
|
|
* static VALUE
|
|
|
|
* your_method(int argc, const VALUE *argv, VALUE self)
|
|
|
|
* {
|
|
|
|
* VALUE f; // file name
|
|
|
|
* VALUE m; // open mode
|
|
|
|
* VALUE p; // permission (O_CREAT)
|
|
|
|
* VALUE k; // keywords
|
|
|
|
* rb_io_enc_t c; // converter
|
|
|
|
* int oflags;
|
|
|
|
* int fmode;
|
|
|
|
*
|
|
|
|
* int n = rb_scan_args(argc, argv, "12:", &f, &m, &p, &k);
|
|
|
|
* rb_io_extract_modeenc(&m, &p, k, &oflags, &fmode, &c);
|
|
|
|
*
|
|
|
|
* // Every local variables declared so far has been properly filled here.
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param[in,out] vmode_p Pointer to a mode object.
|
|
|
|
* @param[in,out] vperm_p Pointer to a permission object.
|
|
|
|
* @param[in] opthash Keyword arguments
|
|
|
|
* @param[out] oflags_p `O_` flags return buffer.
|
|
|
|
* @param[out] fmode_p `FMODE_` flags return buffer.
|
|
|
|
* @param[out] convconfig_p Encoding config return buffer.
|
|
|
|
* @exception rb_eTypeError Unexpected object (e.g. Time) passed.
|
|
|
|
* @exception rb_eArgError Contradiction inside of params.
|
|
|
|
* @post `*vmode_p` is a mode object (filled if any).
|
|
|
|
* @post `*vperm_p` is a permission object (filled if any).
|
|
|
|
* @post `*oflags_p` is filled with `O_` flags.
|
|
|
|
* @post `*fmode_p` is filled with `FMODE_` flags.
|
|
|
|
* @post `*convconfig_p` is filled with conversion instructions.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* ```rbs
|
|
|
|
* class File
|
|
|
|
* def initialize: (
|
|
|
|
* (String | int) path,
|
2021-11-02 05:29:53 -04:00
|
|
|
* ?(String | int) fmode,
|
|
|
|
* ?(String | int) perm,
|
2021-07-07 23:48:32 -04:00
|
|
|
* ?mode: (String | int),
|
|
|
|
* ?flags: int,
|
|
|
|
* ?external_encoding: (Encoding | String),
|
|
|
|
* ?internal_encoding: (Encoding | String),
|
|
|
|
* ?encoding: String,
|
|
|
|
* ?textmode: bool,
|
|
|
|
* ?binmode: bool,
|
|
|
|
* ?autoclose: bool,
|
|
|
|
* ?invalid: :replace,
|
|
|
|
* ?undef: :replace,
|
|
|
|
* ?replace: String,
|
|
|
|
* ?fallback: (Hash | Proc | Method),
|
|
|
|
* ?xml: (:text | :attr),
|
|
|
|
* ?crlf_newline: bool,
|
|
|
|
* ?cr_newline: bool,
|
|
|
|
* ?universal_newline: bool
|
|
|
|
* ) -> void
|
|
|
|
* ```
|
|
|
|
*/
|
2016-06-09 19:01:39 -04:00
|
|
|
void rb_io_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash, int *oflags_p, int *fmode_p, rb_io_enc_t *convconfig_p);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/* :TODO: can this function be __attribute__((warn_unused_result)) or not? */
|
|
|
|
/**
|
|
|
|
* Buffered write to the passed IO.
|
|
|
|
*
|
|
|
|
* @param[out] io Destination IO.
|
|
|
|
* @param[in] buf Contents to go to `io`.
|
|
|
|
* @param[in] size Number of bytes of `buf`.
|
|
|
|
* @exception rb_eFrozenError `io` is frozen.
|
|
|
|
* @exception rb_eIOError `io` is not open for writing.
|
|
|
|
* @exception rb_eSystemCallError `writev(2)` failed for some reason.
|
|
|
|
* @retval -1 Write failed.
|
|
|
|
* @retval otherwise Number of bytes actually written.
|
|
|
|
* @post `buf` is written to `io`.
|
|
|
|
* @note Partial write is a thing. It is a failure not to check the
|
|
|
|
* return value.
|
|
|
|
*/
|
2011-01-14 19:42:14 -05:00
|
|
|
ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 02:32:32 -04:00
|
|
|
|
2021-06-22 23:41:54 -04:00
|
|
|
//RBIMPL_ATTR_DEPRECATED(("use rb_io_maybe_wait_readable"))
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Blocks until the passed file descriptor gets readable.
|
|
|
|
*
|
|
|
|
* @deprecated We now prefer rb_io_maybe_wait_readable() over this one.
|
|
|
|
* @param[in] fd The file descriptor to wait.
|
|
|
|
* @exception rb_eIOError Bad file descriptor.
|
|
|
|
* @return 0 or 1 (meaning unclear).
|
|
|
|
* @post `fd` is ready for reading.
|
|
|
|
*/
|
2020-09-13 20:17:11 -04:00
|
|
|
int rb_io_wait_readable(int fd);
|
2021-06-18 21:47:16 -04:00
|
|
|
|
2021-06-22 23:41:54 -04:00
|
|
|
//RBIMPL_ATTR_DEPRECATED(("use rb_io_maybe_wait_writable"))
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Blocks until the passed file descriptor gets writable.
|
|
|
|
*
|
|
|
|
* @deprecated We now prefer rb_io_maybe_wait_writable() over this one.
|
|
|
|
* @param[in] fd The file descriptor to wait.
|
|
|
|
* @exception rb_eIOError Bad file descriptor.
|
|
|
|
* @return 0 or 1 (meaning unclear).
|
|
|
|
*/
|
2020-09-13 20:17:11 -04:00
|
|
|
int rb_io_wait_writable(int fd);
|
2021-06-18 21:47:16 -04:00
|
|
|
|
2021-06-22 23:41:54 -04:00
|
|
|
//RBIMPL_ATTR_DEPRECATED(("use rb_io_wait"))
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Blocks until the passed file descriptor is ready for the passed events.
|
|
|
|
*
|
|
|
|
* @deprecated We now prefer rb_io_maybe_wait() over this one.
|
|
|
|
* @param[in] fd The file descriptor to wait.
|
|
|
|
* @param[in] events A set of enum ::rb_io_event_t.
|
|
|
|
* @param[in,out] tv Timeout.
|
|
|
|
* @retval 0 Operation timed out.
|
|
|
|
* @retval -1 `select(2)` failed for some reason.
|
|
|
|
* @retval otherwise A set of enum ::rb_io_event_t.
|
|
|
|
* @note Depending on your operating system `tv` might or might not
|
|
|
|
* be updated (POSIX permits both). Portable programs must
|
|
|
|
* have no assumptions.
|
|
|
|
*/
|
2020-08-19 21:51:45 -04:00
|
|
|
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
|
|
|
|
|
2022-10-07 04:48:38 -04:00
|
|
|
/**
|
|
|
|
* Get the timeout associated with the specified io object.
|
|
|
|
*
|
|
|
|
* @param[in] io An IO object.
|
|
|
|
* @retval RUBY_Qnil There is no associated timeout.
|
|
|
|
* @retval Otherwise The timeout value.
|
|
|
|
*/
|
|
|
|
VALUE rb_io_timeout(VALUE io);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the timeout associated with the specified io object. This timeout is
|
|
|
|
* used as a best effort timeout to prevent operations from blocking forever.
|
|
|
|
*
|
|
|
|
* @param[in] io An IO object.
|
|
|
|
* @param[in] timeout A timeout value. Must respond to #to_f.
|
|
|
|
* @
|
|
|
|
*/
|
|
|
|
VALUE rb_io_set_timeout(VALUE io, VALUE timeout);
|
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Blocks until the passed IO is ready for the passed events. The "events"
|
|
|
|
* here is a Ruby level integer, which is an OR-ed value of `IO::READABLE`,
|
|
|
|
* `IO::WRITable`, and `IO::PRIORITY`.
|
|
|
|
*
|
2022-10-07 21:02:34 -04:00
|
|
|
* If timeout is `Qnil`, it will use the default timeout as given by
|
2022-10-07 04:48:38 -04:00
|
|
|
* `rb_io_timeout(io)`.
|
|
|
|
*
|
2021-07-07 23:48:32 -04:00
|
|
|
* @param[in] io An IO object to wait.
|
|
|
|
* @param[in] events See above.
|
|
|
|
* @param[in] timeout Time, or numeric seconds since UNIX epoch.
|
2022-10-07 21:02:34 -04:00
|
|
|
* If Qnil, use the default timeout. If Qfalse
|
|
|
|
* or Qundef, wait forever.
|
2021-07-07 23:48:32 -04:00
|
|
|
* @exception rb_eIOError `io` is not open.
|
|
|
|
* @exception rb_eRangeError `timeout` is out of range.
|
|
|
|
* @exception rb_eSystemCallError `select(2)` failed for some reason.
|
|
|
|
* @retval RUBY_Qfalse Operation timed out.
|
|
|
|
* @retval Otherwise Actual events reached.
|
|
|
|
*/
|
2020-08-19 21:51:45 -04:00
|
|
|
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identical to rb_io_wait() except it additionally takes previous errno. If
|
|
|
|
* the passed errno indicates for instance `EINTR`, this function returns
|
|
|
|
* immediately. This is expected to be called in a loop.
|
|
|
|
*
|
|
|
|
* ```CXX
|
|
|
|
* while (true) {
|
|
|
|
*
|
|
|
|
* ... // Your interesting operation here
|
|
|
|
* // `errno` could be updated
|
|
|
|
*
|
|
|
|
* rb_io_maybe_wait(errno, io, ev, Qnil);
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param[in] error System errno.
|
|
|
|
* @param[in] io An IO object to wait.
|
|
|
|
* @param[in] events An integer set of interests.
|
|
|
|
* @param[in] timeout Time, or numeric seconds since UNIX epoch.
|
|
|
|
* @exception rb_eIOError `io` is not open.
|
|
|
|
* @exception rb_eRangeError `timeout` is out of range.
|
|
|
|
* @exception rb_eSystemCallError `select(2)` failed for some reason.
|
|
|
|
* @retval RUBY_Qfalse Operation timed out.
|
|
|
|
* @retval Otherwise Actual events reached.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* This function to return ::RUBY_Qfalse on timeout could be unintended. It
|
|
|
|
* seems timeout feature has some rough edge.
|
|
|
|
*/
|
2021-05-08 08:13:47 -04:00
|
|
|
VALUE rb_io_maybe_wait(int error, VALUE io, VALUE events, VALUE timeout);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks until the passed IO is ready for reading, if that makes sense for the
|
|
|
|
* passed errno. This is a special case of rb_io_maybe_wait() that only
|
|
|
|
* concerns for reading.
|
|
|
|
*
|
|
|
|
* @param[in] error System errno.
|
|
|
|
* @param[in] io An IO object to wait.
|
|
|
|
* @param[in] timeout Time, or numeric seconds since UNIX epoch.
|
|
|
|
* @exception rb_eIOError `io` is not open.
|
|
|
|
* @exception rb_eRangeError `timeout` is out of range.
|
|
|
|
* @exception rb_eSystemCallError `select(2)` failed for some reason.
|
2022-10-07 04:48:38 -04:00
|
|
|
* @retval 0 Operation timed out.
|
|
|
|
* @retval Otherwise Always returns ::RUBY_IO_READABLE.
|
2021-07-07 23:48:32 -04:00
|
|
|
*/
|
2021-05-08 08:13:47 -04:00
|
|
|
int rb_io_maybe_wait_readable(int error, VALUE io, VALUE timeout);
|
2021-07-07 23:48:32 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks until the passed IO is ready for writing, if that makes sense for the
|
|
|
|
* passed errno. This is a special case of rb_io_maybe_wait() that only
|
|
|
|
* concernsfor writing.
|
|
|
|
*
|
|
|
|
* @param[in] error System errno.
|
|
|
|
* @param[in] io An IO object to wait.
|
|
|
|
* @param[in] timeout Time, or numeric seconds since UNIX epoch.
|
|
|
|
* @exception rb_eIOError `io` is not open.
|
|
|
|
* @exception rb_eRangeError `timeout` is out of range.
|
|
|
|
* @exception rb_eSystemCallError `select(2)` failed for some reason.
|
2022-10-07 04:48:38 -04:00
|
|
|
* @retval 0 Operation timed out.
|
|
|
|
* @retval Otherwise Always returns ::RUBY_IO_WRITABLE.
|
2021-07-07 23:48:32 -04:00
|
|
|
*/
|
2021-05-08 08:13:47 -04:00
|
|
|
int rb_io_maybe_wait_writable(int error, VALUE io, VALUE timeout);
|
2020-08-19 21:51:45 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/** @cond INTERNAL_MACRO */
|
2008-09-05 07:30:35 -04:00
|
|
|
/* compatibility for ruby 1.8 and older */
|
2014-10-09 22:27:41 -04:00
|
|
|
#define rb_io_mode_flags(modestr) [<"rb_io_mode_flags() is obsolete; use rb_io_modestr_fmode()">]
|
|
|
|
#define rb_io_modenum_flags(oflags) [<"rb_io_modenum_flags() is obsolete; use rb_io_oflags_fmode()">]
|
2021-07-07 23:48:32 -04:00
|
|
|
/** @endcond */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated This function once was a thing in the old days, but makes no
|
|
|
|
* sense any longer today. Exists here for backwards
|
|
|
|
* compatibility only. You can safely forget about it.
|
|
|
|
*
|
|
|
|
* @param[in] obj Object in question.
|
|
|
|
* @exception rb_eFrozenError obj is frozen.
|
|
|
|
* @return The passed `obj`
|
|
|
|
*/
|
|
|
|
VALUE rb_io_taint_check(VALUE obj);
|
2008-09-05 07:30:35 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
RBIMPL_ATTR_NORETURN()
|
|
|
|
/**
|
|
|
|
* Utility function to raise ::rb_eEOFError.
|
|
|
|
*
|
|
|
|
* @exception rb_eEOFError End of file situation.
|
|
|
|
* @note It never returns.
|
|
|
|
*/
|
|
|
|
void rb_eof_error(void);
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 02:32:32 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
/**
|
|
|
|
* Blocks until there is a pending read in the passed IO. If there already is
|
|
|
|
* it just returns.
|
|
|
|
*
|
|
|
|
* @param[out] fptr An IO to wait for reading.
|
|
|
|
* @post The are bytes to be read.
|
|
|
|
*/
|
|
|
|
void rb_io_read_check(rb_io_t *fptr);
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 02:32:32 -04:00
|
|
|
|
2021-07-07 23:48:32 -04:00
|
|
|
RBIMPL_ATTR_PURE()
|
|
|
|
/**
|
|
|
|
* Queries if the passed IO has any pending reads. Unlike rb_io_read_check()
|
|
|
|
* this doesn't block; has no side effects.
|
|
|
|
*
|
|
|
|
* @param[in] fptr An IO which can have pending reads.
|
|
|
|
* @retval 0 The IO is empty.
|
|
|
|
* @retval 1 There is something buffered.
|
|
|
|
*/
|
|
|
|
int rb_io_read_pending(rb_io_t *fptr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an instance of ::rb_cStat from the passed information.
|
|
|
|
*
|
|
|
|
* @param[in] st A stat.
|
|
|
|
* @return Allocated new instance of ::rb_cStat.
|
|
|
|
*/
|
|
|
|
VALUE rb_stat_new(const struct stat *st);
|
2013-10-01 07:54:53 -04:00
|
|
|
|
|
|
|
/* gc.c */
|
|
|
|
|
2020-05-04 02:52:56 -04:00
|
|
|
RBIMPL_SYMBOL_EXPORT_END()
|
2007-06-09 23:06:15 -04:00
|
|
|
|
|
|
|
#endif /* RUBY_IO_H */
|