14#include "ruby/internal/config.h"
22# include <sys/cygwin.h>
25#if (defined(LOAD_RELATIVE) || defined(__MACH__)) && 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/parse.h"
57#include "internal/process.h"
58#include "internal/variable.h"
65#define singlebit_only_p(x) !((x) & ((x)-1))
66STATIC_ASSERT(Qnil_1bit_from_Qfalse, singlebit_only_p(
Qnil^
Qfalse));
67STATIC_ASSERT(Qundef_1bit_from_Qnil, singlebit_only_p(
Qundef^
Qnil));
70# define MAXPATHLEN 1024
73# define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
82#ifndef DISABLE_RUBYGEMS
83# define DISABLE_RUBYGEMS 0
86#define DEFAULT_RUBYGEMS_ENABLED "disabled"
88#define DEFAULT_RUBYGEMS_ENABLED "enabled"
91void rb_warning_category_update(
unsigned int mask,
unsigned int bits);
94#define FEATURE_BIT(bit) (1U << feature_##bit)
95#define EACH_FEATURES(X, SEP) \
106 X(frozen_string_literal) \
112#define EACH_DEBUG_FEATURES(X, SEP) \
113 X(frozen_string_literal) \
115#define AMBIGUOUS_FEATURE_NAMES 0
116#define DEFINE_FEATURE(bit) feature_##bit
117#define DEFINE_DEBUG_FEATURE(bit) feature_debug_##bit
118enum feature_flag_bits {
119 EACH_FEATURES(DEFINE_FEATURE, COMMA),
120 feature_debug_flag_first,
121#if defined(MJIT_FORCE_ENABLE) || !USE_YJIT
122 DEFINE_FEATURE(jit) = feature_mjit,
124 DEFINE_FEATURE(jit) = feature_yjit,
126 feature_jit_mask = FEATURE_BIT(mjit) | FEATURE_BIT(yjit),
128 feature_debug_flag_begin = feature_debug_flag_first - 1,
129 EACH_DEBUG_FEATURES(DEFINE_DEBUG_FEATURE, COMMA),
133#define MULTI_BITS_P(bits) ((bits) & ((bits) - 1))
135#define DEBUG_BIT(bit) (1U << feature_debug_##bit)
137#define DUMP_BIT(bit) (1U << dump_##bit)
138#define DEFINE_DUMP(bit) dump_##bit
139#define EACH_DUMPS(X, SEP) \
154 X(parsetree_with_comment) \
158 X(insns_without_opt) \
163 EACH_DUMPS(DEFINE_DUMP, COMMA),
164 dump_error_tolerant_bits = (DUMP_BIT(yydebug) |
165 DUMP_BIT(parsetree) |
166 DUMP_BIT(parsetree_with_comment)),
167 dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) |
168 DUMP_BIT(parsetree) | DUMP_BIT(parsetree_with_comment) |
169 DUMP_BIT(insns) | DUMP_BIT(insns_without_opt))
173rb_feature_set_to(
ruby_features_t *feat,
unsigned int bit_mask,
unsigned int bit_set)
175 feat->mask |= bit_mask;
176 feat->set = (feat->set & ~bit_mask) | bit_set;
179#define FEATURE_SET_TO(feat, bit_mask, bit_set) \
180 rb_feature_set_to(&(feat), bit_mask, bit_set)
181#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits)
182#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask)
183#define FEATURE_SET_P(feat, bits) ((feat).set & FEATURE_BIT(bits))
184#define FEATURE_USED_P(feat, bits) ((feat).mask & FEATURE_BIT(bits))
185#define FEATURE_SET_BITS(feat) ((feat).set & (feat).mask)
189#define src_encoding_index GET_VM()->src_encoding_index
192 COMPILATION_FEATURES = (
194 | FEATURE_BIT(frozen_string_literal)
195 | FEATURE_BIT(debug_frozen_string_literal)
198 (FEATURE_BIT(debug_flag_first)-1)
202 & ~FEATURE_BIT(frozen_string_literal)
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#ifdef MJIT_FORCE_ENABLE
217 opt->features.set |= FEATURE_BIT(mjit);
218#elif defined(YJIT_FORCE_ENABLE)
219 opt->features.set |= FEATURE_BIT(yjit);
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[] =
"";
242show_usage_line(
const char *str,
unsigned int namelen,
unsigned int secondlen,
int help,
int highlight,
unsigned int w)
244 const char *sb = highlight ? esc_bold : esc_none;
245 const char *se = highlight ? esc_reset : esc_none;
246 const int wrap = help && namelen + secondlen - 1 > w;
247 printf(
" %s%.*s%-*.*s%s%-*s%s\n", sb, namelen-1, str,
248 (wrap ? 0 : w - namelen + 1),
249 (help ? secondlen-1 : 0), str + namelen, se,
250 (wrap ? w + 3 : 0), (wrap ?
"\n" :
""),
251 str + namelen + secondlen);
255usage(
const char *name,
int help,
int highlight,
int columns)
260#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
263# define PLATFORM_JIT_OPTION "--yjit"
265# define PLATFORM_JIT_OPTION "--mjit (experimental)"
268 M(
"-0[octal]",
"",
"specify record separator (\\0, if no argument)"),
269 M(
"-a",
"",
"autosplit mode with -n or -p (splits $_ into $F)"),
270 M(
"-c",
"",
"check syntax only"),
271 M(
"-Cdirectory",
"",
"cd to directory before executing your script"),
272 M(
"-d",
", --debug",
"set debugging flags (set $DEBUG to true)"),
273 M(
"-e 'command'",
"",
"one line of script. Several -e's allowed. Omit [programfile]"),
274 M(
"-Eex[:in]",
", --encoding=ex[:in]",
"specify the default external and internal character encodings"),
275 M(
"-Fpattern",
"",
"split() pattern for autosplit (-a)"),
276 M(
"-i[extension]",
"",
"edit ARGV files in place (make backup if extension supplied)"),
277 M(
"-Idirectory",
"",
"specify $LOAD_PATH directory (may be used more than once)"),
278 M(
"-l",
"",
"enable line ending processing"),
279 M(
"-n",
"",
"assume 'while gets(); ... end' loop around your script"),
280 M(
"-p",
"",
"assume loop like -n but print line also like sed"),
281 M(
"-rlibrary",
"",
"require the library before executing your script"),
282 M(
"-s",
"",
"enable some switch parsing for switches after script name"),
283 M(
"-S",
"",
"look for the script using PATH environment variable"),
284 M(
"-v",
"",
"print the version number, then turn on verbose mode"),
285 M(
"-w",
"",
"turn warnings on for your script"),
286 M(
"-W[level=2|:category]",
"",
"set warning level; 0=silence, 1=medium, 2=verbose"),
287 M(
"-x[directory]",
"",
"strip off text before #!ruby line and perhaps cd to directory"),
288 M(
"--jit",
"",
"enable JIT for the platform, same as " PLATFORM_JIT_OPTION),
290 M(
"--mjit",
"",
"enable C compiler-based JIT compiler (experimental)"),
293 M(
"--yjit",
"",
"enable in-process JIT compiler"),
295 M(
"-h",
"",
"show this message, --help for more info"),
298 M(
"--copyright",
"",
"print the copyright"),
299 M(
"--dump={insns|parsetree|...}[,...]",
"",
300 "dump debug information. see below for available dump list"),
301 M(
"--enable={jit|rubyopt|...}[,...]",
", --disable={jit|rubyopt|...}[,...]",
302 "enable or disable features. see below for available features"),
303 M(
"--external-encoding=encoding",
", --internal-encoding=encoding",
304 "specify the default external or internal character encoding"),
305 M(
"--backtrace-limit=num",
"",
"limit the maximum length of backtrace"),
306 M(
"--verbose",
"",
"turn on verbose mode and disable script from stdin"),
307 M(
"--version",
"",
"print the version number, then exit"),
308 M(
"--help",
"",
"show this message, -h for short message"),
311 M(
"insns",
"",
"instruction sequences"),
312 M(
"insns_without_opt",
"",
"instruction sequences compiled with no optimization"),
313 M(
"yydebug(+error-tolerant)",
"",
"yydebug of yacc parser generator"),
314 M(
"parsetree(+error-tolerant)",
"",
"AST"),
315 M(
"parsetree_with_comment(+error-tolerant)",
"",
"AST with comments"),
318 M(
"gems",
"",
"rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED
")"),
319 M(
"error_highlight",
"",
"error_highlight (default: "DEFAULT_RUBYGEMS_ENABLED
")"),
320 M(
"did_you_mean",
"",
"did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED
")"),
321 M(
"syntax_suggest",
"",
"syntax_suggest (default: "DEFAULT_RUBYGEMS_ENABLED
")"),
322 M(
"rubyopt",
"",
"RUBYOPT environment variable (default: enabled)"),
323 M(
"frozen-string-literal",
"",
"freeze all string literals (default: disabled)"),
325 M(
"mjit",
"",
"C compiler-based JIT compiler (default: disabled)"),
328 M(
"yjit",
"",
"in-process JIT compiler (default: disabled)"),
332 M(
"deprecated",
"",
"deprecated features"),
333 M(
"experimental",
"",
"experimental features"),
340 M(
"--yjit-stats",
"",
"Enable collecting YJIT statistics"),
341 M(
"--yjit-exec-mem-size=num",
"",
"Size of executable memory block in MiB (default: 64)"),
342 M(
"--yjit-call-threshold=num",
"",
"Number of calls to trigger JIT (default: 10)"),
343 M(
"--yjit-max-versions=num",
"",
"Maximum number of versions per basic block (default: 4)"),
344 M(
"--yjit-greedy-versioning",
"",
"Greedy versioning mode (default: disabled)"),
348 const char *sb = highlight ? esc_standout+1 : esc_none;
349 const char *se = highlight ? esc_reset : esc_none;
350 const int num = numberof(usage_msg) - (help ? 1 : 0);
351 unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16;
352#define SHOW(m) show_usage_line((m).str, (m).namelen, (m).secondlen, help, highlight, w)
354 printf(
"%sUsage:%s %s [switches] [--] [programfile] [arguments]\n", sb, se, name);
355 for (i = 0; i < num; ++i)
360 if (highlight) sb = esc_standout;
362 for (i = 0; i < numberof(help_msg); ++i)
364 printf(
"%s""Dump List:%s\n", sb, se);
365 for (i = 0; i < numberof(dumps); ++i)
367 printf(
"%s""Features:%s\n", sb, se);
368 for (i = 0; i < numberof(features); ++i)
370 printf(
"%s""Warning categories:%s\n", sb, se);
371 for (i = 0; i < numberof(warn_categories); ++i)
372 SHOW(warn_categories[i]);
374 printf(
"%s""MJIT options (experimental):%s\n", sb, se);
375 for (i = 0; mjit_option_messages[i].str; ++i)
376 SHOW(mjit_option_messages[i]);
379 printf(
"%s""YJIT options:%s\n", sb, se);
380 for (i = 0; i < numberof(yjit_options); ++i)
381 SHOW(yjit_options[i]);
385#define rubylib_path_new rb_str_new
388push_include(
const char *path,
VALUE (*filter)(
VALUE))
392 VALUE load_path = GET_VM()->load_path;
399 for (s = p; *s && *s != sep; s = CharNext(s));
400 rb_ary_push(load_path, (*filter)(rubylib_path_new(p, s - p)));
407push_include_cygwin(
const char *path,
VALUE (*filter)(
VALUE))
410 char rubylib[FILENAME_MAX];
419 for (s = p; *s && *s !=
';'; s = CharNext(s));
424 p = RSTRING_PTR(buf);
428 p = strncpy(RSTRING_PTR(buf), p, len);
431#ifdef HAVE_CYGWIN_CONV_PATH
432#define CONV_TO_POSIX_PATH(p, lib) \
433 cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
435# error no cygwin_conv_path
437 if (CONV_TO_POSIX_PATH(p, rubylib) == 0)
439 push_include(p, filter);
445#define push_include push_include_cygwin
449ruby_push_include(
const char *path,
VALUE (*filter)(
VALUE))
453 push_include(path, filter);
457identical_path(
VALUE path)
462locale_path(
VALUE path)
464 rb_enc_associate(path, rb_locale_encoding());
471 ruby_push_include(path, locale_path);
475expand_include_path(
VALUE path)
477 char *p = RSTRING_PTR(path);
480 if (*p ==
'.' && p[1] ==
'/')
482 return rb_file_expand_path(path,
Qnil);
486ruby_incpush_expand(
const char *path)
488 ruby_push_include(path, expand_include_path);
492#if defined _WIN32 || defined __CYGWIN__
493static HMODULE libruby;
496DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
498 if (reason == DLL_PROCESS_ATTACH)
504rb_libruby_handle(
void)
510translit_char_bin(
char *p,
int from,
int to)
513 if ((
unsigned char)*p == from)
528# define IF_UTF8_PATH(t, f) t
530# define IF_UTF8_PATH(t, f) f
542# define str_conv_enc(str, from, to) (str)
547#if defined(LOAD_RELATIVE) || defined(__MACH__)
549runtime_libruby_path(
void)
551#if defined _WIN32 || defined __CYGWIN__
563 while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath),
564 ret = GetModuleFileNameW(libruby, wlibpath, len),
570 if (!ret || ret > len)
rb_fatal(
"failed to get module file name");
571#if defined __CYGWIN__
573 const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
574 size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0);
575 if (!newsize)
rb_fatal(
"failed to convert module path to cygwin");
577 libpath = RSTRING_PTR(path);
578 if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) {
585 for (len = ret, i = 0; i < len; ++i) {
586 if (wlibpath[i] == L
'\\') {
592 len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL);
594 libpath = RSTRING_PTR(path);
595 WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath, len, NULL, NULL);
599#elif defined(HAVE_DLADDR)
602 const void* addr = (
void *)(
VALUE)expand_include_path;
604 if (!dladdr((
void *)addr, &dli)) {
608 else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
610 path = rb_readlink(fname, NULL);
615 path = rb_realpath_internal(
Qnil, fname, 1);
620# error relative load path is not supported on this platform.
625#define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index")
627VALUE ruby_archlibdir_path, ruby_prefix_path;
630VALUE rb_libruby_selfpath;
636 VALUE load_path, archlibdir = 0;
637 ID id_initial_load_path_mark;
638 const char *paths = ruby_initial_load_paths;
639#if defined(LOAD_RELATIVE) || defined(__MACH__)
640 VALUE libruby_path = runtime_libruby_path();
641# if defined(__MACH__)
642 VALUE selfpath = libruby_path;
643# if defined(LOAD_RELATIVE)
648 rb_libruby_selfpath = selfpath;
649 rb_gc_register_address(&rb_libruby_selfpath);
653#if defined LOAD_RELATIVE
654#if !defined ENABLE_MULTIARCH
655# define RUBY_ARCH_PATH ""
656#elif defined RUBY_ARCH
657# define RUBY_ARCH_PATH "/"RUBY_ARCH
659# define RUBY_ARCH_PATH "/"RUBY_PLATFORM
666 sopath = libruby_path;
667 libpath = RSTRING_PTR(sopath);
669 p = strrchr(libpath,
'/');
671 static const char libdir[] =
"/"
672#ifdef LIBDIR_BASENAME
678 const ptrdiff_t libdir_len = (ptrdiff_t)
sizeof(libdir)
680 static const char bindir[] =
"/bin";
681 const ptrdiff_t bindir_len = (ptrdiff_t)
sizeof(bindir) - 1;
683 const char *p2 = NULL;
685#ifdef ENABLE_MULTIARCH
688 if (p - libpath >= bindir_len && !
STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
694 else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
695 archlibdir =
rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath);
699#ifdef ENABLE_MULTIARCH
705 p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
706 if (p)
goto multiarch;
710 baselen = p - libpath;
716 libpath = RSTRING_PTR(sopath);
717#define PREFIX_PATH() sopath
718#define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
719#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
721 const size_t exec_prefix_len = strlen(ruby_exec_prefix);
722#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len))
723#define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
725 rb_gc_register_address(&ruby_prefix_path);
726 ruby_prefix_path = PREFIX_PATH();
728 if (!archlibdir) archlibdir = ruby_prefix_path;
729 rb_gc_register_address(&ruby_archlibdir_path);
730 ruby_archlibdir_path = archlibdir;
732 load_path = GET_VM()->load_path;
734 ruby_push_include(getenv(
"RUBYLIB"), identical_path);
736 id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
738 size_t len = strlen(paths);
739 VALUE path = RUBY_RELATIVE(paths, len);
740 rb_ivar_set(path, id_initial_load_path_mark, path);
741 rb_ary_push(load_path, path);
745 rb_const_set(rb_cObject, rb_intern_const(
"TMP_RUBY_PREFIX"), ruby_prefix_path);
750add_modules(
VALUE *req_list,
const char *mod)
752 VALUE list = *req_list;
756 *req_list = list = rb_ary_hidden_new(0);
759 rb_ary_push(list, feature);
763require_libraries(
VALUE *req_list)
765 VALUE list = *req_list;
766 VALUE self = rb_vm_top_self();
768 rb_encoding *extenc = rb_default_external_encoding();
772 VALUE feature = rb_ary_shift(list);
773 rb_enc_associate(feature, extenc);
776 rb_funcallv(self, require, 1, &feature);
788process_sflag(
int *sflag)
806 if (s[1] ==
'-' && s[2] ==
'\0')
811 for (p = s + 1; *p; p++) {
820 else if (*p !=
'_' && !
ISALNUM(*p)) {
823 rb_str_new2(
"invalid name for global variable - ");
824 if (!(p = strchr(p,
'='))) {
830 name_error[1] = args[-1];
836 for (p = s + 1; *p; ++p) {
859 VALUE argstr, argary;
867 p = RSTRING_PTR(argstr);
868 if (envopt) *p++ =
' ';
869 memcpy(p, s, len + 1);
875 while (*p && !
ISSPACE(*p)) ++p;
880 argc = RSTRING_LEN(argary) /
sizeof(ap);
883 argv = ptr =
ALLOC_N(
char *, argc);
884 MEMMOVE(argv, RSTRING_PTR(argary),
char *, argc);
886 while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) {
904name_match_p(
const char *name,
const char *str,
size_t len)
906 if (len == 0)
return 0;
908 while (
TOLOWER(*str) == *name) {
909 if (!--len)
return 1;
913 if (*str !=
'-' && *str !=
'_')
return 0;
915 if (*name !=
'-' && *name !=
'_')
return 0;
918 if (--len == 0)
return 1;
922#define NAME_MATCH_P(name, str, len) \
923 ((len) < (int)sizeof(name) && name_match_p((name), (str), (len)))
925#define UNSET_WHEN(name, bit, str, len) \
926 if (NAME_MATCH_P((name), (str), (len))) { \
927 *(unsigned int *)arg &= ~(bit); \
931#define SET_WHEN(name, bit, str, len) \
932 if (NAME_MATCH_P((name), (str), (len))) { \
933 *(unsigned int *)arg |= (bit); \
937#define LITERAL_NAME_ELEMENT(name) #name
940feature_option(
const char *str,
int len,
void *arg,
const unsigned int enable)
942 static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT,
", ");
944 unsigned int mask = ~0
U;
945 unsigned int set = 0
U;
946#if AMBIGUOUS_FEATURE_NAMES
948# define FEATURE_FOUND ++matched
950# define FEATURE_FOUND goto found
952#define SET_FEATURE(bit) \
953 if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;}
954 EACH_FEATURES(SET_FEATURE, ;);
955 if (NAME_MATCH_P(
"jit", str, len)) {
956 set |= mask = FEATURE_BIT(jit);
959 if (NAME_MATCH_P(
"all", str, len)) {
961 mask &= ~feature_jit_mask | FEATURE_BIT(jit);
964#if AMBIGUOUS_FEATURE_NAMES
965 if (matched == 1)
goto found;
968#define ADD_FEATURE_NAME(bit) \
969 if (FEATURE_BIT(bit) & set) { \
970 rb_str_cat_cstr(mesg, #bit); \
971 if (--matched) rb_str_cat_cstr(mesg, ", "); \
973 EACH_FEATURES(ADD_FEATURE_NAME, ;);
976#undef ADD_FEATURE_NAME
981 rb_warn(
"unknown argument for --%s: `%.*s'",
982 enable ?
"enable" :
"disable", len, str);
983 rb_warn(
"features are [%.*s].", (
int)strlen(list), list);
987 FEATURE_SET_TO(*argp, mask, (mask & enable));
992enable_option(
const char *str,
int len,
void *arg)
994 feature_option(str, len, arg, ~0
U);
998disable_option(
const char *str,
int len,
void *arg)
1000 feature_option(str, len, arg, 0
U);
1004int ruby_env_debug_option(
const char *str,
int len,
void *arg);
1007debug_option(
const char *str,
int len,
void *arg)
1009 static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1011#define SET_WHEN_DEBUG(bit) \
1012 if (NAME_MATCH_P(#bit, str, len)) { \
1013 FEATURE_SET(*argp, DEBUG_BIT(bit)); \
1016 EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;);
1018 if (
ruby_patchlevel < 0 && ruby_env_debug_option(str, len, 0))
return;
1020 rb_warn(
"unknown argument for --debug: `%.*s'", len, str);
1021 rb_warn(
"debug features are [%.*s].", (
int)strlen(list), list);
1025memtermspn(
const char *str,
char term,
int len)
1028 if (len <= 0)
return 0;
1029 const char *next = memchr(str, term, len);
1030 return next ? (int)(next - str) : len;
1033static const char additional_opt_sep =
'+';
1036dump_additional_option(
const char *str,
int len,
unsigned int bits,
const char *name)
1039 for (; len-- > 0 && *str++ == additional_opt_sep; len -= w, str += w) {
1040 w = memtermspn(str, additional_opt_sep, len);
1041#define SET_ADDITIONAL(bit) if (NAME_MATCH_P(#bit, str, w)) { \
1042 if (bits & DUMP_BIT(bit)) \
1043 rb_warn("duplicate option to dump %s: `%.*s'", name, w, str); \
1044 bits |= DUMP_BIT(bit); \
1047 if (dump_error_tolerant_bits & bits) {
1048 SET_ADDITIONAL(error_tolerant);
1050 rb_warn(
"don't know how to dump %s with `%.*s'", name, w, str);
1056dump_option(
const char *str,
int len,
void *arg)
1058 static const char list[] = EACH_DUMPS(LITERAL_NAME_ELEMENT,
", ");
1059 int w = memtermspn(str, additional_opt_sep, len);
1061#define SET_WHEN_DUMP(bit) \
1062 if (NAME_MATCH_P(#bit, (str), (w))) { \
1063 *(unsigned int *)arg |= \
1064 dump_additional_option(str + w, len - w, DUMP_BIT(bit), #bit); \
1067 EACH_DUMPS(SET_WHEN_DUMP, ;);
1068 rb_warn(
"don't know how to dump `%.*s',", len, str);
1069 rb_warn(
"but only [%.*s].", (
int)strlen(list), list);
1073set_option_encoding_once(
const char *
type,
VALUE *name,
const char *e,
long elen)
1077 if (!elen) elen = strlen(e);
1081 rb_funcall(ename, rb_intern(
"casecmp"), 1, *name) !=
INT2FIX(0)) {
1083 "%s already set to %"PRIsVALUE,
type, *name);
1088#define set_internal_encoding_once(opt, e, elen) \
1089 set_option_encoding_once("default_internal", &(opt)->intern.enc.name, (e), (elen))
1090#define set_external_encoding_once(opt, e, elen) \
1091 set_option_encoding_once("default_external", &(opt)->ext.enc.name, (e), (elen))
1092#define set_source_encoding_once(opt, e, elen) \
1093 set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen))
1095#define yjit_opt_match_noarg(s, l, name) \
1096 opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --yjit-" name " is ignored"), 1) : 1)
1097#define yjit_opt_match_arg(s, l, name) \
1098 opt_match(s, l, name) && (*(s) && *(s+1) ? 1 : (rb_raise(rb_eRuntimeError, "--yjit-" name " needs an argument"), 0))
1102setup_yjit_options(
const char *s)
1105 bool rb_yjit_parse_option(
const char* s);
1106 bool success = rb_yjit_parse_option(s);
1114 "invalid YJIT option `%s' (--help will show valid yjit options)",
1123 long n, argc0 = argc;
1125 int warning = opt->warning;
1127 if (argc <= 0 || !argv)
1130 for (argc--, argv++; argc > 0; argc--, argv++) {
1131 const char *
const arg = argv[0];
1132 if (!arg || arg[0] !=
'-' || !arg[1])
1139 if (envopt)
goto noenvopt;
1140 opt->do_split = TRUE;
1145 if (envopt)
goto noenvopt;
1146 opt->do_print = TRUE;
1149 if (envopt)
goto noenvopt;
1150 opt->do_loop = TRUE;
1161 if (envopt)
goto noenvopt;
1162 opt->dump |= DUMP_BIT(yydebug);
1171 opt->dump |= DUMP_BIT(version_v);
1174 if (!opt->warning) {
1178 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS);
1184 unsigned int bits = 0;
1185 static const char no_prefix[] =
"no-";
1186 int enable = strncmp(s += 2, no_prefix,
sizeof(no_prefix)-1) != 0;
1187 if (!enable) s +=
sizeof(no_prefix)-1;
1188 size_t len = strlen(s);
1189 if (NAME_MATCH_P(
"deprecated", s, len)) {
1192 else if (NAME_MATCH_P(
"experimental", s, len)) {
1196 rb_warn(
"unknown warning category: `%s'", s);
1198 if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0);
1211 if (!opt->warning) {
1227 FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_ALL_BITS, 0);
1233 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS);
1240 if (envopt)
goto noenvopt;
1241 opt->dump |= DUMP_BIT(syntax);
1246 if (envopt)
goto noenvopt;
1248 if (!opt->sflag) opt->sflag = 1;
1253 if (envopt)
goto noenvopt;
1254 opt->dump |= DUMP_BIT(usage);
1258 if (envopt)
goto noenvopt;
1259 opt->do_line = TRUE;
1265 if (envopt)
goto noenvopt;
1267 opt->do_search = TRUE;
1272 if (envopt)
goto noenvopt;
1279 if (!opt->e_script) {
1281 if (opt->script == 0)
1291 add_modules(&opt->req_list, s);
1293 else if (argc > 1) {
1294 add_modules(&opt->req_list, argv[1]);
1300 if (envopt)
goto noenvopt;
1302 ruby_set_inplace_mode(s + 1);
1306 if (envopt)
goto noenvopt;
1310 if (*s && chdir(s) < 0) {
1317 if (envopt)
goto noenvopt;
1318 if (!*++s && (!--argc || !(s = *++argv) || !*s)) {
1327 if (envopt)
goto noenvopt;
1334 if (!*++s && (!--argc || !(s = *++argv))) {
1340 set_internal_encoding_once(opt,
"UTF-8", 0);
1346 const char *enc_name = 0;
1349 enc_name =
"EUC-JP";
1352 enc_name =
"Windows-31J";
1357 case 'N':
case 'n':
case 'A':
case 'a':
1358 enc_name =
"ASCII-8BIT";
1363 if (!opt->ext.enc.name)
1364 opt->ext.enc.name = opt->src.enc.name;
1373 ruby_incpush_expand(s);
1374 else if (argc > 1) {
1375 ruby_incpush_expand(argv[1]);
1381 if (envopt)
goto noenvopt;
1391 else if (v == 0 && numlen >= 2) {
1402 if (!s[1] || (s[1] ==
'\r' && !s[2])) {
1408# define is_option_end(c, allow_hyphen) \
1409 (!(c) || ((allow_hyphen) && (c) == '-') || (c) == '=')
1410# define check_envopt(name, allow_envopt) \
1411 (((allow_envopt) || !envopt) ? (void)0 : \
1412 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
1413# define need_argument(name, s, needs_arg, next_arg) \
1414 ((*(s) ? !*++(s) : (next_arg) && (!argc || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
1415 rb_raise(rb_eRuntimeError, "missing argument for --" name) \
1417# define is_option_with_arg(name, allow_hyphen, allow_envopt) \
1418 is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue, Qtrue)
1419# define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg, next_arg) \
1420 (strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) && \
1421 (s[n] != '-' || s[n+1]) ? \
1422 (check_envopt(name, (allow_envopt)), s += n, \
1423 need_argument(name, s, needs_arg, next_arg), 1) : 0)
1425 if (strcmp(
"copyright", s) == 0) {
1426 if (envopt)
goto noenvopt_long;
1427 opt->dump |= DUMP_BIT(copyright);
1438 else if (is_option_with_arg(
"enable",
Qtrue,
Qtrue)) {
1441 else if (is_option_with_arg(
"disable",
Qtrue,
Qtrue)) {
1444 else if (is_option_with_arg(
"encoding",
Qfalse,
Qtrue)) {
1448# define set_encoding_part(type) \
1449 if (!(p = strchr(s, ':'))) { \
1450 set_##type##_encoding_once(opt, s, 0); \
1454 set_##type##_encoding_once(opt, s, p-s); \
1456 set_encoding_part(external);
1457 if (!*(s = ++p))
break;
1458 set_encoding_part(internal);
1459 if (!*(s = ++p))
break;
1460#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1461 set_encoding_part(source);
1462 if (!*(s = ++p))
break;
1465 (arg[1] ==
'-' ?
"--encoding" :
"-E"), s);
1466# undef set_encoding_part
1469 else if (is_option_with_arg(
"internal-encoding",
Qfalse,
Qtrue)) {
1470 set_internal_encoding_once(opt, s, 0);
1472 else if (is_option_with_arg(
"external-encoding",
Qfalse,
Qtrue)) {
1473 set_external_encoding_once(opt, s, 0);
1475#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1476 else if (is_option_with_arg(
"source-encoding",
Qfalse,
Qtrue)) {
1477 set_source_encoding_once(opt, s, 0);
1480 else if (strcmp(
"version", s) == 0) {
1481 if (envopt)
goto noenvopt_long;
1482 opt->dump |= DUMP_BIT(version);
1484 else if (strcmp(
"verbose", s) == 0) {
1488 else if (strcmp(
"jit", s) == 0) {
1490 rb_warn(
"Ruby was built without JIT support");
1492 FEATURE_SET(opt->features, FEATURE_BIT(jit));
1495 else if (is_option_with_optarg(
"mjit",
'-',
true,
false,
false)) {
1497 extern void mjit_setup_options(
const char *s,
struct mjit_options *mjit_opt);
1498 FEATURE_SET(opt->features, FEATURE_BIT(mjit));
1499 mjit_setup_options(s, &opt->mjit);
1501 rb_warn(
"MJIT support is disabled.");
1504 else if (is_option_with_optarg(
"yjit",
'-',
true,
false,
false)) {
1506 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
1507 setup_yjit_options(s);
1509 rb_warn(
"Ruby was built without YJIT support."
1510 " You may need to install rustc to build Ruby with YJIT.");
1513 else if (strcmp(
"yydebug", s) == 0) {
1514 if (envopt)
goto noenvopt_long;
1515 opt->dump |= DUMP_BIT(yydebug);
1520 else if (strcmp(
"help", s) == 0) {
1521 if (envopt)
goto noenvopt_long;
1522 opt->dump |= DUMP_BIT(help);
1525 else if (is_option_with_arg(
"backtrace-limit",
Qfalse,
Qfalse)) {
1527 long n = strtol(s, &e, 10);
1529 rb_backtrace_length_limit = n;
1533 "invalid option --%s (-h will show valid options)", s);
1544 "invalid option -%c (-h will show valid options)",
1545 (
int)(
unsigned char)*s);
1560# undef is_option_end
1562# undef need_argument
1563# undef is_option_with_arg
1564# undef is_option_with_optarg
1569 if (warning) opt->warning = warning;
1570 return argc0 - argc;
1573void Init_builtin_features(
void);
1576ruby_init_prelude(
void)
1578 Init_builtin_features();
1582void rb_call_builtin_inits(
void);
1586#if RBIMPL_HAS_ATTRIBUTE(weak)
1590Init_extra_exts(
void)
1597 if (opt->dump & dump_exit_bits)
return;
1599 if (FEATURE_SET_P(opt->features, gems)) {
1601 if (opt->features.set & FEATURE_BIT(error_highlight)) {
1604 if (opt->features.set & FEATURE_BIT(did_you_mean)) {
1607 if (opt->features.set & FEATURE_BIT(syntax_suggest)) {
1612 rb_warning_category_update(opt->warn.mask, opt->warn.set);
1617 mjit_enabled =
true;
1622 rb_call_builtin_inits();
1623 ruby_init_prelude();
1629 mjit_init(&opt->mjit);
1641 require_libraries(&opt->req_list);
1645opt_enc_index(
VALUE enc_name)
1647 const char *s = RSTRING_PTR(enc_name);
1648 int i = rb_enc_find_index(s);
1653 else if (rb_enc_dummy_p(rb_enc_from_index(i))) {
1659#define rb_progname (GET_VM()->progname)
1660#define rb_orig_progname (GET_VM()->orig_progname)
1676#define rb_define_readonly_boolean(name, val) \
1677 rb_define_virtual_variable((name), (val) ? true_value : false_value, 0)
1768setup_pager_env(
void)
1770 if (!getenv(
"LESS")) ruby_setenv(
"LESS",
"-R");
1777 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
1779 if (!GetConsoleMode(h, &m))
return 0;
1780# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
1781# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
1783 if (!(m & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;
1786#elif !defined(HAVE_WORKING_FORK)
1787# define tty_enabled() 0
1796 return rb_enc_associate(
rb_str_dup(str), enc);
1814 char fbuf[MAXPATHLEN];
1815 int i = (int)proc_options(argc, argv, opt, 0);
1816 unsigned int dump = opt->dump & dump_exit_bits;
1818 const long loaded_before_enc =
RARRAY_LEN(vm->loaded_features);
1820 if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
1821 int tty = isatty(1);
1822 const char *
const progname =
1823 (argc > 0 && argv && argv[0] ? argv[0] :
1824 origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] :
1827 if ((opt->dump & DUMP_BIT(help)) && tty) {
1828 const char *pager_env = getenv(
"RUBY_PAGER");
1829 if (!pager_env) pager_env = getenv(
"PAGER");
1830 if (pager_env && *pager_env && isatty(0)) {
1831 const char *columns_env = getenv(
"COLUMNS");
1832 if (columns_env) columns = atoi(columns_env);
1834#ifdef HAVE_WORKING_FORK
1837 rb_pid_t pid = rb_fork();
1842 else if (pid == 0) {
1860 int oldout = dup(1);
1861 int olderr = dup(2);
1862 int fd =
RFILE(port)->fptr->fd;
1863 tty = tty_enabled();
1866 usage(progname, 1, tty, columns);
1876 usage(progname, (opt->dump & DUMP_BIT(help)), tty, columns);
1883 if (FEATURE_SET_P(opt->features, rubyopt) && (s = getenv(
"RUBYOPT"))) {
1884 VALUE src_enc_name = opt->src.enc.name;
1885 VALUE ext_enc_name = opt->ext.enc.name;
1886 VALUE int_enc_name = opt->intern.enc.name;
1890 opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
1891 moreswitches(s, opt, 1);
1893 opt->src.enc.name = src_enc_name;
1895 opt->ext.enc.name = ext_enc_name;
1897 opt->intern.enc.name = int_enc_name;
1898 FEATURE_SET_RESTORE(opt->features, feat);
1899 FEATURE_SET_RESTORE(opt->warn, warn);
1902 if (opt->src.enc.name)
1906 rb_warning(
"-K is specified; it is for 1.8 compatibility and may cause odd behavior");
1908 if (!(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
1910 if (!FEATURE_USED_P(opt->features, yjit) && getenv(
"RUBY_YJIT_ENABLE")) {
1911 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
1915 if (MULTI_BITS_P(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
1916 rb_warn(
"MJIT and YJIT cannot both be enabled at the same time. Exiting");
1921 if (FEATURE_SET_P(opt->features, mjit)) {
1922 opt->mjit.on =
true;
1926 if (FEATURE_SET_P(opt->features, yjit)) {
1930 Init_ruby_description(opt);
1931 if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
1933 if (opt->dump & DUMP_BIT(version))
return Qtrue;
1935 if (opt->dump & DUMP_BIT(copyright)) {
1940 if (!opt->e_script) {
1947 opt->script = argv[0];
1948 if (!opt->script || opt->script[0] ==
'\0') {
1951 else if (opt->do_search) {
1952 const char *path = getenv(
"RUBYPATH");
1956 opt->script = dln_find_file_r(argv[0], path, fbuf,
sizeof(fbuf));
1959 opt->script = dln_find_file_r(argv[0], getenv(
PATH_ENV), fbuf,
sizeof(fbuf));
1962 opt->script = argv[0];
1967 if (opt->script[0] ==
'-' && !opt->script[1]) {
1968 forbid_setid(
"program input from stdin");
1973 opt->script = RSTRING_PTR(opt->script_name);
1976 translit_char_bin(RSTRING_PTR(opt->script_name),
'\\',
'/');
1978 translit_char(RSTRING_PTR(opt->script_name),
'\\',
'/');
1981 ruby_gc_set_params();
1985 lenc = rb_locale_encoding();
1986 rb_enc_associate(rb_progname, lenc);
1988 parser = rb_parser_new();
1989 if (opt->dump & DUMP_BIT(yydebug)) {
1990 rb_parser_set_yydebug(parser,
Qtrue);
1992 if (opt->dump & DUMP_BIT(error_tolerant)) {
1993 rb_parser_error_tolerant(parser);
1995 if (opt->ext.enc.name != 0) {
1996 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
1998 if (opt->intern.enc.name != 0) {
1999 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2001 if (opt->src.enc.name != 0) {
2002 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2003 src_encoding_index = opt->src.enc.index;
2005 if (opt->ext.enc.index >= 0) {
2006 enc = rb_enc_from_index(opt->ext.enc.index);
2009 enc = IF_UTF8_PATH(uenc, lenc);
2011 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2012 if (opt->intern.enc.index >= 0) {
2013 enc = rb_enc_from_index(opt->intern.enc.index);
2014 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2015 opt->intern.enc.index = -1;
2020 script_name = opt->script_name;
2021 rb_enc_associate(opt->script_name, IF_UTF8_PATH(uenc, lenc));
2024 opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
2025 opt->script = RSTRING_PTR(opt->script_name);
2029 if (IF_UTF8_PATH(uenc != lenc, 1)) {
2031 VALUE load_path = vm->load_path;
2032 const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
2033 int modifiable = FALSE;
2035 rb_get_expanded_load_path();
2036 for (i = 0; i <
RARRAY_LEN(load_path); ++i) {
2038 int mark =
rb_attr_get(path, id_initial_load_path_mark) == path;
2041 if (newpath == path)
continue;
2044 if (!(path = copy_str(path, lenc, !mark)))
continue;
2046 if (mark)
rb_ivar_set(path, id_initial_load_path_mark, path);
2048 rb_ary_modify(load_path);
2051 RARRAY_ASET(load_path, i, path);
2054 rb_ary_replace(vm->load_path_snapshot, load_path);
2058 VALUE loaded_features = vm->loaded_features;
2059 bool modified =
false;
2060 for (
long i = loaded_before_enc; i <
RARRAY_LEN(loaded_features); ++i) {
2062 if (!(path = copy_str(path, IF_UTF8_PATH(uenc, lenc),
true)))
continue;
2064 rb_ary_modify(loaded_features);
2067 RARRAY_ASET(loaded_features, i, path);
2070 rb_ary_replace(vm->loaded_features_snapshot, loaded_features);
2074 if (opt->features.mask & COMPILATION_FEATURES) {
2075 VALUE option = rb_hash_new();
2076#define SET_COMPILE_OPTION(h, o, name) \
2077 rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \
2078 RBOOL(FEATURE_SET_P(o->features, name)))
2079 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
2080 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
2081 rb_funcallv(rb_cISeq, rb_intern_const(
"compile_option="), 1, &option);
2082#undef SET_COMPILE_OPTION
2085 process_sflag(&opt->sflag);
2087 if (opt->e_script) {
2088 VALUE progname = rb_progname;
2090 rb_parser_set_context(parser, 0, TRUE);
2092 if (opt->src.enc.index >= 0) {
2093 eenc = rb_enc_from_index(opt->src.enc.index);
2098 if (ienc) eenc = ienc;
2103 opt->e_script = str_conv_enc(opt->e_script, uenc, eenc);
2106 rb_enc_associate(opt->e_script, eenc);
2109 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2110 opt->do_line, opt->do_split);
2111 ast = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
2115 f = open_load_file(script_name, &opt->xflag);
2116 rb_parser_set_context(parser, 0, f ==
rb_stdin);
2117 ast = load_file(parser, opt->script_name, f, 1, opt);
2120 if (dump & DUMP_BIT(yydebug)) {
2121 dump &= ~DUMP_BIT(yydebug);
2122 if (!dump)
return Qtrue;
2125 if (opt->ext.enc.index >= 0) {
2126 enc = rb_enc_from_index(opt->ext.enc.index);
2129 enc = IF_UTF8_PATH(uenc, lenc);
2131 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2132 if (opt->intern.enc.index >= 0) {
2134 enc = rb_enc_from_index(opt->intern.enc.index);
2135 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2137 else if (!rb_default_internal_encoding())
2139 rb_enc_set_default_internal(
Qnil);
2140 rb_stdio_set_default_encoding();
2142 if (!ast->body.root) {
2143 rb_ast_dispose(ast);
2147 process_sflag(&opt->sflag);
2150 if (dump & DUMP_BIT(syntax)) {
2151 printf(
"Syntax OK\n");
2152 dump &= ~DUMP_BIT(syntax);
2153 if (!dump)
return Qtrue;
2163 if (dump & (DUMP_BIT(parsetree)|DUMP_BIT(parsetree_with_comment))) {
2164 rb_io_write(
rb_stdout, rb_parser_dump_tree(ast->body.root, dump & DUMP_BIT(parsetree_with_comment)));
2166 dump &= ~DUMP_BIT(parsetree)&~DUMP_BIT(parsetree_with_comment);
2168 rb_ast_dispose(ast);
2175 if (!opt->e_script && strcmp(opt->script,
"-")) {
2176 path = rb_realpath_internal(
Qnil, script_name, 1);
2179 path = str_conv_enc(path, uenc, lenc);
2183 rb_enc_copy(path, opt->script_name);
2190 const struct rb_block *base_block = toplevel_context(toplevel_binding);
2191 iseq = rb_iseq_new_main(&ast->body, opt->script_name, path, vm_block_iseq(base_block), !(dump & DUMP_BIT(insns_without_opt)));
2192 rb_ast_dispose(ast);
2195 if (dump & (DUMP_BIT(insns) | DUMP_BIT(insns_without_opt))) {
2198 dump &= ~DUMP_BIT(insns);
2199 if (!dump)
return Qtrue;
2201 if (opt->dump & dump_exit_bits)
return Qtrue;
2203 rb_define_readonly_boolean(
"$-p", opt->do_print);
2204 rb_define_readonly_boolean(
"$-l", opt->do_line);
2205 rb_define_readonly_boolean(
"$-a", opt->do_split);
2207 rb_gvar_ractor_local(
"$-p");
2208 rb_gvar_ractor_local(
"$-l");
2209 rb_gvar_ractor_local(
"$-a");
2211 if ((rb_e_script = opt->e_script) != 0) {
2213 rb_gc_register_mark_object(opt->e_script);
2219 if (opt->e_script) {
2221 rb_exec_event_hook_script_compiled(ec, iseq, opt->e_script);
2225 rb_exec_event_hook_script_compiled(ec, iseq,
Qnil);
2233warn_cr_in_shebang(
const char *str,
long len)
2235 if (str[len-1] ==
'\n' && str[len-2] ==
'\r') {
2236 rb_warn(
"shebang line ending with \\r may cause problems");
2240#define warn_cr_in_shebang(str, len) (void)0
2243void rb_reset_argf_lineno(
long n);
2254load_file_internal(
VALUE argp_v)
2257 VALUE parser = argp->parser;
2258 VALUE orig_fname = argp->fname;
2259 int script = argp->script;
2267 CONST_ID(set_encoding,
"set_encoding");
2273 int no_src_enc = !opt->src.enc.name;
2274 int no_ext_enc = !opt->ext.enc.name;
2275 int no_int_enc = !opt->intern.enc.name;
2277 enc = rb_ascii8bit_encoding();
2278 rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
2286 if (len > 2 && str[0] ==
'#' && str[1] ==
'!') {
2287 if (line_start == 1) warn_cr_in_shebang(str, len);
2301 warn_cr_in_shebang(str, len);
2304 goto search_shebang;
2309 if (*str ==
'\n') *str-- =
'\0';
2310 if (*str ==
'\r') *str-- =
'\0';
2312 if ((p = strstr(p,
" -")) != 0) {
2314 moreswitches(p + 1, opt, 0);
2320 else if (!
NIL_P(c)) {
2324 if (no_src_enc && opt->src.enc.name) {
2325 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2326 src_encoding_index = opt->src.enc.index;
2328 if (no_ext_enc && opt->ext.enc.name) {
2329 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2331 if (no_int_enc && opt->intern.enc.name) {
2332 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2335 else if (!
NIL_P(c)) {
2341 rb_reset_argf_lineno(0);
2344 if (opt->src.enc.index >= 0) {
2345 enc = rb_enc_from_index(opt->src.enc.index);
2348 enc = rb_locale_encoding();
2351 enc = rb_utf8_encoding();
2353 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2354 opt->do_line, opt->do_split);
2357 rb_enc_associate(f, enc);
2358 return (
VALUE)rb_parser_compile_string_path(parser, orig_fname, f, line_start);
2360 rb_funcall(f, set_encoding, 2, rb_enc_from_encoding(enc),
rb_str_new_cstr(
"-"));
2361 ast = rb_parser_compile_file_path(parser, orig_fname, f, line_start);
2362 rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser));
2363 if (script && rb_parser_end_seen_p(parser)) {
2384disable_nonblock(
int fd)
2386#if defined(HAVE_FCNTL) && defined(F_SETFL)
2387 if (fcntl(fd, F_SETFL, 0) < 0) {
2388 const int e = errno;
2391 if (e == ENOTSUP)
return 0;
2393# if defined B_UNSUPPORTED
2394 if (e == B_UNSUPPORTED)
return 0;
2403open_load_file(
VALUE fname_v,
int *xflag)
2405 const char *fname = (fname_v = rb_str_encode_ospath(fname_v),
2407 long flen = RSTRING_LEN(fname_v);
2411 if (flen == 1 && fname[0] ==
'-') {
2418 const int MODE_TO_LOAD = O_RDONLY | (
2419#if defined O_NONBLOCK && HAVE_FCNTL
2421 !(O_NONBLOCK & O_ACCMODE) ? O_NONBLOCK :
2423#if defined O_NDELAY && HAVE_FCNTL
2424 !(O_NDELAY & O_ACCMODE) ? O_NDELAY :
2427 int mode = MODE_TO_LOAD;
2428#if defined DOSISH || defined __CYGWIN__
2429# define isdirsep(x) ((x) == '/' || (x) == '\\')
2431 static const char exeext[] =
".exe";
2432 enum {extlen =
sizeof(exeext)-1};
2433 if (flen > extlen && !isdirsep(fname[flen-extlen-1]) &&
2434 STRNCASECMP(fname+flen-extlen, exeext, extlen) == 0) {
2443 if (!rb_gc_for_fd(e)) {
2444 rb_load_fail(fname_v, strerror(e));
2447 rb_load_fail(fname_v, strerror(errno));
2452 if (MODE_TO_LOAD != O_RDONLY && (e = disable_nonblock(fd)) != 0) {
2454 rb_load_fail(fname_v, strerror(e));
2457 e = ruby_is_fd_loadable(fd);
2461 rb_load_fail(fname_v, strerror(e));
2477restore_load_file(
VALUE arg)
2492 arg.parser = parser;
2494 arg.script = script;
2498 restore_load_file, (
VALUE)&arg);
2511 return rb_parser_load_file(rb_parser_new(), fname_v);
2515rb_parser_load_file(
VALUE parser,
VALUE fname_v)
2518 VALUE f = open_load_file(fname_v, &cmdline_options_init(&opt)->xflag);
2519 return load_file(parser, fname_v, f, 0, &opt);
2534proc_argv0(
VALUE process)
2536 return rb_orig_progname;
2561 return ruby_setproctitle(title);
2565ruby_setproctitle(
VALUE title)
2568 setproctitle(
"%.*s", RSTRING_LENINT(title), ptr);
2575 if (origarg.argv == 0)
2582external_str_new_cstr(
const char *p)
2586 str = str_conv_enc(str, NULL, rb_default_external_encoding());
2597 rb_orig_progname = rb_progname = external_str_new_cstr(name);
2598 rb_vm_set_progname(rb_progname);
2609 rb_orig_progname = rb_progname =
rb_str_dup(name);
2610 rb_vm_set_progname(rb_progname);
2616 rb_uid_t uid = getuid();
2617 rb_uid_t euid = geteuid();
2618 rb_gid_t gid = getgid();
2619 rb_gid_t egid = getegid();
2621 if (uid != euid) opt->setids |= 1;
2622 if (egid != gid) opt->setids |= 2;
2629 if (opt->setids & 1)
2631 if (opt->setids & 2)
2636verbose_getter(
ID id,
VALUE *ptr)
2638 return *rb_ruby_verbose_ptr();
2644 *rb_ruby_verbose_ptr() =
RTEST(val) ?
Qtrue : val;
2648opt_W_getter(
ID id,
VALUE *dmy)
2650 VALUE v = *rb_ruby_verbose_ptr();
2665debug_getter(
ID id,
VALUE *dmy)
2667 return *rb_ruby_debug_ptr();
2673 *rb_ruby_debug_ptr() = val;
2686 rb_gvar_ractor_local(
"$VERBOSE");
2687 rb_gvar_ractor_local(
"$-v");
2688 rb_gvar_ractor_local(
"$-w");
2689 rb_gvar_ractor_local(
"$-W");
2690 rb_gvar_ractor_local(
"$DEBUG");
2691 rb_gvar_ractor_local(
"$-d");
2715 for (i = 0; i < argc; i++) {
2716 VALUE arg = external_str_new_cstr(argv[i]);
2719 rb_ary_push(av, arg);
2728 const char *script_name = (argc > 0 && argv[0]) ? argv[0] :
ruby_engine;
2730 if (!origarg.argv || origarg.argc <= 0) {
2731 origarg.argc = argc;
2732 origarg.argv = argv;
2736 rb_gc_register_mark_object(
rb_argv0);
2737 iseq = process_options(argc, argv, cmdline_options_init(&opt));
2739#ifndef HAVE_SETPROCTITLE
2740 ruby_init_setproctitle(argc, argv);
2743 return (
void*)(
struct RData*)iseq;
2747fill_standard_fds(
void)
2749 int f0, f1, f2, fds[2];
2751 f0 = fstat(0, &buf) == -1 && errno == EBADF;
2752 f1 = fstat(1, &buf) == -1 && errno == EBADF;
2753 f2 = fstat(2, &buf) == -1 && errno == EBADF;
2755 if (pipe(fds) == 0) {
2764 if (pipe(fds) == 0) {
2766 if (f1 && fds[1] != 1)
2768 if (f2 && fds[1] != 2)
2770 if (fds[1] != 1 && fds[1] != 2)
2780 rb_w32_sysinit(argc, argv);
2782 if (*argc >= 0 && *argv) {
2783 origarg.argc = *argc;
2784 origarg.argv = *argv;
2786 fill_standard_fds();
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
#define PATH_SEP_CHAR
Identical to PATH_SEP, except it is of type char.
VALUE rb_define_module(const char *name)
Defines a top-level module.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ISSPACE
Old name of rb_isspace.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
#define 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_raise(VALUE exc, const char *fmt,...)
Exception entry point.
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.
void rb_fatal(const char *fmt,...)
Raises the unsung "fatal" exception.
VALUE rb_eNameError
NameError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
VALUE rb_eSecurityError
SecurityError exception.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
VALUE rb_cObject
Documented in include/ruby/internal/globals.h.
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_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
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_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.
VALUE rb_io_write(VALUE io, VALUE str)
Writes the given string to the given IO.
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 $\.
VALUE rb_io_flush(VALUE io)
Flushes any buffered data within the passed IO to the underlying operating system.
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.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the 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_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
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.
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.
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.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
#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.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RFILE(obj)
Convenient casting macro.
@ RSTRING_EMBED_LEN_MAX
Max possible number of characters that can be embedded.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
#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 RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
uintptr_t VALUE
Type that represents a Ruby object.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.