ruby-changes:2066
From: ko1@a...
Date: 29 Sep 2007 05:29:59 +0900
Subject: [ruby-changes:2066] nobu - Ruby:r13557 (trunk): * io.c (rb_io_fdopen): create IO object from fd.
nobu 2007-09-29 05:29:32 +0900 (Sat, 29 Sep 2007) New Revision: 13557 Modified files: trunk/ChangeLog trunk/include/ruby/intern.h trunk/io.c trunk/parse.y trunk/ruby.c Log: * 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. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ruby.c?r1=13557&r2=13556 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/parse.y?r1=13557&r2=13556 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13557&r2=13556 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/io.c?r1=13557&r2=13556 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/intern.h?r1=13557&r2=13556 Index: include/ruby/intern.h =================================================================== --- include/ruby/intern.h (revision 13556) +++ include/ruby/intern.h (revision 13557) @@ -352,6 +352,7 @@ 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*); Index: ChangeLog =================================================================== --- ChangeLog (revision 13556) +++ ChangeLog (revision 13557) @@ -1,3 +1,11 @@ +Sat Sep 29 05:29:30 2007 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * encoding.c (rb_enc_alias): allow encodings multiple aliases. Index: io.c =================================================================== --- io.c (revision 13556) +++ io.c (revision 13557) @@ -4186,6 +4186,15 @@ 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) { Index: parse.y =================================================================== --- parse.y (revision 13556) +++ parse.y (revision 13557) @@ -4659,7 +4659,6 @@ 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 @@ } } - 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 @@ static void parser_set_encode(struct parser_params *parser, const char *name) { - rb_set_kcode(name); parser->enc = rb_enc_find(name); } Index: ruby.c =================================================================== --- ruby.c (revision 13556) +++ ruby.c (revision 13557) @@ -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 @@ 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 @@ 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 @@ 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 @@ } 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 @@ 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 @@ } 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 @@ 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; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml