Ruby  2.0.0p451(2014-02-24revision45167)
io.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  rubyio.h -
4 
5  $Author: nagachika $
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 #if defined __GNUC__ && __GNUC__ >= 4
53 #pragma GCC visibility push(default)
54 #endif
55 
56 typedef struct {
57  char *ptr; /* off + len <= capa */
58  int off;
59  int len;
60  int capa;
62 
63 typedef struct rb_io_t {
64  int fd; /* file descriptor */
65  FILE *stdio_file; /* stdio ptr for read/write if available */
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 
73 
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 {
85  int ecflags;
87  } encs;
88 
91 
97 
99 } rb_io_t;
100 
101 #define HAVE_RB_IO_T 1
102 
103 #define FMODE_READABLE 0x00000001
104 #define FMODE_WRITABLE 0x00000002
105 #define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE)
106 #define FMODE_BINMODE 0x00000004
107 #define FMODE_SYNC 0x00000008
108 #define FMODE_TTY 0x00000010
109 #define FMODE_DUPLEX 0x00000020
110 #define FMODE_APPEND 0x00000040
111 #define FMODE_CREATE 0x00000080
112 /* #define FMODE_NOREVLOOKUP 0x00000100 */
113 #define FMODE_WSPLIT 0x00000200
114 #define FMODE_WSPLIT_INITIALIZED 0x00000400
115 #define FMODE_TRUNC 0x00000800
116 #define FMODE_TEXTMODE 0x00001000
117 /* #define FMODE_PREP 0x00010000 */
118 #define FMODE_SETENC_BY_BOM 0x00100000
119 
120 #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
121 
122 #define RB_IO_BUFFER_INIT(buf) do {\
123  (buf).ptr = NULL;\
124  (buf).off = 0;\
125  (buf).len = 0;\
126  (buf).capa = 0;\
127 } while (0)
128 
129 #define MakeOpenFile(obj, fp) do {\
130  if (RFILE(obj)->fptr) {\
131  rb_io_close(obj);\
132  rb_io_fptr_finalize(RFILE(obj)->fptr);\
133  RFILE(obj)->fptr = 0;\
134  }\
135  (fp) = 0;\
136  RB_IO_FPTR_NEW(fp);\
137  RFILE(obj)->fptr = (fp);\
138 } while (0)
139 
140 #define RB_IO_FPTR_NEW(fp) do {\
141  (fp) = ALLOC(rb_io_t);\
142  (fp)->fd = -1;\
143  (fp)->stdio_file = NULL;\
144  (fp)->mode = 0;\
145  (fp)->pid = 0;\
146  (fp)->lineno = 0;\
147  (fp)->pathv = Qnil;\
148  (fp)->finalize = 0;\
149  RB_IO_BUFFER_INIT((fp)->wbuf);\
150  RB_IO_BUFFER_INIT((fp)->rbuf);\
151  RB_IO_BUFFER_INIT((fp)->cbuf);\
152  (fp)->readconv = NULL;\
153  (fp)->writeconv = NULL;\
154  (fp)->writeconv_asciicompat = Qnil;\
155  (fp)->writeconv_pre_ecflags = 0;\
156  (fp)->writeconv_pre_ecopts = Qnil;\
157  (fp)->writeconv_initialized = 0;\
158  (fp)->tied_io_for_writing = 0;\
159  (fp)->encs.enc = NULL;\
160  (fp)->encs.enc2 = NULL;\
161  (fp)->encs.ecflags = 0;\
162  (fp)->encs.ecopts = Qnil;\
163  (fp)->write_lock = 0;\
164 } while (0)
165 
167 
168 FILE *rb_fdopen(int, const char*);
169 int rb_io_modestr_fmode(const char *modestr);
170 int rb_io_modestr_oflags(const char *modestr);
171 int rb_io_oflags_fmode(int oflags);
184 int rb_io_wait_readable(int);
185 int rb_io_wait_writable(int);
186 int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
187 void rb_io_set_nonblock(rb_io_t *fptr);
188 int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
189 ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
190 
191 /* compatibility for ruby 1.8 and older */
192 #define rb_io_mode_flags(modestr) rb_io_modestr_fmode(modestr)
193 #define rb_io_modenum_flags(oflags) rb_io_oflags_fmode(oflags)
194 
196 NORETURN(void rb_eof_error(void));
197 
201 
202 #if defined __GNUC__ && __GNUC__ >= 4
203 #pragma GCC visibility pop
204 #endif
205 
206 #if defined(__cplusplus)
207 #if 0
208 { /* satisfy cc-mode */
209 #endif
210 } /* extern "C" { */
211 #endif
212 
213 #endif /* RUBY_IO_H */
void rb_io_check_readable(rb_io_t *)
Definition: io.c:781
void rb_io_set_nonblock(rb_io_t *fptr)
Definition: io.c:2338
unsigned long VALUE
Definition: ripper.y:104
void rb_io_synchronized(rb_io_t *)
Definition: io.c:5545
int rb_io_modestr_oflags(const char *modestr)
Definition: io.c:4846
struct rb_io_t rb_io_t
rb_io_buffer_t cbuf
Definition: io.h:90
VALUE ecopts
Definition: io.h:86
Definition: io.h:63
void rb_io_check_initialized(rb_io_t *)
Definition: io.c:596
ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size)
Definition: io.c:1352
void rb_io_check_byte_readable(rb_io_t *fptr)
Definition: io.c:772
char * ptr
Definition: io.h:57
int writeconv_pre_ecflags
Definition: io.h:94
int rb_io_modestr_fmode(const char *modestr)
Definition: io.c:4727
int rb_io_fptr_finalize(rb_io_t *)
Definition: io.c:4186
int writeconv_initialized
Definition: io.h:96
VALUE rb_io_taint_check(VALUE)
Definition: io.c:587
VALUE writeconv_pre_ecopts
Definition: io.h:95
rb_encoding * enc2
Definition: io.h:84
void rb_io_read_check(rb_io_t *)
Definition: io.c:834
int capa
Definition: io.h:60
int mode
Definition: io.h:66
rb_encoding * enc
Definition: io.h:83
VALUE rb_io_get_write_io(VALUE io)
Definition: io.c:626
DEPRECATED(void rb_read_check(FILE *))
VALUE rb_io_check_io(VALUE io)
Definition: io.c:620
int fd
Definition: io.h:64
int rb_io_wait_writable(int)
Definition: io.c:1090
NORETURN(void rb_eof_error(void))
struct rb_io_t::rb_io_enc_t encs
VALUE writeconv_asciicompat
Definition: io.h:93
rb_pid_t pid
Definition: io.h:67
int len
Definition: io.h:59
int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
Definition: io.c:4987
rb_io_buffer_t wbuf
Definition: io.h:72
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
VALUE tied_io_for_writing
Definition: io.h:74
VALUE rb_io_get_io(VALUE io)
Definition: io.c:614
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: thread.c:3689
int rb_io_oflags_fmode(int oflags)
Definition: io.c:4776
rb_econv_t * readconv
Definition: io.h:89
VALUE write_lock
Definition: io.h:98
int rb_io_read_pending(rb_io_t *)
Definition: io.c:817
void rb_io_check_char_readable(rb_io_t *fptr)
Definition: io.c:753
int size
Definition: encoding.c:52
int off
Definition: io.h:58
VALUE pathv
Definition: io.h:69
void(* finalize)(struct rb_io_t *, int)
Definition: io.h:70
FILE * rb_fdopen(int, const char *)
Definition: io.c:5280
FILE * rb_io_stdio_file(rb_io_t *fptr)
Definition: io.c:7155
void rb_io_check_writable(rb_io_t *)
Definition: io.c:805
rb_io_buffer_t rbuf
Definition: io.h:72
FILE * stdio_file
Definition: io.h:65
int lineno
Definition: io.h:68
VALUE rb_io_set_write_io(VALUE io, VALUE w)
Definition: io.c:638
int rb_io_wait_readable(int)
Definition: io.c:1064
void rb_io_check_closed(rb_io_t *)
Definition: io.c:604
rb_econv_t * writeconv
Definition: io.h:92
void rb_read_check(FILE *fp)
Definition: io.c:826
void rb_eof_error(void)
Definition: io.c:581