Ruby  3.1.4p223 (2023-03-30 revision HEAD)
goruby.c
1 static void Init_golf(void);
2 static void *goruby_options(int argc, char **argv);
3 static int goruby_run_node(void *arg);
4 #define ruby_options goruby_options
5 #define ruby_run_node goruby_run_node
6 #include "main.c"
7 #undef ruby_options
8 #undef ruby_run_node
9 
10 #if defined _WIN32
11 #include <io.h>
12 #include <fcntl.h>
13 #define pipe(p) _pipe(p, 32L, _O_NOINHERIT)
14 #elif defined HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 
18 RUBY_EXTERN void *ruby_options(int argc, char **argv);
19 RUBY_EXTERN int ruby_run_node(void*);
20 RUBY_EXTERN void ruby_init_ext(const char *name, void (*init)(void));
21 
22 #include "golf_prelude.c"
23 
24 static VALUE
25 init_golf(VALUE arg)
26 {
27  Init_golf();
28  rb_provide("golf.so");
29  return arg;
30 }
31 
32 void *
33 goruby_options(int argc, char **argv)
34 {
35  static const char cmd[] = "END{require 'irb';IRB.start}";
36  int rw[2], infd;
37  void *ret;
38 
39  if ((isatty(0) && isatty(1) && isatty(2)) && (pipe(rw) == 0)) {
40  ssize_t n;
41  infd = dup(0);
42  if (infd < 0) {
43  close(rw[0]);
44  close(rw[1]);
45  goto no_irb;
46  }
47  dup2(rw[0], 0);
48  close(rw[0]);
49  n = write(rw[1], cmd, sizeof(cmd) - 1);
50  close(rw[1]);
51  ret = n > 0 ? ruby_options(argc, argv) : NULL;
52  dup2(infd, 0);
53  close(infd);
54  return ret;
55  }
56  no_irb:
57  return ruby_options(argc, argv);
58 }
59 
60 int
61 goruby_run_node(void *arg)
62 {
63  int state;
64  if (NIL_P(rb_protect(init_golf, Qtrue, &state))) {
65  return state == EXIT_SUCCESS ? EXIT_FAILURE : state;
66  }
67  return ruby_run_node(arg);
68 }
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:47
#define Qtrue
Old name of RUBY_Qtrue.
#define NIL_P
Old name of RB_NIL_P.
int ruby_run_node(void *n)
Runs the given compiled source and exits this process.
Definition: eval.c:312
void * ruby_options(int argc, char **argv)
Processes command line arguments and compiles the Ruby source to execute.
Definition: eval.c:109
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition: load.c:638
VALUE rb_protect(VALUE(*func)(VALUE args), VALUE args, int *state)
Protects a function call from potential global escapes from the function.
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40