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) \
110#define EACH_DEBUG_FEATURES(X, SEP) \
111 X(frozen_string_literal) \
113#define AMBIGUOUS_FEATURE_NAMES 0
114#define DEFINE_FEATURE(bit) feature_##bit
115#define DEFINE_DEBUG_FEATURE(bit) feature_debug_##bit
116enum feature_flag_bits {
117 EACH_FEATURES(DEFINE_FEATURE, COMMA),
118 DEFINE_FEATURE(frozen_string_literal_set),
119 feature_debug_flag_first,
120 DEFINE_FEATURE(jit) = feature_yjit,
121 feature_jit_mask = FEATURE_BIT(yjit),
123 feature_debug_flag_begin = feature_debug_flag_first - 1,
124 EACH_DEBUG_FEATURES(DEFINE_DEBUG_FEATURE, COMMA),
128#define MULTI_BITS_P(bits) ((bits) & ((bits) - 1))
130#define DEBUG_BIT(bit) (1U << feature_debug_##bit)
132#define DUMP_BIT(bit) (1U << dump_##bit)
133#define DEFINE_DUMP(bit) dump_##bit
134#define EACH_DUMPS(X, SEP) \
153 dump_opt_error_tolerant,
156 EACH_DUMPS(DEFINE_DUMP, COMMA),
157 dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) |
158 DUMP_BIT(parsetree) | DUMP_BIT(insns)),
159 dump_optional_bits = (DUMP_BIT(opt_error_tolerant) |
160 DUMP_BIT(opt_comment) |
161 DUMP_BIT(opt_optimize))
165rb_feature_set_to(
ruby_features_t *feat,
unsigned int bit_mask,
unsigned int bit_set)
167 feat->mask |= bit_mask;
168 feat->set = (feat->set & ~bit_mask) | bit_set;
171#define FEATURE_SET_TO(feat, bit_mask, bit_set) \
172 rb_feature_set_to(&(feat), bit_mask, bit_set)
173#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits)
174#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask)
175#define FEATURE_SET_P(feat, bits) ((feat).set & FEATURE_BIT(bits))
176#define FEATURE_USED_P(feat, bits) ((feat).mask & FEATURE_BIT(bits))
177#define FEATURE_SET_BITS(feat) ((feat).set & (feat).mask)
181#define src_encoding_index GET_VM()->src_encoding_index
184 COMPILATION_FEATURES = (
186 | FEATURE_BIT(frozen_string_literal)
187 | FEATURE_BIT(frozen_string_literal_set)
188 | FEATURE_BIT(debug_frozen_string_literal)
191 (FEATURE_BIT(debug_flag_first)-1)
195 & ~FEATURE_BIT(frozen_string_literal)
196 & ~FEATURE_BIT(frozen_string_literal_set)
201#define BACKTRACE_LENGTH_LIMIT_VALID_P(n) ((n) >= -1)
202#define OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt) \
203 BACKTRACE_LENGTH_LIMIT_VALID_P((opt)->backtrace_length_limit)
210 opt->src.enc.index = src_encoding_index;
211 opt->ext.enc.index = -1;
212 opt->intern.enc.index = -1;
213 opt->features.set = DEFAULT_FEATURES;
214#if defined(YJIT_FORCE_ENABLE)
215 opt->features.set |= FEATURE_BIT(yjit);
217 opt->dump |= DUMP_BIT(opt_optimize);
218 opt->backtrace_length_limit = LONG_MIN;
225static VALUE open_load_file(
VALUE fname_v,
int *xflag);
227#define forbid_setid(s) forbid_setid((s), opt)
234static const char esc_standout[] =
"\n\033[1;7m";
235static const char esc_bold[] =
"\033[1m";
236static const char esc_reset[] =
"\033[0m";
237static const char esc_none[] =
"";
238#define USAGE_INDENT " "
241show_usage_part(
const char *str,
const unsigned int namelen,
242 const char *str2,
const unsigned int secondlen,
244 int help,
int highlight,
unsigned int w,
int columns)
246 static const int indent_width = (int)
rb_strlen_lit(USAGE_INDENT);
247 const char *sb = highlight ? esc_bold : esc_none;
248 const char *se = highlight ? esc_reset : esc_none;
249 unsigned int desclen = (
unsigned int)strcspn(desc,
"\n");
250 if (!help && desclen > 0 && strchr(
".;:", desc[desclen-1])) --desclen;
251 if (help && (namelen + 1 > w) &&
252 (int)(namelen + secondlen + indent_width) >= columns) {
253 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, namelen, str, se);
255 const int second_end = secondlen;
257 if (str2[n] ==
',') n++;
258 if (str2[n] ==
' ') n++;
259 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, second_end-n, str2+n, se);
261 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
264 const int wrap = help && namelen + secondlen >= w;
265 printf(USAGE_INDENT
"%s%.*s%-*.*s%s%-*s%.*s\n", sb, namelen, str,
266 (wrap ? 0 : w - namelen),
267 (help ? secondlen : 0), str2, se,
269 (wrap ?
"\n" USAGE_INDENT :
""),
273 while (desc[desclen]) {
275 desclen = (
unsigned int)strcspn(desc,
"\n");
276 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
283 int help,
int highlight,
unsigned int w,
int columns)
285 const char *str = m->str;
286 const unsigned int namelen = m->namelen, secondlen = m->secondlen;
287 const char *desc = str + namelen + secondlen;
288 show_usage_part(str, namelen - 1, str + namelen, secondlen - 1, desc,
289 help, highlight, w, columns);
293ruby_show_usage_line(
const char *name,
const char *secondary,
const char *description,
294 int help,
int highlight,
unsigned int width,
int columns)
296 unsigned int namelen = (
unsigned int)strlen(name);
297 unsigned int secondlen = (secondary ? (
unsigned int)strlen(secondary) : 0);
298 show_usage_part(name, namelen, secondary, secondlen,
299 description, help, highlight, width, columns);
303usage(
const char *name,
int help,
int highlight,
int columns)
305#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
308# define PLATFORM_JIT_OPTION "--yjit"
314 M(
"-0[octal]",
"",
"Set input record separator ($/):\n"
315 "-0 for \\0; -00 for paragraph mode; -0777 for slurp mode."),
316 M(
"-a",
"",
"Split each input line ($_) into fields ($F)."),
317 M(
"-c",
"",
"Check syntax (no execution)."),
318 M(
"-Cdirpath",
"",
"Execute program in specified directory."),
319 M(
"-d",
", --debug",
"Set debugging flag ($DEBUG) to true."),
320 M(
"-e 'code'",
"",
"Execute given Ruby code; multiple -e allowed."),
321 M(
"-Eex[:in]",
", --encoding=ex[:in]",
"Set default external and internal encodings."),
322 M(
"-Fpattern",
"",
"Set input field separator ($;); used with -a."),
323 M(
"-i[extension]",
"",
"Set ARGF in-place mode;\n"
324 "create backup files with given extension."),
325 M(
"-Idirpath",
"",
"Prepend specified directory to load paths ($LOAD_PATH);\n"
326 "relative paths are expanded; multiple -I are allowed."),
327 M(
"-l",
"",
"Set output record separator ($\\) to $/;\n"
328 "used for line-oriented output."),
329 M(
"-n",
"",
"Run program in gets loop."),
330 M(
"-p",
"",
"Like -n, with printing added."),
331 M(
"-rlibrary",
"",
"Require the given library."),
332 M(
"-s",
"",
"Define global variables using switches following program path."),
333 M(
"-S",
"",
"Search directories found in the PATH environment variable."),
334 M(
"-v",
"",
"Print version; set $VERBOSE to true."),
335 M(
"-w",
"",
"Synonym for -W1."),
336 M(
"-W[level=2|:category]",
"",
"Set warning flag ($-W):\n"
337 "0 for silent; 1 for moderate; 2 for verbose."),
338 M(
"-x[dirpath]",
"",
"Execute Ruby code starting from a #!ruby line."),
340 M(
"--jit",
"",
"Enable JIT for the platform; same as " PLATFORM_JIT_OPTION
"."),
343 M(
"--yjit",
"",
"Enable in-process JIT compiler."),
345 M(
"-h",
"",
"Print this help message; use --help for longer message."),
347 STATIC_ASSERT(usage_msg_size, numberof(usage_msg) < 25);
350 M(
"--backtrace-limit=num",
"",
"Set backtrace limit."),
351 M(
"--copyright",
"",
"Print Ruby copyright."),
352 M(
"--crash-report=template",
"",
"Set template for crash report file."),
353 M(
"--disable=features",
"",
"Disable features; see list below."),
354 M(
"--dump=items",
"",
"Dump items; see list below."),
355 M(
"--enable=features",
"",
"Enable features; see list below."),
356 M(
"--external-encoding=encoding",
"",
"Set default external encoding."),
357 M(
"--help",
"",
"Print long help message; use -h for short message."),
358 M(
"--internal-encoding=encoding",
"",
"Set default internal encoding."),
359 M(
"--parser=parser",
"",
"Set Ruby parser: parse.y or prism."),
360 M(
"--verbose",
"",
"Set $VERBOSE to true; ignore input from $stdin."),
361 M(
"--version",
"",
"Print Ruby version."),
362 M(
"-y",
", --yydebug",
"Print parser log; backward compatibility not guaranteed."),
365 M(
"insns",
"",
"Instruction sequences."),
366 M(
"yydebug",
"",
"yydebug of yacc parser generator."),
367 M(
"parsetree",
"",
"Abstract syntax tree (AST)."),
368 M(
"-optimize",
"",
"Disable optimization (affects insns)."),
369 M(
"+error-tolerant",
"",
"Error-tolerant parsing (affects yydebug, parsetree)."),
370 M(
"+comment",
"",
"Add comments to AST (affects parsetree with --parser=parse.y)."),
373 M(
"gems",
"",
"Rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED
")."),
374 M(
"error_highlight",
"",
"error_highlight (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
375 M(
"did_you_mean",
"",
"did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
376 M(
"syntax_suggest",
"",
"syntax_suggest (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
377 M(
"rubyopt",
"",
"RUBYOPT environment variable (default: enabled)."),
378 M(
"frozen-string-literal",
"",
"Freeze all string literals (default: disabled)."),
380 M(
"yjit",
"",
"In-process JIT compiler (default: disabled)."),
384 M(
"deprecated",
"",
"Deprecated features."),
385 M(
"experimental",
"",
"Experimental features."),
386 M(
"performance",
"",
"Performance issues."),
387 M(
"strict_unused_block",
"",
"Warning unused block strictly"),
390 const char *sb = highlight ? esc_standout+1 : esc_none;
391 const char *se = highlight ? esc_reset : esc_none;
392 const int num = numberof(usage_msg) - (help ? 1 : 0);
393 unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16;
394#define SHOW(m) show_usage_line(&(m), help, highlight, w, columns)
396 printf(
"%sUsage:%s %s [options] [--] [filepath] [arguments]\n", sb, se, name);
397 for (i = 0; i < num; ++i)
402 if (highlight) sb = esc_standout;
404 for (i = 0; i < numberof(help_msg); ++i)
406 printf(
"%s""Dump List:%s\n", sb, se);
407 for (i = 0; i < numberof(dumps); ++i)
409 printf(
"%s""Features:%s\n", sb, se);
410 for (i = 0; i < numberof(features); ++i)
412 printf(
"%s""Warning categories:%s\n", sb, se);
413 for (i = 0; i < numberof(warn_categories); ++i)
414 SHOW(warn_categories[i]);
416 printf(
"%s""YJIT options:%s\n", sb, se);
417 rb_yjit_show_usage(help, highlight, w, columns);
421#define rubylib_path_new rb_str_new
424ruby_push_include(
const char *path,
VALUE (*filter)(
VALUE))
428 VALUE load_path = GET_VM()->load_path;
430 char rubylib[FILENAME_MAX];
432# define is_path_sep(c) ((c) == sep || (c) == ';')
434# define is_path_sep(c) ((c) == sep)
437 if (path == 0)
return;
441 while (is_path_sep(*p))
444 for (s = p; *s && !is_path_sep(*s); s = CharNext(s));
452 p = RSTRING_PTR(buf);
455 rb_str_resize(buf,
len);
456 p = strncpy(RSTRING_PTR(buf), p,
len);
459#ifdef HAVE_CYGWIN_CONV_PATH
460#define CONV_TO_POSIX_PATH(p, lib) \
461 cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
463# error no cygwin_conv_path
465 if (CONV_TO_POSIX_PATH(p, rubylib) == 0) {
470 rb_ary_push(load_path, (*filter)(rubylib_path_new(p,
len)));
476identical_path(
VALUE path)
482locale_path(
VALUE path)
484 rb_enc_associate(path, rb_locale_encoding());
491 ruby_push_include(path, locale_path);
495expand_include_path(
VALUE path)
497 char *p = RSTRING_PTR(path);
500 if (*p ==
'.' && p[1] ==
'/')
502 return rb_file_expand_path(path,
Qnil);
506ruby_incpush_expand(
const char *path)
508 ruby_push_include(path, expand_include_path);
512#if defined _WIN32 || defined __CYGWIN__
513static HMODULE libruby;
516DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
518 if (reason == DLL_PROCESS_ATTACH)
524rb_libruby_handle(
void)
530translit_char_bin(
char *p,
int from,
int to)
533 if ((
unsigned char)*p == from)
542# define chdir rb_w32_uchdir
550# define IF_UTF8_PATH(t, f) t
552# define IF_UTF8_PATH(t, f) f
564# define str_conv_enc(str, from, to) (str)
569#if defined(LOAD_RELATIVE)
571runtime_libruby_path(
void)
573#if defined _WIN32 || defined __CYGWIN__
581 while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath),
582 ret = GetModuleFileNameW(libruby, wlibpath,
len),
588 if (!ret || ret >
len) rb_fatal(
"failed to get module file name");
589#if defined __CYGWIN__
591 const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
592 size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0);
593 if (!newsize) rb_fatal(
"failed to convert module path to cygwin");
595 libpath = RSTRING_PTR(path);
596 if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) {
597 rb_str_resize(path, 0);
603 for (
len = ret, i = 0; i <
len; ++i) {
604 if (wlibpath[i] == L
'\\') {
610 len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL);
612 libpath = RSTRING_PTR(path);
613 WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath,
len, NULL, NULL);
615 rb_str_resize(wsopath, 0);
617#elif defined(HAVE_DLADDR)
620 const void* addr = (
void *)(
VALUE)expand_include_path;
622 if (!dladdr((
void *)addr, &dli)) {
626 else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
628 path = rb_readlink(fname, NULL);
633 path = rb_realpath_internal(
Qnil, fname, 1);
635 rb_str_resize(fname, 0);
638# error relative load path is not supported on this platform.
643#define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index")
645VALUE ruby_archlibdir_path, ruby_prefix_path;
650 VALUE load_path, archlibdir = 0;
651 ID id_initial_load_path_mark;
652 const char *paths = ruby_initial_load_paths;
654#if defined LOAD_RELATIVE
655#if !defined ENABLE_MULTIARCH
656# define RUBY_ARCH_PATH ""
657#elif defined RUBY_ARCH
658# define RUBY_ARCH_PATH "/"RUBY_ARCH
660# define RUBY_ARCH_PATH "/"RUBY_PLATFORM
667 sopath = runtime_libruby_path();
668 libpath = RSTRING_PTR(sopath);
670 p = strrchr(libpath,
'/');
672 static const char libdir[] =
"/"
673#ifdef LIBDIR_BASENAME
679 const ptrdiff_t libdir_len = (ptrdiff_t)
sizeof(libdir)
681 static const char bindir[] =
"/bin";
682 const ptrdiff_t bindir_len = (ptrdiff_t)
sizeof(bindir) - 1;
684 const char *p2 = NULL;
686#ifdef ENABLE_MULTIARCH
689 if (p - libpath >= bindir_len && !
STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
695 else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
696 archlibdir =
rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath);
700#ifdef ENABLE_MULTIARCH
706 p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
707 if (p)
goto multiarch;
711 baselen = p - libpath;
716 rb_str_resize(sopath, baselen);
717 libpath = RSTRING_PTR(sopath);
718#define PREFIX_PATH() sopath
719#define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
720#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
722 const size_t exec_prefix_len = strlen(ruby_exec_prefix);
723#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len))
724#define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
726 rb_gc_register_address(&ruby_prefix_path);
727 ruby_prefix_path = PREFIX_PATH();
729 if (!archlibdir) archlibdir = ruby_prefix_path;
730 rb_gc_register_address(&ruby_archlibdir_path);
731 ruby_archlibdir_path = archlibdir;
733 load_path = GET_VM()->load_path;
735 ruby_push_include(getenv(
"RUBYLIB"), identical_path);
737 id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
739 size_t len = strlen(paths);
740 VALUE path = RUBY_RELATIVE(paths,
len);
741 rb_ivar_set(path, id_initial_load_path_mark, path);
742 rb_ary_push(load_path, path);
751add_modules(
VALUE *req_list,
const char *mod)
753 VALUE list = *req_list;
757 *req_list = list = rb_ary_hidden_new(0);
760 rb_ary_push(list, feature);
764require_libraries(
VALUE *req_list)
766 VALUE list = *req_list;
767 VALUE self = rb_vm_top_self();
769 rb_encoding *extenc = rb_default_external_encoding();
773 VALUE feature = rb_ary_shift(list);
774 rb_enc_associate(feature, extenc);
777 rb_funcallv(self, require, 1, &feature);
789process_sflag(
int sflag)
807 if (s[1] ==
'-' && s[2] ==
'\0')
812 for (p = s + 1; *p; p++) {
821 else if (*p !=
'_' && !
ISALNUM(*p)) {
824 rb_str_new2(
"invalid name for global variable - ");
825 if (!(p = strchr(p,
'='))) {
831 name_error[1] = args[-1];
837 for (p = s + 1; *p; ++p) {
861 VALUE argstr, argary;
864 VALUE src_enc_name = opt->src.enc.name;
865 VALUE ext_enc_name = opt->ext.enc.name;
866 VALUE int_enc_name = opt->intern.enc.name;
869 long backtrace_length_limit = opt->backtrace_length_limit;
870 const char *crash_report = opt->crash_report;
875 opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
877 const int hyphen = *s !=
'-';
881 p = RSTRING_PTR(argstr);
882 if (hyphen) *p =
'-';
883 memcpy(p + hyphen, s,
len + 1);
889 while (*p && !
ISSPACE(*p)) ++p;
894 argc = RSTRING_LEN(argary) /
sizeof(ap);
897 argv = ptr =
ALLOC_N(
char *, argc);
898 MEMMOVE(argv, RSTRING_PTR(argary),
char *, argc);
900 while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) {
912 opt->src.enc.name = src_enc_name;
915 opt->ext.enc.name = ext_enc_name;
918 opt->intern.enc.name = int_enc_name;
920 FEATURE_SET_RESTORE(opt->features, feat);
921 FEATURE_SET_RESTORE(opt->warn, warn);
922 if (BACKTRACE_LENGTH_LIMIT_VALID_P(backtrace_length_limit)) {
923 opt->backtrace_length_limit = backtrace_length_limit;
926 opt->crash_report = crash_report;
931 rb_str_resize(argary, 0);
932 rb_str_resize(argstr, 0);
936name_match_p(
const char *name,
const char *str,
size_t len)
938 if (
len == 0)
return 0;
940 while (
TOLOWER(*str) == *name) {
941 if (!--
len)
return 1;
945 if (*str !=
'-' && *str !=
'_')
return 0;
947 if (*name !=
'-' && *name !=
'_')
return 0;
948 if (!*++name)
return 1;
950 if (--
len == 0)
return 1;
954#define NAME_MATCH_P(name, str, len) \
955 ((len) < (int)sizeof(name) && name_match_p((name), (str), (len)))
957#define UNSET_WHEN(name, bit, str, len) \
958 if (NAME_MATCH_P((name), (str), (len))) { \
959 *(unsigned int *)arg &= ~(bit); \
963#define SET_WHEN(name, bit, str, len) \
964 if (NAME_MATCH_P((name), (str), (len))) { \
965 *(unsigned int *)arg |= (bit); \
969#define LITERAL_NAME_ELEMENT(name) #name
972feature_option(
const char *str,
int len,
void *arg,
const unsigned int enable)
974 static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT,
", ");
976 unsigned int mask = ~0
U;
977 unsigned int set = 0
U;
978#if AMBIGUOUS_FEATURE_NAMES
980# define FEATURE_FOUND ++matched
982# define FEATURE_FOUND goto found
984#define SET_FEATURE(bit) \
985 if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;}
986 EACH_FEATURES(SET_FEATURE, ;);
987 if (NAME_MATCH_P(
"jit", str,
len)) {
988 set |= mask = FEATURE_BIT(jit);
991 if (NAME_MATCH_P(
"all", str,
len)) {
993 mask &= ~feature_jit_mask | FEATURE_BIT(jit);
996#if AMBIGUOUS_FEATURE_NAMES
997 if (matched == 1)
goto found;
999 VALUE mesg = rb_sprintf(
"ambiguous feature: '%.*s' (",
len, str);
1000#define ADD_FEATURE_NAME(bit) \
1001 if (FEATURE_BIT(bit) & set) { \
1002 rb_str_cat_cstr(mesg, #bit); \
1003 if (--matched) rb_str_cat_cstr(mesg, ", "); \
1005 EACH_FEATURES(ADD_FEATURE_NAME, ;);
1008#undef ADD_FEATURE_NAME
1013 rb_warn(
"unknown argument for --%s: '%.*s'",
1014 enable ?
"enable" :
"disable",
len, str);
1015 rb_warn(
"features are [%.*s].", (
int)strlen(list), list);
1019 FEATURE_SET_TO(*argp, mask, (mask & enable));
1020 if (NAME_MATCH_P(
"frozen_string_literal", str,
len)) {
1021 FEATURE_SET_TO(*argp, FEATURE_BIT(frozen_string_literal_set), FEATURE_BIT(frozen_string_literal_set));
1027enable_option(
const char *str,
int len,
void *arg)
1029 feature_option(str,
len, arg, ~0
U);
1033disable_option(
const char *str,
int len,
void *arg)
1035 feature_option(str,
len, arg, 0
U);
1039int ruby_env_debug_option(
const char *str,
int len,
void *arg);
1042debug_option(
const char *str,
int len,
void *arg)
1044 static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1046#define SET_WHEN_DEBUG(bit) \
1047 if (NAME_MATCH_P(#bit, str, len)) { \
1048 FEATURE_SET(*argp, DEBUG_BIT(bit)); \
1051 EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;);
1055 rb_warn(
"unknown argument for --debug: '%.*s'",
len, str);
1056 rb_warn(
"debug features are [%.*s].", (
int)strlen(list), list);
1060memtermspn(
const char *str,
char term,
int len)
1063 if (
len <= 0)
return 0;
1064 const char *next = memchr(str, term,
len);
1065 return next ? (int)(next - str) :
len;
1068static const char additional_opt_sep =
'+';
1071dump_additional_option_flag(
const char *str,
int len,
unsigned int bits,
bool set)
1073#define SET_DUMP_OPT(bit) if (NAME_MATCH_P(#bit, str, len)) { \
1074 return set ? (bits | DUMP_BIT(opt_ ## bit)) : (bits & ~DUMP_BIT(opt_ ## bit)); \
1076 SET_DUMP_OPT(error_tolerant);
1077 SET_DUMP_OPT(comment);
1078 SET_DUMP_OPT(optimize);
1080 rb_warn(
"don't know how to dump with%s '%.*s'", set ?
"" :
"out",
len, str);
1085dump_additional_option(
const char *str,
int len,
unsigned int bits)
1088 for (;
len-- > 0 && *str++ == additional_opt_sep;
len -= w, str += w) {
1089 w = memtermspn(str, additional_opt_sep,
len);
1091 if (*str ==
'-' || *str ==
'+') {
1092 set = *str++ ==
'+';
1096 int n = memtermspn(str,
'-', w);
1097 if (str[n] ==
'-') {
1098 if (NAME_MATCH_P(
"with", str, n)) {
1102 else if (NAME_MATCH_P(
"without", str, n)) {
1109 bits = dump_additional_option_flag(str, w, bits, set);
1115dump_option(
const char *str,
int len,
void *arg)
1117 static const char list[] = EACH_DUMPS(LITERAL_NAME_ELEMENT,
", ");
1118 unsigned int *bits_ptr = (
unsigned int *)arg;
1119 if (*str ==
'+' || *str ==
'-') {
1120 bool set = *str++ ==
'+';
1121 *bits_ptr = dump_additional_option_flag(str, --
len, *bits_ptr, set);
1124 int w = memtermspn(str, additional_opt_sep,
len);
1126#define SET_WHEN_DUMP(bit) \
1127 if (NAME_MATCH_P(#bit "-", (str), (w))) { \
1128 *bits_ptr = dump_additional_option(str + w, len - w, *bits_ptr | DUMP_BIT(bit)); \
1131 EACH_DUMPS(SET_WHEN_DUMP, ;);
1132 rb_warn(
"don't know how to dump '%.*s',",
len, str);
1133 rb_warn(
"but only [%.*s].", (
int)strlen(list), list);
1137set_option_encoding_once(
const char *
type,
VALUE *name,
const char *e,
long elen)
1141 if (!elen) elen = strlen(e);
1147 "%s already set to %"PRIsVALUE,
type, *name);
1152#define set_internal_encoding_once(opt, e, elen) \
1153 set_option_encoding_once("default_internal", &(opt)->intern.enc.name, (e), (elen))
1154#define set_external_encoding_once(opt, e, elen) \
1155 set_option_encoding_once("default_external", &(opt)->ext.enc.name, (e), (elen))
1156#define set_source_encoding_once(opt, e, elen) \
1157 set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen))
1159#define yjit_opt_match_noarg(s, l, name) \
1160 opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --yjit-" name " is ignored"), 1) : 1)
1161#define yjit_opt_match_arg(s, l, name) \
1162 opt_match(s, l, name) && (*(s) && *(s+1) ? 1 : (rb_raise(rb_eRuntimeError, "--yjit-" name " needs an argument"), 0))
1166setup_yjit_options(
const char *s)
1169 bool rb_yjit_parse_option(
const char* s);
1170 bool success = rb_yjit_parse_option(s);
1178 "invalid YJIT option '%s' (--help will show valid yjit options)",
1201 unsigned int bits = 0;
1202 static const char no_prefix[] =
"no-";
1203 int enable = strncmp(s += 2, no_prefix,
sizeof(no_prefix)-1) != 0;
1204 if (!enable) s +=
sizeof(no_prefix)-1;
1205 size_t len = strlen(s);
1206 if (NAME_MATCH_P(
"deprecated", s,
len)) {
1209 else if (NAME_MATCH_P(
"experimental", s,
len)) {
1212 else if (NAME_MATCH_P(
"performance", s,
len)) {
1215 else if (NAME_MATCH_P(
"strict_unused_block", s,
len)) {
1219 rb_warn(
"unknown warning category: '%s'", s);
1221 if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0);
1234 if (!opt->warning) {
1250 FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS, 0);
1256 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1275 if (!opt->e_script) {
1277 if (opt->script == 0)
1290 const char *enc_name = 0;
1293 enc_name =
"EUC-JP";
1296 enc_name =
"Windows-31J";
1301 case 'N':
case 'n':
case 'A':
case 'a':
1302 enc_name =
"ASCII-8BIT";
1307 if (!opt->ext.enc.name)
1308 opt->ext.enc.name = opt->src.enc.name;
1327 else if (v == 0 && numlen >= 2) {
1342# define set_encoding_part(type) \
1343 if (!(p = strchr(s, ':'))) { \
1344 set_##type##_encoding_once(opt, s, 0); \
1348 set_##type##_encoding_once(opt, s, p-s); \
1350 set_encoding_part(external);
1351 if (!*(s = ++p))
return;
1352 set_encoding_part(internal);
1353 if (!*(s = ++p))
return;
1354#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1355 set_encoding_part(source);
1356 if (!*(s = ++p))
return;
1359# undef set_encoding_part
1368# define is_option_end(c, allow_hyphen) \
1369 (!(c) || ((allow_hyphen) && (c) == '-') || (c) == '=')
1370# define check_envopt(name, allow_envopt) \
1371 (((allow_envopt) || !envopt) ? (void)0 : \
1372 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
1373# define need_argument(name, s, needs_arg, next_arg) \
1374 ((*(s) ? !*++(s) : (next_arg) && (argc <= 1 || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
1375 rb_raise(rb_eRuntimeError, "missing argument for --" name) \
1377# define is_option_with_arg(name, allow_hyphen, allow_envopt) \
1378 is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue, Qtrue)
1379# define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg, next_arg) \
1380 (strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) && \
1381 (s[n] != '-' || (s[n] && s[n+1])) ? \
1382 (check_envopt(name, (allow_envopt)), s += n, \
1383 need_argument(name, s, needs_arg, next_arg), 1) : 0)
1385 if (strcmp(
"copyright", s) == 0) {
1386 if (envopt)
goto noenvopt_long;
1387 opt->dump |= DUMP_BIT(copyright);
1398 else if (is_option_with_arg(
"enable",
Qtrue,
Qtrue)) {
1401 else if (is_option_with_arg(
"disable",
Qtrue,
Qtrue)) {
1404 else if (is_option_with_arg(
"encoding",
Qfalse,
Qtrue)) {
1405 proc_encoding_option(opt, s,
"--encoding");
1407 else if (is_option_with_arg(
"internal-encoding",
Qfalse,
Qtrue)) {
1408 set_internal_encoding_once(opt, s, 0);
1410 else if (is_option_with_arg(
"external-encoding",
Qfalse,
Qtrue)) {
1411 set_external_encoding_once(opt, s, 0);
1413 else if (is_option_with_arg(
"parser",
Qfalse,
Qtrue)) {
1414 if (strcmp(
"prism", s) == 0) {
1415 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PRISM);
1417 else if (strcmp(
"parse.y", s) == 0) {
1418 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PARSE_Y);
1424#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1425 else if (is_option_with_arg(
"source-encoding",
Qfalse,
Qtrue)) {
1426 set_source_encoding_once(opt, s, 0);
1429 else if (strcmp(
"version", s) == 0) {
1430 if (envopt)
goto noenvopt_long;
1431 opt->dump |= DUMP_BIT(version);
1433 else if (strcmp(
"verbose", s) == 0) {
1437 else if (strcmp(
"jit", s) == 0) {
1439 FEATURE_SET(opt->features, FEATURE_BIT(jit));
1441 rb_warn(
"Ruby was built without JIT support");
1444 else if (is_option_with_optarg(
"yjit",
'-',
true,
false,
false)) {
1446 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
1447 setup_yjit_options(s);
1449 rb_warn(
"Ruby was built without YJIT support."
1450 " You may need to install rustc to build Ruby with YJIT.");
1453 else if (strcmp(
"yydebug", s) == 0) {
1454 if (envopt)
goto noenvopt_long;
1455 opt->dump |= DUMP_BIT(yydebug);
1460 else if (strcmp(
"help", s) == 0) {
1461 if (envopt)
goto noenvopt_long;
1462 opt->dump |= DUMP_BIT(help);
1465 else if (is_option_with_arg(
"backtrace-limit",
Qfalse,
Qtrue)) {
1467 long n = strtol(s, &e, 10);
1468 if (
errno == ERANGE || !BACKTRACE_LENGTH_LIMIT_VALID_P(n) || *e) {
1472 opt->backtrace_length_limit = n;
1475 else if (is_option_with_arg(
"crash-report",
true,
true)) {
1476 opt->crash_report = s;
1480 "invalid option --%s (-h will show valid options)", s);
1482 return argc0 - argc + 1;
1486# undef is_option_end
1488# undef need_argument
1489# undef is_option_with_arg
1490# undef is_option_with_optarg
1497 long n, argc0 = argc;
1499 int warning = opt->warning;
1501 if (argc <= 0 || !argv)
1504 for (argc--, argv++; argc > 0; argc--, argv++) {
1505 const char *
const arg = argv[0];
1506 if (!arg || arg[0] !=
'-' || !arg[1])
1513 if (envopt)
goto noenvopt;
1514 opt->do_split = TRUE;
1519 if (envopt)
goto noenvopt;
1520 opt->do_print = TRUE;
1523 if (envopt)
goto noenvopt;
1524 opt->do_loop = TRUE;
1535 if (envopt)
goto noenvopt;
1536 opt->dump |= DUMP_BIT(yydebug);
1545 opt->dump |= DUMP_BIT(version_v);
1548 if (!opt->warning) {
1552 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1557 if (!(s = proc_W_option(opt, s, &warning)))
break;
1561 if (envopt)
goto noenvopt;
1562 opt->dump |= DUMP_BIT(syntax);
1567 if (envopt)
goto noenvopt;
1569 if (!opt->sflag) opt->sflag = 1;
1574 if (envopt)
goto noenvopt;
1575 opt->dump |= DUMP_BIT(usage);
1579 if (envopt)
goto noenvopt;
1580 opt->do_line = TRUE;
1586 if (envopt)
goto noenvopt;
1588 opt->do_search = TRUE;
1593 if (envopt)
goto noenvopt;
1594 if (!(n = proc_e_option(opt, s, argc, argv)))
break;
1603 add_modules(&opt->req_list, s);
1605 else if (argc > 1) {
1606 add_modules(&opt->req_list, argv[1]);
1612 if (envopt)
goto noenvopt;
1614 ruby_set_inplace_mode(s + 1);
1618 if (envopt)
goto noenvopt;
1622 if (*s && chdir(s) < 0) {
1623 rb_fatal(
"Can't chdir to %s", s);
1629 if (envopt)
goto noenvopt;
1630 if (!*++s && (!--argc || !(s = *++argv) || !*s)) {
1631 rb_fatal(
"Can't chdir");
1634 rb_fatal(
"Can't chdir to %s", s);
1639 if (envopt)
goto noenvopt;
1646 if (!*++s && (!--argc || !(s = *++argv))) {
1649 proc_encoding_option(opt, s,
"-E");
1653 set_internal_encoding_once(opt,
"UTF-8", 0);
1658 if (!(s = proc_K_option(opt, s)))
break;
1664 ruby_incpush_expand(s);
1665 else if (argc > 1) {
1666 ruby_incpush_expand(argv[1]);
1672 if (envopt)
goto noenvopt;
1673 if (!(s = proc_0_option(opt, s)))
break;
1677 if (!s[1] || (s[1] ==
'\r' && !s[2])) {
1683 if (!(n = proc_long_options(opt, s, argc, argv, envopt)))
goto switch_end;
1694 rb_encoding *enc = IF_UTF8_PATH(rb_utf8_encoding(), rb_locale_encoding());
1695 const char *e = s + strlen(s);
1696 int r = rb_enc_precise_mbclen(s, e, enc);
1697 unsigned int c = (
unsigned char)*s;
1699 c = rb_enc_mbc_to_codepoint(s, e, enc);
1700 if (ONIGENC_IS_CODE_GRAPH(enc, c) ||
1701 ((s = ruby_escaped_char(c)) != 0 &&
1702 (r = (
int)strlen(s), 1))) {
1704 "invalid option -%.*s (-h will show valid options)",
1709 "invalid option -\\x%.2x (-h will show valid options)",
1726 if (warning) opt->warning = warning;
1727 return argc0 - argc;
1730void Init_builtin_features(
void);
1733ruby_init_prelude(
void)
1735 Init_builtin_features();
1739void rb_call_builtin_inits(
void);
1743#if RBIMPL_HAS_ATTRIBUTE(weak)
1747Init_extra_exts(
void)
1754 rb_warning_category_update(opt->warn.mask, opt->warn.set);
1756 if (opt->dump & dump_exit_bits)
return;
1758 if (FEATURE_SET_P(opt->features, gems)) {
1760 if (opt->features.set & FEATURE_BIT(error_highlight)) {
1763 if (opt->features.set & FEATURE_BIT(did_you_mean)) {
1766 if (opt->features.set & FEATURE_BIT(syntax_suggest)) {
1773 if (getenv(
"RUBY_GC_HEAP_INIT_SLOTS")) {
1774 rb_warn_deprecated(
"The environment variable RUBY_GC_HEAP_INIT_SLOTS",
1775 "environment variables RUBY_GC_HEAP_%d_INIT_SLOTS");
1781 GET_VM()->running = 0;
1782 rb_call_builtin_inits();
1783 GET_VM()->running = 1;
1784 memset(ruby_vm_redefined_flag, 0,
sizeof(ruby_vm_redefined_flag));
1786 ruby_init_prelude();
1790 rb_yjit_init(opt->yjit);
1794 void Init_builtin_yjit_hook();
1795 Init_builtin_yjit_hook();
1798 require_libraries(&opt->req_list);
1802opt_enc_index(
VALUE enc_name)
1804 const char *s = RSTRING_PTR(enc_name);
1805 int i = rb_enc_find_index(s);
1810 else if (rb_enc_dummy_p(rb_enc_from_index(i))) {
1816#define rb_progname (GET_VM()->progname)
1817#define rb_orig_progname (GET_VM()->orig_progname)
1833#define rb_define_readonly_boolean(name, val) \
1834 rb_define_virtual_variable((name), (val) ? true_value : false_value, 0)
1843 rb_raise(
rb_eTypeError,
"$_ value need to be String (%s given)",
1925setup_pager_env(
void)
1927 if (!getenv(
"LESS")) {
1929 ruby_setenv(
"LESS",
"-R +/^[A-Z].*");
1937 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
1939 if (!GetConsoleMode(h, &m))
return 0;
1940# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
1941# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
1943 if (!(m & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;
1946#elif !defined(HAVE_WORKING_FORK)
1947# define tty_enabled() 0
1956 return rb_enc_associate(
rb_str_dup(str), enc);
1964env_var_truthy(
const char *name)
1966 const char *value = getenv(name);
1970 if (strcmp(value,
"1") == 0)
1972 if (strcmp(value,
"true") == 0)
1974 if (strcmp(value,
"yes") == 0)
1981rb_pid_t rb_fork_ruby(
int *status);
1984show_help(
const char *progname,
int help)
1986 int tty = isatty(1);
1989 const char *pager_env = getenv(
"RUBY_PAGER");
1990 if (!pager_env) pager_env = getenv(
"PAGER");
1991 if (pager_env && *pager_env && isatty(0)) {
1992 const char *columns_env = getenv(
"COLUMNS");
1993 if (columns_env) columns = atoi(columns_env);
1995#ifdef HAVE_WORKING_FORK
1998 rb_pid_t pid = rb_fork_ruby(NULL);
2003 else if (pid == 0) {
2021 int oldout = dup(1);
2022 int olderr = dup(2);
2023 int fd =
RFILE(port)->fptr->fd;
2024 tty = tty_enabled();
2027 usage(progname, 1, tty, columns);
2037 usage(progname, help, tty, columns);
2045 VALUE parser = rb_parser_new();
2046 const unsigned int dump = opt->dump;
2048 if (dump & DUMP_BIT(yydebug)) {
2049 rb_parser_set_yydebug(parser,
Qtrue);
2052 if ((dump & dump_exit_bits) && (dump & DUMP_BIT(opt_error_tolerant))) {
2053 rb_parser_error_tolerant(parser);
2056 if (opt->e_script) {
2057 VALUE progname = rb_progname;
2058 rb_parser_set_context(parser, 0, TRUE);
2062 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2063 opt->do_line, opt->do_split);
2064 ast_value = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
2068 int xflag = opt->xflag;
2069 f = open_load_file(opt->script_name, &xflag);
2070 opt->xflag = xflag != 0;
2071 rb_parser_set_context(parser, 0, f ==
rb_stdin);
2072 ast_value = load_file(parser, opt->script_name, f, 1, opt);
2074 ast = rb_ruby_ast_data_get(ast_value);
2075 if (!ast->body.root) {
2076 rb_ast_dispose(ast);
2085 uint8_t command_line = 0;
2091 return command_line;
2095prism_script_shebang_callback(
pm_options_t *options,
const uint8_t *source,
size_t length,
void *data)
2100 char *switches = malloc(length + 1);
2101 memcpy(switches, source, length);
2102 switches[length] =
'\0';
2104 int no_src_enc = !opt->src.enc.name;
2105 int no_ext_enc = !opt->ext.enc.name;
2106 int no_int_enc = !opt->intern.enc.name;
2108 moreswitches(switches, opt, 0);
2111 pm_options_command_line_set(options, prism_script_command_line(opt));
2113 if (no_src_enc && opt->src.enc.name) {
2114 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2117 if (no_ext_enc && opt->ext.enc.name) {
2118 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2120 if (no_int_enc && opt->intern.enc.name) {
2121 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2135 pm_options_line_set(options, 1);
2136 pm_options_main_script_set(options,
true);
2138 const bool read_stdin = (strcmp(opt->script,
"-") == 0);
2141 pm_options_encoding_set(options, rb_enc_name(rb_locale_encoding()));
2143 if (opt->src.enc.name != 0) {
2147 uint8_t command_line = prism_script_command_line(opt);
2151 pm_options_command_line_set(options, command_line);
2152 pm_options_filepath_set(options,
"-");
2153 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2156 error = pm_parse_stdin(result);
2165 else if (opt->e_script) {
2167 pm_options_command_line_set(options, command_line);
2170 result->
node.coverage_enabled = 0;
2171 error = pm_parse_string(result, opt->e_script,
rb_str_new2(
"-e"), NULL);
2174 VALUE script_name = rb_str_encode_ospath(opt->script_name);
2176 pm_options_command_line_set(options, command_line);
2177 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2179 error = pm_load_file(result, script_name,
true);
2186 error = pm_parse_file(result, opt->script_name, NULL);
2191 if (
RTEST(rb_get_coverages())) {
2192 result->
node.coverage_enabled = 1;
2199 int xflag = opt->xflag;
2200 VALUE file = open_load_file(script_name, &xflag);
2205 if ((parser->
start + offset < parser->end) && parser->
start[offset] ==
'\r') offset++;
2206 if ((parser->
start + offset < parser->end) && parser->
start[offset] ==
'\n') offset++;
2213 if (!
NIL_P(error)) {
2214 pm_parse_result_free(result);
2224 pm_prettyprint(&output_buffer, &result->
parser, result->
node.ast_node);
2226 pm_buffer_free(&output_buffer);
2233 if (OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt)) {
2234 rb_backtrace_length_limit = opt->backtrace_length_limit;
2244 rb_define_readonly_boolean(
"$-p", opt->do_print);
2245 rb_define_readonly_boolean(
"$-l", opt->do_line);
2246 rb_define_readonly_boolean(
"$-a", opt->do_split);
2248 rb_gvar_ractor_local(
"$-p");
2249 rb_gvar_ractor_local(
"$-l");
2250 rb_gvar_ractor_local(
"$-a");
2252 if ((rb_e_script = opt->e_script) != 0) {
2254 rb_vm_register_global_object(opt->e_script);
2258 VALUE script = (opt->e_script ? opt->e_script :
Qnil);
2259 rb_exec_event_hook_script_compiled(ec, iseq, script);
2270#define dispose_result() \
2271 (result.ast ? rb_ast_dispose(result.ast) : pm_parse_result_free(&result.prism))
2280 char fbuf[MAXPATHLEN];
2281 int i = (int)proc_options(argc, argv, opt, 0);
2282 unsigned int dump = opt->dump & dump_exit_bits;
2284 const long loaded_before_enc =
RARRAY_LEN(vm->loaded_features);
2286 if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
2287 const char *
const progname =
2288 (argc > 0 && argv && argv[0] ? argv[0] :
2289 origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] :
2291 show_help(progname, (opt->dump & DUMP_BIT(help)));
2298 if (FEATURE_SET_P(opt->features, rubyopt) && (s = getenv(
"RUBYOPT"))) {
2299 moreswitches(s, opt, 1);
2302 if (opt->src.enc.name)
2306 rb_warning(
"-K is specified; it is for 1.8 compatibility and may cause odd behavior");
2308 if (!(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2310 if (!FEATURE_USED_P(opt->features, yjit) && env_var_truthy(
"RUBY_YJIT_ENABLE")) {
2311 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
2315 if (MULTI_BITS_P(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2316 rb_warn(
"Only one JIT can be enabled at the same time. Exiting");
2321 if (FEATURE_SET_P(opt->features, yjit)) {
2322 bool rb_yjit_option_disable(
void);
2323 opt->yjit = !rb_yjit_option_disable();
2327 ruby_mn_threads_params();
2328 Init_ruby_description(opt);
2330 if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
2332 if (opt->dump & DUMP_BIT(version))
return Qtrue;
2334 if (opt->dump & DUMP_BIT(copyright)) {
2339 if (!opt->e_script) {
2346 opt->script = argv[0];
2347 if (!opt->script || opt->script[0] ==
'\0') {
2350 else if (opt->do_search) {
2351 const char *path = getenv(
"RUBYPATH");
2355 opt->script = dln_find_file_r(argv[0], path, fbuf,
sizeof(fbuf));
2358 opt->script = dln_find_file_r(argv[0], getenv(
PATH_ENV), fbuf,
sizeof(fbuf));
2361 opt->script = argv[0];
2366 if (opt->script[0] ==
'-' && !opt->script[1]) {
2367 forbid_setid(
"program input from stdin");
2372 opt->script = RSTRING_PTR(opt->script_name);
2375 translit_char_bin(RSTRING_PTR(opt->script_name),
'\\',
'/');
2378 ruby_gc_set_params();
2382 lenc = rb_locale_encoding();
2383 rb_enc_associate(rb_progname, lenc);
2385 if (opt->ext.enc.name != 0) {
2386 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2388 if (opt->intern.enc.name != 0) {
2389 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2391 if (opt->src.enc.name != 0) {
2392 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2393 src_encoding_index = opt->src.enc.index;
2395 if (opt->ext.enc.index >= 0) {
2396 enc = rb_enc_from_index(opt->ext.enc.index);
2399 enc = IF_UTF8_PATH(uenc, lenc);
2401 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2402 if (opt->intern.enc.index >= 0) {
2403 enc = rb_enc_from_index(opt->intern.enc.index);
2404 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2405 opt->intern.enc.index = -1;
2410 rb_enc_associate(opt->script_name, IF_UTF8_PATH(uenc, lenc));
2413 opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
2414 opt->script = RSTRING_PTR(opt->script_name);
2418 if (IF_UTF8_PATH(uenc != lenc, 1)) {
2420 VALUE load_path = vm->load_path;
2421 const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
2422 int modifiable = FALSE;
2424 rb_get_expanded_load_path();
2425 for (i = 0; i <
RARRAY_LEN(load_path); ++i) {
2427 int mark = rb_attr_get(path, id_initial_load_path_mark) == path;
2430 if (newpath == path)
continue;
2433 if (!(path = copy_str(path, lenc, !mark)))
continue;
2435 if (mark)
rb_ivar_set(path, id_initial_load_path_mark, path);
2437 rb_ary_modify(load_path);
2443 rb_ary_replace(vm->load_path_snapshot, load_path);
2447 VALUE loaded_features = vm->loaded_features;
2448 bool modified =
false;
2449 for (
long i = loaded_before_enc; i <
RARRAY_LEN(loaded_features); ++i) {
2451 if (!(path = copy_str(path, IF_UTF8_PATH(uenc, lenc),
true)))
continue;
2453 rb_ary_modify(loaded_features);
2459 rb_ary_replace(vm->loaded_features_snapshot, loaded_features);
2463 if (opt->features.mask & COMPILATION_FEATURES) {
2464 VALUE option = rb_hash_new();
2465#define SET_COMPILE_OPTION(h, o, name) \
2466 rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \
2467 RBOOL(FEATURE_SET_P(o->features, name)))
2469 if (FEATURE_SET_P(opt->features, frozen_string_literal_set)) {
2470 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
2472 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
2473 rb_funcallv(rb_cISeq,
rb_intern_const(
"compile_option="), 1, &option);
2474#undef SET_COMPILE_OPTION
2477 opt->sflag = process_sflag(opt->sflag);
2479 if (opt->e_script) {
2481 if (opt->src.enc.index >= 0) {
2482 eenc = rb_enc_from_index(opt->src.enc.index);
2487 if (ienc) eenc = ienc;
2492 opt->e_script = str_conv_enc(opt->e_script, uenc, eenc);
2495 rb_enc_associate(opt->e_script, eenc);
2498 if (!rb_ruby_prism_p()) {
2499 ast_value = process_script(opt);
2500 if (!(result.ast = rb_ruby_ast_data_get(ast_value)))
return Qfalse;
2503 prism_script(opt, &result.prism);
2506 if ((dump & DUMP_BIT(yydebug)) && !(dump &= ~DUMP_BIT(yydebug))) {
2511 if (opt->ext.enc.index >= 0) {
2512 enc = rb_enc_from_index(opt->ext.enc.index);
2515 enc = IF_UTF8_PATH(uenc, lenc);
2517 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2518 if (opt->intern.enc.index >= 0) {
2520 enc = rb_enc_from_index(opt->intern.enc.index);
2521 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2523 else if (!rb_default_internal_encoding())
2525 rb_enc_set_default_internal(
Qnil);
2526 rb_stdio_set_default_encoding();
2528 opt->sflag = process_sflag(opt->sflag);
2531 if (dump & DUMP_BIT(syntax)) {
2532 printf(
"Syntax OK\n");
2533 dump &= ~DUMP_BIT(syntax);
2540 if (dump & DUMP_BIT(parsetree)) {
2543 int comment = opt->dump & DUMP_BIT(opt_comment);
2544 tree = rb_parser_dump_tree(result.ast->body.root, comment);
2547 tree = prism_dump_tree(&result.prism);
2551 dump &= ~DUMP_BIT(parsetree);
2560 if (!opt->e_script && strcmp(opt->script,
"-")) {
2561 path = rb_realpath_internal(
Qnil, opt->script_name, 1);
2564 path = str_conv_enc(path, uenc, lenc);
2568 rb_enc_copy(path, opt->script_name);
2573 GetBindingPtr(
rb_const_get(rb_cObject, rb_intern(
"TOPLEVEL_BINDING")), toplevel_binding);
2574 const struct rb_block *base_block = toplevel_context(toplevel_binding);
2575 const rb_iseq_t *parent = vm_block_iseq(base_block);
2576 bool optimize = (opt->dump & DUMP_BIT(opt_optimize)) != 0;
2581 iseq = pm_iseq_new_main(&pm->
node, opt->script_name, path, parent, optimize, &error_state);
2583 pm_parse_result_free(pm);
2587 rb_jump_tag(error_state);
2592 iseq = rb_iseq_new_main(ast_value, opt->script_name, path, parent, optimize);
2593 rb_ast_dispose(ast);
2597 if (dump & DUMP_BIT(insns)) {
2600 dump &= ~DUMP_BIT(insns);
2601 if (!dump)
return Qtrue;
2603 if (opt->dump & dump_exit_bits)
return Qtrue;
2605 process_options_global_setup(opt, iseq);
2611warn_cr_in_shebang(
const char *str,
long len)
2613 if (
len > 1 && str[
len-1] ==
'\n' && str[
len-2] ==
'\r') {
2614 rb_warn(
"shebang line ending with \\r may cause problems");
2618#define warn_cr_in_shebang(str, len) (void)0
2621void rb_reset_argf_lineno(
long n);
2631void rb_set_script_lines_for(
VALUE vparser,
VALUE path);
2634load_file_internal(
VALUE argp_v)
2637 VALUE parser = argp->parser;
2638 VALUE orig_fname = argp->fname;
2639 int script = argp->script;
2647 CONST_ID(set_encoding,
"set_encoding");
2653 int no_src_enc = !opt->src.enc.name;
2654 int no_ext_enc = !opt->ext.enc.name;
2655 int no_int_enc = !opt->intern.enc.name;
2657 enc = rb_ascii8bit_encoding();
2658 rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
2666 if (
len > 2 && str[0] ==
'#' && str[1] ==
'!') {
2667 if (line_start == 1) warn_cr_in_shebang(str,
len);
2681 warn_cr_in_shebang(str,
len);
2684 goto search_shebang;
2689 if (*str ==
'\n') *str-- =
'\0';
2690 if (*str ==
'\r') *str-- =
'\0';
2692 if ((p = strstr(p,
" -")) != 0) {
2694 moreswitches(p + 1, opt, 0);
2700 else if (!
NIL_P(c)) {
2704 if (no_src_enc && opt->src.enc.name) {
2705 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2706 src_encoding_index = opt->src.enc.index;
2708 if (no_ext_enc && opt->ext.enc.name) {
2709 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2711 if (no_int_enc && opt->intern.enc.name) {
2712 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2715 else if (!
NIL_P(c)) {
2721 rb_reset_argf_lineno(0);
2724 if (opt->src.enc.index >= 0) {
2725 enc = rb_enc_from_index(opt->src.enc.index);
2728 enc = rb_locale_encoding();
2731 enc = rb_utf8_encoding();
2733 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2734 opt->do_line, opt->do_split);
2736 rb_set_script_lines_for(parser, orig_fname);
2740 rb_enc_associate(f, enc);
2741 return rb_parser_compile_string_path(parser, orig_fname, f, line_start);
2744 ast_value = rb_parser_compile_file_path(parser, orig_fname, f, line_start);
2745 rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser));
2746 if (script && rb_parser_end_seen_p(parser)) {
2767disable_nonblock(
int fd)
2769#if defined(HAVE_FCNTL) && defined(F_SETFL)
2770 if (fcntl(fd, F_SETFL, 0) < 0) {
2771 const int e =
errno;
2774 if (e == ENOTSUP)
return 0;
2776# if defined B_UNSUPPORTED
2777 if (e == B_UNSUPPORTED)
return 0;
2786open_load_file(
VALUE fname_v,
int *xflag)
2788 const char *fname = (fname_v = rb_str_encode_ospath(fname_v),
2790 long flen = RSTRING_LEN(fname_v);
2794 if (flen == 1 && fname[0] ==
'-') {
2801 const int MODE_TO_LOAD = O_RDONLY | (
2802#if defined O_NONBLOCK && HAVE_FCNTL
2804 !(O_NONBLOCK & O_ACCMODE) ? O_NONBLOCK :
2806#if defined O_NDELAY && HAVE_FCNTL
2807 !(O_NDELAY & O_ACCMODE) ? O_NDELAY :
2810 int mode = MODE_TO_LOAD;
2811#if defined DOSISH || defined __CYGWIN__
2812# define isdirsep(x) ((x) == '/' || (x) == '\\')
2814 static const char exeext[] =
".exe";
2815 enum {extlen =
sizeof(exeext)-1};
2816 if (flen > extlen && !isdirsep(fname[flen-extlen-1]) &&
2817 STRNCASECMP(fname+flen-extlen, exeext, extlen) == 0) {
2826 if (!rb_gc_for_fd(e)) {
2827 rb_load_fail(fname_v, strerror(e));
2830 rb_load_fail(fname_v, strerror(
errno));
2835 if (MODE_TO_LOAD != O_RDONLY && (e = disable_nonblock(fd)) != 0) {
2837 rb_load_fail(fname_v, strerror(e));
2840 e = ruby_is_fd_loadable(fd);
2844 rb_load_fail(fname_v, strerror(e));
2860restore_load_file(
VALUE arg)
2875 arg.parser = parser;
2877 arg.script = script;
2881 restore_load_file, (
VALUE)&arg);
2895 ast_value = rb_parser_load_file(rb_parser_new(), fname_v);
2896 return (
void *)rb_ruby_ast_data_get(ast_value);
2900rb_parser_load_file(
VALUE parser,
VALUE fname_v)
2904 VALUE f = open_load_file(fname_v, &xflag);
2905 cmdline_options_init(&opt)->xflag = xflag != 0;
2906 return load_file(parser, fname_v, f, 0, &opt);
2921proc_argv0(
VALUE process)
2923 return rb_orig_progname;
2948 return ruby_setproctitle(title);
2952ruby_setproctitle(
VALUE title)
2962 if (origarg.argv == 0)
2969external_str_new_cstr(
const char *p)
2973 str = str_conv_enc(str, NULL, rb_default_external_encoding());
2981set_progname(
VALUE name)
2983 rb_orig_progname = rb_progname = name;
2984 rb_vm_set_progname(rb_progname);
3008 rb_uid_t uid = getuid();
3009 rb_uid_t euid = geteuid();
3010 rb_gid_t gid = getgid();
3011 rb_gid_t egid = getegid();
3013 if (uid != euid) opt->setids |= 1;
3014 if (egid != gid) opt->setids |= 2;
3021 if (opt->setids & 1)
3023 if (opt->setids & 2)
3028verbose_getter(
ID id,
VALUE *ptr)
3030 return *rb_ruby_verbose_ptr();
3036 *rb_ruby_verbose_ptr() =
RTEST(val) ?
Qtrue : val;
3040opt_W_getter(
ID id,
VALUE *dmy)
3042 VALUE v = *rb_ruby_verbose_ptr();
3057debug_getter(
ID id,
VALUE *dmy)
3059 return *rb_ruby_debug_ptr();
3065 *rb_ruby_debug_ptr() = val;
3078 rb_gvar_ractor_local(
"$VERBOSE");
3079 rb_gvar_ractor_local(
"$-v");
3080 rb_gvar_ractor_local(
"$-w");
3081 rb_gvar_ractor_local(
"$-W");
3082 rb_gvar_ractor_local(
"$DEBUG");
3083 rb_gvar_ractor_local(
"$-d");
3107 for (i = 0; i < argc; i++) {
3108 VALUE arg = external_str_new_cstr(argv[i]);
3111 rb_ary_push(av, arg);
3120 const char *script_name = (argc > 0 && argv[0]) ? argv[0] :
ruby_engine;
3122 if (!origarg.argv || origarg.argc <= 0) {
3123 origarg.argc = argc;
3124 origarg.argv = argv;
3126 set_progname(external_str_new_cstr(script_name));
3128 rb_vm_register_global_object(
rb_argv0);
3130#ifndef HAVE_SETPROCTITLE
3131 ruby_init_setproctitle(argc, argv);
3134 iseq = process_options(argc, argv, cmdline_options_init(&opt));
3136 if (opt.crash_report && *opt.crash_report) {
3137 void ruby_set_crash_report(
const char *
template);
3138 ruby_set_crash_report(opt.crash_report);
3141 if (getenv(
"RUBY_FREE_AT_EXIT")) {
3142 rb_free_at_exit =
true;
3146 return (
void*)(
struct RData*)iseq;
3150fill_standard_fds(
void)
3152 int f0, f1, f2, fds[2];
3154 f0 = fstat(0, &buf) == -1 &&
errno == EBADF;
3155 f1 = fstat(1, &buf) == -1 &&
errno == EBADF;
3156 f2 = fstat(2, &buf) == -1 &&
errno == EBADF;
3158 if (pipe(fds) == 0) {
3167 if (pipe(fds) == 0) {
3169 if (f1 && fds[1] != 1)
3171 if (f2 && fds[1] != 2)
3173 if (fds[1] != 1 && fds[1] != 2)
3183 rb_w32_sysinit(argc, argv);
3185 if (*argc >= 0 && *argv) {
3186 origarg.argc = *argc;
3187 origarg.argv = *argv;
3189 fill_standard_fds();
#define RUBY_ASSERT(...)
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
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 SIZET2NUM
Old name of RB_SIZE2NUM.
#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.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
#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_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
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_STRICT_UNUSED_BLOCK
Warning is for checking unused block strictly.
@ 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_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
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.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
#define rb_external_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "default external" encoding.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
#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.
static const uint8_t PM_OPTIONS_COMMAND_LINE_E
A bit representing whether or not the command line -e option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_L
A bit representing whether or not the command line -l option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_A
A bit representing whether or not the command line -a option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_N
A bit representing whether or not the command line -n option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_X
A bit representing whether or not the command line -x option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_P
A bit representing whether or not the command line -p option was set.
#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.
const uint8_t * start
A pointer to the start location of the range in the source.
The options that can be passed to the parser.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_parser_t parser
The parser that will do the actual parsing.
pm_options_t options
The options that will be passed to the parser.
This struct represents the overall parser.
pm_location_t data_loc
An optional location that represents the location of the END marker and the rest of the content of th...
const uint8_t * start
The pointer to the start of the source.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.