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 DEFINE_FEATURE(frozen_string_literal_set),
121 feature_debug_flag_first,
122 DEFINE_FEATURE(jit) = feature_yjit,
123 feature_jit_mask = FEATURE_BIT(yjit) | FEATURE_BIT(zjit),
125 feature_debug_flag_begin = feature_debug_flag_first - 1,
126 EACH_DEBUG_FEATURES(DEFINE_DEBUG_FEATURE, COMMA),
130#define MULTI_BITS_P(bits) ((bits) & ((bits) - 1))
132#define DEBUG_BIT(bit) (1U << feature_debug_##bit)
134#define DUMP_BIT(bit) (1U << dump_##bit)
135#define DEFINE_DUMP(bit) dump_##bit
136#define EACH_DUMPS(X, SEP) \
155 dump_opt_error_tolerant,
158 EACH_DUMPS(DEFINE_DUMP, COMMA),
159 dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) |
160 DUMP_BIT(parsetree) | DUMP_BIT(insns)),
161 dump_optional_bits = (DUMP_BIT(opt_error_tolerant) |
162 DUMP_BIT(opt_comment) |
163 DUMP_BIT(opt_optimize))
167rb_feature_set_to(
ruby_features_t *feat,
unsigned int bit_mask,
unsigned int bit_set)
169 feat->mask |= bit_mask;
170 feat->set = (feat->set & ~bit_mask) | bit_set;
173#define FEATURE_SET_TO(feat, bit_mask, bit_set) \
174 rb_feature_set_to(&(feat), bit_mask, bit_set)
175#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits)
176#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask)
177#define FEATURE_SET_P(feat, bits) ((feat).set & FEATURE_BIT(bits))
178#define FEATURE_USED_P(feat, bits) ((feat).mask & FEATURE_BIT(bits))
179#define FEATURE_SET_BITS(feat) ((feat).set & (feat).mask)
183#define src_encoding_index GET_VM()->src_encoding_index
186 COMPILATION_FEATURES = (
188 | FEATURE_BIT(frozen_string_literal)
189 | FEATURE_BIT(frozen_string_literal_set)
190 | FEATURE_BIT(debug_frozen_string_literal)
193 (FEATURE_BIT(debug_flag_first)-1)
197 & ~FEATURE_BIT(frozen_string_literal)
198 & ~FEATURE_BIT(frozen_string_literal_set)
203#define BACKTRACE_LENGTH_LIMIT_VALID_P(n) ((n) >= -1)
204#define OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt) \
205 BACKTRACE_LENGTH_LIMIT_VALID_P((opt)->backtrace_length_limit)
212 opt->src.enc.index = src_encoding_index;
213 opt->ext.enc.index = -1;
214 opt->intern.enc.index = -1;
215 opt->features.set = DEFAULT_FEATURES;
216#if defined(YJIT_FORCE_ENABLE)
217 opt->features.set |= FEATURE_BIT(yjit);
219 opt->dump |= DUMP_BIT(opt_optimize);
220 opt->backtrace_length_limit = LONG_MIN;
227static VALUE open_load_file(
VALUE fname_v,
int *xflag);
229#define forbid_setid(s) forbid_setid((s), opt)
236static const char esc_standout[] =
"\n\033[1;7m";
237static const char esc_bold[] =
"\033[1m";
238static const char esc_reset[] =
"\033[0m";
239static const char esc_none[] =
"";
240#define USAGE_INDENT " "
243show_usage_part(
const char *str,
const unsigned int namelen,
244 const char *str2,
const unsigned int secondlen,
246 int help,
int highlight,
unsigned int w,
int columns)
248 static const int indent_width = (int)
rb_strlen_lit(USAGE_INDENT);
249 const char *sb = highlight ? esc_bold : esc_none;
250 const char *se = highlight ? esc_reset : esc_none;
251 unsigned int desclen = (
unsigned int)strcspn(desc,
"\n");
252 if (!help && desclen > 0 && strchr(
".;:", desc[desclen-1])) --desclen;
253 if (help && (namelen + 1 > w) &&
254 (int)(namelen + secondlen + indent_width) >= columns) {
255 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, namelen, str, se);
257 const int second_end = secondlen;
259 if (str2[n] ==
',') n++;
260 if (str2[n] ==
' ') n++;
261 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, second_end-n, str2+n, se);
263 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
266 const int wrap = help && namelen + secondlen >= w;
267 printf(USAGE_INDENT
"%s%.*s%-*.*s%s%-*s%.*s\n", sb, namelen, str,
268 (wrap ? 0 : w - namelen),
269 (help ? secondlen : 0), str2, se,
271 (wrap ?
"\n" USAGE_INDENT :
""),
275 while (desc[desclen]) {
277 desclen = (
unsigned int)strcspn(desc,
"\n");
278 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
285 int help,
int highlight,
unsigned int w,
int columns)
287 const char *str = m->str;
288 const unsigned int namelen = m->namelen, secondlen = m->secondlen;
289 const char *desc = str + namelen + secondlen;
290 show_usage_part(str, namelen - 1, str + namelen, secondlen - 1, desc,
291 help, highlight, w, columns);
295ruby_show_usage_line(
const char *name,
const char *secondary,
const char *description,
296 int help,
int highlight,
unsigned int width,
int columns)
298 unsigned int namelen = (
unsigned int)strlen(name);
299 unsigned int secondlen = (secondary ? (
unsigned int)strlen(secondary) : 0);
300 show_usage_part(name, namelen, secondary, secondlen,
301 description, help, highlight, width, columns);
305usage(
const char *name,
int help,
int highlight,
int columns)
307#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
310# define PLATFORM_JIT_OPTION "--yjit"
316 M(
"-0[octal]",
"",
"Set input record separator ($/):\n"
317 "-0 for \\0; -00 for paragraph mode; -0777 for slurp mode."),
318 M(
"-a",
"",
"Split each input line ($_) into fields ($F)."),
319 M(
"-c",
"",
"Check syntax (no execution)."),
320 M(
"-Cdirpath",
"",
"Execute program in specified directory."),
321 M(
"-d",
", --debug",
"Set debugging flag ($DEBUG) to true."),
322 M(
"-e 'code'",
"",
"Execute given Ruby code; multiple -e allowed."),
323 M(
"-Eex[:in]",
", --encoding=ex[:in]",
"Set default external and internal encodings."),
324 M(
"-Fpattern",
"",
"Set input field separator ($;); used with -a."),
325 M(
"-i[extension]",
"",
"Set ARGF in-place mode;\n"
326 "create backup files with given extension."),
327 M(
"-Idirpath",
"",
"Prepend specified directory to load paths ($LOAD_PATH);\n"
328 "relative paths are expanded; multiple -I are allowed."),
329 M(
"-l",
"",
"Set output record separator ($\\) to $/;\n"
330 "used for line-oriented output."),
331 M(
"-n",
"",
"Run program in gets loop."),
332 M(
"-p",
"",
"Like -n, with printing added."),
333 M(
"-rlibrary",
"",
"Require the given library."),
334 M(
"-s",
"",
"Define global variables using switches following program path."),
335 M(
"-S",
"",
"Search directories found in the PATH environment variable."),
336 M(
"-v",
"",
"Print version; set $VERBOSE to true."),
337 M(
"-w",
"",
"Synonym for -W1."),
338 M(
"-W[level=2|:category]",
"",
"Set warning flag ($-W):\n"
339 "0 for silent; 1 for moderate; 2 for verbose."),
340 M(
"-x[dirpath]",
"",
"Execute Ruby code starting from a #!ruby line."),
342 M(
"--jit",
"",
"Enable JIT for the platform; same as " PLATFORM_JIT_OPTION
"."),
345 M(
"--yjit",
"",
"Enable in-process JIT compiler."),
347 M(
"--zjit",
"",
"Enable in-process JIT compiler."),
348 M(
"-h",
"",
"Print this help message; use --help for longer message."),
350 STATIC_ASSERT(usage_msg_size, numberof(usage_msg) < 26);
353 M(
"--backtrace-limit=num",
"",
"Set backtrace limit."),
354 M(
"--copyright",
"",
"Print Ruby copyright."),
355 M(
"--crash-report=template",
"",
"Set template for crash report file."),
356 M(
"--disable=features",
"",
"Disable features; see list below."),
357 M(
"--dump=items",
"",
"Dump items; see list below."),
358 M(
"--enable=features",
"",
"Enable features; see list below."),
359 M(
"--external-encoding=encoding",
"",
"Set default external encoding."),
360 M(
"--help",
"",
"Print long help message; use -h for short message."),
361 M(
"--internal-encoding=encoding",
"",
"Set default internal encoding."),
362 M(
"--parser=parser",
"",
"Set Ruby parser: parse.y or prism."),
363 M(
"--verbose",
"",
"Set $VERBOSE to true; ignore input from $stdin."),
364 M(
"--version",
"",
"Print Ruby version."),
365 M(
"-y",
", --yydebug",
"Print parser log; backward compatibility not guaranteed."),
368 M(
"insns",
"",
"Instruction sequences."),
369 M(
"yydebug",
"",
"yydebug of yacc parser generator."),
370 M(
"parsetree",
"",
"Abstract syntax tree (AST)."),
371 M(
"-optimize",
"",
"Disable optimization (affects insns)."),
372 M(
"+error-tolerant",
"",
"Error-tolerant parsing (affects yydebug, parsetree)."),
373 M(
"+comment",
"",
"Add comments to AST (affects parsetree with --parser=parse.y)."),
376 M(
"gems",
"",
"Rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED
")."),
377 M(
"error_highlight",
"",
"error_highlight (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
378 M(
"did_you_mean",
"",
"did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
379 M(
"syntax_suggest",
"",
"syntax_suggest (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
380 M(
"rubyopt",
"",
"RUBYOPT environment variable (default: enabled)."),
381 M(
"frozen-string-literal",
"",
"Freeze all string literals (default: disabled)."),
383 M(
"yjit",
"",
"In-process JIT compiler (default: disabled)."),
387 M(
"deprecated",
"",
"Deprecated features."),
388 M(
"experimental",
"",
"Experimental features."),
389 M(
"performance",
"",
"Performance issues."),
390 M(
"strict_unused_block",
"",
"Warning unused block strictly"),
393 const char *sb = highlight ? esc_standout+1 : esc_none;
394 const char *se = highlight ? esc_reset : esc_none;
395 const int num = numberof(usage_msg) - (help ? 1 : 0);
396 unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16;
397#define SHOW(m) show_usage_line(&(m), help, highlight, w, columns)
399 printf(
"%sUsage:%s %s [options] [--] [filepath] [arguments]\n", sb, se, name);
400 for (i = 0; i < num; ++i)
405 if (highlight) sb = esc_standout;
407 for (i = 0; i < numberof(help_msg); ++i)
409 printf(
"%s""Dump List:%s\n", sb, se);
410 for (i = 0; i < numberof(dumps); ++i)
412 printf(
"%s""Features:%s\n", sb, se);
413 for (i = 0; i < numberof(features); ++i)
415 printf(
"%s""Warning categories:%s\n", sb, se);
416 for (i = 0; i < numberof(warn_categories); ++i)
417 SHOW(warn_categories[i]);
419 printf(
"%s""YJIT options:%s\n", sb, se);
420 rb_yjit_show_usage(help, highlight, w, columns);
424#define rubylib_path_new rb_str_new
427ruby_push_include(
const char *path,
VALUE (*filter)(
VALUE))
431 VALUE load_path = GET_VM()->load_path;
433 char rubylib[FILENAME_MAX];
435# define is_path_sep(c) ((c) == sep || (c) == ';')
437# define is_path_sep(c) ((c) == sep)
440 if (path == 0)
return;
444 while (is_path_sep(*p))
447 for (s = p; *s && !is_path_sep(*s); s = CharNext(s));
455 p = RSTRING_PTR(buf);
458 rb_str_resize(buf,
len);
459 p = strncpy(RSTRING_PTR(buf), p,
len);
462#ifdef HAVE_CYGWIN_CONV_PATH
463#define CONV_TO_POSIX_PATH(p, lib) \
464 cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
466# error no cygwin_conv_path
468 if (CONV_TO_POSIX_PATH(p, rubylib) == 0) {
473 rb_ary_push(load_path, (*filter)(rubylib_path_new(p,
len)));
479identical_path(
VALUE path)
485locale_path(
VALUE path)
487 rb_enc_associate(path, rb_locale_encoding());
494 ruby_push_include(path, locale_path);
498expand_include_path(
VALUE path)
500 char *p = RSTRING_PTR(path);
503 if (*p ==
'.' && p[1] ==
'/')
505 return rb_file_expand_path(path,
Qnil);
509ruby_incpush_expand(
const char *path)
511 ruby_push_include(path, expand_include_path);
515#if defined _WIN32 || defined __CYGWIN__
516static HMODULE libruby;
519DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
521 if (reason == DLL_PROCESS_ATTACH)
527rb_libruby_handle(
void)
533translit_char_bin(
char *p,
int from,
int to)
536 if ((
unsigned char)*p == from)
545# define chdir rb_w32_uchdir
553# define IF_UTF8_PATH(t, f) t
555# define IF_UTF8_PATH(t, f) f
567# define str_conv_enc(str, from, to) (str)
572#if defined(LOAD_RELATIVE)
574runtime_libruby_path(
void)
576#if defined _WIN32 || defined __CYGWIN__
584 while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath),
585 ret = GetModuleFileNameW(libruby, wlibpath,
len),
591 if (!ret || ret >
len) rb_fatal(
"failed to get module file name");
592#if defined __CYGWIN__
594 const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
595 size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0);
596 if (!newsize) rb_fatal(
"failed to convert module path to cygwin");
598 libpath = RSTRING_PTR(path);
599 if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) {
600 rb_str_resize(path, 0);
606 for (
len = ret, i = 0; i <
len; ++i) {
607 if (wlibpath[i] == L
'\\') {
613 len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL);
615 libpath = RSTRING_PTR(path);
616 WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath,
len, NULL, NULL);
618 rb_str_resize(wsopath, 0);
620#elif defined(HAVE_DLADDR)
623 const void* addr = (
void *)(
VALUE)expand_include_path;
625 if (!dladdr((
void *)addr, &dli)) {
629 else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
631 path = rb_readlink(fname, NULL);
636 path = rb_realpath_internal(
Qnil, fname, 1);
638 rb_str_resize(fname, 0);
641# error relative load path is not supported on this platform.
646#define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index")
648VALUE ruby_archlibdir_path, ruby_prefix_path;
653 VALUE load_path, archlibdir = 0;
654 ID id_initial_load_path_mark;
655 const char *paths = ruby_initial_load_paths;
657#if defined LOAD_RELATIVE
658#if !defined ENABLE_MULTIARCH
659# define RUBY_ARCH_PATH ""
660#elif defined RUBY_ARCH
661# define RUBY_ARCH_PATH "/"RUBY_ARCH
663# define RUBY_ARCH_PATH "/"RUBY_PLATFORM
670 sopath = runtime_libruby_path();
671 libpath = RSTRING_PTR(sopath);
673 p = strrchr(libpath,
'/');
675 static const char libdir[] =
"/"
676#ifdef LIBDIR_BASENAME
682 const ptrdiff_t libdir_len = (ptrdiff_t)
sizeof(libdir)
684 static const char bindir[] =
"/bin";
685 const ptrdiff_t bindir_len = (ptrdiff_t)
sizeof(bindir) - 1;
687 const char *p2 = NULL;
689#ifdef ENABLE_MULTIARCH
692 if (p - libpath >= bindir_len && !
STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
698 else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
699 archlibdir =
rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath);
703#ifdef ENABLE_MULTIARCH
709 p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
710 if (p)
goto multiarch;
714 baselen = p - libpath;
719 rb_str_resize(sopath, baselen);
720 libpath = RSTRING_PTR(sopath);
721#define PREFIX_PATH() sopath
722#define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
723#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
725 const size_t exec_prefix_len = strlen(ruby_exec_prefix);
726#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len))
727#define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
729 rb_gc_register_address(&ruby_prefix_path);
730 ruby_prefix_path = PREFIX_PATH();
732 if (!archlibdir) archlibdir = ruby_prefix_path;
733 rb_gc_register_address(&ruby_archlibdir_path);
734 ruby_archlibdir_path = archlibdir;
736 load_path = GET_VM()->load_path;
738 ruby_push_include(getenv(
"RUBYLIB"), identical_path);
740 id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
742 size_t len = strlen(paths);
743 VALUE path = RUBY_RELATIVE(paths,
len);
744 rb_ivar_set(path, id_initial_load_path_mark, path);
745 rb_ary_push(load_path, path);
754add_modules(
VALUE *req_list,
const char *mod)
756 VALUE list = *req_list;
760 *req_list = list = rb_ary_hidden_new(0);
763 rb_ary_push(list, feature);
767require_libraries(
VALUE *req_list)
769 VALUE list = *req_list;
770 VALUE self = rb_vm_top_self();
772 rb_encoding *extenc = rb_default_external_encoding();
776 VALUE feature = rb_ary_shift(list);
777 rb_enc_associate(feature, extenc);
780 rb_funcallv(self, require, 1, &feature);
792process_sflag(
int sflag)
810 if (s[1] ==
'-' && s[2] ==
'\0')
815 for (p = s + 1; *p; p++) {
824 else if (*p !=
'_' && !
ISALNUM(*p)) {
827 rb_str_new2(
"invalid name for global variable - ");
828 if (!(p = strchr(p,
'='))) {
834 name_error[1] = args[-1];
840 for (p = s + 1; *p; ++p) {
864 VALUE argstr, argary;
867 VALUE src_enc_name = opt->src.enc.name;
868 VALUE ext_enc_name = opt->ext.enc.name;
869 VALUE int_enc_name = opt->intern.enc.name;
872 long backtrace_length_limit = opt->backtrace_length_limit;
873 const char *crash_report = opt->crash_report;
878 opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
880 const int hyphen = *s !=
'-';
884 p = RSTRING_PTR(argstr);
885 if (hyphen) *p =
'-';
886 memcpy(p + hyphen, s,
len + 1);
892 while (*p && !
ISSPACE(*p)) ++p;
897 argc = RSTRING_LEN(argary) /
sizeof(ap);
900 argv = ptr =
ALLOC_N(
char *, argc);
901 MEMMOVE(argv, RSTRING_PTR(argary),
char *, argc);
903 while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) {
915 opt->src.enc.name = src_enc_name;
918 opt->ext.enc.name = ext_enc_name;
921 opt->intern.enc.name = int_enc_name;
923 FEATURE_SET_RESTORE(opt->features, feat);
924 FEATURE_SET_RESTORE(opt->warn, warn);
925 if (BACKTRACE_LENGTH_LIMIT_VALID_P(backtrace_length_limit)) {
926 opt->backtrace_length_limit = backtrace_length_limit;
929 opt->crash_report = crash_report;
934 rb_str_resize(argary, 0);
935 rb_str_resize(argstr, 0);
939name_match_p(
const char *name,
const char *str,
size_t len)
941 if (
len == 0)
return 0;
943 while (
TOLOWER(*str) == *name) {
944 if (!--
len)
return 1;
948 if (*str !=
'-' && *str !=
'_')
return 0;
950 if (*name !=
'-' && *name !=
'_')
return 0;
951 if (!*++name)
return 1;
953 if (--
len == 0)
return 1;
957#define NAME_MATCH_P(name, str, len) \
958 ((len) < (int)sizeof(name) && name_match_p((name), (str), (len)))
960#define UNSET_WHEN(name, bit, str, len) \
961 if (NAME_MATCH_P((name), (str), (len))) { \
962 *(unsigned int *)arg &= ~(bit); \
966#define SET_WHEN(name, bit, str, len) \
967 if (NAME_MATCH_P((name), (str), (len))) { \
968 *(unsigned int *)arg |= (bit); \
972#define LITERAL_NAME_ELEMENT(name) #name
975feature_option(
const char *str,
int len,
void *arg,
const unsigned int enable)
977 static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT,
", ");
979 unsigned int mask = ~0
U;
980 unsigned int set = 0
U;
981#if AMBIGUOUS_FEATURE_NAMES
983# define FEATURE_FOUND ++matched
985# define FEATURE_FOUND goto found
987#define SET_FEATURE(bit) \
988 if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;}
989 EACH_FEATURES(SET_FEATURE, ;);
990 if (NAME_MATCH_P(
"jit", str,
len)) {
991 set |= mask = FEATURE_BIT(jit);
994 if (NAME_MATCH_P(
"all", str,
len)) {
996 mask &= ~feature_jit_mask | FEATURE_BIT(jit);
999#if AMBIGUOUS_FEATURE_NAMES
1000 if (matched == 1)
goto found;
1002 VALUE mesg = rb_sprintf(
"ambiguous feature: '%.*s' (",
len, str);
1003#define ADD_FEATURE_NAME(bit) \
1004 if (FEATURE_BIT(bit) & set) { \
1005 rb_str_cat_cstr(mesg, #bit); \
1006 if (--matched) rb_str_cat_cstr(mesg, ", "); \
1008 EACH_FEATURES(ADD_FEATURE_NAME, ;);
1011#undef ADD_FEATURE_NAME
1016 rb_warn(
"unknown argument for --%s: '%.*s'",
1017 enable ?
"enable" :
"disable",
len, str);
1018 rb_warn(
"features are [%.*s].", (
int)strlen(list), list);
1022 FEATURE_SET_TO(*argp, mask, (mask & enable));
1023 if (NAME_MATCH_P(
"frozen_string_literal", str,
len)) {
1024 FEATURE_SET_TO(*argp, FEATURE_BIT(frozen_string_literal_set), FEATURE_BIT(frozen_string_literal_set));
1030enable_option(
const char *str,
int len,
void *arg)
1032 feature_option(str,
len, arg, ~0
U);
1036disable_option(
const char *str,
int len,
void *arg)
1038 feature_option(str,
len, arg, 0
U);
1042int ruby_env_debug_option(
const char *str,
int len,
void *arg);
1045debug_option(
const char *str,
int len,
void *arg)
1047 static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1049#define SET_WHEN_DEBUG(bit) \
1050 if (NAME_MATCH_P(#bit, str, len)) { \
1051 FEATURE_SET(*argp, DEBUG_BIT(bit)); \
1054 EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;);
1058 rb_warn(
"unknown argument for --debug: '%.*s'",
len, str);
1059 rb_warn(
"debug features are [%.*s].", (
int)strlen(list), list);
1063memtermspn(
const char *str,
char term,
int len)
1066 if (
len <= 0)
return 0;
1067 const char *next = memchr(str, term,
len);
1068 return next ? (int)(next - str) :
len;
1071static const char additional_opt_sep =
'+';
1074dump_additional_option_flag(
const char *str,
int len,
unsigned int bits,
bool set)
1076#define SET_DUMP_OPT(bit) if (NAME_MATCH_P(#bit, str, len)) { \
1077 return set ? (bits | DUMP_BIT(opt_ ## bit)) : (bits & ~DUMP_BIT(opt_ ## bit)); \
1079 SET_DUMP_OPT(error_tolerant);
1080 SET_DUMP_OPT(comment);
1081 SET_DUMP_OPT(optimize);
1083 rb_warn(
"don't know how to dump with%s '%.*s'", set ?
"" :
"out",
len, str);
1088dump_additional_option(
const char *str,
int len,
unsigned int bits)
1091 for (;
len-- > 0 && *str++ == additional_opt_sep;
len -= w, str += w) {
1092 w = memtermspn(str, additional_opt_sep,
len);
1094 if (*str ==
'-' || *str ==
'+') {
1095 set = *str++ ==
'+';
1099 int n = memtermspn(str,
'-', w);
1100 if (str[n] ==
'-') {
1101 if (NAME_MATCH_P(
"with", str, n)) {
1105 else if (NAME_MATCH_P(
"without", str, n)) {
1112 bits = dump_additional_option_flag(str, w, bits, set);
1118dump_option(
const char *str,
int len,
void *arg)
1120 static const char list[] = EACH_DUMPS(LITERAL_NAME_ELEMENT,
", ");
1121 unsigned int *bits_ptr = (
unsigned int *)arg;
1122 if (*str ==
'+' || *str ==
'-') {
1123 bool set = *str++ ==
'+';
1124 *bits_ptr = dump_additional_option_flag(str, --
len, *bits_ptr, set);
1127 int w = memtermspn(str, additional_opt_sep,
len);
1129#define SET_WHEN_DUMP(bit) \
1130 if (NAME_MATCH_P(#bit "-", (str), (w))) { \
1131 *bits_ptr = dump_additional_option(str + w, len - w, *bits_ptr | DUMP_BIT(bit)); \
1134 EACH_DUMPS(SET_WHEN_DUMP, ;);
1135 rb_warn(
"don't know how to dump '%.*s',",
len, str);
1136 rb_warn(
"but only [%.*s].", (
int)strlen(list), list);
1140set_option_encoding_once(
const char *
type,
VALUE *name,
const char *e,
long elen)
1144 if (!elen) elen = strlen(e);
1150 "%s already set to %"PRIsVALUE,
type, *name);
1155#define set_internal_encoding_once(opt, e, elen) \
1156 set_option_encoding_once("default_internal", &(opt)->intern.enc.name, (e), (elen))
1157#define set_external_encoding_once(opt, e, elen) \
1158 set_option_encoding_once("default_external", &(opt)->ext.enc.name, (e), (elen))
1159#define set_source_encoding_once(opt, e, elen) \
1160 set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen))
1162#define yjit_opt_match_noarg(s, l, name) \
1163 opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --yjit-" name " is ignored"), 1) : 1)
1164#define yjit_opt_match_arg(s, l, name) \
1165 opt_match(s, l, name) && (*(s) && *(s+1) ? 1 : (rb_raise(rb_eRuntimeError, "--yjit-" name " needs an argument"), 0))
1169setup_yjit_options(
const char *s)
1172 bool rb_yjit_parse_option(
const char* s);
1173 bool success = rb_yjit_parse_option(s);
1181 "invalid YJIT option '%s' (--help will show valid yjit options)",
1192 extern void *rb_zjit_init_options(
void);
1193 extern bool rb_zjit_parse_option(
void *options,
const char *s);
1195 if (!opt->zjit) opt->zjit = rb_zjit_init_options();
1196 if (!rb_zjit_parse_option(opt->zjit, s)) {
1197 rb_raise(
rb_eRuntimeError,
"invalid ZJIT option '%s' (--help will show valid zjit options)", s);
1219 unsigned int bits = 0;
1220 static const char no_prefix[] =
"no-";
1221 int enable = strncmp(s += 2, no_prefix,
sizeof(no_prefix)-1) != 0;
1222 if (!enable) s +=
sizeof(no_prefix)-1;
1223 size_t len = strlen(s);
1224 if (NAME_MATCH_P(
"deprecated", s,
len)) {
1227 else if (NAME_MATCH_P(
"experimental", s,
len)) {
1230 else if (NAME_MATCH_P(
"performance", s,
len)) {
1233 else if (NAME_MATCH_P(
"strict_unused_block", s,
len)) {
1237 rb_warn(
"unknown warning category: '%s'", s);
1239 if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0);
1252 if (!opt->warning) {
1268 FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS, 0);
1274 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1293 if (!opt->e_script) {
1295 if (opt->script == 0)
1308 const char *enc_name = 0;
1311 enc_name =
"EUC-JP";
1314 enc_name =
"Windows-31J";
1319 case 'N':
case 'n':
case 'A':
case 'a':
1320 enc_name =
"ASCII-8BIT";
1325 if (!opt->ext.enc.name)
1326 opt->ext.enc.name = opt->src.enc.name;
1345 else if (v == 0 && numlen >= 2) {
1346 rb_rs = rb_fstring_lit(
"");
1360# define set_encoding_part(type) \
1361 if (!(p = strchr(s, ':'))) { \
1362 set_##type##_encoding_once(opt, s, 0); \
1366 set_##type##_encoding_once(opt, s, p-s); \
1368 set_encoding_part(external);
1369 if (!*(s = ++p))
return;
1370 set_encoding_part(internal);
1371 if (!*(s = ++p))
return;
1372#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1373 set_encoding_part(source);
1374 if (!*(s = ++p))
return;
1377# undef set_encoding_part
1386# define is_option_end(c, allow_hyphen) \
1387 (!(c) || ((allow_hyphen) && (c) == '-') || (c) == '=')
1388# define check_envopt(name, allow_envopt) \
1389 (((allow_envopt) || !envopt) ? (void)0 : \
1390 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
1391# define need_argument(name, s, needs_arg, next_arg) \
1392 ((*(s) ? !*++(s) : (next_arg) && (argc <= 1 || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
1393 rb_raise(rb_eRuntimeError, "missing argument for --" name) \
1395# define is_option_with_arg(name, allow_hyphen, allow_envopt) \
1396 is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue, Qtrue)
1397# define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg, next_arg) \
1398 (strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) && \
1399 (s[n] != '-' || (s[n] && s[n+1])) ? \
1400 (check_envopt(name, (allow_envopt)), s += n, \
1401 need_argument(name, s, needs_arg, next_arg), 1) : 0)
1403 if (strcmp(
"copyright", s) == 0) {
1404 if (envopt)
goto noenvopt_long;
1405 opt->dump |= DUMP_BIT(copyright);
1416 else if (is_option_with_arg(
"enable",
Qtrue,
Qtrue)) {
1419 else if (is_option_with_arg(
"disable",
Qtrue,
Qtrue)) {
1422 else if (is_option_with_arg(
"encoding",
Qfalse,
Qtrue)) {
1423 proc_encoding_option(opt, s,
"--encoding");
1425 else if (is_option_with_arg(
"internal-encoding",
Qfalse,
Qtrue)) {
1426 set_internal_encoding_once(opt, s, 0);
1428 else if (is_option_with_arg(
"external-encoding",
Qfalse,
Qtrue)) {
1429 set_external_encoding_once(opt, s, 0);
1431 else if (is_option_with_arg(
"parser",
Qfalse,
Qtrue)) {
1432 if (strcmp(
"prism", s) == 0) {
1433 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PRISM);
1435 else if (strcmp(
"parse.y", s) == 0) {
1436 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PARSE_Y);
1442#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1443 else if (is_option_with_arg(
"source-encoding",
Qfalse,
Qtrue)) {
1444 set_source_encoding_once(opt, s, 0);
1447 else if (strcmp(
"version", s) == 0) {
1448 if (envopt)
goto noenvopt_long;
1449 opt->dump |= DUMP_BIT(version);
1451 else if (strcmp(
"verbose", s) == 0) {
1455 else if (strcmp(
"jit", s) == 0) {
1457 FEATURE_SET(opt->features, FEATURE_BIT(jit));
1459 rb_warn(
"Ruby was built without JIT support");
1462 else if (is_option_with_optarg(
"yjit",
'-',
true,
false,
false)) {
1464 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
1465 setup_yjit_options(s);
1467 rb_warn(
"Ruby was built without YJIT support."
1468 " You may need to install rustc to build Ruby with YJIT.");
1471 else if (is_option_with_optarg(
"zjit",
'-',
true,
false,
false)) {
1473 FEATURE_SET(opt->features, FEATURE_BIT(zjit));
1474 setup_zjit_options(opt, s);
1476 rb_warn(
"Ruby was built without ZJIT support."
1477 " You may need to install rustc to build Ruby with ZJIT.");
1480 else if (strcmp(
"yydebug", s) == 0) {
1481 if (envopt)
goto noenvopt_long;
1482 opt->dump |= DUMP_BIT(yydebug);
1487 else if (strcmp(
"help", s) == 0) {
1488 if (envopt)
goto noenvopt_long;
1489 opt->dump |= DUMP_BIT(help);
1492 else if (is_option_with_arg(
"backtrace-limit",
Qfalse,
Qtrue)) {
1494 long n = strtol(s, &e, 10);
1495 if (
errno == ERANGE || !BACKTRACE_LENGTH_LIMIT_VALID_P(n) || *e) {
1499 opt->backtrace_length_limit = n;
1502 else if (is_option_with_arg(
"crash-report",
true,
true)) {
1503 opt->crash_report = s;
1507 "invalid option --%s (-h will show valid options)", s);
1509 return argc0 - argc + 1;
1513# undef is_option_end
1515# undef need_argument
1516# undef is_option_with_arg
1517# undef is_option_with_optarg
1524 long n, argc0 = argc;
1526 int warning = opt->warning;
1528 if (argc <= 0 || !argv)
1531 for (argc--, argv++; argc > 0; argc--, argv++) {
1532 const char *
const arg = argv[0];
1533 if (!arg || arg[0] !=
'-' || !arg[1])
1540 if (envopt)
goto noenvopt;
1541 opt->do_split = TRUE;
1546 if (envopt)
goto noenvopt;
1547 opt->do_print = TRUE;
1550 if (envopt)
goto noenvopt;
1551 opt->do_loop = TRUE;
1562 if (envopt)
goto noenvopt;
1563 opt->dump |= DUMP_BIT(yydebug);
1572 opt->dump |= DUMP_BIT(version_v);
1575 if (!opt->warning) {
1579 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1584 if (!(s = proc_W_option(opt, s, &warning)))
break;
1588 if (envopt)
goto noenvopt;
1589 opt->dump |= DUMP_BIT(syntax);
1594 if (envopt)
goto noenvopt;
1596 if (!opt->sflag) opt->sflag = 1;
1601 if (envopt)
goto noenvopt;
1602 opt->dump |= DUMP_BIT(usage);
1606 if (envopt)
goto noenvopt;
1607 opt->do_line = TRUE;
1613 if (envopt)
goto noenvopt;
1615 opt->do_search = TRUE;
1620 if (envopt)
goto noenvopt;
1621 if (!(n = proc_e_option(opt, s, argc, argv)))
break;
1630 add_modules(&opt->req_list, s);
1632 else if (argc > 1) {
1633 add_modules(&opt->req_list, argv[1]);
1639 if (envopt)
goto noenvopt;
1641 ruby_set_inplace_mode(s + 1);
1645 if (envopt)
goto noenvopt;
1649 if (*s && chdir(s) < 0) {
1650 rb_fatal(
"Can't chdir to %s", s);
1656 if (envopt)
goto noenvopt;
1657 if (!*++s && (!--argc || !(s = *++argv) || !*s)) {
1658 rb_fatal(
"Can't chdir");
1661 rb_fatal(
"Can't chdir to %s", s);
1666 if (envopt)
goto noenvopt;
1673 if (!*++s && (!--argc || !(s = *++argv))) {
1676 proc_encoding_option(opt, s,
"-E");
1680 set_internal_encoding_once(opt,
"UTF-8", 0);
1685 if (!(s = proc_K_option(opt, s)))
break;
1691 ruby_incpush_expand(s);
1692 else if (argc > 1) {
1693 ruby_incpush_expand(argv[1]);
1699 if (envopt)
goto noenvopt;
1700 if (!(s = proc_0_option(opt, s)))
break;
1704 if (!s[1] || (s[1] ==
'\r' && !s[2])) {
1710 if (!(n = proc_long_options(opt, s, argc, argv, envopt)))
goto switch_end;
1721 rb_encoding *enc = IF_UTF8_PATH(rb_utf8_encoding(), rb_locale_encoding());
1722 const char *e = s + strlen(s);
1723 int r = rb_enc_precise_mbclen(s, e, enc);
1724 unsigned int c = (
unsigned char)*s;
1726 c = rb_enc_mbc_to_codepoint(s, e, enc);
1727 if (ONIGENC_IS_CODE_GRAPH(enc, c) ||
1728 ((s = ruby_escaped_char(c)) != 0 &&
1729 (r = (
int)strlen(s), 1))) {
1731 "invalid option -%.*s (-h will show valid options)",
1736 "invalid option -\\x%.2x (-h will show valid options)",
1753 if (warning) opt->warning = warning;
1754 return argc0 - argc;
1757void Init_builtin_features(
void);
1760ruby_init_prelude(
void)
1762 Init_builtin_features();
1766void rb_call_builtin_inits(
void);
1770#if RBIMPL_HAS_ATTRIBUTE(weak)
1774Init_extra_exts(
void)
1781 rb_warning_category_update(opt->warn.mask, opt->warn.set);
1783 if (opt->dump & dump_exit_bits)
return;
1785 if (FEATURE_SET_P(opt->features, gems)) {
1787 if (opt->features.set & FEATURE_BIT(error_highlight)) {
1790 if (opt->features.set & FEATURE_BIT(did_you_mean)) {
1793 if (opt->features.set & FEATURE_BIT(syntax_suggest)) {
1800 if (getenv(
"RUBY_GC_HEAP_INIT_SLOTS")) {
1801 rb_warn_deprecated(
"The environment variable RUBY_GC_HEAP_INIT_SLOTS",
1802 "environment variables RUBY_GC_HEAP_%d_INIT_SLOTS");
1808 GET_VM()->running = 0;
1809 rb_call_builtin_inits();
1810 GET_VM()->running = 1;
1811 memset(ruby_vm_redefined_flag, 0,
sizeof(ruby_vm_redefined_flag));
1813 ruby_init_prelude();
1817 rb_yjit_init(opt->yjit);
1821 extern void rb_zjit_init(
void *options);
1822 rb_zjit_init(opt->zjit);
1828 void Init_builtin_yjit_hook();
1829 Init_builtin_yjit_hook();
1833 require_libraries(&opt->req_list);
1837opt_enc_index(
VALUE enc_name)
1839 const char *s = RSTRING_PTR(enc_name);
1840 int i = rb_enc_find_index(s);
1845 else if (rb_enc_dummy_p(rb_enc_from_index(i))) {
1851#define rb_progname (GET_VM()->progname)
1852#define rb_orig_progname (GET_VM()->orig_progname)
1868#define rb_define_readonly_boolean(name, val) \
1869 rb_define_virtual_variable((name), (val) ? true_value : false_value, 0)
1878 rb_raise(
rb_eTypeError,
"$_ value need to be String (%s given)",
1960setup_pager_env(
void)
1962 if (!getenv(
"LESS")) {
1964 ruby_setenv(
"LESS",
"-R +/^[A-Z].*");
1972 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
1974 if (!GetConsoleMode(h, &m))
return 0;
1975# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
1976# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
1978 if (!(m & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;
1981#elif !defined(HAVE_WORKING_FORK)
1982# define tty_enabled() 0
1991 return rb_enc_associate(
rb_str_dup(str), enc);
1999env_var_truthy(
const char *name)
2001 const char *value = getenv(name);
2005 if (strcmp(value,
"1") == 0)
2007 if (strcmp(value,
"true") == 0)
2009 if (strcmp(value,
"yes") == 0)
2016rb_pid_t rb_fork_ruby(
int *status);
2019show_help(
const char *progname,
int help)
2021 int tty = isatty(1);
2024 const char *pager_env = getenv(
"RUBY_PAGER");
2025 if (!pager_env) pager_env = getenv(
"PAGER");
2026 if (pager_env && *pager_env && isatty(0)) {
2027 const char *columns_env = getenv(
"COLUMNS");
2028 if (columns_env) columns = atoi(columns_env);
2030#ifdef HAVE_WORKING_FORK
2033 rb_pid_t pid = rb_fork_ruby(NULL);
2038 else if (pid == 0) {
2056 int oldout = dup(1);
2057 int olderr = dup(2);
2058 int fd =
RFILE(port)->fptr->fd;
2059 tty = tty_enabled();
2062 usage(progname, 1, tty, columns);
2072 usage(progname, help, tty, columns);
2080 VALUE parser = rb_parser_new();
2081 const unsigned int dump = opt->dump;
2083 if (dump & DUMP_BIT(yydebug)) {
2084 rb_parser_set_yydebug(parser,
Qtrue);
2087 if ((dump & dump_exit_bits) && (dump & DUMP_BIT(opt_error_tolerant))) {
2088 rb_parser_error_tolerant(parser);
2091 if (opt->e_script) {
2092 VALUE progname = rb_progname;
2093 rb_parser_set_context(parser, 0, TRUE);
2097 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2098 opt->do_line, opt->do_split);
2099 ast_value = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
2103 int xflag = opt->xflag;
2104 f = open_load_file(opt->script_name, &xflag);
2105 opt->xflag = xflag != 0;
2106 rb_parser_set_context(parser, 0, f ==
rb_stdin);
2107 ast_value = load_file(parser, opt->script_name, f, 1, opt);
2109 ast = rb_ruby_ast_data_get(ast_value);
2110 if (!ast->body.root) {
2111 rb_ast_dispose(ast);
2120 uint8_t command_line = 0;
2126 return command_line;
2130prism_script_shebang_callback(
pm_options_t *options,
const uint8_t *source,
size_t length,
void *data)
2135 char *switches = malloc(length + 1);
2136 memcpy(switches, source, length);
2137 switches[length] =
'\0';
2139 int no_src_enc = !opt->src.enc.name;
2140 int no_ext_enc = !opt->ext.enc.name;
2141 int no_int_enc = !opt->intern.enc.name;
2143 moreswitches(switches, opt, 0);
2146 pm_options_command_line_set(options, prism_script_command_line(opt));
2148 if (no_src_enc && opt->src.enc.name) {
2149 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2152 if (no_ext_enc && opt->ext.enc.name) {
2153 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2155 if (no_int_enc && opt->intern.enc.name) {
2156 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2170 pm_options_line_set(options, 1);
2171 pm_options_main_script_set(options,
true);
2173 const bool read_stdin = (strcmp(opt->script,
"-") == 0);
2176 pm_options_encoding_set(options, rb_enc_name(rb_locale_encoding()));
2178 if (opt->src.enc.name != 0) {
2182 uint8_t command_line = prism_script_command_line(opt);
2186 pm_options_command_line_set(options, command_line);
2187 pm_options_filepath_set(options,
"-");
2188 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2191 error = pm_parse_stdin(result);
2200 else if (opt->e_script) {
2202 pm_options_command_line_set(options, command_line);
2205 result->
node.coverage_enabled = 0;
2206 error = pm_parse_string(result, opt->e_script,
rb_str_new2(
"-e"), NULL);
2209 VALUE script_name = rb_str_encode_ospath(opt->script_name);
2211 pm_options_command_line_set(options, command_line);
2212 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2214 error = pm_load_file(result, script_name,
true);
2221 error = pm_parse_file(result, opt->script_name, NULL);
2226 if (
RTEST(rb_get_coverages())) {
2227 result->
node.coverage_enabled = 1;
2234 int xflag = opt->xflag;
2235 VALUE file = open_load_file(script_name, &xflag);
2240 if ((parser->
start + offset < parser->end) && parser->
start[offset] ==
'\r') offset++;
2241 if ((parser->
start + offset < parser->end) && parser->
start[offset] ==
'\n') offset++;
2248 if (!
NIL_P(error)) {
2249 pm_parse_result_free(result);
2259 pm_prettyprint(&output_buffer, &result->
parser, result->
node.ast_node);
2261 pm_buffer_free(&output_buffer);
2268 if (OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt)) {
2269 rb_backtrace_length_limit = opt->backtrace_length_limit;
2279 rb_define_readonly_boolean(
"$-p", opt->do_print);
2280 rb_define_readonly_boolean(
"$-l", opt->do_line);
2281 rb_define_readonly_boolean(
"$-a", opt->do_split);
2283 rb_gvar_ractor_local(
"$-p");
2284 rb_gvar_ractor_local(
"$-l");
2285 rb_gvar_ractor_local(
"$-a");
2287 if ((rb_e_script = opt->e_script) != 0) {
2289 rb_vm_register_global_object(opt->e_script);
2293 VALUE script = (opt->e_script ? opt->e_script :
Qnil);
2294 rb_exec_event_hook_script_compiled(ec, iseq, script);
2305#define dispose_result() \
2306 (result.ast ? rb_ast_dispose(result.ast) : pm_parse_result_free(&result.prism))
2315 char fbuf[MAXPATHLEN];
2316 int i = (int)proc_options(argc, argv, opt, 0);
2317 unsigned int dump = opt->dump & dump_exit_bits;
2319 const long loaded_before_enc =
RARRAY_LEN(vm->loaded_features);
2321 if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
2322 const char *
const progname =
2323 (argc > 0 && argv && argv[0] ? argv[0] :
2324 origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] :
2326 show_help(progname, (opt->dump & DUMP_BIT(help)));
2333 if (FEATURE_SET_P(opt->features, rubyopt) && (s = getenv(
"RUBYOPT"))) {
2334 moreswitches(s, opt, 1);
2337 if (opt->src.enc.name)
2341 rb_warning(
"-K is specified; it is for 1.8 compatibility and may cause odd behavior");
2343 if (!(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2345 if (!FEATURE_USED_P(opt->features, yjit) && env_var_truthy(
"RUBY_YJIT_ENABLE")) {
2346 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
2350 if (MULTI_BITS_P(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2351 rb_warn(
"Only one JIT can be enabled at the same time. Exiting");
2356 if (FEATURE_SET_P(opt->features, yjit)) {
2357 bool rb_yjit_option_disable(
void);
2358 opt->yjit = !rb_yjit_option_disable();
2362 if (FEATURE_SET_P(opt->features, zjit) && !opt->zjit) {
2363 extern void *rb_zjit_init_options(
void);
2364 opt->zjit = rb_zjit_init_options();
2368 ruby_mn_threads_params();
2369 Init_ruby_description(opt);
2371 if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
2373 if (opt->dump & DUMP_BIT(version))
return Qtrue;
2375 if (opt->dump & DUMP_BIT(copyright)) {
2380 if (!opt->e_script) {
2387 opt->script = argv[0];
2388 if (!opt->script || opt->script[0] ==
'\0') {
2391 else if (opt->do_search) {
2392 const char *path = getenv(
"RUBYPATH");
2396 opt->script = dln_find_file_r(argv[0], path, fbuf,
sizeof(fbuf));
2399 opt->script = dln_find_file_r(argv[0], getenv(
PATH_ENV), fbuf,
sizeof(fbuf));
2402 opt->script = argv[0];
2407 if (opt->script[0] ==
'-' && !opt->script[1]) {
2408 forbid_setid(
"program input from stdin");
2413 opt->script = RSTRING_PTR(opt->script_name);
2416 translit_char_bin(RSTRING_PTR(opt->script_name),
'\\',
'/');
2419 ruby_gc_set_params();
2423 lenc = rb_locale_encoding();
2424 rb_enc_associate(rb_progname, lenc);
2426 if (opt->ext.enc.name != 0) {
2427 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2429 if (opt->intern.enc.name != 0) {
2430 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2432 if (opt->src.enc.name != 0) {
2433 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2434 src_encoding_index = opt->src.enc.index;
2436 if (opt->ext.enc.index >= 0) {
2437 enc = rb_enc_from_index(opt->ext.enc.index);
2440 enc = IF_UTF8_PATH(uenc, lenc);
2442 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2443 if (opt->intern.enc.index >= 0) {
2444 enc = rb_enc_from_index(opt->intern.enc.index);
2445 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2446 opt->intern.enc.index = -1;
2451 rb_enc_associate(opt->script_name, IF_UTF8_PATH(uenc, lenc));
2454 opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
2455 opt->script = RSTRING_PTR(opt->script_name);
2459 if (IF_UTF8_PATH(uenc != lenc, 1)) {
2461 VALUE load_path = vm->load_path;
2462 const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
2463 int modifiable = FALSE;
2465 rb_get_expanded_load_path();
2466 for (i = 0; i <
RARRAY_LEN(load_path); ++i) {
2468 int mark = rb_attr_get(path, id_initial_load_path_mark) == path;
2471 if (newpath == path)
continue;
2474 if (!(path = copy_str(path, lenc, !mark)))
continue;
2476 if (mark)
rb_ivar_set(path, id_initial_load_path_mark, path);
2478 rb_ary_modify(load_path);
2484 rb_ary_replace(vm->load_path_snapshot, load_path);
2488 VALUE loaded_features = vm->loaded_features;
2489 bool modified =
false;
2490 for (
long i = loaded_before_enc; i <
RARRAY_LEN(loaded_features); ++i) {
2492 if (!(path = copy_str(path, IF_UTF8_PATH(uenc, lenc),
true)))
continue;
2494 rb_ary_modify(loaded_features);
2500 rb_ary_replace(vm->loaded_features_snapshot, loaded_features);
2504 if (opt->features.mask & COMPILATION_FEATURES) {
2505 VALUE option = rb_hash_new();
2506#define SET_COMPILE_OPTION(h, o, name) \
2507 rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \
2508 RBOOL(FEATURE_SET_P(o->features, name)))
2510 if (FEATURE_SET_P(opt->features, frozen_string_literal_set)) {
2511 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
2513 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
2514 rb_funcallv(rb_cISeq,
rb_intern_const(
"compile_option="), 1, &option);
2515#undef SET_COMPILE_OPTION
2518 opt->sflag = process_sflag(opt->sflag);
2520 if (opt->e_script) {
2522 if (opt->src.enc.index >= 0) {
2523 eenc = rb_enc_from_index(opt->src.enc.index);
2528 if (ienc) eenc = ienc;
2533 opt->e_script = str_conv_enc(opt->e_script, uenc, eenc);
2536 rb_enc_associate(opt->e_script, eenc);
2539 if (!rb_ruby_prism_p()) {
2540 ast_value = process_script(opt);
2541 if (!(result.ast = rb_ruby_ast_data_get(ast_value)))
return Qfalse;
2544 prism_script(opt, &result.prism);
2547 if ((dump & DUMP_BIT(yydebug)) && !(dump &= ~DUMP_BIT(yydebug))) {
2552 if (opt->ext.enc.index >= 0) {
2553 enc = rb_enc_from_index(opt->ext.enc.index);
2556 enc = IF_UTF8_PATH(uenc, lenc);
2558 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2559 if (opt->intern.enc.index >= 0) {
2561 enc = rb_enc_from_index(opt->intern.enc.index);
2562 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2564 else if (!rb_default_internal_encoding())
2566 rb_enc_set_default_internal(
Qnil);
2567 rb_stdio_set_default_encoding();
2569 opt->sflag = process_sflag(opt->sflag);
2572 if (dump & DUMP_BIT(syntax)) {
2573 printf(
"Syntax OK\n");
2574 dump &= ~DUMP_BIT(syntax);
2581 if (dump & DUMP_BIT(parsetree)) {
2584 int comment = opt->dump & DUMP_BIT(opt_comment);
2585 tree = rb_parser_dump_tree(result.ast->body.root, comment);
2588 tree = prism_dump_tree(&result.prism);
2592 dump &= ~DUMP_BIT(parsetree);
2601 if (!opt->e_script && strcmp(opt->script,
"-")) {
2602 path = rb_realpath_internal(
Qnil, opt->script_name, 1);
2605 path = str_conv_enc(path, uenc, lenc);
2609 rb_enc_copy(path, opt->script_name);
2614 GetBindingPtr(
rb_const_get(rb_cObject, rb_intern(
"TOPLEVEL_BINDING")), toplevel_binding);
2615 const struct rb_block *base_block = toplevel_context(toplevel_binding);
2616 const rb_iseq_t *parent = vm_block_iseq(base_block);
2617 bool optimize = (opt->dump & DUMP_BIT(opt_optimize)) != 0;
2622 iseq = pm_iseq_new_main(&pm->
node, opt->script_name, path, parent, optimize, &error_state);
2624 pm_parse_result_free(pm);
2628 rb_jump_tag(error_state);
2633 iseq = rb_iseq_new_main(ast_value, opt->script_name, path, parent, optimize);
2634 rb_ast_dispose(ast);
2638 if (dump & DUMP_BIT(insns)) {
2641 dump &= ~DUMP_BIT(insns);
2642 if (!dump)
return Qtrue;
2644 if (opt->dump & dump_exit_bits)
return Qtrue;
2646 process_options_global_setup(opt, iseq);
2652warn_cr_in_shebang(
const char *str,
long len)
2654 if (
len > 1 && str[
len-1] ==
'\n' && str[
len-2] ==
'\r') {
2655 rb_warn(
"shebang line ending with \\r may cause problems");
2659#define warn_cr_in_shebang(str, len) (void)0
2662void rb_reset_argf_lineno(
long n);
2672void rb_set_script_lines_for(
VALUE vparser,
VALUE path);
2675load_file_internal(
VALUE argp_v)
2678 VALUE parser = argp->parser;
2679 VALUE orig_fname = argp->fname;
2680 int script = argp->script;
2688 CONST_ID(set_encoding,
"set_encoding");
2694 int no_src_enc = !opt->src.enc.name;
2695 int no_ext_enc = !opt->ext.enc.name;
2696 int no_int_enc = !opt->intern.enc.name;
2698 enc = rb_ascii8bit_encoding();
2699 rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
2707 if (
len > 2 && str[0] ==
'#' && str[1] ==
'!') {
2708 if (line_start == 1) warn_cr_in_shebang(str,
len);
2722 warn_cr_in_shebang(str,
len);
2725 goto search_shebang;
2730 if (*str ==
'\n') *str-- =
'\0';
2731 if (*str ==
'\r') *str-- =
'\0';
2733 if ((p = strstr(p,
" -")) != 0) {
2735 moreswitches(p + 1, opt, 0);
2741 else if (!
NIL_P(c)) {
2745 if (no_src_enc && opt->src.enc.name) {
2746 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2747 src_encoding_index = opt->src.enc.index;
2749 if (no_ext_enc && opt->ext.enc.name) {
2750 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2752 if (no_int_enc && opt->intern.enc.name) {
2753 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2756 else if (!
NIL_P(c)) {
2762 rb_reset_argf_lineno(0);
2765 if (opt->src.enc.index >= 0) {
2766 enc = rb_enc_from_index(opt->src.enc.index);
2769 enc = rb_locale_encoding();
2772 enc = rb_utf8_encoding();
2774 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2775 opt->do_line, opt->do_split);
2777 rb_set_script_lines_for(parser, orig_fname);
2781 rb_enc_associate(f, enc);
2782 return rb_parser_compile_string_path(parser, orig_fname, f, line_start);
2785 ast_value = rb_parser_compile_file_path(parser, orig_fname, f, line_start);
2786 rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser));
2787 if (script && rb_parser_end_seen_p(parser)) {
2808disable_nonblock(
int fd)
2810#if defined(HAVE_FCNTL) && defined(F_SETFL)
2811 if (fcntl(fd, F_SETFL, 0) < 0) {
2812 const int e =
errno;
2815 if (e == ENOTSUP)
return 0;
2817# if defined B_UNSUPPORTED
2818 if (e == B_UNSUPPORTED)
return 0;
2827open_load_file(
VALUE fname_v,
int *xflag)
2829 const char *fname = (fname_v = rb_str_encode_ospath(fname_v),
2831 long flen = RSTRING_LEN(fname_v);
2835 if (flen == 1 && fname[0] ==
'-') {
2842 const int MODE_TO_LOAD = O_RDONLY | (
2843#if defined O_NONBLOCK && HAVE_FCNTL
2845 !(O_NONBLOCK & O_ACCMODE) ? O_NONBLOCK :
2847#if defined O_NDELAY && HAVE_FCNTL
2848 !(O_NDELAY & O_ACCMODE) ? O_NDELAY :
2851 int mode = MODE_TO_LOAD;
2852#if defined DOSISH || defined __CYGWIN__
2853# define isdirsep(x) ((x) == '/' || (x) == '\\')
2855 static const char exeext[] =
".exe";
2856 enum {extlen =
sizeof(exeext)-1};
2857 if (flen > extlen && !isdirsep(fname[flen-extlen-1]) &&
2858 STRNCASECMP(fname+flen-extlen, exeext, extlen) == 0) {
2867 if (!rb_gc_for_fd(e)) {
2868 rb_load_fail(fname_v, strerror(e));
2871 rb_load_fail(fname_v, strerror(
errno));
2876 if (MODE_TO_LOAD != O_RDONLY && (e = disable_nonblock(fd)) != 0) {
2878 rb_load_fail(fname_v, strerror(e));
2881 e = ruby_is_fd_loadable(fd);
2885 rb_load_fail(fname_v, strerror(e));
2901restore_load_file(
VALUE arg)
2916 arg.parser = parser;
2918 arg.script = script;
2922 restore_load_file, (
VALUE)&arg);
2936 ast_value = rb_parser_load_file(rb_parser_new(), fname_v);
2937 return (
void *)rb_ruby_ast_data_get(ast_value);
2941rb_parser_load_file(
VALUE parser,
VALUE fname_v)
2945 VALUE f = open_load_file(fname_v, &xflag);
2946 cmdline_options_init(&opt)->xflag = xflag != 0;
2947 return load_file(parser, fname_v, f, 0, &opt);
2962proc_argv0(
VALUE process)
2964 return rb_orig_progname;
2989 return ruby_setproctitle(title);
2993ruby_setproctitle(
VALUE title)
3003 if (origarg.argv == 0)
3010external_str_new_cstr(
const char *p)
3014 str = str_conv_enc(str, NULL, rb_default_external_encoding());
3022set_progname(
VALUE name)
3024 rb_orig_progname = rb_progname = name;
3025 rb_vm_set_progname(rb_progname);
3049 rb_uid_t uid = getuid();
3050 rb_uid_t euid = geteuid();
3051 rb_gid_t gid = getgid();
3052 rb_gid_t egid = getegid();
3054 if (uid != euid) opt->setids |= 1;
3055 if (egid != gid) opt->setids |= 2;
3062 if (opt->setids & 1)
3064 if (opt->setids & 2)
3069verbose_getter(
ID id,
VALUE *ptr)
3071 return *rb_ruby_verbose_ptr();
3077 *rb_ruby_verbose_ptr() =
RTEST(val) ?
Qtrue : val;
3081opt_W_getter(
ID id,
VALUE *dmy)
3083 VALUE v = *rb_ruby_verbose_ptr();
3098debug_getter(
ID id,
VALUE *dmy)
3100 return *rb_ruby_debug_ptr();
3106 *rb_ruby_debug_ptr() = val;
3119 rb_gvar_ractor_local(
"$VERBOSE");
3120 rb_gvar_ractor_local(
"$-v");
3121 rb_gvar_ractor_local(
"$-w");
3122 rb_gvar_ractor_local(
"$-W");
3123 rb_gvar_ractor_local(
"$DEBUG");
3124 rb_gvar_ractor_local(
"$-d");
3148 for (i = 0; i < argc; i++) {
3149 VALUE arg = external_str_new_cstr(argv[i]);
3152 rb_ary_push(av, arg);
3161 const char *script_name = (argc > 0 && argv[0]) ? argv[0] :
ruby_engine;
3163 if (!origarg.argv || origarg.argc <= 0) {
3164 origarg.argc = argc;
3165 origarg.argv = argv;
3167 set_progname(external_str_new_cstr(script_name));
3169 rb_vm_register_global_object(
rb_argv0);
3171#ifndef HAVE_SETPROCTITLE
3172 ruby_init_setproctitle(argc, argv);
3175 if (getenv(
"RUBY_FREE_AT_EXIT")) {
3176 rb_free_at_exit =
true;
3180 iseq = process_options(argc, argv, cmdline_options_init(&opt));
3182 if (opt.crash_report && *opt.crash_report) {
3183 void ruby_set_crash_report(
const char *
template);
3184 ruby_set_crash_report(opt.crash_report);
3187 return (
void*)(
struct RData*)iseq;
3191fill_standard_fds(
void)
3193 int f0, f1, f2, fds[2];
3195 f0 = fstat(0, &buf) == -1 &&
errno == EBADF;
3196 f1 = fstat(1, &buf) == -1 &&
errno == EBADF;
3197 f2 = fstat(2, &buf) == -1 &&
errno == EBADF;
3199 if (pipe(fds) == 0) {
3208 if (pipe(fds) == 0) {
3210 if (f1 && fds[1] != 1)
3212 if (f2 && fds[1] != 2)
3214 if (fds[1] != 1 && fds[1] != 2)
3224 rb_w32_sysinit(argc, argv);
3226 if (*argc >= 0 && *argv) {
3227 origarg.argc = *argc;
3228 origarg.argv = *argv;
3230 fill_standard_fds();
3233#ifdef RUBY_ASAN_ENABLED
3234RUBY_SYMBOL_EXPORT_BEGIN
3235const char ruby_asan_default_options[] =
"use_sigaltstack=0:detect_leaks=0";
3236RUBY_SYMBOL_EXPORT_END
#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.