Ruby: include/ruby/io.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

io.h
Go to the documentation of this file.
1#ifndef RUBY_IO_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RUBY_IO_H 1
13#include "ruby/internal/config.h"
14
15#include <stdio.h>
16#include "ruby/encoding.h"
17
18#if defined(HAVE_STDIO_EXT_H)
19#include <stdio_ext.h>
20#endif
21
22#include <errno.h>
23#if defined(HAVE_POLL)
24# ifdef _AIX
25# define reqevents events
26# define rtnevents revents
27# endif
28# include <poll.h>
29# ifdef _AIX
30# undef reqevents
31# undef rtnevents
32# undef events
33# undef revents
34# endif
35# define RB_WAITFD_IN POLLIN
36# define RB_WAITFD_PRI POLLPRI
37# define RB_WAITFD_OUT POLLOUT
38#else
39# define RB_WAITFD_IN 0x001
40# define RB_WAITFD_PRI 0x002
41# define RB_WAITFD_OUT 0x004
42#endif
43
44 typedef enum {
45 RUBY_IO_READABLE = RB_WAITFD_IN,
46 RUBY_IO_WRITABLE = RB_WAITFD_OUT,
47 RUBY_IO_PRIORITY = RB_WAITFD_PRI,
48} rb_io_event_t;
49
50#include "ruby/internal/dllexport.h"
51 RBIMPL_SYMBOL_EXPORT_BEGIN()
52
53 PACKED_STRUCT_UNALIGNED(struct rb_io_buffer_t {
54 char *ptr; /* off + len <= capa */
55 int off;
56 int len;
57 int capa;
58});
59 typedef struct rb_io_buffer_t rb_io_buffer_t;
60
61 typedef struct rb_io_t {
62 VALUE self;
63
64 FILE *stdio_file; /* stdio ptr for read/write if available */
65 int fd; /* file descriptor */
66 int mode; /* mode flags: FMODE_XXXs */
67 rb_pid_t pid; /* child's pid (for pipes) */
68 int lineno; /* number of lines read */
69 VALUE pathv; /* pathname for file */
70 void (*finalize)(struct rb_io_t*,int); /* finalize proc */
71
72 rb_io_buffer_t wbuf, rbuf;
73
74 VALUE tied_io_for_writing;
75
76 /*
77 * enc enc2 read action write action
78 * NULL NULL force_encoding(default_external) write the byte sequence of str
79 * e1 NULL force_encoding(e1) convert str.encoding to e1
80 * e1 e2 convert from e2 to e1 convert str.encoding to e2
81 */
82 struct rb_io_enc_t {
83 rb_encoding *enc;
84 rb_encoding *enc2;
85 int ecflags;
86 VALUE ecopts;
87 } encs;
88
89 rb_econv_t *readconv;
90 rb_io_buffer_t cbuf;
91
92 rb_econv_t *writeconv;
93 VALUE writeconv_asciicompat;
94 int writeconv_initialized;
95 int writeconv_pre_ecflags;
96 VALUE writeconv_pre_ecopts;
97
98 VALUE write_lock;
99} rb_io_t;
100
101 typedef struct rb_io_enc_t rb_io_enc_t;
102
103#define HAVE_RB_IO_T 1
104
105#define FMODE_READABLE 0x00000001
106#define FMODE_WRITABLE 0x00000002
107#define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE)
108#define FMODE_BINMODE 0x00000004
109#define FMODE_SYNC 0x00000008
110#define FMODE_TTY 0x00000010
111#define FMODE_DUPLEX 0x00000020
112#define FMODE_APPEND 0x00000040
113#define FMODE_CREATE 0x00000080
114/* #define FMODE_NOREVLOOKUP 0x00000100 */
115#define FMODE_EXCL 0x00000400
116#define FMODE_TRUNC 0x00000800
117#define FMODE_TEXTMODE 0x00001000
118/* #define FMODE_PREP 0x00010000 */
119#define FMODE_SETENC_BY_BOM 0x00100000
120/* #define FMODE_UNIX 0x00200000 */
121/* #define FMODE_INET 0x00400000 */
122/* #define FMODE_INET6 0x00800000 */
123
124#define RB_IO_POINTER(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
125#define GetOpenFile RB_IO_POINTER
126
127#define RB_IO_OPEN(obj, fp) do {\
128 (fp) = rb_io_make_open_file(obj);\
129} while (0)
130#define MakeOpenFile RB_IO_OPEN
131
132 rb_io_t *rb_io_make_open_file(VALUE obj);
133
134 FILE *rb_io_stdio_file(rb_io_t *fptr);
135
136 FILE *rb_fdopen(int, const char*);
137int rb_io_modestr_fmode(const char *modestr);
138int rb_io_modestr_oflags(const char *modestr);
139 CONSTFUNC(int rb_io_oflags_fmode(int oflags));
140void rb_io_check_writable(rb_io_t*);
141void rb_io_check_readable(rb_io_t*);
142void rb_io_check_char_readable(rb_io_t *fptr);
143void rb_io_check_byte_readable(rb_io_t *fptr);
144int rb_io_fptr_finalize(rb_io_t*);
145void rb_io_synchronized(rb_io_t*);
146void rb_io_check_initialized(rb_io_t*);
147void rb_io_check_closed(rb_io_t*);
148 VALUE rb_io_get_io(VALUE io);
149 VALUE rb_io_check_io(VALUE io);
150 VALUE rb_io_get_write_io(VALUE io);
151 VALUE rb_io_set_write_io(VALUE io, VALUE w);
152void rb_io_set_nonblock(rb_io_t *fptr);
153int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
154void rb_io_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash, int *oflags_p, int *fmode_p, rb_io_enc_t *convconfig_p);
155ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
156
157int rb_io_wait_readable(int fd);
158int rb_io_wait_writable(int fd);
159int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
160
161 VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout);
162
163/* compatibility for ruby 1.8 and older */
164#define rb_io_mode_flags(modestr) [<"rb_io_mode_flags() is obsolete; use rb_io_modestr_fmode()">]
165#define rb_io_modenum_flags(oflags) [<"rb_io_modenum_flags() is obsolete; use rb_io_oflags_fmode()">]
166
167 VALUE rb_io_taint_check(VALUE);
168 NORETURN(void rb_eof_error(void));
169
170void rb_io_read_check(rb_io_t*);
171int rb_io_read_pending(rb_io_t*);
172
173struct stat;
174 VALUE rb_stat_new(const struct stat *);
175
176/* gc.c */
177
178 RBIMPL_SYMBOL_EXPORT_END()
179
180#endif /* RUBY_IO_H */
CONSTFUNC
#define CONSTFUNC(x)
Definition: attributes.h:51
NORETURN
#define NORETURN(x)
Definition: attributes.h:152
PACKED_STRUCT_UNALIGNED
#define PACKED_STRUCT_UNALIGNED(x)
Definition: attributes.h:163
ptr
struct RIMemo * ptr
Definition: debug.c:88
dllexport.h
Tewaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
len
uint8_t len
Definition: escape.c:17
encoding.h
config.h
Thin wrapper to ruby/config.h.
rb_io_modestr_fmode
int rb_io_modestr_fmode(const char *modestr)
Definition: io.c:5580
rb_io_get_io
VALUE rb_io_get_io(VALUE io)
Definition: io.c:790
rb_io_modestr_oflags
int rb_io_modestr_oflags(const char *modestr)
Definition: io.c:5713
rb_io_check_byte_readable
void rb_io_check_byte_readable(rb_io_t *fptr)
Definition: io.c:947
rb_io_make_open_file
rb_io_t * rb_io_make_open_file(VALUE obj)
Definition: io.c:8278
RB_WAITFD_OUT
#define RB_WAITFD_OUT
Definition: io.h:41
rb_io_extract_encoding_option
int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
Definition: io.c:5862
rb_io_oflags_fmode
int rb_io_oflags_fmode(int oflags)
Definition: io.c:5637
rb_wait_for_single_fd
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: io.c:1384
rb_io_enc_t
struct rb_io_enc_t rb_io_enc_t
Definition: io.h:101
RB_WAITFD_PRI
#define RB_WAITFD_PRI
Definition: io.h:40
rb_io_taint_check
VALUE rb_io_taint_check(VALUE)
Definition: io.c:760
rb_io_check_closed
void rb_io_check_closed(rb_io_t *)
Definition: io.c:775
rb_io_check_writable
void rb_io_check_writable(rb_io_t *)
Definition: io.c:980
rb_io_stdio_file
FILE * rb_io_stdio_file(rb_io_t *fptr)
Definition: io.c:8229
rb_fdopen
FILE * rb_fdopen(int, const char *)
Definition: io.c:6183
rb_io_check_readable
void rb_io_check_readable(rb_io_t *)
Definition: io.c:956
rb_io_read_pending
int rb_io_read_pending(rb_io_t *)
Definition: io.c:992
RB_WAITFD_IN
#define RB_WAITFD_IN
Definition: io.h:39
rb_io_check_io
VALUE rb_io_check_io(VALUE io)
Definition: io.c:796
rb_io_bufwrite
ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size)
Definition: io.c:1698
rb_io_read_check
void rb_io_read_check(rb_io_t *)
Definition: io.c:1001
rb_io_check_char_readable
void rb_io_check_char_readable(rb_io_t *fptr)
Definition: io.c:928
rb_io_buffer_t
struct rb_io_buffer_t rb_io_buffer_t
Definition: io.h:59
rb_io_get_write_io
VALUE rb_io_get_write_io(VALUE io)
Definition: io.c:802
rb_io_set_nonblock
void rb_io_set_nonblock(rb_io_t *fptr)
Definition: io.c:2942
rb_io_wait_writable
int rb_io_wait_writable(int fd)
Definition: io.c:1341
rb_io_set_write_io
VALUE rb_io_set_write_io(VALUE io, VALUE w)
Definition: io.c:813
rb_stat_new
VALUE rb_stat_new(const struct stat *)
Definition: file.c:547
rb_io_synchronized
void rb_io_synchronized(rb_io_t *)
Definition: io.c:6486
rb_eof_error
void rb_eof_error(void)
Definition: io.c:754
rb_io_event_t
rb_io_event_t
Definition: io.h:44
RUBY_IO_READABLE
@ RUBY_IO_READABLE
Definition: io.h:45
RUBY_IO_PRIORITY
@ RUBY_IO_PRIORITY
Definition: io.h:47
RUBY_IO_WRITABLE
@ RUBY_IO_WRITABLE
Definition: io.h:46
rb_io_check_initialized
void rb_io_check_initialized(rb_io_t *)
Definition: io.c:767
rb_io_wait_readable
int rb_io_wait_readable(int fd)
Definition: io.c:1307
rb_io_wait
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Definition: io.c:1265
rb_io_fptr_finalize
#define rb_io_fptr_finalize(...)
Definition: internal.h:68
size
voidpf void uLong size
Definition: ioapi.h:138
int
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
buf
voidpf void * buf
Definition: ioapi.h:138
rb_io_extract_modeenc
#define rb_io_extract_modeenc
Definition: stringio.c:36
__sFILE
Definition: vsnprintf.c:169
OnigEncodingTypeST
Definition: onigmo.h:160
rb_econv_t
Definition: transcode.c:120
rb_io_t::rb_io_enc_t
Definition: io.h:82
rb_io_t::rb_io_enc_t::ecflags
int ecflags
Definition: io.h:85
rb_io_t::rb_io_enc_t::enc
rb_encoding * enc
Definition: io.h:83
rb_io_t::rb_io_enc_t::enc2
rb_encoding * enc2
Definition: io.h:84
rb_io_t::rb_io_enc_t::ecopts
VALUE ecopts
Definition: io.h:86
rb_io_t
Definition: io.h:61
rb_io_t::fd
int fd
Definition: io.h:65
rb_io_t::encs
struct rb_io_t::rb_io_enc_t encs
rb_io_t::lineno
int lineno
Definition: io.h:68
rb_io_t::writeconv
rb_econv_t * writeconv
Definition: io.h:92
rb_io_t::pid
rb_pid_t pid
Definition: io.h:67
rb_io_t::wbuf
rb_io_buffer_t wbuf
Definition: io.h:72
rb_io_t::readconv
rb_econv_t * readconv
Definition: io.h:89
rb_io_t::writeconv_asciicompat
VALUE writeconv_asciicompat
Definition: io.h:93
rb_io_t::stdio_file
FILE * stdio_file
Definition: io.h:64
rb_io_t::writeconv_initialized
int writeconv_initialized
Definition: io.h:94
rb_io_t::pathv
VALUE pathv
Definition: io.h:69
rb_io_t::mode
int mode
Definition: io.h:66
rb_io_t::writeconv_pre_ecflags
int writeconv_pre_ecflags
Definition: io.h:95
rb_io_t::write_lock
VALUE write_lock
Definition: io.h:98
rb_io_t::cbuf
rb_io_buffer_t cbuf
Definition: io.h:90
rb_io_t::self
VALUE self
Definition: io.h:62
rb_io_t::writeconv_pre_ecopts
VALUE writeconv_pre_ecopts
Definition: io.h:96
rb_io_t::tied_io_for_writing
VALUE tied_io_for_writing
Definition: io.h:74
rb_io_t::finalize
void(* finalize)(struct rb_io_t *, int)
Definition: io.h:70
rb_io_t::rbuf
rb_io_buffer_t rbuf
Definition: io.h:72
timeval
Definition: missing.h:51
VALUE
unsigned long VALUE
Definition: value.h:38
stat
#define stat
Definition: win32.h:195