Ruby  2.4.2p198(2017-09-14revision59899)
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  Init_golf();
24  rb_provide("golf.so");
25  return arg;
26 }
27 
28 void *
29 goruby_options(int argc, char **argv)
30 {
31  static const char cmd[] = "END{require 'irb';IRB.start}";
32  int rw[2], infd;
33  void *ret;
34 
35  if ((isatty(0) && isatty(1) && isatty(2)) && (pipe(rw) == 0)) {
36  ssize_t n;
37  infd = dup(0);
38  if (infd < 0) return NULL;
39  dup2(rw[0], 0);
40  close(rw[0]);
41  n = write(rw[1], cmd, sizeof(cmd) - 1);
42  close(rw[1]);
43  ret = n > 0 ? ruby_options(argc, argv) : NULL;
44  dup2(infd, 0);
45  close(infd);
46  return ret;
47  }
48  else {
49  return ruby_options(argc, argv);
50  }
51 }
52 
53 int
54 goruby_run_node(void *arg)
55 {
56  int state;
57  if (NIL_P(rb_protect(init_golf, Qtrue, &state))) {
58  return state == EXIT_SUCCESS ? EXIT_FAILURE : state;
59  }
60  return ruby_run_node(arg);
61 }
#define Qtrue
Definition: ruby.h:437
void * goruby_options(int argc, char **argv)
Definition: goruby.c:29
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:891
void Init_golf(void)
RUBY_EXTERN void ruby_init_ext(const char *name, void(*init)(void))
Definition: load.c:1079
#define NIL_P(v)
Definition: ruby.h:451
int argc
Definition: ruby.c:183
#define EXIT_FAILURE
Definition: eval_intern.h:33
#define RUBY_EXTERN
Definition: missing.h:77
#define EXIT_SUCCESS
Definition: error.c:32
unsigned long VALUE
Definition: ruby.h:85
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:54
const char * name
Definition: nkf.c:208
RUBY_EXTERN int dup2(int, int)
Definition: dup2.c:27
#define NULL
Definition: _sdbm.c:102
void rb_provide(const char *)
Definition: load.c:572
char ** argv
Definition: ruby.c:184