Ruby  2.0.0p247(2013-06-27revision41674)
goruby.c
Go to the documentation of this file.
00001 void Init_golf(void);
00002 #define ruby_options goruby_options
00003 #define ruby_run_node goruby_run_node
00004 #include "main.c"
00005 #undef ruby_options
00006 #undef ruby_run_node
00007 
00008 #if defined _WIN32
00009 #include <io.h>
00010 #include <fcntl.h>
00011 #define pipe(p) _pipe(p, 32L, _O_NOINHERIT)
00012 #elif defined HAVE_UNISTD_H
00013 #include <unistd.h>
00014 #endif
00015 
00016 RUBY_EXTERN void *ruby_options(int argc, char **argv);
00017 RUBY_EXTERN int ruby_run_node(void*);
00018 RUBY_EXTERN void ruby_init_ext(const char *name, void (*init)(void));
00019 
00020 static VALUE
00021 init_golf(VALUE arg)
00022 {
00023     ruby_init_ext("golf", Init_golf);
00024     return arg;
00025 }
00026 
00027 void *
00028 goruby_options(int argc, char **argv)
00029 {
00030     static const char cmd[] = "END{require 'irb';IRB.start}";
00031     int rw[2], infd;
00032     void *ret;
00033 
00034     if ((isatty(0) && isatty(1) && isatty(2)) && (pipe(rw) == 0)) {
00035         infd = dup(0);
00036         dup2(rw[0], 0);
00037         close(rw[0]);
00038         write(rw[1], cmd, sizeof(cmd) - 1);
00039         close(rw[1]);
00040         ret = ruby_options(argc, argv);
00041         dup2(infd, 0);
00042         close(infd);
00043         return ret;
00044     }
00045     else {
00046         return ruby_options(argc, argv);
00047     }
00048 }
00049 
00050 int
00051 goruby_run_node(void *arg)
00052 {
00053     int state;
00054     if (NIL_P(rb_protect(init_golf, Qtrue, &state))) {
00055         return state == EXIT_SUCCESS ? EXIT_FAILURE : state;
00056     }
00057     return ruby_run_node(arg);
00058 }
00059