00001 /********************************************************************** 00002 00003 rubyio.h - 00004 00005 $Author: nobu $ 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 typedef struct rb_io_t { 00031 int fd; /* file descriptor */ 00032 FILE *stdio_file; /* stdio ptr for read/write if available */ 00033 int mode; /* mode flags: FMODE_XXXs */ 00034 rb_pid_t pid; /* child's pid (for pipes) */ 00035 int lineno; /* number of lines read */ 00036 VALUE pathv; /* pathname for file */ 00037 void (*finalize)(struct rb_io_t*,int); /* finalize proc */ 00038 00039 char *wbuf; /* wbuf_off + wbuf_len <= wbuf_capa */ 00040 int wbuf_off; 00041 int wbuf_len; 00042 int wbuf_capa; 00043 00044 char *rbuf; /* rbuf_off + rbuf_len <= rbuf_capa */ 00045 int rbuf_off; 00046 int rbuf_len; 00047 int rbuf_capa; 00048 00049 VALUE tied_io_for_writing; 00050 00051 /* 00052 * enc enc2 read action write action 00053 * NULL NULL force_encoding(default_external) write the byte sequence of str 00054 * e1 NULL force_encoding(e1) convert str.encoding to e1 00055 * e1 e2 convert from e2 to e1 convert str.encoding to e2 00056 */ 00057 struct rb_io_enc_t { 00058 rb_encoding *enc; 00059 rb_encoding *enc2; 00060 int ecflags; 00061 VALUE ecopts; 00062 } encs; 00063 00064 rb_econv_t *readconv; 00065 char *cbuf; /* cbuf_off + cbuf_len <= cbuf_capa */ 00066 int cbuf_off; 00067 int cbuf_len; 00068 int cbuf_capa; 00069 00070 rb_econv_t *writeconv; 00071 VALUE writeconv_asciicompat; 00072 int writeconv_pre_ecflags; 00073 VALUE writeconv_pre_ecopts; 00074 int writeconv_initialized; 00075 00076 VALUE write_lock; 00077 } rb_io_t; 00078 00079 #define HAVE_RB_IO_T 1 00080 00081 #define FMODE_READABLE 0x00000001 00082 #define FMODE_WRITABLE 0x00000002 00083 #define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE) 00084 #define FMODE_BINMODE 0x00000004 00085 #define FMODE_SYNC 0x00000008 00086 #define FMODE_TTY 0x00000010 00087 #define FMODE_DUPLEX 0x00000020 00088 #define FMODE_APPEND 0x00000040 00089 #define FMODE_CREATE 0x00000080 00090 /* #define FMODE_NOREVLOOKUP 0x00000100 */ 00091 #define FMODE_WSPLIT 0x00000200 00092 #define FMODE_WSPLIT_INITIALIZED 0x00000400 00093 #define FMODE_TRUNC 0x00000800 00094 #define FMODE_TEXTMODE 0x00001000 00095 /* #define FMODE_PREP 0x00010000 */ 00096 #define FMODE_SETENC_BY_BOM 0x00100000 00097 00098 #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr) 00099 00100 #define MakeOpenFile(obj, fp) do {\ 00101 if (RFILE(obj)->fptr) {\ 00102 rb_io_close(obj);\ 00103 rb_io_fptr_finalize(RFILE(obj)->fptr);\ 00104 RFILE(obj)->fptr = 0;\ 00105 }\ 00106 fp = 0;\ 00107 fp = RFILE(obj)->fptr = ALLOC(rb_io_t);\ 00108 fp->fd = -1;\ 00109 fp->stdio_file = NULL;\ 00110 fp->mode = 0;\ 00111 fp->pid = 0;\ 00112 fp->lineno = 0;\ 00113 fp->pathv = Qnil;\ 00114 fp->finalize = 0;\ 00115 fp->wbuf = NULL;\ 00116 fp->wbuf_off = 0;\ 00117 fp->wbuf_len = 0;\ 00118 fp->wbuf_capa = 0;\ 00119 fp->rbuf = NULL;\ 00120 fp->rbuf_off = 0;\ 00121 fp->rbuf_len = 0;\ 00122 fp->rbuf_capa = 0;\ 00123 fp->readconv = NULL;\ 00124 fp->cbuf = NULL;\ 00125 fp->cbuf_off = 0;\ 00126 fp->cbuf_len = 0;\ 00127 fp->cbuf_capa = 0;\ 00128 fp->writeconv = NULL;\ 00129 fp->writeconv_asciicompat = Qnil;\ 00130 fp->writeconv_pre_ecflags = 0;\ 00131 fp->writeconv_pre_ecopts = Qnil;\ 00132 fp->writeconv_initialized = 0;\ 00133 fp->tied_io_for_writing = 0;\ 00134 fp->encs.enc = NULL;\ 00135 fp->encs.enc2 = NULL;\ 00136 fp->encs.ecflags = 0;\ 00137 fp->encs.ecopts = Qnil;\ 00138 fp->write_lock = 0;\ 00139 } while (0) 00140 00141 FILE *rb_io_stdio_file(rb_io_t *fptr); 00142 00143 FILE *rb_fdopen(int, const char*); 00144 int rb_io_modestr_fmode(const char *modestr); 00145 int rb_io_modestr_oflags(const char *modestr); 00146 int rb_io_oflags_fmode(int oflags); 00147 void rb_io_check_writable(rb_io_t*); 00148 void rb_io_check_readable(rb_io_t*); 00149 int rb_io_fptr_finalize(rb_io_t*); 00150 void rb_io_synchronized(rb_io_t*); 00151 void rb_io_check_initialized(rb_io_t*); 00152 void rb_io_check_closed(rb_io_t*); 00153 int rb_io_wait_readable(int); 00154 int rb_io_wait_writable(int); 00155 void rb_io_set_nonblock(rb_io_t *fptr); 00156 00157 /* compatibility for ruby 1.8 and older */ 00158 #define rb_io_mode_flags(modestr) rb_io_modestr_fmode(modestr) 00159 #define rb_io_modenum_flags(oflags) rb_io_oflags_fmode(oflags) 00160 00161 VALUE rb_io_taint_check(VALUE); 00162 NORETURN(void rb_eof_error(void)); 00163 00164 void rb_io_read_check(rb_io_t*); 00165 int rb_io_read_pending(rb_io_t*); 00166 DEPRECATED(void rb_read_check(FILE*)); 00167 00168 #if defined(__cplusplus) 00169 #if 0 00170 { /* satisfy cc-mode */ 00171 #endif 00172 } /* extern "C" { */ 00173 #endif 00174 00175 #endif /* RUBY_IO_H */ 00176