1 /**********************************************************************
2 
3   rubyio.h -
4 
5   $Author: odaira $
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 #ifdef RUBY_INTERNAL_H
16 #error "Include this file before internal.h"
17 #endif
18 
19 #if defined(__cplusplus)
20 extern "C" {
21 #if 0
22 } /* satisfy cc-mode */
23 #endif
24 #endif
25 
26 #include <stdio.h>
27 #include "ruby/encoding.h"
28 
29 #if defined(HAVE_STDIO_EXT_H)
30 #include <stdio_ext.h>
31 #endif
32 
33 #include "ruby/config.h"
34 #include <errno.h>
35 #if defined(HAVE_POLL)
36 #  ifdef _AIX
37 #    define reqevents events
38 #    define rtnevents revents
39 #  endif
40 #  include <poll.h>
41 #  ifdef _AIX
42 #    undef reqevents
43 #    undef rtnevents
44 #    undef events
45 #    undef revents
46 #  endif
47 #  define RB_WAITFD_IN  POLLIN
48 #  define RB_WAITFD_PRI POLLPRI
49 #  define RB_WAITFD_OUT POLLOUT
50 #else
51 #  define RB_WAITFD_IN  0x001
52 #  define RB_WAITFD_PRI 0x002
53 #  define RB_WAITFD_OUT 0x004
54 #endif
55 
56 RUBY_SYMBOL_EXPORT_BEGIN
57 
58 PACKED_STRUCT_UNALIGNED(struct rb_io_buffer_t {
59     char *ptr;                  /* off + len <= capa */
60     int off;
61     int len;
62     int capa;
63 });
64 typedef struct rb_io_buffer_t rb_io_buffer_t;
65 
66 typedef struct rb_io_t {
67     FILE *stdio_file;		/* stdio ptr for read/write if available */
68     int fd;                     /* file descriptor */
69     int mode;			/* mode flags: FMODE_XXXs */
70     rb_pid_t pid;		/* child's pid (for pipes) */
71     int lineno;			/* number of lines read */
72     VALUE pathv;		/* pathname for file */
73     void (*finalize)(struct rb_io_t*,int); /* finalize proc */
74 
75     rb_io_buffer_t wbuf, rbuf;
76 
77     VALUE tied_io_for_writing;
78 
79     /*
80      * enc  enc2 read action                      write action
81      * NULL NULL force_encoding(default_external) write the byte sequence of str
82      * e1   NULL force_encoding(e1)               convert str.encoding to e1
83      * e1   e2   convert from e2 to e1            convert str.encoding to e2
84      */
85     struct rb_io_enc_t {
86         rb_encoding *enc;
87         rb_encoding *enc2;
88         int ecflags;
89         VALUE ecopts;
90     } encs;
91 
92     rb_econv_t *readconv;
93     rb_io_buffer_t cbuf;
94 
95     rb_econv_t *writeconv;
96     VALUE writeconv_asciicompat;
97     int writeconv_initialized;
98     int writeconv_pre_ecflags;
99     VALUE writeconv_pre_ecopts;
100 
101     VALUE write_lock;
102 } rb_io_t;
103 
104 #define HAVE_RB_IO_T 1
105 
106 #define FMODE_READABLE              0x00000001
107 #define FMODE_WRITABLE              0x00000002
108 #define FMODE_READWRITE             (FMODE_READABLE|FMODE_WRITABLE)
109 #define FMODE_BINMODE               0x00000004
110 #define FMODE_SYNC                  0x00000008
111 #define FMODE_TTY                   0x00000010
112 #define FMODE_DUPLEX                0x00000020
113 #define FMODE_APPEND                0x00000040
114 #define FMODE_CREATE                0x00000080
115 /* #define FMODE_NOREVLOOKUP        0x00000100 */
116 #define FMODE_EXCL                  0x00000400
117 #define FMODE_TRUNC                 0x00000800
118 #define FMODE_TEXTMODE              0x00001000
119 /* #define FMODE_PREP               0x00010000 */
120 #define FMODE_SETENC_BY_BOM         0x00100000
121 /* #define FMODE_UNIX                  0x00200000 */
122 /* #define FMODE_INET                  0x00400000 */
123 /* #define FMODE_INET6                 0x00800000 */
124 
125 #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
126 
127 #define MakeOpenFile(obj, fp) do {\
128     (fp) = rb_io_make_open_file(obj);\
129 } while (0)
130 
131 rb_io_t *rb_io_make_open_file(VALUE obj);
132 
133 FILE *rb_io_stdio_file(rb_io_t *fptr);
134 
135 FILE *rb_fdopen(int, const char*);
136 int rb_io_modestr_fmode(const char *modestr);
137 int rb_io_modestr_oflags(const char *modestr);
138 CONSTFUNC(int rb_io_oflags_fmode(int oflags));
139 void rb_io_check_writable(rb_io_t*);
140 void rb_io_check_readable(rb_io_t*);
141 void rb_io_check_char_readable(rb_io_t *fptr);
142 void rb_io_check_byte_readable(rb_io_t *fptr);
143 int rb_io_fptr_finalize(rb_io_t*);
144 void rb_io_synchronized(rb_io_t*);
145 void rb_io_check_initialized(rb_io_t*);
146 void rb_io_check_closed(rb_io_t*);
147 VALUE rb_io_get_io(VALUE io);
148 VALUE rb_io_check_io(VALUE io);
149 VALUE rb_io_get_write_io(VALUE io);
150 VALUE rb_io_set_write_io(VALUE io, VALUE w);
151 int rb_io_wait_readable(int);
152 int rb_io_wait_writable(int);
153 int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
154 void rb_io_set_nonblock(rb_io_t *fptr);
155 int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
156 ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
157 
158 /* compatibility for ruby 1.8 and older */
159 #define rb_io_mode_flags(modestr) [<"rb_io_mode_flags() is obsolete; use rb_io_modestr_fmode()">]
160 #define rb_io_modenum_flags(oflags) [<"rb_io_modenum_flags() is obsolete; use rb_io_oflags_fmode()">]
161 
162 VALUE rb_io_taint_check(VALUE);
163 NORETURN(void rb_eof_error(void));
164 
165 void rb_io_read_check(rb_io_t*);
166 int rb_io_read_pending(rb_io_t*);
167 
168 struct stat;
169 VALUE rb_stat_new(const struct stat *);
170 
171 /* gc.c */
172 
173 RUBY_SYMBOL_EXPORT_END
174 
175 #if defined(__cplusplus)
176 #if 0
177 { /* satisfy cc-mode */
178 #endif
179 }  /* extern "C" { */
180 #endif
181 
182 #endif /* RUBY_IO_H */
183