Ruby  1.9.3p551(2014-11-13revision48407)
io.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  rubyio.h -
4 
5  $Author: kosaki $
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 # include <poll.h>
33 # define RB_WAITFD_IN POLLIN
34 # define RB_WAITFD_PRI POLLPRI
35 # define RB_WAITFD_OUT POLLOUT
36 #else
37 # define RB_WAITFD_IN 0x001
38 # define RB_WAITFD_PRI 0x002
39 # define RB_WAITFD_OUT 0x004
40 #endif
41 
42 #if defined __GNUC__ && __GNUC__ >= 4
43 #pragma GCC visibility push(default)
44 #endif
45 
46 typedef struct {
47  char *ptr; /* off + len <= capa */
48  int off;
49  int len;
50  int capa;
52 
53 typedef struct rb_io_t {
54  int fd; /* file descriptor */
55  FILE *stdio_file; /* stdio ptr for read/write if available */
56  int mode; /* mode flags: FMODE_XXXs */
57  rb_pid_t pid; /* child's pid (for pipes) */
58  int lineno; /* number of lines read */
59  VALUE pathv; /* pathname for file */
60  void (*finalize)(struct rb_io_t*,int); /* finalize proc */
61 
63 
65 
66  /*
67  * enc enc2 read action write action
68  * NULL NULL force_encoding(default_external) write the byte sequence of str
69  * e1 NULL force_encoding(e1) convert str.encoding to e1
70  * e1 e2 convert from e2 to e1 convert str.encoding to e2
71  */
72  struct rb_io_enc_t {
75  int ecflags;
77  } encs;
78 
81 
87 
89 } rb_io_t;
90 
91 #define HAVE_RB_IO_T 1
92 
93 #define FMODE_READABLE 0x00000001
94 #define FMODE_WRITABLE 0x00000002
95 #define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE)
96 #define FMODE_BINMODE 0x00000004
97 #define FMODE_SYNC 0x00000008
98 #define FMODE_TTY 0x00000010
99 #define FMODE_DUPLEX 0x00000020
100 #define FMODE_APPEND 0x00000040
101 #define FMODE_CREATE 0x00000080
102 /* #define FMODE_NOREVLOOKUP 0x00000100 */
103 #define FMODE_WSPLIT 0x00000200
104 #define FMODE_WSPLIT_INITIALIZED 0x00000400
105 #define FMODE_TRUNC 0x00000800
106 #define FMODE_TEXTMODE 0x00001000
107 /* #define FMODE_PREP 0x00010000 */
108 #define FMODE_SETENC_BY_BOM 0x00100000
109 
110 #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
111 
112 #define RB_IO_BUFFER_INIT(buf) do {\
113  (buf).ptr = NULL;\
114  (buf).off = 0;\
115  (buf).len = 0;\
116  (buf).capa = 0;\
117 } while (0)
118 
119 #define MakeOpenFile(obj, fp) do {\
120  if (RFILE(obj)->fptr) {\
121  rb_io_close(obj);\
122  rb_io_fptr_finalize(RFILE(obj)->fptr);\
123  RFILE(obj)->fptr = 0;\
124  }\
125  (fp) = 0;\
126  RB_IO_FPTR_NEW(fp);\
127  RFILE(obj)->fptr = (fp);\
128 } while (0)
129 
130 #define RB_IO_FPTR_NEW(fp) do {\
131  (fp) = ALLOC(rb_io_t);\
132  (fp)->fd = -1;\
133  (fp)->stdio_file = NULL;\
134  (fp)->mode = 0;\
135  (fp)->pid = 0;\
136  (fp)->lineno = 0;\
137  (fp)->pathv = Qnil;\
138  (fp)->finalize = 0;\
139  RB_IO_BUFFER_INIT((fp)->wbuf);\
140  RB_IO_BUFFER_INIT((fp)->rbuf);\
141  RB_IO_BUFFER_INIT((fp)->cbuf);\
142  (fp)->readconv = NULL;\
143  (fp)->writeconv = NULL;\
144  (fp)->writeconv_asciicompat = Qnil;\
145  (fp)->writeconv_pre_ecflags = 0;\
146  (fp)->writeconv_pre_ecopts = Qnil;\
147  (fp)->writeconv_initialized = 0;\
148  (fp)->tied_io_for_writing = 0;\
149  (fp)->encs.enc = NULL;\
150  (fp)->encs.enc2 = NULL;\
151  (fp)->encs.ecflags = 0;\
152  (fp)->encs.ecopts = Qnil;\
153  (fp)->write_lock = 0;\
154 } while (0)
155 
157 
158 FILE *rb_fdopen(int, const char*);
159 int rb_io_modestr_fmode(const char *modestr);
160 int rb_io_modestr_oflags(const char *modestr);
161 int rb_io_oflags_fmode(int oflags);
173 int rb_io_wait_readable(int);
174 int rb_io_wait_writable(int);
175 int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
176 void rb_io_set_nonblock(rb_io_t *fptr);
177 int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
178 ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
179 
180 /* compatibility for ruby 1.8 and older */
181 #define rb_io_mode_flags(modestr) rb_io_modestr_fmode(modestr)
182 #define rb_io_modenum_flags(oflags) rb_io_oflags_fmode(oflags)
183 
185 NORETURN(void rb_eof_error(void));
186 
190 
191 #if defined __GNUC__ && __GNUC__ >= 4
192 #pragma GCC visibility pop
193 #endif
194 
195 #if defined(__cplusplus)
196 #if 0
197 { /* satisfy cc-mode */
198 #endif
199 } /* extern "C" { */
200 #endif
201 
202 #endif /* RUBY_IO_H */
203