14#include "ruby/internal/config.h"
22# include <sys/cygwin.h>
25#if defined(LOAD_RELATIVE) && defined(HAVE_DLADDR)
33#if defined(HAVE_FCNTL_H)
35#elif defined(HAVE_SYS_FCNTL_H)
36# include <sys/fcntl.h>
39#ifdef HAVE_SYS_PARAM_H
40# include <sys/param.h>
44#include "eval_intern.h"
46#include "internal/cmdlineopt.h"
47#include "internal/cont.h"
48#include "internal/error.h"
49#include "internal/file.h"
50#include "internal/inits.h"
51#include "internal/io.h"
52#include "internal/load.h"
53#include "internal/loadpath.h"
54#include "internal/missing.h"
55#include "internal/object.h"
56#include "internal/thread.h"
57#include "internal/ruby_parser.h"
58#include "internal/variable.h"
65#define singlebit_only_p(x) !((x) & ((x)-1))
66STATIC_ASSERT(Qnil_1bit_from_Qfalse, singlebit_only_p(
Qnil^
Qfalse));
67STATIC_ASSERT(Qundef_1bit_from_Qnil, singlebit_only_p(
Qundef^
Qnil));
70# define MAXPATHLEN 1024
73# define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
82#ifndef DISABLE_RUBYGEMS
83# define DISABLE_RUBYGEMS 0
86#define DEFAULT_RUBYGEMS_ENABLED "disabled"
88#define DEFAULT_RUBYGEMS_ENABLED "enabled"
91void rb_warning_category_update(
unsigned int mask,
unsigned int bits);
94#define FEATURE_BIT(bit) (1U << feature_##bit)
95#define EACH_FEATURES(X, SEP) \
106 X(frozen_string_literal) \
112#define EACH_DEBUG_FEATURES(X, SEP) \
113 X(frozen_string_literal) \
115#define AMBIGUOUS_FEATURE_NAMES 0
116#define DEFINE_FEATURE(bit) feature_##bit
117#define DEFINE_DEBUG_FEATURE(bit) feature_debug_##bit
118enum feature_flag_bits {
119 EACH_FEATURES(DEFINE_FEATURE, COMMA),
120 feature_debug_flag_first,
121#if defined(RJIT_FORCE_ENABLE) || !USE_YJIT
122 DEFINE_FEATURE(jit) = feature_rjit,
124 DEFINE_FEATURE(jit) = feature_yjit,
126 feature_jit_mask = FEATURE_BIT(rjit) | FEATURE_BIT(yjit),
128 feature_debug_flag_begin = feature_debug_flag_first - 1,
129 EACH_DEBUG_FEATURES(DEFINE_DEBUG_FEATURE, COMMA),
133#define MULTI_BITS_P(bits) ((bits) & ((bits) - 1))
135#define DEBUG_BIT(bit) (1U << feature_debug_##bit)
137#define DUMP_BIT(bit) (1U << dump_##bit)
138#define DEFINE_DUMP(bit) dump_##bit
139#define EACH_DUMPS(X, SEP) \
154 X(parsetree_with_comment) \
158 X(insns_without_opt) \
163 EACH_DUMPS(DEFINE_DUMP, COMMA),
164 dump_error_tolerant_bits = (DUMP_BIT(yydebug) |
165 DUMP_BIT(parsetree) |
166 DUMP_BIT(parsetree_with_comment)),
167 dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) |
168 DUMP_BIT(parsetree) | DUMP_BIT(parsetree_with_comment) |
169 DUMP_BIT(insns) | DUMP_BIT(insns_without_opt))
173rb_feature_set_to(
ruby_features_t *feat,
unsigned int bit_mask,
unsigned int bit_set)
175 feat->mask |= bit_mask;
176 feat->set = (feat->set & ~bit_mask) | bit_set;
179#define FEATURE_SET_TO(feat, bit_mask, bit_set) \
180 rb_feature_set_to(&(feat), bit_mask, bit_set)
181#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits)
182#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask)
183#define FEATURE_SET_P(feat, bits) ((feat).set & FEATURE_BIT(bits))
184#define FEATURE_USED_P(feat, bits) ((feat).mask & FEATURE_BIT(bits))
185#define FEATURE_SET_BITS(feat) ((feat).set & (feat).mask)
189#define src_encoding_index GET_VM()->src_encoding_index
192 COMPILATION_FEATURES = (
194 | FEATURE_BIT(frozen_string_literal)
195 | FEATURE_BIT(debug_frozen_string_literal)
198 (FEATURE_BIT(debug_flag_first)-1)
202 & ~FEATURE_BIT(frozen_string_literal)
207#define BACKTRACE_LENGTH_LIMIT_VALID_P(n) ((n) >= -1)
208#define OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt) \
209 BACKTRACE_LENGTH_LIMIT_VALID_P((opt)->backtrace_length_limit)
216 opt->src.enc.index = src_encoding_index;
217 opt->ext.enc.index = -1;
218 opt->intern.enc.index = -1;
219 opt->features.set = DEFAULT_FEATURES;
220#ifdef RJIT_FORCE_ENABLE
221 opt->features.set |= FEATURE_BIT(rjit);
222#elif defined(YJIT_FORCE_ENABLE)
223 opt->features.set |= FEATURE_BIT(yjit);
225 opt->backtrace_length_limit = LONG_MIN;
232static VALUE open_load_file(
VALUE fname_v,
int *xflag);
234#define forbid_setid(s) forbid_setid((s), opt)
241static const char esc_standout[] =
"\n\033[1;7m";
242static const char esc_bold[] =
"\033[1m";
243static const char esc_reset[] =
"\033[0m";
244static const char esc_none[] =
"";
245#define USAGE_INDENT " "
248show_usage_part(
const char *str,
const unsigned int namelen,
249 const char *str2,
const unsigned int secondlen,
251 int help,
int highlight,
unsigned int w,
int columns)
253 static const int indent_width = (int)
rb_strlen_lit(USAGE_INDENT);
254 const char *sb = highlight ? esc_bold : esc_none;
255 const char *se = highlight ? esc_reset : esc_none;
256 unsigned int desclen = (
unsigned int)strcspn(desc,
"\n");
257 if (help && (namelen + 1 > w) &&
258 (
int)(namelen + secondlen + indent_width) >= columns) {
259 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, namelen, str, se);
261 const int second_end = secondlen;
263 if (str2[n] ==
',') n++;
264 if (str2[n] ==
' ') n++;
265 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, second_end-n, str2+n, se);
267 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
270 const int wrap = help && namelen + secondlen >= w;
271 printf(USAGE_INDENT
"%s%.*s%-*.*s%s%-*s%.*s\n", sb, namelen, str,
272 (wrap ? 0 : w - namelen),
273 (help ? secondlen : 0), str2, se,
275 (wrap ?
"\n" USAGE_INDENT :
""),
279 while (desc[desclen]) {
281 desclen = (
unsigned int)strcspn(desc,
"\n");
282 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
289 int help,
int highlight,
unsigned int w,
int columns)
291 const char *str = m->str;
292 const unsigned int namelen = m->namelen, secondlen = m->secondlen;
293 const char *desc = str + namelen + secondlen;
294 show_usage_part(str, namelen - 1, str + namelen, secondlen - 1, desc,
295 help, highlight, w, columns);
299ruby_show_usage_line(
const char *name,
const char *secondary,
const char *description,
300 int help,
int highlight,
unsigned int width,
int columns)
302 unsigned int namelen = (
unsigned int)strlen(name);
303 unsigned int secondlen = (secondary ? (
unsigned int)strlen(secondary) : 0);
304 show_usage_part(name, namelen, secondary, secondlen,
305 description, help, highlight, width, columns);
309usage(
const char *name,
int help,
int highlight,
int columns)
311#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
314# define PLATFORM_JIT_OPTION "--yjit"
316# define PLATFORM_JIT_OPTION "--rjit (experimental)"
322 M(
"-0[octal]",
"",
"specify record separator (\\0, if no argument)\n"
323 "(-00 for paragraph mode, -0777 for slurp mode)"),
324 M(
"-a",
"",
"autosplit mode with -n or -p (splits $_ into $F)"),
325 M(
"-c",
"",
"check syntax only"),
326 M(
"-Cdirectory",
"",
"cd to directory before executing your script"),
327 M(
"-d",
", --debug",
"set debugging flags (set $DEBUG to true)"),
328 M(
"-e 'command'",
"",
"one line of script. Several -e's allowed. Omit [programfile]"),
329 M(
"-Eex[:in]",
", --encoding=ex[:in]",
"specify the default external and internal character encodings"),
330 M(
"-Fpattern",
"",
"split() pattern for autosplit (-a)"),
331 M(
"-i[extension]",
"",
"edit ARGV files in place (make backup if extension supplied)"),
332 M(
"-Idirectory",
"",
"specify $LOAD_PATH directory (may be used more than once)"),
333 M(
"-l",
"",
"enable line ending processing"),
334 M(
"-n",
"",
"assume 'while gets(); ... end' loop around your script"),
335 M(
"-p",
"",
"assume loop like -n but print line also like sed"),
336 M(
"-rlibrary",
"",
"require the library before executing your script"),
337 M(
"-s",
"",
"enable some switch parsing for switches after script name"),
338 M(
"-S",
"",
"look for the script using PATH environment variable"),
339 M(
"-v",
"",
"print the version number, then turn on verbose mode"),
340 M(
"-w",
"",
"turn warnings on for your script"),
341 M(
"-W[level=2|:category]",
"",
"set warning level; 0=silence, 1=medium, 2=verbose"),
342 M(
"-x[directory]",
"",
"strip off text before #!ruby line and perhaps cd to directory"),
343 M(
"--jit",
"",
"enable JIT for the platform, same as " PLATFORM_JIT_OPTION),
345 M(
"--yjit",
"",
"enable in-process JIT compiler"),
348 M(
"--rjit",
"",
"enable pure-Ruby JIT compiler (experimental)"),
350 M(
"-h",
"",
"show this message, --help for more info"),
352 STATIC_ASSERT(usage_msg_size, numberof(usage_msg) < 25);
355 M(
"--copyright",
"",
"print the copyright"),
356 M(
"--dump={insns|parsetree|...}[,...]",
"",
357 "dump debug information. see below for available dump list"),
358 M(
"--enable={jit|rubyopt|...}[,...]",
", --disable={jit|rubyopt|...}[,...]",
359 "enable or disable features. see below for available features"),
360 M(
"--external-encoding=encoding",
", --internal-encoding=encoding",
361 "specify the default external or internal character encoding"),
362 M(
"--parser={parse.y|prism}",
", --parser=prism",
363 "the parser used to parse Ruby code (experimental)"),
364 M(
"--backtrace-limit=num",
"",
"limit the maximum length of backtrace"),
365 M(
"--verbose",
"",
"turn on verbose mode and disable script from stdin"),
366 M(
"--version",
"",
"print the version number, then exit"),
367 M(
"--crash-report=TEMPLATE",
"",
"template of crash report files"),
368 M(
"-y",
", --yydebug",
"print log of parser. Backward compatibility is not guaranteed"),
369 M(
"--help",
"",
"show this message, -h for short message"),
372 M(
"insns",
"",
"instruction sequences"),
373 M(
"insns_without_opt",
"",
"instruction sequences compiled with no optimization"),
374 M(
"yydebug(+error-tolerant)",
"",
"yydebug of yacc parser generator"),
375 M(
"parsetree(+error-tolerant)",
"",
"AST"),
376 M(
"parsetree_with_comment(+error-tolerant)",
"",
"AST with comments"),
377 M(
"prism_parsetree",
"",
"Prism AST with comments"),
380 M(
"gems",
"",
"rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED
")"),
381 M(
"error_highlight",
"",
"error_highlight (default: "DEFAULT_RUBYGEMS_ENABLED
")"),
382 M(
"did_you_mean",
"",
"did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED
")"),
383 M(
"syntax_suggest",
"",
"syntax_suggest (default: "DEFAULT_RUBYGEMS_ENABLED
")"),
384 M(
"rubyopt",
"",
"RUBYOPT environment variable (default: enabled)"),
385 M(
"frozen-string-literal",
"",
"freeze all string literals (default: disabled)"),
387 M(
"yjit",
"",
"in-process JIT compiler (default: disabled)"),
390 M(
"rjit",
"",
"pure-Ruby JIT compiler (experimental, default: disabled)"),
394 M(
"deprecated",
"",
"deprecated features"),
395 M(
"experimental",
"",
"experimental features"),
396 M(
"performance",
"",
"performance issues"),
402 const char *sb = highlight ? esc_standout+1 : esc_none;
403 const char *se = highlight ? esc_reset : esc_none;
404 const int num = numberof(usage_msg) - (help ? 1 : 0);
405 unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16;
406#define SHOW(m) show_usage_line(&(m), help, highlight, w, columns)
408 printf(
"%sUsage:%s %s [switches] [--] [programfile] [arguments]\n", sb, se, name);
409 for (i = 0; i < num; ++i)
414 if (highlight) sb = esc_standout;
416 for (i = 0; i < numberof(help_msg); ++i)
418 printf(
"%s""Dump List:%s\n", sb, se);
419 for (i = 0; i < numberof(dumps); ++i)
421 printf(
"%s""Features:%s\n", sb, se);
422 for (i = 0; i < numberof(features); ++i)
424 printf(
"%s""Warning categories:%s\n", sb, se);
425 for (i = 0; i < numberof(warn_categories); ++i)
426 SHOW(warn_categories[i]);
428 printf(
"%s""YJIT options:%s\n", sb, se);
429 rb_yjit_show_usage(help, highlight, w, columns);
432 printf(
"%s""RJIT options (experimental):%s\n", sb, se);
433 for (i = 0; rb_rjit_option_messages[i].str; ++i)
434 SHOW(rb_rjit_option_messages[i]);
438#define rubylib_path_new rb_str_new
441push_include(
const char *path,
VALUE (*filter)(
VALUE))
445 VALUE load_path = GET_VM()->load_path;
452 for (s = p; *s && *s != sep; s = CharNext(s));
453 rb_ary_push(load_path, (*filter)(rubylib_path_new(p, s - p)));
460push_include_cygwin(
const char *path,
VALUE (*filter)(
VALUE))
463 char rubylib[FILENAME_MAX];
472 for (s = p; *s && *s !=
';'; s = CharNext(s));
477 p = RSTRING_PTR(buf);
480 rb_str_resize(buf,
len);
481 p = strncpy(RSTRING_PTR(buf), p,
len);
484#ifdef HAVE_CYGWIN_CONV_PATH
485#define CONV_TO_POSIX_PATH(p, lib) \
486 cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
488# error no cygwin_conv_path
490 if (CONV_TO_POSIX_PATH(p, rubylib) == 0)
492 push_include(p, filter);
498#define push_include push_include_cygwin
502ruby_push_include(
const char *path,
VALUE (*filter)(
VALUE))
506 push_include(path, filter);
510identical_path(
VALUE path)
515locale_path(
VALUE path)
517 rb_enc_associate(path, rb_locale_encoding());
524 ruby_push_include(path, locale_path);
528expand_include_path(
VALUE path)
530 char *p = RSTRING_PTR(path);
533 if (*p ==
'.' && p[1] ==
'/')
535 return rb_file_expand_path(path,
Qnil);
539ruby_incpush_expand(
const char *path)
541 ruby_push_include(path, expand_include_path);
545#if defined _WIN32 || defined __CYGWIN__
546static HMODULE libruby;
549DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
551 if (reason == DLL_PROCESS_ATTACH)
557rb_libruby_handle(
void)
563translit_char_bin(
char *p,
int from,
int to)
566 if ((
unsigned char)*p == from)
575# define chdir rb_w32_uchdir
583# define IF_UTF8_PATH(t, f) t
585# define IF_UTF8_PATH(t, f) f
597# define str_conv_enc(str, from, to) (str)
602#if defined(LOAD_RELATIVE)
604runtime_libruby_path(
void)
606#if defined _WIN32 || defined __CYGWIN__
614 while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath),
615 ret = GetModuleFileNameW(libruby, wlibpath,
len),
619 rb_str_set_len(wsopath, (
len +=
len)*
sizeof(WCHAR));
621 if (!ret || ret >
len) rb_fatal(
"failed to get module file name");
622#if defined __CYGWIN__
624 const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
625 size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0);
626 if (!newsize) rb_fatal(
"failed to convert module path to cygwin");
628 libpath = RSTRING_PTR(path);
629 if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) {
630 rb_str_resize(path, 0);
636 for (
len = ret, i = 0; i <
len; ++i) {
637 if (wlibpath[i] == L
'\\') {
643 len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL);
645 libpath = RSTRING_PTR(path);
646 WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath,
len, NULL, NULL);
648 rb_str_resize(wsopath, 0);
650#elif defined(HAVE_DLADDR)
653 const void* addr = (
void *)(
VALUE)expand_include_path;
655 if (!dladdr((
void *)addr, &dli)) {
659 else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
661 path = rb_readlink(fname, NULL);
666 path = rb_realpath_internal(
Qnil, fname, 1);
668 rb_str_resize(fname, 0);
671# error relative load path is not supported on this platform.
676#define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index")
678VALUE ruby_archlibdir_path, ruby_prefix_path;
683 VALUE load_path, archlibdir = 0;
684 ID id_initial_load_path_mark;
685 const char *paths = ruby_initial_load_paths;
687#if defined LOAD_RELATIVE
688#if !defined ENABLE_MULTIARCH
689# define RUBY_ARCH_PATH ""
690#elif defined RUBY_ARCH
691# define RUBY_ARCH_PATH "/"RUBY_ARCH
693# define RUBY_ARCH_PATH "/"RUBY_PLATFORM
700 sopath = runtime_libruby_path();
701 libpath = RSTRING_PTR(sopath);
703 p = strrchr(libpath,
'/');
705 static const char libdir[] =
"/"
706#ifdef LIBDIR_BASENAME
712 const ptrdiff_t libdir_len = (ptrdiff_t)
sizeof(libdir)
714 static const char bindir[] =
"/bin";
715 const ptrdiff_t bindir_len = (ptrdiff_t)
sizeof(bindir) - 1;
717 const char *p2 = NULL;
719#ifdef ENABLE_MULTIARCH
722 if (p - libpath >= bindir_len && !
STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
724 archlibdir = rb_str_subseq(sopath, 0, p - libpath);
728 else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
729 archlibdir = rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath);
733#ifdef ENABLE_MULTIARCH
739 p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
740 if (p)
goto multiarch;
744 baselen = p - libpath;
749 rb_str_resize(sopath, baselen);
750 libpath = RSTRING_PTR(sopath);
751#define PREFIX_PATH() sopath
752#define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
753#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
755 const size_t exec_prefix_len = strlen(ruby_exec_prefix);
756#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len))
757#define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
759 rb_gc_register_address(&ruby_prefix_path);
760 ruby_prefix_path = PREFIX_PATH();
762 if (!archlibdir) archlibdir = ruby_prefix_path;
763 rb_gc_register_address(&ruby_archlibdir_path);
764 ruby_archlibdir_path = archlibdir;
766 load_path = GET_VM()->load_path;
768 ruby_push_include(getenv(
"RUBYLIB"), identical_path);
770 id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
772 size_t len = strlen(paths);
773 VALUE path = RUBY_RELATIVE(paths,
len);
774 rb_ivar_set(path, id_initial_load_path_mark, path);
775 rb_ary_push(load_path, path);
784add_modules(
VALUE *req_list,
const char *mod)
786 VALUE list = *req_list;
790 *req_list = list = rb_ary_hidden_new(0);
793 rb_ary_push(list, feature);
797require_libraries(
VALUE *req_list)
799 VALUE list = *req_list;
800 VALUE self = rb_vm_top_self();
802 rb_encoding *extenc = rb_default_external_encoding();
806 VALUE feature = rb_ary_shift(list);
807 rb_enc_associate(feature, extenc);
810 rb_funcallv(self, require, 1, &feature);
822process_sflag(
int sflag)
840 if (s[1] ==
'-' && s[2] ==
'\0')
845 for (p = s + 1; *p; p++) {
854 else if (*p !=
'_' && !
ISALNUM(*p)) {
857 rb_str_new2(
"invalid name for global variable - ");
858 if (!(p = strchr(p,
'='))) {
862 rb_str_cat(name_error[0], s, p - s);
864 name_error[1] = args[-1];
870 for (p = s + 1; *p; ++p) {
894 VALUE argstr, argary;
897 VALUE src_enc_name = opt->src.enc.name;
898 VALUE ext_enc_name = opt->ext.enc.name;
899 VALUE int_enc_name = opt->intern.enc.name;
902 long backtrace_length_limit = opt->backtrace_length_limit;
903 const char *crash_report = opt->crash_report;
908 opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
910 const int hyphen = *s !=
'-';
914 p = RSTRING_PTR(argstr);
915 if (hyphen) *p =
'-';
916 memcpy(p + hyphen, s,
len + 1);
918 rb_str_cat(argary, (
char *)&ap,
sizeof(ap));
921 rb_str_cat(argary, (
char *)&ap,
sizeof(ap));
922 while (*p && !
ISSPACE(*p)) ++p;
927 argc = RSTRING_LEN(argary) /
sizeof(ap);
929 rb_str_cat(argary, (
char *)&ap,
sizeof(ap));
930 argv = ptr =
ALLOC_N(
char *, argc);
931 MEMMOVE(argv, RSTRING_PTR(argary),
char *, argc);
933 while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) {
945 opt->src.enc.name = src_enc_name;
948 opt->ext.enc.name = ext_enc_name;
951 opt->intern.enc.name = int_enc_name;
953 FEATURE_SET_RESTORE(opt->features, feat);
954 FEATURE_SET_RESTORE(opt->warn, warn);
955 if (BACKTRACE_LENGTH_LIMIT_VALID_P(backtrace_length_limit)) {
956 opt->backtrace_length_limit = backtrace_length_limit;
959 opt->crash_report = crash_report;
964 rb_str_resize(argary, 0);
965 rb_str_resize(argstr, 0);
969name_match_p(
const char *name,
const char *str,
size_t len)
971 if (
len == 0)
return 0;
973 while (
TOLOWER(*str) == *name) {
974 if (!--
len)
return 1;
978 if (*str !=
'-' && *str !=
'_')
return 0;
980 if (*name !=
'-' && *name !=
'_')
return 0;
983 if (--
len == 0)
return 1;
987#define NAME_MATCH_P(name, str, len) \
988 ((len) < (int)sizeof(name) && name_match_p((name), (str), (len)))
990#define UNSET_WHEN(name, bit, str, len) \
991 if (NAME_MATCH_P((name), (str), (len))) { \
992 *(unsigned int *)arg &= ~(bit); \
996#define SET_WHEN(name, bit, str, len) \
997 if (NAME_MATCH_P((name), (str), (len))) { \
998 *(unsigned int *)arg |= (bit); \
1002#define LITERAL_NAME_ELEMENT(name) #name
1005feature_option(
const char *str,
int len,
void *arg,
const unsigned int enable)
1007 static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1009 unsigned int mask = ~0
U;
1010 unsigned int set = 0
U;
1011#if AMBIGUOUS_FEATURE_NAMES
1013# define FEATURE_FOUND ++matched
1015# define FEATURE_FOUND goto found
1017#define SET_FEATURE(bit) \
1018 if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;}
1019 EACH_FEATURES(SET_FEATURE, ;);
1020 if (NAME_MATCH_P(
"jit", str,
len)) {
1021 set |= mask = FEATURE_BIT(jit);
1024 if (NAME_MATCH_P(
"all", str,
len)) {
1026 mask &= ~feature_jit_mask | FEATURE_BIT(jit);
1029#if AMBIGUOUS_FEATURE_NAMES
1030 if (matched == 1)
goto found;
1032 VALUE mesg = rb_sprintf(
"ambiguous feature: `%.*s' (",
len, str);
1033#define ADD_FEATURE_NAME(bit) \
1034 if (FEATURE_BIT(bit) & set) { \
1035 rb_str_cat_cstr(mesg, #bit); \
1036 if (--matched) rb_str_cat_cstr(mesg, ", "); \
1038 EACH_FEATURES(ADD_FEATURE_NAME, ;);
1041#undef ADD_FEATURE_NAME
1046 rb_warn(
"unknown argument for --%s: `%.*s'",
1047 enable ?
"enable" :
"disable",
len, str);
1048 rb_warn(
"features are [%.*s].", (
int)strlen(list), list);
1052 FEATURE_SET_TO(*argp, mask, (mask & enable));
1057enable_option(
const char *str,
int len,
void *arg)
1059 feature_option(str,
len, arg, ~0
U);
1063disable_option(
const char *str,
int len,
void *arg)
1065 feature_option(str,
len, arg, 0
U);
1069int ruby_env_debug_option(
const char *str,
int len,
void *arg);
1072debug_option(
const char *str,
int len,
void *arg)
1074 static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1076#define SET_WHEN_DEBUG(bit) \
1077 if (NAME_MATCH_P(#bit, str, len)) { \
1078 FEATURE_SET(*argp, DEBUG_BIT(bit)); \
1081 EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;);
1085 rb_warn(
"unknown argument for --debug: `%.*s'",
len, str);
1086 rb_warn(
"debug features are [%.*s].", (
int)strlen(list), list);
1090memtermspn(
const char *str,
char term,
int len)
1093 if (
len <= 0)
return 0;
1094 const char *next = memchr(str, term,
len);
1095 return next ? (int)(next - str) :
len;
1098static const char additional_opt_sep =
'+';
1101dump_additional_option(
const char *str,
int len,
unsigned int bits,
const char *name)
1104 for (;
len-- > 0 && *str++ == additional_opt_sep;
len -= w, str += w) {
1105 w = memtermspn(str, additional_opt_sep,
len);
1106#define SET_ADDITIONAL(bit) if (NAME_MATCH_P(#bit, str, w)) { \
1107 if (bits & DUMP_BIT(bit)) \
1108 rb_warn("duplicate option to dump %s: `%.*s'", name, w, str); \
1109 bits |= DUMP_BIT(bit); \
1112 if (dump_error_tolerant_bits & bits) {
1113 SET_ADDITIONAL(error_tolerant);
1115 rb_warn(
"don't know how to dump %s with `%.*s'", name, w, str);
1121dump_option(
const char *str,
int len,
void *arg)
1123 static const char list[] = EACH_DUMPS(LITERAL_NAME_ELEMENT,
", ");
1124 int w = memtermspn(str, additional_opt_sep,
len);
1126#define SET_WHEN_DUMP(bit) \
1127 if (NAME_MATCH_P(#bit, (str), (w))) { \
1128 *(unsigned int *)arg |= \
1129 dump_additional_option(str + w, len - w, DUMP_BIT(bit), #bit); \
1132 EACH_DUMPS(SET_WHEN_DUMP, ;);
1133 rb_warn(
"don't know how to dump `%.*s',",
len, str);
1134 rb_warn(
"but only [%.*s].", (
int)strlen(list), list);
1138set_option_encoding_once(
const char *
type,
VALUE *name,
const char *e,
long elen)
1142 if (!elen) elen = strlen(e);
1148 "%s already set to %"PRIsVALUE,
type, *name);
1153#define set_internal_encoding_once(opt, e, elen) \
1154 set_option_encoding_once("default_internal", &(opt)->intern.enc.name, (e), (elen))
1155#define set_external_encoding_once(opt, e, elen) \
1156 set_option_encoding_once("default_external", &(opt)->ext.enc.name, (e), (elen))
1157#define set_source_encoding_once(opt, e, elen) \
1158 set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen))
1160#define yjit_opt_match_noarg(s, l, name) \
1161 opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --yjit-" name " is ignored"), 1) : 1)
1162#define yjit_opt_match_arg(s, l, name) \
1163 opt_match(s, l, name) && (*(s) && *(s+1) ? 1 : (rb_raise(rb_eRuntimeError, "--yjit-" name " needs an argument"), 0))
1167setup_yjit_options(
const char *s)
1170 bool rb_yjit_parse_option(
const char* s);
1171 bool success = rb_yjit_parse_option(s);
1179 "invalid YJIT option `%s' (--help will show valid yjit options)",
1202 unsigned int bits = 0;
1203 static const char no_prefix[] =
"no-";
1204 int enable = strncmp(s += 2, no_prefix,
sizeof(no_prefix)-1) != 0;
1205 if (!enable) s +=
sizeof(no_prefix)-1;
1206 size_t len = strlen(s);
1207 if (NAME_MATCH_P(
"deprecated", s,
len)) {
1210 else if (NAME_MATCH_P(
"experimental", s,
len)) {
1213 else if (NAME_MATCH_P(
"performance", s,
len)) {
1217 rb_warn(
"unknown warning category: `%s'", s);
1219 if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0);
1232 if (!opt->warning) {
1248 FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS, 0);
1254 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1273 if (!opt->e_script) {
1275 if (opt->script == 0)
1288 const char *enc_name = 0;
1291 enc_name =
"EUC-JP";
1294 enc_name =
"Windows-31J";
1299 case 'N':
case 'n':
case 'A':
case 'a':
1300 enc_name =
"ASCII-8BIT";
1305 if (!opt->ext.enc.name)
1306 opt->ext.enc.name = opt->src.enc.name;
1325 else if (v == 0 && numlen >= 2) {
1340# define set_encoding_part(type) \
1341 if (!(p = strchr(s, ':'))) { \
1342 set_##type##_encoding_once(opt, s, 0); \
1346 set_##type##_encoding_once(opt, s, p-s); \
1348 set_encoding_part(external);
1349 if (!*(s = ++p))
return;
1350 set_encoding_part(internal);
1351 if (!*(s = ++p))
return;
1352#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1353 set_encoding_part(source);
1354 if (!*(s = ++p))
return;
1357# undef set_encoding_part
1366# define is_option_end(c, allow_hyphen) \
1367 (!(c) || ((allow_hyphen) && (c) == '-') || (c) == '=')
1368# define check_envopt(name, allow_envopt) \
1369 (((allow_envopt) || !envopt) ? (void)0 : \
1370 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
1371# define need_argument(name, s, needs_arg, next_arg) \
1372 ((*(s) ? !*++(s) : (next_arg) && (!argc || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
1373 rb_raise(rb_eRuntimeError, "missing argument for --" name) \
1375# define is_option_with_arg(name, allow_hyphen, allow_envopt) \
1376 is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue, Qtrue)
1377# define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg, next_arg) \
1378 (strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) && \
1379 (s[n] != '-' || s[n+1]) ? \
1380 (check_envopt(name, (allow_envopt)), s += n, \
1381 need_argument(name, s, needs_arg, next_arg), 1) : 0)
1383 if (strcmp(
"copyright", s) == 0) {
1384 if (envopt)
goto noenvopt_long;
1385 opt->dump |= DUMP_BIT(copyright);
1396 else if (is_option_with_arg(
"enable",
Qtrue,
Qtrue)) {
1399 else if (is_option_with_arg(
"disable",
Qtrue,
Qtrue)) {
1402 else if (is_option_with_arg(
"encoding",
Qfalse,
Qtrue)) {
1403 proc_encoding_option(opt, s,
"--encoding");
1405 else if (is_option_with_arg(
"internal-encoding",
Qfalse,
Qtrue)) {
1406 set_internal_encoding_once(opt, s, 0);
1408 else if (is_option_with_arg(
"external-encoding",
Qfalse,
Qtrue)) {
1409 set_external_encoding_once(opt, s, 0);
1411 else if (is_option_with_arg(
"parser",
Qfalse,
Qtrue)) {
1412 if (strcmp(
"prism", s) == 0) {
1413 (*rb_ruby_prism_ptr()) =
true;
1414 rb_warn(
"The compiler based on the Prism parser is currently experimental and "
1415 "compatibility with the compiler based on parse.y "
1416 "is not yet complete. Please report any issues you "
1417 "find on the `ruby/prism` issue tracker.");
1419 else if (strcmp(
"parse.y", s) == 0) {
1426#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1427 else if (is_option_with_arg(
"source-encoding",
Qfalse,
Qtrue)) {
1428 set_source_encoding_once(opt, s, 0);
1431 else if (strcmp(
"version", s) == 0) {
1432 if (envopt)
goto noenvopt_long;
1433 opt->dump |= DUMP_BIT(version);
1435 else if (strcmp(
"verbose", s) == 0) {
1439 else if (strcmp(
"jit", s) == 0) {
1440#if USE_YJIT || USE_RJIT
1441 FEATURE_SET(opt->features, FEATURE_BIT(jit));
1443 rb_warn(
"Ruby was built without JIT support");
1446 else if (is_option_with_optarg(
"rjit",
'-',
true,
false,
false)) {
1448 extern void rb_rjit_setup_options(
const char *s,
struct rb_rjit_options *rjit_opt);
1449 FEATURE_SET(opt->features, FEATURE_BIT(rjit));
1450 rb_rjit_setup_options(s, &opt->rjit);
1452 rb_warn(
"RJIT support is disabled.");
1455 else if (is_option_with_optarg(
"yjit",
'-',
true,
false,
false)) {
1457 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
1458 setup_yjit_options(s);
1460 rb_warn(
"Ruby was built without YJIT support."
1461 " You may need to install rustc to build Ruby with YJIT.");
1464 else if (strcmp(
"yydebug", s) == 0) {
1465 if (envopt)
goto noenvopt_long;
1466 opt->dump |= DUMP_BIT(yydebug);
1471 else if (strcmp(
"help", s) == 0) {
1472 if (envopt)
goto noenvopt_long;
1473 opt->dump |= DUMP_BIT(help);
1476 else if (is_option_with_arg(
"backtrace-limit",
Qfalse,
Qtrue)) {
1478 long n = strtol(s, &e, 10);
1479 if (
errno == ERANGE || !BACKTRACE_LENGTH_LIMIT_VALID_P(n) || *e) {
1483 opt->backtrace_length_limit = n;
1486 else if (is_option_with_arg(
"crash-report",
true,
true)) {
1487 opt->crash_report = s;
1491 "invalid option --%s (-h will show valid options)", s);
1493 return argc0 - argc + 1;
1497# undef is_option_end
1499# undef need_argument
1500# undef is_option_with_arg
1501# undef is_option_with_optarg
1508 long n, argc0 = argc;
1510 int warning = opt->warning;
1512 if (argc <= 0 || !argv)
1515 for (argc--, argv++; argc > 0; argc--, argv++) {
1516 const char *
const arg = argv[0];
1517 if (!arg || arg[0] !=
'-' || !arg[1])
1524 if (envopt)
goto noenvopt;
1525 opt->do_split = TRUE;
1530 if (envopt)
goto noenvopt;
1531 opt->do_print = TRUE;
1534 if (envopt)
goto noenvopt;
1535 opt->do_loop = TRUE;
1546 if (envopt)
goto noenvopt;
1547 opt->dump |= DUMP_BIT(yydebug);
1556 opt->dump |= DUMP_BIT(version_v);
1559 if (!opt->warning) {
1563 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1568 if (!(s = proc_W_option(opt, s, &warning)))
break;
1572 if (envopt)
goto noenvopt;
1573 opt->dump |= DUMP_BIT(syntax);
1578 if (envopt)
goto noenvopt;
1580 if (!opt->sflag) opt->sflag = 1;
1585 if (envopt)
goto noenvopt;
1586 opt->dump |= DUMP_BIT(usage);
1590 if (envopt)
goto noenvopt;
1591 opt->do_line = TRUE;
1597 if (envopt)
goto noenvopt;
1599 opt->do_search = TRUE;
1604 if (envopt)
goto noenvopt;
1605 if (!(n = proc_e_option(opt, s, argc, argv)))
break;
1614 add_modules(&opt->req_list, s);
1616 else if (argc > 1) {
1617 add_modules(&opt->req_list, argv[1]);
1623 if (envopt)
goto noenvopt;
1625 ruby_set_inplace_mode(s + 1);
1629 if (envopt)
goto noenvopt;
1633 if (*s && chdir(s) < 0) {
1634 rb_fatal(
"Can't chdir to %s", s);
1640 if (envopt)
goto noenvopt;
1641 if (!*++s && (!--argc || !(s = *++argv) || !*s)) {
1642 rb_fatal(
"Can't chdir");
1645 rb_fatal(
"Can't chdir to %s", s);
1650 if (envopt)
goto noenvopt;
1657 if (!*++s && (!--argc || !(s = *++argv))) {
1660 proc_encoding_option(opt, s,
"-E");
1664 set_internal_encoding_once(opt,
"UTF-8", 0);
1669 if (!(s = proc_K_option(opt, s)))
break;
1675 ruby_incpush_expand(s);
1676 else if (argc > 1) {
1677 ruby_incpush_expand(argv[1]);
1683 if (envopt)
goto noenvopt;
1684 if (!(s = proc_0_option(opt, s)))
break;
1688 if (!s[1] || (s[1] ==
'\r' && !s[2])) {
1694 if (!(n = proc_long_options(opt, s, argc, argv, envopt)))
goto switch_end;
1706 "invalid option -%c (-h will show valid options)",
1707 (
int)(
unsigned char)*s);
1721 if (warning) opt->warning = warning;
1722 return argc0 - argc;
1725void Init_builtin_features(
void);
1728ruby_init_prelude(
void)
1730 Init_builtin_features();
1734void rb_call_builtin_inits(
void);
1738#if RBIMPL_HAS_ATTRIBUTE(weak)
1742Init_extra_exts(
void)
1749 if (opt->dump & dump_exit_bits)
return;
1751 if (FEATURE_SET_P(opt->features, gems)) {
1753 if (opt->features.set & FEATURE_BIT(error_highlight)) {
1756 if (opt->features.set & FEATURE_BIT(did_you_mean)) {
1759 if (opt->features.set & FEATURE_BIT(syntax_suggest)) {
1764 rb_warning_category_update(opt->warn.mask, opt->warn.set);
1768 if (getenv(
"RUBY_GC_HEAP_INIT_SLOTS")) {
1769 rb_warn_deprecated(
"The environment variable RUBY_GC_HEAP_INIT_SLOTS",
1770 "environment variables RUBY_GC_HEAP_%d_INIT_SLOTS");
1773 if (getenv(
"RUBY_FREE_AT_EXIT")) {
1775 rb_free_at_exit =
true;
1781 rb_rjit_enabled =
true;
1782 if (opt->rjit.stats)
1783 rb_rjit_stats_enabled =
true;
1784 if (opt->rjit.trace_exits)
1785 rb_rjit_trace_exits_enabled =
true;
1790 rb_call_builtin_inits();
1791 ruby_init_prelude();
1797 rb_rjit_init(&opt->rjit);
1800 rb_yjit_init(opt->yjit);
1804 require_libraries(&opt->req_list);
1808opt_enc_index(
VALUE enc_name)
1810 const char *s = RSTRING_PTR(enc_name);
1811 int i = rb_enc_find_index(s);
1816 else if (rb_enc_dummy_p(rb_enc_from_index(i))) {
1822#define rb_progname (GET_VM()->progname)
1823#define rb_orig_progname (GET_VM()->orig_progname)
1839#define rb_define_readonly_boolean(name, val) \
1840 rb_define_virtual_variable((name), (val) ? true_value : false_value, 0)
1849 rb_raise(
rb_eTypeError,
"$_ value need to be String (%s given)",
1931setup_pager_env(
void)
1933 if (!getenv(
"LESS")) {
1935 ruby_setenv(
"LESS",
"-R +/^[A-Z].*");
1943 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
1945 if (!GetConsoleMode(h, &m))
return 0;
1946# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
1947# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
1949 if (!(m & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;
1952#elif !defined(HAVE_WORKING_FORK)
1953# define tty_enabled() 0
1962 return rb_enc_associate(rb_str_dup(str), enc);
1970env_var_truthy(
const char *name)
1972 const char *value = getenv(name);
1976 if (strcmp(value,
"1") == 0)
1978 if (strcmp(value,
"true") == 0)
1980 if (strcmp(value,
"yes") == 0)
1987rb_pid_t rb_fork_ruby(
int *status);
1993 VALUE parser = rb_parser_new();
1995 if (opt->dump & DUMP_BIT(yydebug)) {
1996 rb_parser_set_yydebug(parser,
Qtrue);
1999 if (opt->dump & DUMP_BIT(error_tolerant)) {
2000 rb_parser_error_tolerant(parser);
2003 if (opt->e_script) {
2004 VALUE progname = rb_progname;
2005 rb_parser_set_context(parser, 0, TRUE);
2009 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2010 opt->do_line, opt->do_split);
2011 ast = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
2015 int xflag = opt->xflag;
2016 f = open_load_file(opt->script_name, &xflag);
2017 opt->xflag = xflag != 0;
2018 rb_parser_set_context(parser, 0, f ==
rb_stdin);
2019 ast = load_file(parser, opt->script_name, f, 1, opt);
2021 if (!ast->body.root) {
2022 rb_ast_dispose(ast);
2033 if (strcmp(opt->script,
"-") == 0) {
2034 rb_warn(
"Prism support for streaming code from stdin is not currently supported");
2035 pm_string_constant_init(input,
"", 0);
2036 pm_options_filepath_set(options,
"-e");
2038 else if (opt->e_script) {
2039 pm_string_constant_init(input, RSTRING_PTR(opt->e_script), RSTRING_LEN(opt->e_script));
2040 pm_options_filepath_set(options,
"-e");
2043 pm_string_mapped_init(input, RSTRING_PTR(opt->script_name));
2044 pm_options_filepath_set(options, RSTRING_PTR(opt->script_name));
2052 pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
2058 pm_prettyprint(&output_buffer, &parser, node);
2062 pm_buffer_free(&output_buffer);
2063 pm_node_destroy(&parser, node);
2064 pm_parser_free(&parser);
2076#define dispose_result() \
2077 (ast ? rb_ast_dispose(ast) : (pm_string_free(&pm_input), pm_options_free(&pm_options)))
2086 char fbuf[MAXPATHLEN];
2087 int i = (int)proc_options(argc, argv, opt, 0);
2088 unsigned int dump = opt->dump & dump_exit_bits;
2090 const long loaded_before_enc =
RARRAY_LEN(vm->loaded_features);
2092 if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
2093 int tty = isatty(1);
2094 const char *
const progname =
2095 (argc > 0 && argv && argv[0] ? argv[0] :
2096 origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] :
2099 if ((opt->dump & DUMP_BIT(help)) && tty) {
2100 const char *pager_env = getenv(
"RUBY_PAGER");
2101 if (!pager_env) pager_env = getenv(
"PAGER");
2102 if (pager_env && *pager_env && isatty(0)) {
2103 const char *columns_env = getenv(
"COLUMNS");
2104 if (columns_env) columns = atoi(columns_env);
2106#ifdef HAVE_WORKING_FORK
2109 rb_pid_t pid = rb_fork_ruby(NULL);
2114 else if (pid == 0) {
2132 int oldout = dup(1);
2133 int olderr = dup(2);
2134 int fd =
RFILE(port)->fptr->fd;
2135 tty = tty_enabled();
2138 usage(progname, 1, tty, columns);
2148 usage(progname, (opt->dump & DUMP_BIT(help)), tty, columns);
2155 if (FEATURE_SET_P(opt->features, rubyopt) && (s = getenv(
"RUBYOPT"))) {
2156 moreswitches(s, opt, 1);
2159 if (opt->src.enc.name)
2163 rb_warning(
"-K is specified; it is for 1.8 compatibility and may cause odd behavior");
2165 if (!(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2167 if (!FEATURE_USED_P(opt->features, yjit) && env_var_truthy(
"RUBY_YJIT_ENABLE")) {
2168 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
2172 if (MULTI_BITS_P(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2173 rb_warn(
"RJIT and YJIT cannot both be enabled at the same time. Exiting");
2178 if (FEATURE_SET_P(opt->features, rjit)) {
2179 opt->rjit.on =
true;
2183 if (FEATURE_SET_P(opt->features, yjit)) {
2184 bool rb_yjit_option_disable(
void);
2185 opt->yjit = !rb_yjit_option_disable();
2189 ruby_mn_threads_params();
2190 Init_ruby_description(opt);
2192 if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
2194 if (opt->dump & DUMP_BIT(version))
return Qtrue;
2196 if (opt->dump & DUMP_BIT(copyright)) {
2201 if (!opt->e_script) {
2208 opt->script = argv[0];
2209 if (!opt->script || opt->script[0] ==
'\0') {
2212 else if (opt->do_search) {
2213 const char *path = getenv(
"RUBYPATH");
2217 opt->script = dln_find_file_r(argv[0], path, fbuf,
sizeof(fbuf));
2220 opt->script = dln_find_file_r(argv[0], getenv(
PATH_ENV), fbuf,
sizeof(fbuf));
2223 opt->script = argv[0];
2228 if (opt->script[0] ==
'-' && !opt->script[1]) {
2229 forbid_setid(
"program input from stdin");
2234 opt->script = RSTRING_PTR(opt->script_name);
2237 translit_char_bin(RSTRING_PTR(opt->script_name),
'\\',
'/');
2239 translit_char(RSTRING_PTR(opt->script_name),
'\\',
'/');
2242 ruby_gc_set_params();
2246 lenc = rb_locale_encoding();
2247 rb_enc_associate(rb_progname, lenc);
2248 rb_obj_freeze(rb_progname);
2249 if (opt->ext.enc.name != 0) {
2250 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2252 if (opt->intern.enc.name != 0) {
2253 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2255 if (opt->src.enc.name != 0) {
2256 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2257 src_encoding_index = opt->src.enc.index;
2259 if (opt->ext.enc.index >= 0) {
2260 enc = rb_enc_from_index(opt->ext.enc.index);
2263 enc = IF_UTF8_PATH(uenc, lenc);
2265 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2266 if (opt->intern.enc.index >= 0) {
2267 enc = rb_enc_from_index(opt->intern.enc.index);
2268 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2269 opt->intern.enc.index = -1;
2274 rb_enc_associate(opt->script_name, IF_UTF8_PATH(uenc, lenc));
2277 opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
2278 opt->script = RSTRING_PTR(opt->script_name);
2281 rb_obj_freeze(opt->script_name);
2282 if (IF_UTF8_PATH(uenc != lenc, 1)) {
2284 VALUE load_path = vm->load_path;
2285 const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
2286 int modifiable = FALSE;
2288 rb_get_expanded_load_path();
2289 for (i = 0; i <
RARRAY_LEN(load_path); ++i) {
2291 int mark = rb_attr_get(path, id_initial_load_path_mark) == path;
2294 if (newpath == path)
continue;
2297 if (!(path = copy_str(path, lenc, !mark)))
continue;
2299 if (mark)
rb_ivar_set(path, id_initial_load_path_mark, path);
2301 rb_ary_modify(load_path);
2307 rb_ary_replace(vm->load_path_snapshot, load_path);
2311 VALUE loaded_features = vm->loaded_features;
2312 bool modified =
false;
2313 for (
long i = loaded_before_enc; i <
RARRAY_LEN(loaded_features); ++i) {
2315 if (!(path = copy_str(path, IF_UTF8_PATH(uenc, lenc),
true)))
continue;
2317 rb_ary_modify(loaded_features);
2323 rb_ary_replace(vm->loaded_features_snapshot, loaded_features);
2327 if (opt->features.mask & COMPILATION_FEATURES) {
2328 VALUE option = rb_hash_new();
2329#define SET_COMPILE_OPTION(h, o, name) \
2330 rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \
2331 RBOOL(FEATURE_SET_P(o->features, name)))
2332 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
2333 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
2334 rb_funcallv(rb_cISeq,
rb_intern_const(
"compile_option="), 1, &option);
2335#undef SET_COMPILE_OPTION
2338 opt->sflag = process_sflag(opt->sflag);
2340 if (opt->e_script) {
2342 if (opt->src.enc.index >= 0) {
2343 eenc = rb_enc_from_index(opt->src.enc.index);
2348 if (ienc) eenc = ienc;
2353 opt->e_script = str_conv_enc(opt->e_script, uenc, eenc);
2356 rb_enc_associate(opt->e_script, eenc);
2359 if (!(*rb_ruby_prism_ptr())) {
2360 if (!(ast = process_script(opt)))
return Qfalse;
2363 prism_script(opt, &pm_input, &pm_options);
2366 if ((dump & DUMP_BIT(yydebug)) && !(dump &= ~DUMP_BIT(yydebug))) {
2371 if (opt->ext.enc.index >= 0) {
2372 enc = rb_enc_from_index(opt->ext.enc.index);
2375 enc = IF_UTF8_PATH(uenc, lenc);
2377 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2378 if (opt->intern.enc.index >= 0) {
2380 enc = rb_enc_from_index(opt->intern.enc.index);
2381 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2383 else if (!rb_default_internal_encoding())
2385 rb_enc_set_default_internal(
Qnil);
2386 rb_stdio_set_default_encoding();
2388 opt->sflag = process_sflag(opt->sflag);
2391 if (dump & DUMP_BIT(syntax)) {
2392 printf(
"Syntax OK\n");
2393 dump &= ~DUMP_BIT(syntax);
2394 if (!dump)
return Qtrue;
2404 if (dump & (DUMP_BIT(parsetree)|DUMP_BIT(parsetree_with_comment))) {
2407 int comment = dump & DUMP_BIT(parsetree_with_comment);
2408 tree = rb_parser_dump_tree(ast->body.root, comment);
2411 tree = prism_dump_tree(&pm_input, &pm_options);
2415 dump &= ~DUMP_BIT(parsetree)&~DUMP_BIT(parsetree_with_comment);
2424 if (!opt->e_script && strcmp(opt->script,
"-")) {
2425 path = rb_realpath_internal(
Qnil, opt->script_name, 1);
2428 path = str_conv_enc(path, uenc, lenc);
2432 rb_enc_copy(path, opt->script_name);
2436 bool optimize = !(dump & DUMP_BIT(insns_without_opt));
2439 iseq = rb_iseq_new_main_prism(&pm_input, &pm_options, path);
2443 GetBindingPtr(
rb_const_get(rb_cObject, rb_intern(
"TOPLEVEL_BINDING")),
2445 const struct rb_block *base_block = toplevel_context(toplevel_binding);
2446 iseq = rb_iseq_new_main(&ast->body, opt->script_name, path, vm_block_iseq(base_block), optimize);
2447 rb_ast_dispose(ast);
2451 if (dump & (DUMP_BIT(insns) | DUMP_BIT(insns_without_opt))) {
2454 dump &= ~DUMP_BIT(insns);
2455 if (!dump)
return Qtrue;
2457 if (opt->dump & dump_exit_bits)
return Qtrue;
2459 if (OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt)) {
2460 rb_backtrace_length_limit = opt->backtrace_length_limit;
2463 rb_define_readonly_boolean(
"$-p", opt->do_print);
2464 rb_define_readonly_boolean(
"$-l", opt->do_line);
2465 rb_define_readonly_boolean(
"$-a", opt->do_split);
2467 rb_gvar_ractor_local(
"$-p");
2468 rb_gvar_ractor_local(
"$-l");
2469 rb_gvar_ractor_local(
"$-a");
2471 if ((rb_e_script = opt->e_script) != 0) {
2473 rb_gc_register_mark_object(opt->e_script);
2479 if (opt->e_script) {
2481 rb_exec_event_hook_script_compiled(ec, iseq, opt->e_script);
2485 rb_exec_event_hook_script_compiled(ec, iseq,
Qnil);
2493warn_cr_in_shebang(
const char *str,
long len)
2495 if (str[
len-1] ==
'\n' && str[
len-2] ==
'\r') {
2496 rb_warn(
"shebang line ending with \\r may cause problems");
2500#define warn_cr_in_shebang(str, len) (void)0
2503void rb_reset_argf_lineno(
long n);
2513VALUE rb_script_lines_for(
VALUE path,
bool add);
2516load_file_internal(
VALUE argp_v)
2519 VALUE parser = argp->parser;
2520 VALUE orig_fname = argp->fname;
2521 int script = argp->script;
2529 CONST_ID(set_encoding,
"set_encoding");
2535 int no_src_enc = !opt->src.enc.name;
2536 int no_ext_enc = !opt->ext.enc.name;
2537 int no_int_enc = !opt->intern.enc.name;
2539 enc = rb_ascii8bit_encoding();
2540 rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
2548 if (
len > 2 && str[0] ==
'#' && str[1] ==
'!') {
2549 if (line_start == 1) warn_cr_in_shebang(str,
len);
2563 warn_cr_in_shebang(str,
len);
2566 goto search_shebang;
2571 if (*str ==
'\n') *str-- =
'\0';
2572 if (*str ==
'\r') *str-- =
'\0';
2574 if ((p = strstr(p,
" -")) != 0) {
2576 moreswitches(p + 1, opt, 0);
2582 else if (!
NIL_P(c)) {
2586 if (no_src_enc && opt->src.enc.name) {
2587 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2588 src_encoding_index = opt->src.enc.index;
2590 if (no_ext_enc && opt->ext.enc.name) {
2591 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2593 if (no_int_enc && opt->intern.enc.name) {
2594 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2597 else if (!
NIL_P(c)) {
2603 rb_reset_argf_lineno(0);
2606 if (opt->src.enc.index >= 0) {
2607 enc = rb_enc_from_index(opt->src.enc.index);
2610 enc = rb_locale_encoding();
2613 enc = rb_utf8_encoding();
2615 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2616 opt->do_line, opt->do_split);
2618 VALUE lines = rb_script_lines_for(orig_fname,
true);
2619 if (!
NIL_P(lines)) {
2620 rb_parser_set_script_lines(parser, lines);
2625 rb_enc_associate(f, enc);
2626 return (
VALUE)rb_parser_compile_string_path(parser, orig_fname, f, line_start);
2629 ast = rb_parser_compile_file_path(parser, orig_fname, f, line_start);
2630 rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser));
2631 if (script && rb_parser_end_seen_p(parser)) {
2652disable_nonblock(
int fd)
2654#if defined(HAVE_FCNTL) && defined(F_SETFL)
2655 if (fcntl(fd, F_SETFL, 0) < 0) {
2656 const int e =
errno;
2659 if (e == ENOTSUP)
return 0;
2661# if defined B_UNSUPPORTED
2662 if (e == B_UNSUPPORTED)
return 0;
2671open_load_file(
VALUE fname_v,
int *xflag)
2673 const char *fname = (fname_v = rb_str_encode_ospath(fname_v),
2675 long flen = RSTRING_LEN(fname_v);
2679 if (flen == 1 && fname[0] ==
'-') {
2686 const int MODE_TO_LOAD = O_RDONLY | (
2687#if defined O_NONBLOCK && HAVE_FCNTL
2689 !(O_NONBLOCK & O_ACCMODE) ? O_NONBLOCK :
2691#if defined O_NDELAY && HAVE_FCNTL
2692 !(O_NDELAY & O_ACCMODE) ? O_NDELAY :
2695 int mode = MODE_TO_LOAD;
2696#if defined DOSISH || defined __CYGWIN__
2697# define isdirsep(x) ((x) == '/' || (x) == '\\')
2699 static const char exeext[] =
".exe";
2700 enum {extlen =
sizeof(exeext)-1};
2701 if (flen > extlen && !isdirsep(fname[flen-extlen-1]) &&
2702 STRNCASECMP(fname+flen-extlen, exeext, extlen) == 0) {
2711 if (!rb_gc_for_fd(e)) {
2712 rb_load_fail(fname_v, strerror(e));
2715 rb_load_fail(fname_v, strerror(
errno));
2720 if (MODE_TO_LOAD != O_RDONLY && (e = disable_nonblock(fd)) != 0) {
2722 rb_load_fail(fname_v, strerror(e));
2725 e = ruby_is_fd_loadable(fd);
2729 rb_load_fail(fname_v, strerror(e));
2745restore_load_file(
VALUE arg)
2760 arg.parser = parser;
2762 arg.script = script;
2766 restore_load_file, (
VALUE)&arg);
2779 return rb_parser_load_file(rb_parser_new(), fname_v);
2783rb_parser_load_file(
VALUE parser,
VALUE fname_v)
2787 VALUE f = open_load_file(fname_v, &xflag);
2788 cmdline_options_init(&opt)->xflag = xflag != 0;
2789 return load_file(parser, fname_v, f, 0, &opt);
2804proc_argv0(
VALUE process)
2806 return rb_orig_progname;
2831 return ruby_setproctitle(title);
2835ruby_setproctitle(
VALUE title)
2845 if (origarg.argv == 0)
2848 rb_progname = rb_str_new_frozen(ruby_setproctitle(val));
2852external_str_new_cstr(
const char *p)
2856 str = str_conv_enc(str, NULL, rb_default_external_encoding());
2864set_progname(
VALUE name)
2866 rb_orig_progname = rb_progname = name;
2867 rb_vm_set_progname(rb_progname);
2885 set_progname(rb_str_new_frozen(name));
2891 rb_uid_t uid = getuid();
2892 rb_uid_t euid = geteuid();
2893 rb_gid_t gid = getgid();
2894 rb_gid_t egid = getegid();
2896 if (uid != euid) opt->setids |= 1;
2897 if (egid != gid) opt->setids |= 2;
2904 if (opt->setids & 1)
2906 if (opt->setids & 2)
2911verbose_getter(
ID id,
VALUE *ptr)
2913 return *rb_ruby_verbose_ptr();
2919 *rb_ruby_verbose_ptr() =
RTEST(val) ?
Qtrue : val;
2923opt_W_getter(
ID id,
VALUE *dmy)
2925 VALUE v = *rb_ruby_verbose_ptr();
2940debug_getter(
ID id,
VALUE *dmy)
2942 return *rb_ruby_debug_ptr();
2948 *rb_ruby_debug_ptr() = val;
2961 rb_gvar_ractor_local(
"$VERBOSE");
2962 rb_gvar_ractor_local(
"$-v");
2963 rb_gvar_ractor_local(
"$-w");
2964 rb_gvar_ractor_local(
"$-W");
2965 rb_gvar_ractor_local(
"$DEBUG");
2966 rb_gvar_ractor_local(
"$-d");
2990 for (i = 0; i < argc; i++) {
2991 VALUE arg = external_str_new_cstr(argv[i]);
2994 rb_ary_push(av, arg);
3003 const char *script_name = (argc > 0 && argv[0]) ? argv[0] :
ruby_engine;
3005 (*rb_ruby_prism_ptr()) =
false;
3007 if (!origarg.argv || origarg.argc <= 0) {
3008 origarg.argc = argc;
3009 origarg.argv = argv;
3011 set_progname(external_str_new_cstr(script_name));
3013 rb_gc_register_mark_object(
rb_argv0);
3015#ifndef HAVE_SETPROCTITLE
3016 ruby_init_setproctitle(argc, argv);
3019 iseq = process_options(argc, argv, cmdline_options_init(&opt));
3021 if (opt.crash_report && *opt.crash_report) {
3022 void ruby_set_crash_report(
const char *
template);
3023 ruby_set_crash_report(opt.crash_report);
3025 return (
void*)(
struct RData*)iseq;
3029fill_standard_fds(
void)
3031 int f0, f1, f2, fds[2];
3033 f0 = fstat(0, &buf) == -1 &&
errno == EBADF;
3034 f1 = fstat(1, &buf) == -1 &&
errno == EBADF;
3035 f2 = fstat(2, &buf) == -1 &&
errno == EBADF;
3037 if (pipe(fds) == 0) {
3046 if (pipe(fds) == 0) {
3048 if (f1 && fds[1] != 1)
3050 if (f2 && fds[1] != 2)
3052 if (fds[1] != 1 && fds[1] != 2)
3062 rb_w32_sysinit(argc, argv);
3064 if (*argc >= 0 && *argv) {
3065 origarg.argc = *argc;
3066 origarg.argv = *argv;
3068 fill_standard_fds();
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
#define PATH_SEP_CHAR
Identical to PATH_SEP, except it is of type char.
VALUE rb_define_module(const char *name)
Defines a top-level module.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ISSPACE
Old name of rb_isspace.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define STRNCASECMP
Old name of st_locale_insensitive_strncasecmp.
#define TOLOWER
Old name of rb_tolower.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
#define NIL_P
Old name of RB_NIL_P.
#define scan_oct(s, l, e)
Old name of ruby_scan_oct.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ISALNUM
Old name of rb_isalnum.
#define rb_str_new4
Old name of rb_str_new_frozen.
void ruby_script(const char *name)
Sets the current script name to this value.
void ruby_set_argv(int argc, char **argv)
Sets argv that ruby understands.
void ruby_set_script_name(VALUE name)
Sets the current script name to this value.
void ruby_init_loadpath(void)
Sets up $LOAD_PATH.
void * ruby_process_options(int argc, char **argv)
Identical to ruby_options(), except it raises ruby-level exceptions on failure.
void ruby_prog_init(void)
Defines built-in variables.
void ruby_incpush(const char *path)
Appends the given path to the end of the load path.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNameError
NameError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
VALUE rb_eSecurityError
SecurityError exception.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
VALUE rb_mProcess
Process module.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_stdin
STDIN constant.
VALUE rb_stdout
STDOUT constant.
VALUE rb_cString
String class.
void ruby_show_copyright(void)
Prints the copyright notice of the CRuby interpreter to stdout.
void ruby_sysinit(int *argc, char ***argv)
Initializes the process for libruby.
void ruby_show_version(void)
Prints the version information of the CRuby interpreter to stdout.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
Identical to rb_str_conv_enc(), except it additionally takes IO encoder options.
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
VALUE rb_funcall_passing_block(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv_public(), except you can pass the passed block.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_io_gets(VALUE io)
Reads a "line" from the given IO.
VALUE rb_io_ungetbyte(VALUE io, VALUE b)
Identical to rb_io_ungetc(), except it doesn't take the encoding of the passed IO into account.
VALUE rb_io_getbyte(VALUE io)
Reads a byte from the given IO.
VALUE rb_io_fdopen(int fd, int flags, const char *path)
Creates an IO instance whose backend is the given file descriptor.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
VALUE rb_fs
The field separator character for inputs, or the $;.
VALUE rb_output_rs
The record separator character for outputs, or the $\.
int rb_pipe(int *pipes)
This is an rb_cloexec_pipe() + rb_update_max_fd() combo.
VALUE rb_io_close(VALUE io)
Closes the IO.
void rb_lastline_set(VALUE str)
Updates $_.
VALUE rb_lastline_get(void)
Queries the last line, or the $_.
rb_pid_t rb_waitpid(rb_pid_t pid, int *status, int flags)
Waits for a process, with releasing GVL.
VALUE rb_f_exec(int argc, const VALUE *argv)
Replaces the current process by running the given external command.
VALUE rb_reg_new(const char *src, long len, int opts)
Creates a new Regular expression.
#define rb_utf8_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "UTF-8" encoding.
#define rb_str_new_lit(str)
Identical to rb_str_new_static(), except it cannot take string variables.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_external_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "defaultexternal" encoding.
#define rb_strlen_lit(str)
Length of a string literal.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
VALUE rb_const_remove(VALUE space, ID name)
Identical to rb_mod_remove_const(), except it takes the name as ID instead of VALUE.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
rb_gvar_setter_t rb_gvar_readonly_setter
This function just raises rb_eNameError.
VALUE rb_gv_set(const char *name, VALUE val)
Assigns to a global variable.
@ RUBY_IO_READABLE
IO::READABLE
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
int len
Length of the buffer.
void ruby_each_words(const char *str, void(*func)(const char *word, int len, void *argv), void *argv)
Scans the passed string, with calling the callback function every time it encounters a "word".
const char ruby_engine[]
This is just "ruby" for us.
const int ruby_patchlevel
This is a monotonic increasing integer that describes specific "patch" level.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define MEMMOVE(p1, p2, type, n)
Handy macro to call memmove.
void rb_define_hooked_variable(const char *q, VALUE *w, type *e, void_type *r)
Define a function-backended global variable.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define RARRAY_LEN
Just another name of rb_array_len.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RFILE(obj)
Convenient casting macro.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
VALUE rb_argv0
The value of $0 at process bootup.
void * rb_load_file_str(VALUE file)
Identical to rb_load_file(), except it takes the argument as a Ruby's string instead of C's.
void * rb_load_file(const char *file)
Loads the given file.
#define rb_argv
Just another name of rb_get_argv.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define errno
Ractor-aware version of errno.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
size_t length
The length of the buffer in bytes.
char * value
A pointer to the start of the buffer.
This is the base structure that represents a node in the syntax tree.
The options that can be passed to the parser.
This struct represents the overall parser.
A generic string type that can have various ownership semantics.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.