diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | include/ruby/intern.h | 1 | ||||
-rw-r--r-- | io.c | 9 | ||||
-rw-r--r-- | parse.y | 5 | ||||
-rw-r--r-- | ruby.c | 67 |
5 files changed, 65 insertions, 25 deletions
@@ -1,3 +1,11 @@ +Sat Sep 29 05:29:30 2007 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * io.c (rb_io_fdopen): create IO object from fd. + + * parse.y (yycompile): use encoding of the source as default. + + * ruby.c (proc_options, load_file): ditto. + Sat Sep 29 04:27:08 2007 Nobuyoshi Nakada <nobu@ruby-lang.org> * encoding.c (rb_enc_alias): allow encodings multiple aliases. diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 1b453d1f30..500ec6a358 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -352,6 +352,7 @@ VALUE rb_io_addstr(VALUE, VALUE); VALUE rb_io_printf(int, VALUE*, VALUE); VALUE rb_io_print(int, VALUE*, VALUE); VALUE rb_io_puts(int, VALUE*, VALUE); +VALUE rb_io_fdopen(int, int, const char*); VALUE rb_file_open(const char*, const char*); VALUE rb_gets(void); void rb_write_error(const char*); @@ -4186,6 +4186,15 @@ prep_io(int fd, int mode, VALUE klass, const char *path) return io; } +VALUE +rb_io_fdopen(int fd, int mode, const char *path) +{ + VALUE klass = rb_cIO; + + if (path && strcmp(path, "-")) klass = rb_cFile; + return prep_io(fd, rb_io_modenum_flags(mode), klass, path); +} + static VALUE prep_stdio(FILE *f, int mode, VALUE klass, const char *path) { @@ -4659,7 +4659,6 @@ static NODE* yycompile(struct parser_params *parser, const char *f, int line) { int n; - const char *kcode_save; NODE *tree; if (!compile_for_eval && rb_safe_level() == 0) { @@ -4673,14 +4672,13 @@ yycompile(struct parser_params *parser, const char *f, int line) } } - kcode_save = rb_get_kcode(); + parser->enc = rb_enc_get(lex_input); ruby_sourcefile = rb_source_filename(f); ruby_sourceline = line - 1; parser_prepare(parser); n = yyparse((void*)parser); ruby_debug_lines = 0; compile_for_eval = 0; - rb_set_kcode(kcode_save); lex_strterm = 0; if (parser->nerr) { @@ -5522,7 +5520,6 @@ lvar_defined_gen(struct parser_params *parser, ID id) static void parser_set_encode(struct parser_params *parser, const char *name) { - rb_set_kcode(name); parser->enc = rb_enc_find(name); } @@ -22,6 +22,7 @@ #endif #include "ruby/ruby.h" #include "ruby/node.h" +#include "ruby/encoding.h" #include "dln.h" #include <stdio.h> #include <sys/types.h> @@ -34,6 +35,11 @@ #ifdef HAVE_UNISTD_H #include <unistd.h> #endif +#if defined(HAVE_FCNTL_H) +#include <fcntl.h> +#elif defined(HAVE_SYS_FCNTL_H) +#include <sys/fcntl.h> +#endif #ifdef HAVE_SYS_PARAM_H # include <sys/param.h> #endif @@ -64,12 +70,13 @@ extern int ruby_yydebug; char *ruby_inplace_mode = 0; -static NODE *load_file(VALUE *, const char *, int); +static NODE *load_file(VALUE, const char *, int); static void forbid_setid(const char *); static VALUE do_loop = Qfalse, do_print = Qfalse; static VALUE do_check = Qfalse, do_line = Qfalse; static VALUE do_split = Qfalse; +static int enc_index = 0; static int origargc; static char **origargv; @@ -682,7 +689,27 @@ proc_options(int argc, char **argv) case 'K': if (*++s) { - rb_set_kcode(s); + rb_encoding *enc = 0; +#if 0 + if ((enc_index = rb_enc_find_index(s)) >= 0) break; +#endif + switch (*s) { + case 'E': case 'e': + enc = ONIG_ENCODING_EUC_JP; + break; + case 'S': case 's': + enc = ONIG_ENCODING_SJIS; + break; + case 'U': case 'u': + enc = ONIG_ENCODING_UTF8; + break; + case 'N': case 'n': case 'A': case 'a': + enc = ONIG_ENCODING_ASCII; + break; + default: + rb_raise(rb_eRuntimeError, "unknown encoding name - %s", s); + } + enc_index = rb_enc_to_index(enc); s++; } goto reswitch; @@ -880,16 +907,17 @@ proc_options(int argc, char **argv) process_sflag(); ruby_init_loadpath(); + parser = rb_parser_new(); if (e_script) { + rb_enc_associate_index(e_script, enc_index); require_libraries(); - parser = rb_parser_new(); tree = rb_parser_compile_string(parser, script, e_script, 1); } else { if (script[0] == '-' && !script[1]) { forbid_setid("program input from stdin"); } - tree = load_file(&parser, script, 1); + tree = load_file(parser, script, 1); } process_sflag(); @@ -911,7 +939,7 @@ proc_options(int argc, char **argv) } static NODE * -load_file(VALUE *parser, const char *fname, int script) +load_file(VALUE parser, const char *fname, int script) { extern VALUE rb_stdin; VALUE f; @@ -924,21 +952,19 @@ load_file(VALUE *parser, const char *fname, int script) f = rb_stdin; } else { - FILE *fp = fopen(fname, "r"); - - if (fp == NULL) { - rb_load_fail(fname); - } - fclose(fp); - - f = rb_file_open(fname, "r"); + int fd, mode = O_RDONLY; #if defined DOSISH || defined __CYGWIN__ { - char *ext = strrchr(fname, '.'); + const char *ext = strrchr(fname, '.'); if (ext && strcasecmp(ext, ".exe") == 0) - rb_io_binmode(f); + mode |= O_BINARY; } #endif + if ((fd = open(fname, mode)) < 0) { + rb_load_fail(fname); + } + + f = rb_io_fdopen(fd, mode, fname); } if (script) { @@ -1027,9 +1053,10 @@ load_file(VALUE *parser, const char *fname, int script) } require_libraries(); /* Why here? unnatural */ } - *parser = rb_parser_new(); - tree = (NODE *)rb_parser_compile_file(*parser, fname, f, line_start); - if (script && rb_parser_end_seen_p(*parser)) { + rb_enc_associate_index(f, enc_index); + parser = rb_parser_new(); + tree = (NODE *)rb_parser_compile_file(parser, fname, f, line_start); + if (script && rb_parser_end_seen_p(parser)) { rb_define_global_const("DATA", f); } else if (f != rb_stdin) { @@ -1041,9 +1068,7 @@ load_file(VALUE *parser, const char *fname, int script) void * rb_load_file(const char *fname) { - VALUE parser; - - return load_file(&parser, fname, 0); + return load_file(rb_parser_new(), fname, 0); } VALUE rb_progname; |