Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

io.h
Go to the documentation of this file.
1 /**********************************************************************
2
3 rubyio.h -
4
5 $Author: nobu $
6 created at: Fri Nov 12 16:47:09 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10 **********************************************************************/
11
12 #ifndef RUBY_IO_H
13 #define RUBY_IO_H 1
14
15 #if defined(__cplusplus)
16 extern "C" {
17 #if 0
18 } /* satisfy cc-mode */
19 #endif
20 #endif
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include "ruby/encoding.h"
25
26 #if defined(HAVE_STDIO_EXT_H)
27 #include <stdio_ext.h>
28 #endif
29
30 #include "ruby/config.h"
31 #if defined(HAVE_POLL)
32 # ifdef _AIX
33 # define reqevents events
34 # define rtnevents revents
35 # endif
36 # include <poll.h>
37 # ifdef _AIX
38 # undef reqevents
39 # undef rtnevents
40 # undef events
41 # undef revents
42 # endif
43 # define RB_WAITFD_IN POLLIN
44 # define RB_WAITFD_PRI POLLPRI
45 # define RB_WAITFD_OUT POLLOUT
46 #else
47 # define RB_WAITFD_IN 0x001
48 # define RB_WAITFD_PRI 0x002
49 # define RB_WAITFD_OUT 0x004
50 #endif
51
52 RUBY_SYMBOL_EXPORT_BEGIN
53
54 typedef struct {
55 char *ptr; /* off + len <= capa */
56 int off;
57 int len;
58 int capa;
59 } rb_io_buffer_t;
60
61 typedef struct rb_io_t {
62 int fd; /* file descriptor */
63 FILE *stdio_file; /* stdio ptr for read/write if available */
64 int mode; /* mode flags: FMODE_XXXs */
65 rb_pid_t pid; /* child's pid (for pipes) */
66 int lineno; /* number of lines read */
67 VALUE pathv; /* pathname for file */
68 void (*finalize)(struct rb_io_t*,int); /* finalize proc */
69
70 rb_io_buffer_t wbuf, rbuf;
71
72 VALUE tied_io_for_writing;
73
74 /*
75 * enc enc2 read action write action
76 * NULL NULL force_encoding(default_external) write the byte sequence of str
77 * e1 NULL force_encoding(e1) convert str.encoding to e1
78 * e1 e2 convert from e2 to e1 convert str.encoding to e2
79 */
80 struct rb_io_enc_t {
81 rb_encoding *enc;
82 rb_encoding *enc2;
83 int ecflags;
84 VALUE ecopts;
85 } encs;
86
87 rb_econv_t *readconv;
88 rb_io_buffer_t cbuf;
89
90 rb_econv_t *writeconv;
91 VALUE writeconv_asciicompat;
92 int writeconv_pre_ecflags;
93 VALUE writeconv_pre_ecopts;
94 int writeconv_initialized;
95
96 VALUE write_lock;
97 } rb_io_t;
98
99 #define HAVE_RB_IO_T 1
100
101 #define FMODE_READABLE 0x00000001
102 #define FMODE_WRITABLE 0x00000002
103 #define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE)
104 #define FMODE_BINMODE 0x00000004
105 #define FMODE_SYNC 0x00000008
106 #define FMODE_TTY 0x00000010
107 #define FMODE_DUPLEX 0x00000020
108 #define FMODE_APPEND 0x00000040
109 #define FMODE_CREATE 0x00000080
110 /* #define FMODE_NOREVLOOKUP 0x00000100 */
111 #define FMODE_WSPLIT 0x00000200
112 #define FMODE_WSPLIT_INITIALIZED 0x00000400
113 #define FMODE_TRUNC 0x00000800
114 #define FMODE_TEXTMODE 0x00001000
115 /* #define FMODE_PREP 0x00010000 */
116 #define FMODE_SETENC_BY_BOM 0x00100000
117
118 #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
119
120 #define RB_IO_BUFFER_INIT(buf) do {\
121 (buf).ptr = NULL;\
122 (buf).off = 0;\
123 (buf).len = 0;\
124 (buf).capa = 0;\
125 } while (0)
126
127 #define MakeOpenFile(obj, fp) do {\
128 if (RFILE(obj)->fptr) {\
129 rb_io_close(obj);\
130 rb_io_fptr_finalize(RFILE(obj)->fptr);\
131 RFILE(obj)->fptr = 0;\
132 }\
133 (fp) = 0;\
134 RB_IO_FPTR_NEW(fp);\
135 RFILE(obj)->fptr = (fp);\
136 } while (0)
137
138 #define RB_IO_FPTR_NEW(fp) do {\
139 (fp) = ALLOC(rb_io_t);\
140 (fp)->fd = -1;\
141 (fp)->stdio_file = NULL;\
142 (fp)->mode = 0;\
143 (fp)->pid = 0;\
144 (fp)->lineno = 0;\
145 (fp)->pathv = Qnil;\
146 (fp)->finalize = 0;\
147 RB_IO_BUFFER_INIT((fp)->wbuf);\
148 RB_IO_BUFFER_INIT((fp)->rbuf);\
149 RB_IO_BUFFER_INIT((fp)->cbuf);\
150 (fp)->readconv = NULL;\
151 (fp)->writeconv = NULL;\
152 (fp)->writeconv_asciicompat = Qnil;\
153 (fp)->writeconv_pre_ecflags = 0;\
154 (fp)->writeconv_pre_ecopts = Qnil;\
155 (fp)->writeconv_initialized = 0;\
156 (fp)->tied_io_for_writing = 0;\
157 (fp)->encs.enc = NULL;\
158 (fp)->encs.enc2 = NULL;\
159 (fp)->encs.ecflags = 0;\
160 (fp)->encs.ecopts = Qnil;\
161 (fp)->write_lock = 0;\
162 } while (0)
163
164 FILE *rb_io_stdio_file(rb_io_t *fptr);
165
166 FILE *rb_fdopen(int, const char*);
167 int rb_io_modestr_fmode(const char *modestr);
168 int rb_io_modestr_oflags(const char *modestr);
169 int rb_io_oflags_fmode(int oflags);
170 void rb_io_check_writable(rb_io_t*);
171 void rb_io_check_readable(rb_io_t*);
172 void rb_io_check_char_readable(rb_io_t *fptr);
173 void rb_io_check_byte_readable(rb_io_t *fptr);
174 int rb_io_fptr_finalize(rb_io_t*);
175 void rb_io_synchronized(rb_io_t*);
176 void rb_io_check_initialized(rb_io_t*);
177 void rb_io_check_closed(rb_io_t*);
178 VALUE rb_io_get_io(VALUE io);
179 VALUE rb_io_check_io(VALUE io);
180 VALUE rb_io_get_write_io(VALUE io);
181 VALUE rb_io_set_write_io(VALUE io, VALUE w);
182 int rb_io_wait_readable(int);
183 int rb_io_wait_writable(int);
184 int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
185 void rb_io_set_nonblock(rb_io_t *fptr);
186 int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
187 ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
188
189 /* compatibility for ruby 1.8 and older */
190 #define rb_io_mode_flags(modestr) rb_io_modestr_fmode(modestr)
191 #define rb_io_modenum_flags(oflags) rb_io_oflags_fmode(oflags)
192
193 VALUE rb_io_taint_check(VALUE);
194 NORETURN(void rb_eof_error(void));
195
196 void rb_io_read_check(rb_io_t*);
197 int rb_io_read_pending(rb_io_t*);
198 DEPRECATED(void rb_read_check(FILE*));
199
200 struct stat;
201 VALUE rb_stat_new(const struct stat *);
202
203 /* gc.c */
204
205 RUBY_SYMBOL_EXPORT_END
206
207 #if defined(__cplusplus)
208 #if 0
209 { /* satisfy cc-mode */
210 #endif
211 } /* extern "C" { */
212 #endif
213
214 #endif /* RUBY_IO_H */
rb_io_check_readable
void rb_io_check_readable(rb_io_t *)
Definition: io.c:794
rb_io_set_nonblock
void rb_io_set_nonblock(rb_io_t *fptr)
Definition: io.c:2378
rb_io_synchronized
void rb_io_synchronized(rb_io_t *)
Definition: io.c:5638
rb_io_modestr_oflags
int rb_io_modestr_oflags(const char *modestr)
Definition: io.c:4931
rb_io_t
struct rb_io_t rb_io_t
rb_io_t::cbuf
rb_io_buffer_t cbuf
Definition: io.h:88
rb_io_t::rb_io_enc_t::ecopts
VALUE ecopts
Definition: io.h:84
rb_io_t
Definition: io.h:61
rb_io_check_initialized
void rb_io_check_initialized(rb_io_t *)
Definition: io.c:609
rb_io_bufwrite
ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size)
Definition: io.c:1365
rb_io_check_byte_readable
void rb_io_check_byte_readable(rb_io_t *fptr)
Definition: io.c:785
rb_io_buffer_t::ptr
char * ptr
Definition: io.h:55
rb_io_t::writeconv_pre_ecflags
int writeconv_pre_ecflags
Definition: io.h:92
rb_io_modestr_fmode
int rb_io_modestr_fmode(const char *modestr)
Definition: io.c:4812
rb_io_fptr_finalize
int rb_io_fptr_finalize(rb_io_t *)
Definition: io.c:4257
rb_io_t::writeconv_initialized
int writeconv_initialized
Definition: io.h:94
rb_io_taint_check
VALUE rb_io_taint_check(VALUE)
Definition: io.c:602
rb_io_t::writeconv_pre_ecopts
VALUE writeconv_pre_ecopts
Definition: io.h:93
rb_io_t::rb_io_enc_t::enc2
rb_encoding * enc2
Definition: io.h:82
rb_io_read_check
void rb_io_read_check(rb_io_t *)
Definition: io.c:847
rb_io_buffer_t
Definition: io.h:54
rb_io_buffer_t::capa
int capa
Definition: io.h:58
__sFILE
Definition: vsnprintf.c:175
rb_io_t::mode
int mode
Definition: io.h:64
rb_io_t::rb_io_enc_t::enc
rb_encoding * enc
Definition: io.h:81
rb_io_get_write_io
VALUE rb_io_get_write_io(VALUE io)
Definition: io.c:639
DEPRECATED
DEPRECATED(void rb_read_check(FILE *))
rb_io_check_io
VALUE rb_io_check_io(VALUE io)
Definition: io.c:633
rb_io_t::fd
int fd
Definition: io.h:62
rb_io_wait_writable
int rb_io_wait_writable(int)
Definition: io.c:1103
NORETURN
NORETURN(void rb_eof_error(void))
rb_io_t::encs
struct rb_io_t::rb_io_enc_t encs
rb_io_t::writeconv_asciicompat
VALUE writeconv_asciicompat
Definition: io.h:91
rb_io_t::pid
rb_pid_t pid
Definition: io.h:65
rb_io_buffer_t::len
int len
Definition: io.h:57
encoding.h
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:5076
rb_io_t::wbuf
rb_io_buffer_t wbuf
Definition: io.h:70
timeval
Definition: ossl_asn1.c:16
RUBY_SYMBOL_EXPORT_END
#define RUBY_SYMBOL_EXPORT_END
Definition: missing.h:39
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
rb_io_t::tied_io_for_writing
VALUE tied_io_for_writing
Definition: io.h:72
rb_io_t::rb_io_enc_t
Definition: io.h:80
VALUE
unsigned long VALUE
Definition: ruby.h:88
rb_io_get_io
VALUE rb_io_get_io(VALUE io)
Definition: io.c:627
RUBY_SYMBOL_EXPORT_BEGIN
#define RUBY_SYMBOL_EXPORT_BEGIN
Definition: missing.h:38
rb_wait_for_single_fd
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: thread.c:3775
rb_io_oflags_fmode
int rb_io_oflags_fmode(int oflags)
Definition: io.c:4861
rb_io_t::readconv
rb_econv_t * readconv
Definition: io.h:87
rb_io_t::write_lock
VALUE write_lock
Definition: io.h:96
rb_io_read_pending
int rb_io_read_pending(rb_io_t *)
Definition: io.c:830
rb_io_check_char_readable
void rb_io_check_char_readable(rb_io_t *fptr)
Definition: io.c:766
size
int size
Definition: encoding.c:49
rb_econv_t
Definition: transcode.c:111
rb_io_buffer_t::off
int off
Definition: io.h:56
rb_io_t::pathv
VALUE pathv
Definition: io.h:67
rb_io_t::finalize
void(* finalize)(struct rb_io_t *, int)
Definition: io.h:68
rb_fdopen
FILE * rb_fdopen(int, const char *)
Definition: io.c:5373
rb_io_t::rb_io_enc_t::ecflags
int ecflags
Definition: io.h:83
rb_io_stdio_file
FILE * rb_io_stdio_file(rb_io_t *fptr)
Definition: io.c:7247
rb_io_check_writable
void rb_io_check_writable(rb_io_t *)
Definition: io.c:818
rb_io_t::rbuf
rb_io_buffer_t rbuf
Definition: io.h:70
rb_stat_new
VALUE rb_stat_new(const struct stat *)
Definition: file.c:381
rb_io_t::stdio_file
FILE * stdio_file
Definition: io.h:63
rb_io_t::lineno
int lineno
Definition: io.h:66
stat
#define stat(path, st)
Definition: win32.h:213
rb_io_set_write_io
VALUE rb_io_set_write_io(VALUE io, VALUE w)
Definition: io.c:651
rb_io_wait_readable
int rb_io_wait_readable(int)
Definition: io.c:1077
rb_io_check_closed
void rb_io_check_closed(rb_io_t *)
Definition: io.c:617
rb_io_t::writeconv
rb_econv_t * writeconv
Definition: io.h:90
rb_read_check
void rb_read_check(FILE *fp)
Definition: io.c:839
OnigEncodingTypeST
Definition: oniguruma.h:158
rb_eof_error
void rb_eof_error(void)
Definition: io.c:596