Ruby 3.5.0dev (2025-01-10 revision 5fab31b15e32622c4b71d1d347a41937e9f9c212)
goruby.c (5fab31b15e32622c4b71d1d347a41937e9f9c212)
1static void Init_golf_prelude(void);
2static void *goruby_options(int argc, char **argv);
3static 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
18RUBY_EXTERN void *ruby_options(int argc, char **argv);
19RUBY_EXTERN int ruby_run_node(void*);
20
21#include "golf_prelude.rbbin"
22
23static VALUE
24init_golf(VALUE arg)
25{
26 Init_golf_prelude();
27 rb_provide("golf.so");
28 return arg;
29}
30
31void *
32goruby_options(int argc, char **argv)
33{
34 static const char cmd[] = "END{require 'irb';IRB.start}";
35 int rw[2], infd;
36 void *ret;
37
38 if ((isatty(0) && isatty(1) && isatty(2)) && (pipe(rw) == 0)) {
39 ssize_t n;
40 infd = dup(0);
41 if (infd < 0) {
42 close(rw[0]);
43 close(rw[1]);
44 goto no_irb;
45 }
46 dup2(rw[0], 0);
47 close(rw[0]);
48 n = write(rw[1], cmd, sizeof(cmd) - 1);
49 close(rw[1]);
50 ret = n > 0 ? ruby_options(argc, argv) : NULL;
51 dup2(infd, 0);
52 close(infd);
53 return ret;
54 }
55 no_irb:
56 return ruby_options(argc, argv);
57}
58
59int
60goruby_run_node(void *arg)
61{
62 int state;
63 if (ruby_executable_node(arg, NULL) &&
64 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:45
#define Qtrue
Old name of RUBY_Qtrue.
#define NIL_P
Old name of RB_NIL_P.
int ruby_executable_node(void *n, int *status)
Checks the return value of ruby_options().
Definition eval.c:294
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:715
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40