Ruby  2.0.0p594(2014-10-27revision48167)
goruby.c
Go to the documentation of this file.
1 void Init_golf(void);
2 #define ruby_options goruby_options
3 #define ruby_run_node goruby_run_node
4 #include "main.c"
5 #undef ruby_options
6 #undef ruby_run_node
7 
8 #if defined _WIN32
9 #include <io.h>
10 #include <fcntl.h>
11 #define pipe(p) _pipe(p, 32L, _O_NOINHERIT)
12 #elif defined HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 
16 RUBY_EXTERN void *ruby_options(int argc, char **argv);
17 RUBY_EXTERN int ruby_run_node(void*);
18 RUBY_EXTERN void ruby_init_ext(const char *name, void (*init)(void));
19 
20 static VALUE
22 {
23  ruby_init_ext("golf", Init_golf);
24  return arg;
25 }
26 
27 void *
28 goruby_options(int argc, char **argv)
29 {
30  static const char cmd[] = "END{require 'irb';IRB.start}";
31  int rw[2], infd;
32  void *ret;
33 
34  if ((isatty(0) && isatty(1) && isatty(2)) && (pipe(rw) == 0)) {
35  infd = dup(0);
36  dup2(rw[0], 0);
37  close(rw[0]);
38  write(rw[1], cmd, sizeof(cmd) - 1);
39  close(rw[1]);
40  ret = ruby_options(argc, argv);
41  dup2(infd, 0);
42  close(infd);
43  return ret;
44  }
45  else {
46  return ruby_options(argc, argv);
47  }
48 }
49 
50 int
52 {
53  int state;
54  if (NIL_P(rb_protect(init_golf, Qtrue, &state))) {
55  return state == EXIT_SUCCESS ? EXIT_FAILURE : state;
56  }
57  return ruby_run_node(arg);
58 }
int ret
Definition: tcltklib.c:280
void * goruby_options(int argc, char **argv)
Definition: goruby.c:28
NIL_P(eventloop_thread)
Definition: tcltklib.c:4067
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:771
return Qtrue
Definition: tcltklib.c:9609
void Init_golf(void)
int state
Definition: tcltklib.c:1461
RUBY_EXTERN void ruby_init_ext(const char *name, void(*init)(void))
Definition: load.c:1047
#define EXIT_FAILURE
Definition: eval_intern.h:24
VALUE * argv
Definition: tcltklib.c:1970
#define EXIT_SUCCESS
Definition: error.c:31
int argc
Definition: tcltklib.c:1969
const char * cmd
Definition: tcltklib.c:278
static VALUE init_golf(VALUE arg)
Definition: goruby.c:21
#define ruby_run_node
Definition: goruby.c:3
#define ruby_options
Definition: goruby.c:2
int goruby_run_node(void *arg)
Definition: goruby.c:51
arg
Definition: ripper.y:1317
RUBY_EXTERN int dup2(int, int)
Definition: dup2.c:27
#define RUBY_EXTERN
Definition: defines.h:188
unsigned long VALUE
Definition: ripper.y:104
const char * name
Definition: nkf.c:208