Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
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
21 init_golf(VALUE arg)
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) {
39  close(rw[0]);
40  close(rw[1]);
41  goto no_irb;
42  }
43  dup2(rw[0], 0);
44  close(rw[0]);
45  n = write(rw[1], cmd, sizeof(cmd) - 1);
46  close(rw[1]);
47  ret = n > 0 ? ruby_options(argc, argv) : NULL;
48  dup2(infd, 0);
49  close(infd);
50  return ret;
51  }
52  else {
53  no_irb:
54  return ruby_options(argc, argv);
55  }
56 }
57 
58 int
60 {
61  int state;
62  if (NIL_P(rb_protect(init_golf, Qtrue, &state))) {
63  return state == EXIT_SUCCESS ? EXIT_FAILURE : state;
64  }
65  return ruby_run_node(arg);
66 }
pipe
int pipe(int __fildes[2])
n
const char size_t n
Definition: rb_mjit_min_header-2.7.1.h:5417
VALUE
unsigned long VALUE
Definition: ruby.h:102
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.1.h:5562
EXIT_FAILURE
#define EXIT_FAILURE
Definition: eval_intern.h:32
goruby_run_node
int goruby_run_node(void *arg)
Definition: goruby.c:59
NULL
#define NULL
Definition: _sdbm.c:101
rb_protect
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:1072
main.c
Init_golf
void Init_golf(void)
ruby_init_ext
RUBY_EXTERN void ruby_init_ext(const char *name, void(*init)(void))
Definition: load.c:1138
dup
int dup(int __fildes)
EXIT_SUCCESS
#define EXIT_SUCCESS
Definition: error.c:39
goruby_options
void * goruby_options(int argc, char **argv)
Definition: goruby.c:29
isatty
int isatty(int __fildes)
write
_ssize_t write(int __fd, const void *__buf, size_t __nbyte)
dup2
RUBY_EXTERN int dup2(int, int)
Definition: dup2.c:27
rb_provide
void rb_provide(const char *)
Definition: load.c:563
argv
char ** argv
Definition: ruby.c:223
ssize_t
_ssize_t ssize_t
Definition: rb_mjit_min_header-2.7.1.h:1324
close
int close(int __fildes)
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
io.h
argc
int argc
Definition: ruby.c:222
RUBY_EXTERN
#define RUBY_EXTERN
Definition: missing.h:77
Qtrue
#define Qtrue
Definition: ruby.h:468
ruby_run_node
#define ruby_run_node
Definition: goruby.c:3
ruby_options
#define ruby_options
Definition: goruby.c:2
name
const char * name
Definition: nkf.c:208