Ruby  2.0.0p247(2013-06-27revision41674)
include/ruby/io.h
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   rubyio.h -
00004 
00005   $Author: nagachika $
00006   created at: Fri Nov 12 16:47:09 JST 1993
00007 
00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
00009 
00010 **********************************************************************/
00011 
00012 #ifndef RUBY_IO_H
00013 #define RUBY_IO_H 1
00014 
00015 #if defined(__cplusplus)
00016 extern "C" {
00017 #if 0
00018 } /* satisfy cc-mode */
00019 #endif
00020 #endif
00021 
00022 #include <stdio.h>
00023 #include <errno.h>
00024 #include "ruby/encoding.h"
00025 
00026 #if defined(HAVE_STDIO_EXT_H)
00027 #include <stdio_ext.h>
00028 #endif
00029 
00030 #include "ruby/config.h"
00031 #if defined(HAVE_POLL)
00032 #  ifdef _AIX
00033 #    define reqevents events
00034 #    define rtnevents revents
00035 #  endif
00036 #  include <poll.h>
00037 #  ifdef _AIX
00038 #    undef reqevents
00039 #    undef rtnevents
00040 #    undef events
00041 #    undef revents
00042 #  endif
00043 #  define RB_WAITFD_IN  POLLIN
00044 #  define RB_WAITFD_PRI POLLPRI
00045 #  define RB_WAITFD_OUT POLLOUT
00046 #else
00047 #  define RB_WAITFD_IN  0x001
00048 #  define RB_WAITFD_PRI 0x002
00049 #  define RB_WAITFD_OUT 0x004
00050 #endif
00051 
00052 #if defined __GNUC__ && __GNUC__ >= 4
00053 #pragma GCC visibility push(default)
00054 #endif
00055 
00056 typedef struct {
00057     char *ptr;                  /* off + len <= capa */
00058     int off;
00059     int len;
00060     int capa;
00061 } rb_io_buffer_t;
00062 
00063 typedef struct rb_io_t {
00064     int fd;                     /* file descriptor */
00065     FILE *stdio_file;           /* stdio ptr for read/write if available */
00066     int mode;                   /* mode flags: FMODE_XXXs */
00067     rb_pid_t pid;               /* child's pid (for pipes) */
00068     int lineno;                 /* number of lines read */
00069     VALUE pathv;                /* pathname for file */
00070     void (*finalize)(struct rb_io_t*,int); /* finalize proc */
00071 
00072     rb_io_buffer_t wbuf, rbuf;
00073 
00074     VALUE tied_io_for_writing;
00075 
00076     /*
00077      * enc  enc2 read action                      write action
00078      * NULL NULL force_encoding(default_external) write the byte sequence of str
00079      * e1   NULL force_encoding(e1)               convert str.encoding to e1
00080      * e1   e2   convert from e2 to e1            convert str.encoding to e2
00081      */
00082     struct rb_io_enc_t {
00083         rb_encoding *enc;
00084         rb_encoding *enc2;
00085         int ecflags;
00086         VALUE ecopts;
00087     } encs;
00088 
00089     rb_econv_t *readconv;
00090     rb_io_buffer_t cbuf;
00091 
00092     rb_econv_t *writeconv;
00093     VALUE writeconv_asciicompat;
00094     int writeconv_pre_ecflags;
00095     VALUE writeconv_pre_ecopts;
00096     int writeconv_initialized;
00097 
00098     VALUE write_lock;
00099 } rb_io_t;
00100 
00101 #define HAVE_RB_IO_T 1
00102 
00103 #define FMODE_READABLE              0x00000001
00104 #define FMODE_WRITABLE              0x00000002
00105 #define FMODE_READWRITE             (FMODE_READABLE|FMODE_WRITABLE)
00106 #define FMODE_BINMODE               0x00000004
00107 #define FMODE_SYNC                  0x00000008
00108 #define FMODE_TTY                   0x00000010
00109 #define FMODE_DUPLEX                0x00000020
00110 #define FMODE_APPEND                0x00000040
00111 #define FMODE_CREATE                0x00000080
00112 /* #define FMODE_NOREVLOOKUP        0x00000100 */
00113 #define FMODE_WSPLIT                0x00000200
00114 #define FMODE_WSPLIT_INITIALIZED    0x00000400
00115 #define FMODE_TRUNC                 0x00000800
00116 #define FMODE_TEXTMODE              0x00001000
00117 /* #define FMODE_PREP               0x00010000 */
00118 #define FMODE_SETENC_BY_BOM         0x00100000
00119 
00120 #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
00121 
00122 #define RB_IO_BUFFER_INIT(buf) do {\
00123     (buf).ptr = NULL;\
00124     (buf).off = 0;\
00125     (buf).len = 0;\
00126     (buf).capa = 0;\
00127 } while (0)
00128 
00129 #define MakeOpenFile(obj, fp) do {\
00130     if (RFILE(obj)->fptr) {\
00131         rb_io_close(obj);\
00132         rb_io_fptr_finalize(RFILE(obj)->fptr);\
00133         RFILE(obj)->fptr = 0;\
00134     }\
00135     (fp) = 0;\
00136     RB_IO_FPTR_NEW(fp);\
00137     RFILE(obj)->fptr = (fp);\
00138 } while (0)
00139 
00140 #define RB_IO_FPTR_NEW(fp) do {\
00141     (fp) = ALLOC(rb_io_t);\
00142     (fp)->fd = -1;\
00143     (fp)->stdio_file = NULL;\
00144     (fp)->mode = 0;\
00145     (fp)->pid = 0;\
00146     (fp)->lineno = 0;\
00147     (fp)->pathv = Qnil;\
00148     (fp)->finalize = 0;\
00149     RB_IO_BUFFER_INIT((fp)->wbuf);\
00150     RB_IO_BUFFER_INIT((fp)->rbuf);\
00151     RB_IO_BUFFER_INIT((fp)->cbuf);\
00152     (fp)->readconv = NULL;\
00153     (fp)->writeconv = NULL;\
00154     (fp)->writeconv_asciicompat = Qnil;\
00155     (fp)->writeconv_pre_ecflags = 0;\
00156     (fp)->writeconv_pre_ecopts = Qnil;\
00157     (fp)->writeconv_initialized = 0;\
00158     (fp)->tied_io_for_writing = 0;\
00159     (fp)->encs.enc = NULL;\
00160     (fp)->encs.enc2 = NULL;\
00161     (fp)->encs.ecflags = 0;\
00162     (fp)->encs.ecopts = Qnil;\
00163     (fp)->write_lock = 0;\
00164 } while (0)
00165 
00166 FILE *rb_io_stdio_file(rb_io_t *fptr);
00167 
00168 FILE *rb_fdopen(int, const char*);
00169 int rb_io_modestr_fmode(const char *modestr);
00170 int rb_io_modestr_oflags(const char *modestr);
00171 int rb_io_oflags_fmode(int oflags);
00172 void rb_io_check_writable(rb_io_t*);
00173 void rb_io_check_readable(rb_io_t*);
00174 void rb_io_check_char_readable(rb_io_t *fptr);
00175 void rb_io_check_byte_readable(rb_io_t *fptr);
00176 int rb_io_fptr_finalize(rb_io_t*);
00177 void rb_io_synchronized(rb_io_t*);
00178 void rb_io_check_initialized(rb_io_t*);
00179 void rb_io_check_closed(rb_io_t*);
00180 VALUE rb_io_get_io(VALUE io);
00181 VALUE rb_io_check_io(VALUE io);
00182 VALUE rb_io_get_write_io(VALUE io);
00183 VALUE rb_io_set_write_io(VALUE io, VALUE w);
00184 int rb_io_wait_readable(int);
00185 int rb_io_wait_writable(int);
00186 int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
00187 void rb_io_set_nonblock(rb_io_t *fptr);
00188 int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
00189 ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
00190 
00191 /* compatibility for ruby 1.8 and older */
00192 #define rb_io_mode_flags(modestr) rb_io_modestr_fmode(modestr)
00193 #define rb_io_modenum_flags(oflags) rb_io_oflags_fmode(oflags)
00194 
00195 VALUE rb_io_taint_check(VALUE);
00196 NORETURN(void rb_eof_error(void));
00197 
00198 void rb_io_read_check(rb_io_t*);
00199 int rb_io_read_pending(rb_io_t*);
00200 DEPRECATED(void rb_read_check(FILE*));
00201 
00202 #if defined __GNUC__ && __GNUC__ >= 4
00203 #pragma GCC visibility pop
00204 #endif
00205 
00206 #if defined(__cplusplus)
00207 #if 0
00208 { /* satisfy cc-mode */
00209 #endif
00210 }  /* extern "C" { */
00211 #endif
00212 
00213 #endif /* RUBY_IO_H */
00214