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)
76void Init_ruby_description(ruby_cmdline_options_t *opt);
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#if defined(RJIT_FORCE_ENABLE) || !USE_YJIT
123 DEFINE_FEATURE(jit) = feature_rjit,
125 DEFINE_FEATURE(jit) = feature_yjit,
127 feature_jit_mask = FEATURE_BIT(rjit) | FEATURE_BIT(yjit),
129 feature_debug_flag_begin = feature_debug_flag_first - 1,
130 EACH_DEBUG_FEATURES(DEFINE_DEBUG_FEATURE, COMMA),
134#define MULTI_BITS_P(bits) ((bits) & ((bits) - 1))
136#define DEBUG_BIT(bit) (1U << feature_debug_##bit)
138#define DUMP_BIT(bit) (1U << dump_##bit)
139#define DEFINE_DUMP(bit) dump_##bit
140#define EACH_DUMPS(X, SEP) \
159 dump_opt_error_tolerant,
162 EACH_DUMPS(DEFINE_DUMP, COMMA),
163 dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) |
164 DUMP_BIT(parsetree) | DUMP_BIT(insns)),
165 dump_optional_bits = (DUMP_BIT(opt_error_tolerant) |
166 DUMP_BIT(opt_comment) |
167 DUMP_BIT(opt_optimize))
171rb_feature_set_to(
ruby_features_t *feat,
unsigned int bit_mask,
unsigned int bit_set)
173 feat->mask |= bit_mask;
174 feat->set = (feat->set & ~bit_mask) | bit_set;
177#define FEATURE_SET_TO(feat, bit_mask, bit_set) \
178 rb_feature_set_to(&(feat), bit_mask, bit_set)
179#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits)
180#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask)
181#define FEATURE_SET_P(feat, bits) ((feat).set & FEATURE_BIT(bits))
182#define FEATURE_USED_P(feat, bits) ((feat).mask & FEATURE_BIT(bits))
183#define FEATURE_SET_BITS(feat) ((feat).set & (feat).mask)
185static void init_ids(ruby_cmdline_options_t *);
187#define src_encoding_index GET_VM()->src_encoding_index
190 COMPILATION_FEATURES = (
192 | FEATURE_BIT(frozen_string_literal)
193 | FEATURE_BIT(frozen_string_literal_set)
194 | FEATURE_BIT(debug_frozen_string_literal)
197 (FEATURE_BIT(debug_flag_first)-1)
201 & ~FEATURE_BIT(frozen_string_literal)
202 & ~FEATURE_BIT(frozen_string_literal_set)
207#define BACKTRACE_LENGTH_LIMIT_VALID_P(n) ((n) >= -1)
208#define OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt) \
209 BACKTRACE_LENGTH_LIMIT_VALID_P((opt)->backtrace_length_limit)
211static ruby_cmdline_options_t *
212cmdline_options_init(ruby_cmdline_options_t *opt)
216 opt->src.enc.index = src_encoding_index;
217 opt->ext.enc.index = -1;
218 opt->intern.enc.index = -1;
219 opt->features.set = DEFAULT_FEATURES;
220#ifdef RJIT_FORCE_ENABLE
221 opt->features.set |= FEATURE_BIT(rjit);
222#elif defined(YJIT_FORCE_ENABLE)
223 opt->features.set |= FEATURE_BIT(yjit);
225 opt->dump |= DUMP_BIT(opt_optimize);
226 opt->backtrace_length_limit = LONG_MIN;
232 ruby_cmdline_options_t *opt);
233static VALUE open_load_file(
VALUE fname_v,
int *xflag);
234static void forbid_setid(
const char *,
const ruby_cmdline_options_t *);
235#define forbid_setid(s) forbid_setid((s), opt)
242static const char esc_standout[] =
"\n\033[1;7m";
243static const char esc_bold[] =
"\033[1m";
244static const char esc_reset[] =
"\033[0m";
245static const char esc_none[] =
"";
246#define USAGE_INDENT " "
249show_usage_part(
const char *str,
const unsigned int namelen,
250 const char *str2,
const unsigned int secondlen,
252 int help,
int highlight,
unsigned int w,
int columns)
254 static const int indent_width = (int)
rb_strlen_lit(USAGE_INDENT);
255 const char *sb = highlight ? esc_bold : esc_none;
256 const char *se = highlight ? esc_reset : esc_none;
257 unsigned int desclen = (
unsigned int)strcspn(desc,
"\n");
258 if (!help && desclen > 0 && strchr(
".;:", desc[desclen-1])) --desclen;
259 if (help && (namelen + 1 > w) &&
260 (
int)(namelen + secondlen + indent_width) >= columns) {
261 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, namelen, str, se);
263 const int second_end = secondlen;
265 if (str2[n] ==
',') n++;
266 if (str2[n] ==
' ') n++;
267 printf(USAGE_INDENT
"%s" "%.*s" "%s\n", sb, second_end-n, str2+n, se);
269 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
272 const int wrap = help && namelen + secondlen >= w;
273 printf(USAGE_INDENT
"%s%.*s%-*.*s%s%-*s%.*s\n", sb, namelen, str,
274 (wrap ? 0 : w - namelen),
275 (help ? secondlen : 0), str2, se,
277 (wrap ?
"\n" USAGE_INDENT :
""),
281 while (desc[desclen]) {
283 desclen = (
unsigned int)strcspn(desc,
"\n");
284 printf(
"%-*s%.*s\n", w + indent_width, USAGE_INDENT, desclen, desc);
291 int help,
int highlight,
unsigned int w,
int columns)
293 const char *str = m->str;
294 const unsigned int namelen = m->namelen, secondlen = m->secondlen;
295 const char *desc = str + namelen + secondlen;
296 show_usage_part(str, namelen - 1, str + namelen, secondlen - 1, desc,
297 help, highlight, w, columns);
301ruby_show_usage_line(
const char *name,
const char *secondary,
const char *description,
302 int help,
int highlight,
unsigned int width,
int columns)
304 unsigned int namelen = (
unsigned int)strlen(name);
305 unsigned int secondlen = (secondary ? (
unsigned int)strlen(secondary) : 0);
306 show_usage_part(name, namelen, secondary, secondlen,
307 description, help, highlight, width, columns);
311usage(
const char *name,
int help,
int highlight,
int columns)
313#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
316# define PLATFORM_JIT_OPTION "--yjit"
318# define PLATFORM_JIT_OPTION "--rjit (experimental)"
324 M(
"-0[octal]",
"",
"Set input record separator ($/):\n"
325 "-0 for \\0; -00 for paragraph mode; -0777 for slurp mode."),
326 M(
"-a",
"",
"Split each input line ($_) into fields ($F)."),
327 M(
"-c",
"",
"Check syntax (no execution)."),
328 M(
"-Cdirpath",
"",
"Execute program in specified directory."),
329 M(
"-d",
", --debug",
"Set debugging flag ($DEBUG) to true."),
330 M(
"-e 'code'",
"",
"Execute given Ruby code; multiple -e allowed."),
331 M(
"-Eex[:in]",
", --encoding=ex[:in]",
"Set default external and internal encodings."),
332 M(
"-Fpattern",
"",
"Set input field separator ($;); used with -a."),
333 M(
"-i[extension]",
"",
"Set ARGF in-place mode;\n"
334 "create backup files with given extension."),
335 M(
"-Idirpath",
"",
"Add specified directory to load paths ($LOAD_PATH);\n"
336 "multiple -I allowed."),
337 M(
"-l",
"",
"Set output record separator ($\\) to $/;\n"
338 "used for line-oriented output."),
339 M(
"-n",
"",
"Run program in gets loop."),
340 M(
"-p",
"",
"Like -n, with printing added."),
341 M(
"-rlibrary",
"",
"Require the given library."),
342 M(
"-s",
"",
"Define global variables using switches following program path."),
343 M(
"-S",
"",
"Search directories found in the PATH environment variable."),
344 M(
"-v",
"",
"Print version; set $VERBOSE to true."),
345 M(
"-w",
"",
"Synonym for -W1."),
346 M(
"-W[level=2|:category]",
"",
"Set warning flag ($-W):\n"
347 "0 for silent; 1 for moderate; 2 for verbose."),
348 M(
"-x[dirpath]",
"",
"Execute Ruby code starting from a #!ruby line."),
349 M(
"--jit",
"",
"Enable JIT for the platform; same as " PLATFORM_JIT_OPTION
"."),
351 M(
"--yjit",
"",
"Enable in-process JIT compiler."),
354 M(
"--rjit",
"",
"Enable pure-Ruby JIT compiler (experimental)."),
356 M(
"-h",
"",
"Print this help message; use --help for longer message."),
358 STATIC_ASSERT(usage_msg_size, numberof(usage_msg) < 25);
361 M(
"--backtrace-limit=num",
"",
"Set backtrace limit."),
362 M(
"--copyright",
"",
"Print Ruby copyright."),
363 M(
"--crash-report=template",
"",
"Set template for crash report file."),
364 M(
"--disable=features",
"",
"Disable features; see list below."),
365 M(
"--dump=items",
"",
"Dump items; see list below."),
366 M(
"--enable=features",
"",
"Enable features; see list below."),
367 M(
"--external-encoding=encoding",
"",
"Set default external encoding."),
368 M(
"--help",
"",
"Print long help message; use -h for short message."),
369 M(
"--internal-encoding=encoding",
"",
"Set default internal encoding."),
370 M(
"--parser=parser",
"",
"Set Ruby parser: parse.y or prism."),
371 M(
"--verbose",
"",
"Set $VERBOSE to true; ignore input from $stdin."),
372 M(
"--version",
"",
"Print Ruby version."),
373 M(
"-y",
", --yydebug",
"Print parser log; backward compatibility not guaranteed."),
376 M(
"insns",
"",
"Instruction sequences."),
377 M(
"yydebug",
"",
"yydebug of yacc parser generator."),
378 M(
"parsetree",
"",
"Abstract syntax tree (AST)."),
379 M(
"-optimize",
"",
"Disable optimization (affects insns)."),
380 M(
"+error-tolerant",
"",
"Error-tolerant parsing (affects yydebug, parsetree)."),
381 M(
"+comment",
"",
"Add comments to AST (affects parsetree with --parser=parse.y)."),
384 M(
"gems",
"",
"Rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED
")."),
385 M(
"error_highlight",
"",
"error_highlight (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
386 M(
"did_you_mean",
"",
"did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
387 M(
"syntax_suggest",
"",
"syntax_suggest (default: "DEFAULT_RUBYGEMS_ENABLED
")."),
388 M(
"rubyopt",
"",
"RUBYOPT environment variable (default: enabled)."),
389 M(
"frozen-string-literal",
"",
"Freeze all string literals (default: disabled)."),
391 M(
"yjit",
"",
"In-process JIT compiler (default: disabled)."),
394 M(
"rjit",
"",
"Pure-Ruby JIT compiler (experimental, default: disabled)."),
398 M(
"deprecated",
"",
"Deprecated features."),
399 M(
"experimental",
"",
"Experimental features."),
400 M(
"performance",
"",
"Performance issues."),
401 M(
"strict_unused_block",
"",
"Warning unused block strictly"),
407 const char *sb = highlight ? esc_standout+1 : esc_none;
408 const char *se = highlight ? esc_reset : esc_none;
409 const int num = numberof(usage_msg) - (help ? 1 : 0);
410 unsigned int w = (columns > 80 ? (columns - 79) / 2 : 0) + 16;
411#define SHOW(m) show_usage_line(&(m), help, highlight, w, columns)
413 printf(
"%sUsage:%s %s [options] [--] [filepath] [arguments]\n", sb, se, name);
414 for (i = 0; i < num; ++i)
419 if (highlight) sb = esc_standout;
421 for (i = 0; i < numberof(help_msg); ++i)
423 printf(
"%s""Dump List:%s\n", sb, se);
424 for (i = 0; i < numberof(dumps); ++i)
426 printf(
"%s""Features:%s\n", sb, se);
427 for (i = 0; i < numberof(features); ++i)
429 printf(
"%s""Warning categories:%s\n", sb, se);
430 for (i = 0; i < numberof(warn_categories); ++i)
431 SHOW(warn_categories[i]);
433 printf(
"%s""YJIT options:%s\n", sb, se);
434 rb_yjit_show_usage(help, highlight, w, columns);
437 printf(
"%s""RJIT options (experimental):%s\n", sb, se);
438 for (i = 0; rb_rjit_option_messages[i].str; ++i)
439 SHOW(rb_rjit_option_messages[i]);
443#define rubylib_path_new rb_str_new
446ruby_push_include(
const char *path,
VALUE (*filter)(
VALUE))
450 VALUE load_path = GET_VM()->load_path;
452 char rubylib[FILENAME_MAX];
454# define is_path_sep(c) ((c) == sep || (c) == ';')
456# define is_path_sep(c) ((c) == sep)
459 if (path == 0)
return;
463 while (is_path_sep(*p))
466 for (s = p; *s && !is_path_sep(*s); s = CharNext(s));
474 p = RSTRING_PTR(buf);
477 rb_str_resize(buf,
len);
478 p = strncpy(RSTRING_PTR(buf), p,
len);
481#ifdef HAVE_CYGWIN_CONV_PATH
482#define CONV_TO_POSIX_PATH(p, lib) \
483 cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
485# error no cygwin_conv_path
487 if (CONV_TO_POSIX_PATH(p, rubylib) == 0) {
492 rb_ary_push(load_path, (*filter)(rubylib_path_new(p,
len)));
498identical_path(
VALUE path)
504locale_path(
VALUE path)
506 rb_enc_associate(path, rb_locale_encoding());
513 ruby_push_include(path, locale_path);
517expand_include_path(
VALUE path)
519 char *p = RSTRING_PTR(path);
522 if (*p ==
'.' && p[1] ==
'/')
524 return rb_file_expand_path(path,
Qnil);
528ruby_incpush_expand(
const char *path)
530 ruby_push_include(path, expand_include_path);
534#if defined _WIN32 || defined __CYGWIN__
535static HMODULE libruby;
538DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
540 if (reason == DLL_PROCESS_ATTACH)
546rb_libruby_handle(
void)
552translit_char_bin(
char *p,
int from,
int to)
555 if ((
unsigned char)*p == from)
564# define chdir rb_w32_uchdir
572# define IF_UTF8_PATH(t, f) t
574# define IF_UTF8_PATH(t, f) f
579str_conv_enc(
VALUE str, rb_encoding *from, rb_encoding *to)
586# define str_conv_enc(str, from, to) (str)
591#if defined(LOAD_RELATIVE)
593runtime_libruby_path(
void)
595#if defined _WIN32 || defined __CYGWIN__
603 while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath),
604 ret = GetModuleFileNameW(libruby, wlibpath,
len),
610 if (!ret || ret >
len) rb_fatal(
"failed to get module file name");
611#if defined __CYGWIN__
613 const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
614 size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0);
615 if (!newsize) rb_fatal(
"failed to convert module path to cygwin");
617 libpath = RSTRING_PTR(path);
618 if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) {
619 rb_str_resize(path, 0);
625 for (
len = ret, i = 0; i <
len; ++i) {
626 if (wlibpath[i] == L
'\\') {
632 len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL);
634 libpath = RSTRING_PTR(path);
635 WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath,
len, NULL, NULL);
637 rb_str_resize(wsopath, 0);
639#elif defined(HAVE_DLADDR)
642 const void* addr = (
void *)(
VALUE)expand_include_path;
644 if (!dladdr((
void *)addr, &dli)) {
648 else if (origarg.argc > 0 && origarg.argv && dli.dli_fname == origarg.argv[0]) {
650 path = rb_readlink(fname, NULL);
655 path = rb_realpath_internal(
Qnil, fname, 1);
657 rb_str_resize(fname, 0);
660# error relative load path is not supported on this platform.
665#define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index")
667VALUE ruby_archlibdir_path, ruby_prefix_path;
672 VALUE load_path, archlibdir = 0;
673 ID id_initial_load_path_mark;
674 const char *paths = ruby_initial_load_paths;
676#if defined LOAD_RELATIVE
677#if !defined ENABLE_MULTIARCH
678# define RUBY_ARCH_PATH ""
679#elif defined RUBY_ARCH
680# define RUBY_ARCH_PATH "/"RUBY_ARCH
682# define RUBY_ARCH_PATH "/"RUBY_PLATFORM
689 sopath = runtime_libruby_path();
690 libpath = RSTRING_PTR(sopath);
692 p = strrchr(libpath,
'/');
694 static const char libdir[] =
"/"
695#ifdef LIBDIR_BASENAME
701 const ptrdiff_t libdir_len = (ptrdiff_t)
sizeof(libdir)
703 static const char bindir[] =
"/bin";
704 const ptrdiff_t bindir_len = (ptrdiff_t)
sizeof(bindir) - 1;
706 const char *p2 = NULL;
708#ifdef ENABLE_MULTIARCH
711 if (p - libpath >= bindir_len && !
STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
717 else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
718 archlibdir =
rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath);
722#ifdef ENABLE_MULTIARCH
728 p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
729 if (p)
goto multiarch;
733 baselen = p - libpath;
738 rb_str_resize(sopath, baselen);
739 libpath = RSTRING_PTR(sopath);
740#define PREFIX_PATH() sopath
741#define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
742#define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
744 const size_t exec_prefix_len = strlen(ruby_exec_prefix);
745#define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len))
746#define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
748 rb_gc_register_address(&ruby_prefix_path);
749 ruby_prefix_path = PREFIX_PATH();
751 if (!archlibdir) archlibdir = ruby_prefix_path;
752 rb_gc_register_address(&ruby_archlibdir_path);
753 ruby_archlibdir_path = archlibdir;
755 load_path = GET_VM()->load_path;
757 ruby_push_include(getenv(
"RUBYLIB"), identical_path);
759 id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
761 size_t len = strlen(paths);
762 VALUE path = RUBY_RELATIVE(paths,
len);
763 rb_ivar_set(path, id_initial_load_path_mark, path);
764 rb_ary_push(load_path, path);
773add_modules(
VALUE *req_list,
const char *mod)
775 VALUE list = *req_list;
779 *req_list = list = rb_ary_hidden_new(0);
782 rb_ary_push(list, feature);
786require_libraries(
VALUE *req_list)
788 VALUE list = *req_list;
789 VALUE self = rb_vm_top_self();
791 rb_encoding *extenc = rb_default_external_encoding();
795 VALUE feature = rb_ary_shift(list);
796 rb_enc_associate(feature, extenc);
799 rb_funcallv(self, require, 1, &feature);
811process_sflag(
int sflag)
829 if (s[1] ==
'-' && s[2] ==
'\0')
834 for (p = s + 1; *p; p++) {
843 else if (*p !=
'_' && !
ISALNUM(*p)) {
846 rb_str_new2(
"invalid name for global variable - ");
847 if (!(p = strchr(p,
'='))) {
853 name_error[1] = args[-1];
859 for (p = s + 1; *p; ++p) {
875static long proc_options(
long argc,
char **argv, ruby_cmdline_options_t *opt,
int envopt);
878moreswitches(
const char *s, ruby_cmdline_options_t *opt,
int envopt)
883 VALUE argstr, argary;
886 VALUE src_enc_name = opt->src.enc.name;
887 VALUE ext_enc_name = opt->ext.enc.name;
888 VALUE int_enc_name = opt->intern.enc.name;
891 long backtrace_length_limit = opt->backtrace_length_limit;
892 const char *crash_report = opt->crash_report;
897 opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
899 const int hyphen = *s !=
'-';
903 p = RSTRING_PTR(argstr);
904 if (hyphen) *p =
'-';
905 memcpy(p + hyphen, s,
len + 1);
911 while (*p && !
ISSPACE(*p)) ++p;
916 argc = RSTRING_LEN(argary) /
sizeof(ap);
919 argv = ptr =
ALLOC_N(
char *, argc);
920 MEMMOVE(argv, RSTRING_PTR(argary),
char *, argc);
922 while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) {
934 opt->src.enc.name = src_enc_name;
937 opt->ext.enc.name = ext_enc_name;
940 opt->intern.enc.name = int_enc_name;
942 FEATURE_SET_RESTORE(opt->features, feat);
943 FEATURE_SET_RESTORE(opt->warn, warn);
944 if (BACKTRACE_LENGTH_LIMIT_VALID_P(backtrace_length_limit)) {
945 opt->backtrace_length_limit = backtrace_length_limit;
948 opt->crash_report = crash_report;
953 rb_str_resize(argary, 0);
954 rb_str_resize(argstr, 0);
958name_match_p(
const char *name,
const char *str,
size_t len)
960 if (
len == 0)
return 0;
962 while (
TOLOWER(*str) == *name) {
963 if (!--
len)
return 1;
967 if (*str !=
'-' && *str !=
'_')
return 0;
969 if (*name !=
'-' && *name !=
'_')
return 0;
970 if (!*++name)
return 1;
972 if (--
len == 0)
return 1;
976#define NAME_MATCH_P(name, str, len) \
977 ((len) < (int)sizeof(name) && name_match_p((name), (str), (len)))
979#define UNSET_WHEN(name, bit, str, len) \
980 if (NAME_MATCH_P((name), (str), (len))) { \
981 *(unsigned int *)arg &= ~(bit); \
985#define SET_WHEN(name, bit, str, len) \
986 if (NAME_MATCH_P((name), (str), (len))) { \
987 *(unsigned int *)arg |= (bit); \
991#define LITERAL_NAME_ELEMENT(name) #name
994feature_option(
const char *str,
int len,
void *arg,
const unsigned int enable)
996 static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT,
", ");
998 unsigned int mask = ~0
U;
999 unsigned int set = 0
U;
1000#if AMBIGUOUS_FEATURE_NAMES
1002# define FEATURE_FOUND ++matched
1004# define FEATURE_FOUND goto found
1006#define SET_FEATURE(bit) \
1007 if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;}
1008 EACH_FEATURES(SET_FEATURE, ;);
1009 if (NAME_MATCH_P(
"jit", str,
len)) {
1010 set |= mask = FEATURE_BIT(jit);
1013 if (NAME_MATCH_P(
"all", str,
len)) {
1015 mask &= ~feature_jit_mask | FEATURE_BIT(jit);
1018#if AMBIGUOUS_FEATURE_NAMES
1019 if (matched == 1)
goto found;
1021 VALUE mesg = rb_sprintf(
"ambiguous feature: '%.*s' (",
len, str);
1022#define ADD_FEATURE_NAME(bit) \
1023 if (FEATURE_BIT(bit) & set) { \
1024 rb_str_cat_cstr(mesg, #bit); \
1025 if (--matched) rb_str_cat_cstr(mesg, ", "); \
1027 EACH_FEATURES(ADD_FEATURE_NAME, ;);
1030#undef ADD_FEATURE_NAME
1035 rb_warn(
"unknown argument for --%s: '%.*s'",
1036 enable ?
"enable" :
"disable",
len, str);
1037 rb_warn(
"features are [%.*s].", (
int)strlen(list), list);
1041 FEATURE_SET_TO(*argp, mask, (mask & enable));
1042 if (NAME_MATCH_P(
"frozen_string_literal", str,
len)) {
1043 FEATURE_SET_TO(*argp, FEATURE_BIT(frozen_string_literal_set), FEATURE_BIT(frozen_string_literal_set));
1049enable_option(
const char *str,
int len,
void *arg)
1051 feature_option(str,
len, arg, ~0
U);
1055disable_option(
const char *str,
int len,
void *arg)
1057 feature_option(str,
len, arg, 0
U);
1061int ruby_env_debug_option(
const char *str,
int len,
void *arg);
1064debug_option(
const char *str,
int len,
void *arg)
1066 static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT,
", ");
1068#define SET_WHEN_DEBUG(bit) \
1069 if (NAME_MATCH_P(#bit, str, len)) { \
1070 FEATURE_SET(*argp, DEBUG_BIT(bit)); \
1073 EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;);
1077 rb_warn(
"unknown argument for --debug: '%.*s'",
len, str);
1078 rb_warn(
"debug features are [%.*s].", (
int)strlen(list), list);
1082memtermspn(
const char *str,
char term,
int len)
1085 if (
len <= 0)
return 0;
1086 const char *next = memchr(str, term,
len);
1087 return next ? (int)(next - str) :
len;
1090static const char additional_opt_sep =
'+';
1093dump_additional_option_flag(
const char *str,
int len,
unsigned int bits,
bool set)
1095#define SET_DUMP_OPT(bit) if (NAME_MATCH_P(#bit, str, len)) { \
1096 return set ? (bits | DUMP_BIT(opt_ ## bit)) : (bits & ~DUMP_BIT(opt_ ## bit)); \
1098 SET_DUMP_OPT(error_tolerant);
1099 SET_DUMP_OPT(comment);
1100 SET_DUMP_OPT(optimize);
1102 rb_warn(
"don't know how to dump with%s '%.*s'", set ?
"" :
"out",
len, str);
1107dump_additional_option(
const char *str,
int len,
unsigned int bits)
1110 for (;
len-- > 0 && *str++ == additional_opt_sep;
len -= w, str += w) {
1111 w = memtermspn(str, additional_opt_sep,
len);
1113 if (*str ==
'-' || *str ==
'+') {
1114 set = *str++ ==
'+';
1118 int n = memtermspn(str,
'-', w);
1119 if (str[n] ==
'-') {
1120 if (NAME_MATCH_P(
"with", str, n)) {
1124 else if (NAME_MATCH_P(
"without", str, n)) {
1131 bits = dump_additional_option_flag(str, w, bits, set);
1137dump_option(
const char *str,
int len,
void *arg)
1139 static const char list[] = EACH_DUMPS(LITERAL_NAME_ELEMENT,
", ");
1140 unsigned int *bits_ptr = (
unsigned int *)arg;
1141 if (*str ==
'+' || *str ==
'-') {
1142 bool set = *str++ ==
'+';
1143 *bits_ptr = dump_additional_option_flag(str, --
len, *bits_ptr, set);
1146 int w = memtermspn(str, additional_opt_sep,
len);
1148#define SET_WHEN_DUMP(bit) \
1149 if (NAME_MATCH_P(#bit "-", (str), (w))) { \
1150 *bits_ptr = dump_additional_option(str + w, len - w, *bits_ptr | DUMP_BIT(bit)); \
1153 EACH_DUMPS(SET_WHEN_DUMP, ;);
1154 rb_warn(
"don't know how to dump '%.*s',",
len, str);
1155 rb_warn(
"but only [%.*s].", (
int)strlen(list), list);
1159set_option_encoding_once(
const char *
type,
VALUE *name,
const char *e,
long elen)
1163 if (!elen) elen = strlen(e);
1169 "%s already set to %"PRIsVALUE,
type, *name);
1174#define set_internal_encoding_once(opt, e, elen) \
1175 set_option_encoding_once("default_internal", &(opt)->intern.enc.name, (e), (elen))
1176#define set_external_encoding_once(opt, e, elen) \
1177 set_option_encoding_once("default_external", &(opt)->ext.enc.name, (e), (elen))
1178#define set_source_encoding_once(opt, e, elen) \
1179 set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen))
1181#define yjit_opt_match_noarg(s, l, name) \
1182 opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --yjit-" name " is ignored"), 1) : 1)
1183#define yjit_opt_match_arg(s, l, name) \
1184 opt_match(s, l, name) && (*(s) && *(s+1) ? 1 : (rb_raise(rb_eRuntimeError, "--yjit-" name " needs an argument"), 0))
1188setup_yjit_options(
const char *s)
1191 bool rb_yjit_parse_option(
const char* s);
1192 bool success = rb_yjit_parse_option(s);
1200 "invalid YJIT option '%s' (--help will show valid yjit options)",
1220proc_W_option(ruby_cmdline_options_t *opt,
const char *s,
int *warning)
1223 unsigned int bits = 0;
1224 static const char no_prefix[] =
"no-";
1225 int enable = strncmp(s += 2, no_prefix,
sizeof(no_prefix)-1) != 0;
1226 if (!enable) s +=
sizeof(no_prefix)-1;
1227 size_t len = strlen(s);
1228 if (NAME_MATCH_P(
"deprecated", s,
len)) {
1231 else if (NAME_MATCH_P(
"experimental", s,
len)) {
1234 else if (NAME_MATCH_P(
"performance", s,
len)) {
1237 else if (NAME_MATCH_P(
"strict_unused_block", s,
len)) {
1241 rb_warn(
"unknown warning category: '%s'", s);
1243 if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0);
1256 if (!opt->warning) {
1272 FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS, 0);
1278 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1287proc_e_option(ruby_cmdline_options_t *opt,
const char *s,
long argc,
char **argv)
1297 if (!opt->e_script) {
1299 if (opt->script == 0)
1309proc_K_option(ruby_cmdline_options_t *opt,
const char *s)
1312 const char *enc_name = 0;
1315 enc_name =
"EUC-JP";
1318 enc_name =
"Windows-31J";
1323 case 'N':
case 'n':
case 'A':
case 'a':
1324 enc_name =
"ASCII-8BIT";
1329 if (!opt->ext.enc.name)
1330 opt->ext.enc.name = opt->src.enc.name;
1339proc_0_option(ruby_cmdline_options_t *opt,
const char *s)
1349 else if (v == 0 && numlen >= 2) {
1361proc_encoding_option(ruby_cmdline_options_t *opt,
const char *s,
const char *opt_name)
1364# define set_encoding_part(type) \
1365 if (!(p = strchr(s, ':'))) { \
1366 set_##type##_encoding_once(opt, s, 0); \
1370 set_##type##_encoding_once(opt, s, p-s); \
1372 set_encoding_part(external);
1373 if (!*(s = ++p))
return;
1374 set_encoding_part(internal);
1375 if (!*(s = ++p))
return;
1376#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1377 set_encoding_part(source);
1378 if (!*(s = ++p))
return;
1381# undef set_encoding_part
1386proc_long_options(ruby_cmdline_options_t *opt,
const char *s,
long argc,
char **argv,
int envopt)
1390# define is_option_end(c, allow_hyphen) \
1391 (!(c) || ((allow_hyphen) && (c) == '-') || (c) == '=')
1392# define check_envopt(name, allow_envopt) \
1393 (((allow_envopt) || !envopt) ? (void)0 : \
1394 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
1395# define need_argument(name, s, needs_arg, next_arg) \
1396 ((*(s) ? !*++(s) : (next_arg) && (argc <= 1 || !((s) = argv[1]) || (--argc, ++argv, 0))) && (needs_arg) ? \
1397 rb_raise(rb_eRuntimeError, "missing argument for --" name) \
1399# define is_option_with_arg(name, allow_hyphen, allow_envopt) \
1400 is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue, Qtrue)
1401# define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg, next_arg) \
1402 (strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) && \
1403 (s[n] != '-' || (s[n] && s[n+1])) ? \
1404 (check_envopt(name, (allow_envopt)), s += n, \
1405 need_argument(name, s, needs_arg, next_arg), 1) : 0)
1407 if (strcmp(
"copyright", s) == 0) {
1408 if (envopt)
goto noenvopt_long;
1409 opt->dump |= DUMP_BIT(copyright);
1420 else if (is_option_with_arg(
"enable",
Qtrue,
Qtrue)) {
1423 else if (is_option_with_arg(
"disable",
Qtrue,
Qtrue)) {
1426 else if (is_option_with_arg(
"encoding",
Qfalse,
Qtrue)) {
1427 proc_encoding_option(opt, s,
"--encoding");
1429 else if (is_option_with_arg(
"internal-encoding",
Qfalse,
Qtrue)) {
1430 set_internal_encoding_once(opt, s, 0);
1432 else if (is_option_with_arg(
"external-encoding",
Qfalse,
Qtrue)) {
1433 set_external_encoding_once(opt, s, 0);
1435 else if (is_option_with_arg(
"parser",
Qfalse,
Qtrue)) {
1436 if (strcmp(
"prism", s) == 0) {
1437 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PRISM);
1439 else if (strcmp(
"parse.y", s) == 0) {
1440 rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PARSE_Y);
1446#if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1447 else if (is_option_with_arg(
"source-encoding",
Qfalse,
Qtrue)) {
1448 set_source_encoding_once(opt, s, 0);
1451 else if (strcmp(
"version", s) == 0) {
1452 if (envopt)
goto noenvopt_long;
1453 opt->dump |= DUMP_BIT(version);
1455 else if (strcmp(
"verbose", s) == 0) {
1459 else if (strcmp(
"jit", s) == 0) {
1460#if USE_YJIT || USE_RJIT
1461 FEATURE_SET(opt->features, FEATURE_BIT(jit));
1463 rb_warn(
"Ruby was built without JIT support");
1466 else if (is_option_with_optarg(
"rjit",
'-',
true,
false,
false)) {
1468 extern void rb_rjit_setup_options(
const char *s,
struct rb_rjit_options *rjit_opt);
1469 FEATURE_SET(opt->features, FEATURE_BIT(rjit));
1470 rb_rjit_setup_options(s, &opt->rjit);
1472 rb_warn(
"RJIT support is disabled.");
1475 else if (is_option_with_optarg(
"yjit",
'-',
true,
false,
false)) {
1477 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
1478 setup_yjit_options(s);
1480 rb_warn(
"Ruby was built without YJIT support."
1481 " You may need to install rustc to build Ruby with YJIT.");
1484 else if (strcmp(
"yydebug", s) == 0) {
1485 if (envopt)
goto noenvopt_long;
1486 opt->dump |= DUMP_BIT(yydebug);
1491 else if (strcmp(
"help", s) == 0) {
1492 if (envopt)
goto noenvopt_long;
1493 opt->dump |= DUMP_BIT(help);
1496 else if (is_option_with_arg(
"backtrace-limit",
Qfalse,
Qtrue)) {
1498 long n = strtol(s, &e, 10);
1499 if (
errno == ERANGE || !BACKTRACE_LENGTH_LIMIT_VALID_P(n) || *e) {
1503 opt->backtrace_length_limit = n;
1506 else if (is_option_with_arg(
"crash-report",
true,
true)) {
1507 opt->crash_report = s;
1511 "invalid option --%s (-h will show valid options)", s);
1513 return argc0 - argc + 1;
1517# undef is_option_end
1519# undef need_argument
1520# undef is_option_with_arg
1521# undef is_option_with_optarg
1526proc_options(
long argc,
char **argv, ruby_cmdline_options_t *opt,
int envopt)
1528 long n, argc0 = argc;
1530 int warning = opt->warning;
1532 if (argc <= 0 || !argv)
1535 for (argc--, argv++; argc > 0; argc--, argv++) {
1536 const char *
const arg = argv[0];
1537 if (!arg || arg[0] !=
'-' || !arg[1])
1544 if (envopt)
goto noenvopt;
1545 opt->do_split = TRUE;
1550 if (envopt)
goto noenvopt;
1551 opt->do_print = TRUE;
1554 if (envopt)
goto noenvopt;
1555 opt->do_loop = TRUE;
1566 if (envopt)
goto noenvopt;
1567 opt->dump |= DUMP_BIT(yydebug);
1576 opt->dump |= DUMP_BIT(version_v);
1579 if (!opt->warning) {
1583 FEATURE_SET(opt->warn, RB_WARN_CATEGORY_DEFAULT_BITS);
1588 if (!(s = proc_W_option(opt, s, &warning)))
break;
1592 if (envopt)
goto noenvopt;
1593 opt->dump |= DUMP_BIT(syntax);
1598 if (envopt)
goto noenvopt;
1600 if (!opt->sflag) opt->sflag = 1;
1605 if (envopt)
goto noenvopt;
1606 opt->dump |= DUMP_BIT(usage);
1610 if (envopt)
goto noenvopt;
1611 opt->do_line = TRUE;
1617 if (envopt)
goto noenvopt;
1619 opt->do_search = TRUE;
1624 if (envopt)
goto noenvopt;
1625 if (!(n = proc_e_option(opt, s, argc, argv)))
break;
1634 add_modules(&opt->req_list, s);
1636 else if (argc > 1) {
1637 add_modules(&opt->req_list, argv[1]);
1643 if (envopt)
goto noenvopt;
1645 ruby_set_inplace_mode(s + 1);
1649 if (envopt)
goto noenvopt;
1653 if (*s && chdir(s) < 0) {
1654 rb_fatal(
"Can't chdir to %s", s);
1660 if (envopt)
goto noenvopt;
1661 if (!*++s && (!--argc || !(s = *++argv) || !*s)) {
1662 rb_fatal(
"Can't chdir");
1665 rb_fatal(
"Can't chdir to %s", s);
1670 if (envopt)
goto noenvopt;
1677 if (!*++s && (!--argc || !(s = *++argv))) {
1680 proc_encoding_option(opt, s,
"-E");
1684 set_internal_encoding_once(opt,
"UTF-8", 0);
1689 if (!(s = proc_K_option(opt, s)))
break;
1695 ruby_incpush_expand(s);
1696 else if (argc > 1) {
1697 ruby_incpush_expand(argv[1]);
1703 if (envopt)
goto noenvopt;
1704 if (!(s = proc_0_option(opt, s)))
break;
1708 if (!s[1] || (s[1] ==
'\r' && !s[2])) {
1714 if (!(n = proc_long_options(opt, s, argc, argv, envopt)))
goto switch_end;
1726 "invalid option -%c (-h will show valid options)",
1727 (
int)(
unsigned char)*s);
1741 if (warning) opt->warning = warning;
1742 return argc0 - argc;
1745void Init_builtin_features(
void);
1748ruby_init_prelude(
void)
1750 Init_builtin_features();
1754void rb_call_builtin_inits(
void);
1758#if RBIMPL_HAS_ATTRIBUTE(weak)
1762Init_extra_exts(
void)
1767ruby_opt_init(ruby_cmdline_options_t *opt)
1769 rb_warning_category_update(opt->warn.mask, opt->warn.set);
1771 if (opt->dump & dump_exit_bits)
return;
1773 if (FEATURE_SET_P(opt->features, gems)) {
1775 if (opt->features.set & FEATURE_BIT(error_highlight)) {
1778 if (opt->features.set & FEATURE_BIT(did_you_mean)) {
1781 if (opt->features.set & FEATURE_BIT(syntax_suggest)) {
1788 if (getenv(
"RUBY_GC_HEAP_INIT_SLOTS")) {
1789 rb_warn_deprecated(
"The environment variable RUBY_GC_HEAP_INIT_SLOTS",
1790 "environment variables RUBY_GC_HEAP_%d_INIT_SLOTS");
1796 rb_rjit_enabled =
true;
1797 if (opt->rjit.stats)
1798 rb_rjit_stats_enabled =
true;
1799 if (opt->rjit.trace_exits)
1800 rb_rjit_trace_exits_enabled =
true;
1806 GET_VM()->running = 0;
1807 rb_call_builtin_inits();
1808 GET_VM()->running = 1;
1809 memset(ruby_vm_redefined_flag, 0,
sizeof(ruby_vm_redefined_flag));
1811 ruby_init_prelude();
1817 rb_rjit_init(&opt->rjit);
1820 rb_yjit_init(opt->yjit);
1824 void Init_builtin_yjit_hook();
1825 Init_builtin_yjit_hook();
1828 require_libraries(&opt->req_list);
1832opt_enc_index(
VALUE enc_name)
1834 const char *s = RSTRING_PTR(enc_name);
1835 int i = rb_enc_find_index(s);
1840 else if (rb_enc_dummy_p(rb_enc_from_index(i))) {
1846#define rb_progname (GET_VM()->progname)
1847#define rb_orig_progname (GET_VM()->orig_progname)
1863#define rb_define_readonly_boolean(name, val) \
1864 rb_define_virtual_variable((name), (val) ? true_value : false_value, 0)
1873 rb_raise(
rb_eTypeError,
"$_ value need to be String (%s given)",
1955setup_pager_env(
void)
1957 if (!getenv(
"LESS")) {
1959 ruby_setenv(
"LESS",
"-R +/^[A-Z].*");
1967 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
1969 if (!GetConsoleMode(h, &m))
return 0;
1970# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
1971# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
1973 if (!(m & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
return 0;
1976#elif !defined(HAVE_WORKING_FORK)
1977# define tty_enabled() 0
1981copy_str(
VALUE str, rb_encoding *enc,
bool intern)
1986 return rb_enc_associate(
rb_str_dup(str), enc);
1994env_var_truthy(
const char *name)
1996 const char *value = getenv(name);
2000 if (strcmp(value,
"1") == 0)
2002 if (strcmp(value,
"true") == 0)
2004 if (strcmp(value,
"yes") == 0)
2011rb_pid_t rb_fork_ruby(
int *status);
2014show_help(
const char *progname,
int help)
2016 int tty = isatty(1);
2019 const char *pager_env = getenv(
"RUBY_PAGER");
2020 if (!pager_env) pager_env = getenv(
"PAGER");
2021 if (pager_env && *pager_env && isatty(0)) {
2022 const char *columns_env = getenv(
"COLUMNS");
2023 if (columns_env) columns = atoi(columns_env);
2025#ifdef HAVE_WORKING_FORK
2028 rb_pid_t pid = rb_fork_ruby(NULL);
2033 else if (pid == 0) {
2051 int oldout = dup(1);
2052 int olderr = dup(2);
2053 int fd =
RFILE(port)->fptr->fd;
2054 tty = tty_enabled();
2057 usage(progname, 1, tty, columns);
2067 usage(progname, help, tty, columns);
2071process_script(ruby_cmdline_options_t *opt)
2075 VALUE parser = rb_parser_new();
2076 const unsigned int dump = opt->dump;
2078 if (dump & DUMP_BIT(yydebug)) {
2079 rb_parser_set_yydebug(parser,
Qtrue);
2082 if ((dump & dump_exit_bits) && (dump & DUMP_BIT(opt_error_tolerant))) {
2083 rb_parser_error_tolerant(parser);
2086 if (opt->e_script) {
2087 VALUE progname = rb_progname;
2088 rb_parser_set_context(parser, 0, TRUE);
2092 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2093 opt->do_line, opt->do_split);
2094 ast_value = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
2098 int xflag = opt->xflag;
2099 f = open_load_file(opt->script_name, &xflag);
2100 opt->xflag = xflag != 0;
2101 rb_parser_set_context(parser, 0, f ==
rb_stdin);
2102 ast_value = load_file(parser, opt->script_name, f, 1, opt);
2104 ast = rb_ruby_ast_data_get(ast_value);
2105 if (!ast->body.root) {
2106 rb_ast_dispose(ast);
2113prism_script_command_line(ruby_cmdline_options_t *opt)
2115 uint8_t command_line = 0;
2121 return command_line;
2125prism_script_shebang_callback(
pm_options_t *options,
const uint8_t *source,
size_t length,
void *data)
2127 ruby_cmdline_options_t *opt = (ruby_cmdline_options_t *) data;
2130 char *switches = malloc(length + 1);
2131 memcpy(switches, source, length);
2132 switches[length] =
'\0';
2134 int no_src_enc = !opt->src.enc.name;
2135 int no_ext_enc = !opt->ext.enc.name;
2136 int no_int_enc = !opt->intern.enc.name;
2138 moreswitches(switches, opt, 0);
2141 pm_options_command_line_set(options, prism_script_command_line(opt));
2143 if (no_src_enc && opt->src.enc.name) {
2144 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2147 if (no_ext_enc && opt->ext.enc.name) {
2148 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2150 if (no_int_enc && opt->intern.enc.name) {
2151 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2165 pm_options_line_set(options, 1);
2166 pm_options_main_script_set(options,
true);
2168 const bool read_stdin = (strcmp(opt->script,
"-") == 0);
2171 pm_options_encoding_set(options, rb_enc_name(rb_locale_encoding()));
2173 if (opt->src.enc.name != 0) {
2177 uint8_t command_line = prism_script_command_line(opt);
2181 pm_options_command_line_set(options, command_line);
2182 pm_options_filepath_set(options,
"-");
2183 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2186 error = pm_parse_stdin(result);
2195 else if (opt->e_script) {
2197 pm_options_command_line_set(options, command_line);
2200 result->
node.coverage_enabled = 0;
2201 error = pm_parse_string(result, opt->e_script,
rb_str_new2(
"-e"), NULL);
2204 VALUE script_name = rb_str_encode_ospath(opt->script_name);
2206 pm_options_command_line_set(options, command_line);
2207 pm_options_shebang_callback_set(options, prism_script_shebang_callback, (
void *) opt);
2209 error = pm_load_file(result, script_name,
true);
2216 error = pm_parse_file(result, opt->script_name, NULL);
2221 if (
RTEST(rb_get_coverages())) {
2222 result->
node.coverage_enabled = 1;
2229 int xflag = opt->xflag;
2230 VALUE file = open_load_file(script_name, &xflag);
2235 if ((parser->
start + offset < parser->end) && parser->
start[offset] ==
'\r') offset++;
2236 if ((parser->
start + offset < parser->end) && parser->
start[offset] ==
'\n') offset++;
2243 if (!
NIL_P(error)) {
2244 pm_parse_result_free(result);
2245 rb_exc_raise(error);
2254 pm_prettyprint(&output_buffer, &result->
parser, result->
node.ast_node);
2256 pm_buffer_free(&output_buffer);
2261process_options_global_setup(
const ruby_cmdline_options_t *opt,
const rb_iseq_t *iseq)
2263 if (OPT_BACKTRACE_LENGTH_LIMIT_VALID_P(opt)) {
2264 rb_backtrace_length_limit = opt->backtrace_length_limit;
2274 rb_define_readonly_boolean(
"$-p", opt->do_print);
2275 rb_define_readonly_boolean(
"$-l", opt->do_line);
2276 rb_define_readonly_boolean(
"$-a", opt->do_split);
2278 rb_gvar_ractor_local(
"$-p");
2279 rb_gvar_ractor_local(
"$-l");
2280 rb_gvar_ractor_local(
"$-a");
2282 if ((rb_e_script = opt->e_script) != 0) {
2284 rb_vm_register_global_object(opt->e_script);
2287 rb_execution_context_t *ec = GET_EC();
2288 VALUE script = (opt->e_script ? opt->e_script :
Qnil);
2289 rb_exec_event_hook_script_compiled(ec, iseq, script);
2293process_options(
int argc,
char **argv, ruby_cmdline_options_t *opt)
2298 pm_parse_result_t prism;
2300#define dispose_result() \
2301 (result.ast ? rb_ast_dispose(result.ast) : pm_parse_result_free(&result.prism))
2303 const rb_iseq_t *iseq;
2304 rb_encoding *enc, *lenc;
2306 rb_encoding *ienc = 0;
2307 rb_encoding *
const uenc = rb_utf8_encoding();
2310 char fbuf[MAXPATHLEN];
2311 int i = (int)proc_options(argc, argv, opt, 0);
2312 unsigned int dump = opt->dump & dump_exit_bits;
2313 rb_vm_t *vm = GET_VM();
2314 const long loaded_before_enc =
RARRAY_LEN(vm->loaded_features);
2316 if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
2317 const char *
const progname =
2318 (argc > 0 && argv && argv[0] ? argv[0] :
2319 origarg.argc > 0 && origarg.argv && origarg.argv[0] ? origarg.argv[0] :
2321 show_help(progname, (opt->dump & DUMP_BIT(help)));
2328 if (FEATURE_SET_P(opt->features, rubyopt) && (s = getenv(
"RUBYOPT"))) {
2329 moreswitches(s, opt, 1);
2332 if (opt->src.enc.name)
2336 rb_warning(
"-K is specified; it is for 1.8 compatibility and may cause odd behavior");
2338 if (!(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2340 if (!FEATURE_USED_P(opt->features, yjit) && env_var_truthy(
"RUBY_YJIT_ENABLE")) {
2341 FEATURE_SET(opt->features, FEATURE_BIT(yjit));
2345 if (MULTI_BITS_P(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {
2346 rb_warn(
"RJIT and YJIT cannot both be enabled at the same time. Exiting");
2351 if (FEATURE_SET_P(opt->features, rjit)) {
2352 opt->rjit.on =
true;
2356 if (FEATURE_SET_P(opt->features, yjit)) {
2357 bool rb_yjit_option_disable(
void);
2358 opt->yjit = !rb_yjit_option_disable();
2362 ruby_mn_threads_params();
2363 Init_ruby_description(opt);
2365 if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
2367 if (opt->dump & DUMP_BIT(version))
return Qtrue;
2369 if (opt->dump & DUMP_BIT(copyright)) {
2374 if (!opt->e_script) {
2381 opt->script = argv[0];
2382 if (!opt->script || opt->script[0] ==
'\0') {
2385 else if (opt->do_search) {
2386 const char *path = getenv(
"RUBYPATH");
2390 opt->script = dln_find_file_r(argv[0], path, fbuf,
sizeof(fbuf));
2393 opt->script = dln_find_file_r(argv[0], getenv(
PATH_ENV), fbuf,
sizeof(fbuf));
2396 opt->script = argv[0];
2401 if (opt->script[0] ==
'-' && !opt->script[1]) {
2402 forbid_setid(
"program input from stdin");
2407 opt->script = RSTRING_PTR(opt->script_name);
2410 translit_char_bin(RSTRING_PTR(opt->script_name),
'\\',
'/');
2413 ruby_gc_set_params();
2417 lenc = rb_locale_encoding();
2418 rb_enc_associate(rb_progname, lenc);
2420 if (opt->ext.enc.name != 0) {
2421 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2423 if (opt->intern.enc.name != 0) {
2424 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2426 if (opt->src.enc.name != 0) {
2427 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2428 src_encoding_index = opt->src.enc.index;
2430 if (opt->ext.enc.index >= 0) {
2431 enc = rb_enc_from_index(opt->ext.enc.index);
2434 enc = IF_UTF8_PATH(uenc, lenc);
2436 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2437 if (opt->intern.enc.index >= 0) {
2438 enc = rb_enc_from_index(opt->intern.enc.index);
2439 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2440 opt->intern.enc.index = -1;
2445 rb_enc_associate(opt->script_name, IF_UTF8_PATH(uenc, lenc));
2448 opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
2449 opt->script = RSTRING_PTR(opt->script_name);
2453 if (IF_UTF8_PATH(uenc != lenc, 1)) {
2455 VALUE load_path = vm->load_path;
2456 const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
2457 int modifiable = FALSE;
2459 rb_get_expanded_load_path();
2460 for (i = 0; i <
RARRAY_LEN(load_path); ++i) {
2462 int mark = rb_attr_get(path, id_initial_load_path_mark) == path;
2465 if (newpath == path)
continue;
2468 if (!(path = copy_str(path, lenc, !mark)))
continue;
2470 if (mark)
rb_ivar_set(path, id_initial_load_path_mark, path);
2472 rb_ary_modify(load_path);
2478 rb_ary_replace(vm->load_path_snapshot, load_path);
2482 VALUE loaded_features = vm->loaded_features;
2483 bool modified =
false;
2484 for (
long i = loaded_before_enc; i <
RARRAY_LEN(loaded_features); ++i) {
2486 if (!(path = copy_str(path, IF_UTF8_PATH(uenc, lenc),
true)))
continue;
2488 rb_ary_modify(loaded_features);
2494 rb_ary_replace(vm->loaded_features_snapshot, loaded_features);
2498 if (opt->features.mask & COMPILATION_FEATURES) {
2499 VALUE option = rb_hash_new();
2500#define SET_COMPILE_OPTION(h, o, name) \
2501 rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \
2502 RBOOL(FEATURE_SET_P(o->features, name)))
2504 if (FEATURE_SET_P(opt->features, frozen_string_literal_set)) {
2505 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
2507 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
2508 rb_funcallv(rb_cISeq,
rb_intern_const(
"compile_option="), 1, &option);
2509#undef SET_COMPILE_OPTION
2512 opt->sflag = process_sflag(opt->sflag);
2514 if (opt->e_script) {
2516 if (opt->src.enc.index >= 0) {
2517 eenc = rb_enc_from_index(opt->src.enc.index);
2522 if (ienc) eenc = ienc;
2527 opt->e_script = str_conv_enc(opt->e_script, uenc, eenc);
2530 rb_enc_associate(opt->e_script, eenc);
2533 if (!rb_ruby_prism_p()) {
2534 ast_value = process_script(opt);
2535 if (!(result.ast = rb_ruby_ast_data_get(ast_value)))
return Qfalse;
2538 prism_script(opt, &result.prism);
2541 if ((dump & DUMP_BIT(yydebug)) && !(dump &= ~DUMP_BIT(yydebug))) {
2546 if (opt->ext.enc.index >= 0) {
2547 enc = rb_enc_from_index(opt->ext.enc.index);
2550 enc = IF_UTF8_PATH(uenc, lenc);
2552 rb_enc_set_default_external(rb_enc_from_encoding(enc));
2553 if (opt->intern.enc.index >= 0) {
2555 enc = rb_enc_from_index(opt->intern.enc.index);
2556 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
2558 else if (!rb_default_internal_encoding())
2560 rb_enc_set_default_internal(
Qnil);
2561 rb_stdio_set_default_encoding();
2563 opt->sflag = process_sflag(opt->sflag);
2566 if (dump & DUMP_BIT(syntax)) {
2567 printf(
"Syntax OK\n");
2568 dump &= ~DUMP_BIT(syntax);
2575 if (dump & DUMP_BIT(parsetree)) {
2578 int comment = opt->dump & DUMP_BIT(opt_comment);
2579 tree = rb_parser_dump_tree(result.ast->body.root, comment);
2582 tree = prism_dump_tree(&result.prism);
2586 dump &= ~DUMP_BIT(parsetree);
2595 if (!opt->e_script && strcmp(opt->script,
"-")) {
2596 path = rb_realpath_internal(
Qnil, opt->script_name, 1);
2599 path = str_conv_enc(path, uenc, lenc);
2603 rb_enc_copy(path, opt->script_name);
2608 GetBindingPtr(
rb_const_get(rb_cObject, rb_intern(
"TOPLEVEL_BINDING")), toplevel_binding);
2609 const struct rb_block *base_block = toplevel_context(toplevel_binding);
2610 const rb_iseq_t *parent = vm_block_iseq(base_block);
2611 bool optimize = (opt->dump & DUMP_BIT(opt_optimize)) != 0;
2616 iseq = pm_iseq_new_main(&pm->
node, opt->script_name, path, parent, optimize, &error_state);
2618 pm_parse_result_free(pm);
2622 rb_jump_tag(error_state);
2626 rb_ast_t *ast = result.ast;
2627 iseq = rb_iseq_new_main(ast_value, opt->script_name, path, parent, optimize);
2628 rb_ast_dispose(ast);
2632 if (dump & DUMP_BIT(insns)) {
2633 rb_io_write(
rb_stdout, rb_iseq_disasm((
const rb_iseq_t *)iseq));
2635 dump &= ~DUMP_BIT(insns);
2636 if (!dump)
return Qtrue;
2638 if (opt->dump & dump_exit_bits)
return Qtrue;
2640 process_options_global_setup(opt, iseq);
2646warn_cr_in_shebang(
const char *str,
long len)
2648 if (
len > 1 && str[
len-1] ==
'\n' && str[
len-2] ==
'\r') {
2649 rb_warn(
"shebang line ending with \\r may cause problems");
2653#define warn_cr_in_shebang(str, len) (void)0
2656void rb_reset_argf_lineno(
long n);
2662 ruby_cmdline_options_t *opt;
2666void rb_set_script_lines_for(
VALUE vparser,
VALUE path);
2669load_file_internal(
VALUE argp_v)
2672 VALUE parser = argp->parser;
2673 VALUE orig_fname = argp->fname;
2674 int script = argp->script;
2675 ruby_cmdline_options_t *opt = argp->opt;
2682 CONST_ID(set_encoding,
"set_encoding");
2688 int no_src_enc = !opt->src.enc.name;
2689 int no_ext_enc = !opt->ext.enc.name;
2690 int no_int_enc = !opt->intern.enc.name;
2692 enc = rb_ascii8bit_encoding();
2693 rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
2701 if (
len > 2 && str[0] ==
'#' && str[1] ==
'!') {
2702 if (line_start == 1) warn_cr_in_shebang(str,
len);
2716 warn_cr_in_shebang(str,
len);
2719 goto search_shebang;
2724 if (*str ==
'\n') *str-- =
'\0';
2725 if (*str ==
'\r') *str-- =
'\0';
2727 if ((p = strstr(p,
" -")) != 0) {
2729 moreswitches(p + 1, opt, 0);
2735 else if (!
NIL_P(c)) {
2739 if (no_src_enc && opt->src.enc.name) {
2740 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
2741 src_encoding_index = opt->src.enc.index;
2743 if (no_ext_enc && opt->ext.enc.name) {
2744 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
2746 if (no_int_enc && opt->intern.enc.name) {
2747 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
2750 else if (!
NIL_P(c)) {
2756 rb_reset_argf_lineno(0);
2759 if (opt->src.enc.index >= 0) {
2760 enc = rb_enc_from_index(opt->src.enc.index);
2763 enc = rb_locale_encoding();
2766 enc = rb_utf8_encoding();
2768 rb_parser_set_options(parser, opt->do_print, opt->do_loop,
2769 opt->do_line, opt->do_split);
2771 rb_set_script_lines_for(parser, orig_fname);
2775 rb_enc_associate(f, enc);
2776 return rb_parser_compile_string_path(parser, orig_fname, f, line_start);
2779 ast_value = rb_parser_compile_file_path(parser, orig_fname, f, line_start);
2780 rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser));
2781 if (script && rb_parser_end_seen_p(parser)) {
2802disable_nonblock(
int fd)
2804#if defined(HAVE_FCNTL) && defined(F_SETFL)
2805 if (fcntl(fd, F_SETFL, 0) < 0) {
2806 const int e =
errno;
2809 if (e == ENOTSUP)
return 0;
2811# if defined B_UNSUPPORTED
2812 if (e == B_UNSUPPORTED)
return 0;
2821open_load_file(
VALUE fname_v,
int *xflag)
2823 const char *fname = (fname_v = rb_str_encode_ospath(fname_v),
2825 long flen = RSTRING_LEN(fname_v);
2829 if (flen == 1 && fname[0] ==
'-') {
2836 const int MODE_TO_LOAD = O_RDONLY | (
2837#if defined O_NONBLOCK && HAVE_FCNTL
2839 !(O_NONBLOCK & O_ACCMODE) ? O_NONBLOCK :
2841#if defined O_NDELAY && HAVE_FCNTL
2842 !(O_NDELAY & O_ACCMODE) ? O_NDELAY :
2845 int mode = MODE_TO_LOAD;
2846#if defined DOSISH || defined __CYGWIN__
2847# define isdirsep(x) ((x) == '/' || (x) == '\\')
2849 static const char exeext[] =
".exe";
2850 enum {extlen =
sizeof(exeext)-1};
2851 if (flen > extlen && !isdirsep(fname[flen-extlen-1]) &&
2852 STRNCASECMP(fname+flen-extlen, exeext, extlen) == 0) {
2861 if (!rb_gc_for_fd(e)) {
2862 rb_load_fail(fname_v, strerror(e));
2865 rb_load_fail(fname_v, strerror(
errno));
2870 if (MODE_TO_LOAD != O_RDONLY && (e = disable_nonblock(fd)) != 0) {
2872 rb_load_fail(fname_v, strerror(e));
2875 e = ruby_is_fd_loadable(fd);
2879 rb_load_fail(fname_v, strerror(e));
2895restore_load_file(
VALUE arg)
2907load_file(
VALUE parser,
VALUE fname,
VALUE f,
int script, ruby_cmdline_options_t *opt)
2910 arg.parser = parser;
2912 arg.script = script;
2916 restore_load_file, (
VALUE)&arg);
2930 ast_value = rb_parser_load_file(rb_parser_new(), fname_v);
2931 return (
void *)rb_ruby_ast_data_get(ast_value);
2935rb_parser_load_file(
VALUE parser,
VALUE fname_v)
2937 ruby_cmdline_options_t opt;
2939 VALUE f = open_load_file(fname_v, &xflag);
2940 cmdline_options_init(&opt)->xflag = xflag != 0;
2941 return load_file(parser, fname_v, f, 0, &opt);
2956proc_argv0(
VALUE process)
2958 return rb_orig_progname;
2983 return ruby_setproctitle(title);
2987ruby_setproctitle(
VALUE title)
2997 if (origarg.argv == 0)
3004external_str_new_cstr(
const char *p)
3008 str = str_conv_enc(str, NULL, rb_default_external_encoding());
3016set_progname(
VALUE name)
3018 rb_orig_progname = rb_progname = name;
3019 rb_vm_set_progname(rb_progname);
3041init_ids(ruby_cmdline_options_t *opt)
3043 rb_uid_t uid = getuid();
3044 rb_uid_t euid = geteuid();
3045 rb_gid_t gid = getgid();
3046 rb_gid_t egid = getegid();
3048 if (uid != euid) opt->setids |= 1;
3049 if (egid != gid) opt->setids |= 2;
3054forbid_setid(
const char *s,
const ruby_cmdline_options_t *opt)
3056 if (opt->setids & 1)
3058 if (opt->setids & 2)
3063verbose_getter(
ID id,
VALUE *ptr)
3065 return *rb_ruby_verbose_ptr();
3071 *rb_ruby_verbose_ptr() =
RTEST(val) ?
Qtrue : val;
3075opt_W_getter(
ID id,
VALUE *dmy)
3077 VALUE v = *rb_ruby_verbose_ptr();
3092debug_getter(
ID id,
VALUE *dmy)
3094 return *rb_ruby_debug_ptr();
3100 *rb_ruby_debug_ptr() = val;
3113 rb_gvar_ractor_local(
"$VERBOSE");
3114 rb_gvar_ractor_local(
"$-v");
3115 rb_gvar_ractor_local(
"$-w");
3116 rb_gvar_ractor_local(
"$-W");
3117 rb_gvar_ractor_local(
"$DEBUG");
3118 rb_gvar_ractor_local(
"$-d");
3142 for (i = 0; i < argc; i++) {
3143 VALUE arg = external_str_new_cstr(argv[i]);
3146 rb_ary_push(av, arg);
3153 ruby_cmdline_options_t opt;
3155 const char *script_name = (argc > 0 && argv[0]) ? argv[0] :
ruby_engine;
3157 if (!origarg.argv || origarg.argc <= 0) {
3158 origarg.argc = argc;
3159 origarg.argv = argv;
3161 set_progname(external_str_new_cstr(script_name));
3163 rb_vm_register_global_object(
rb_argv0);
3165#ifndef HAVE_SETPROCTITLE
3166 ruby_init_setproctitle(argc, argv);
3169 if (getenv(
"RUBY_FREE_AT_EXIT")) {
3170 rb_free_at_exit =
true;
3174 iseq = process_options(argc, argv, cmdline_options_init(&opt));
3176 if (opt.crash_report && *opt.crash_report) {
3177 void ruby_set_crash_report(
const char *
template);
3178 ruby_set_crash_report(opt.crash_report);
3181 return (
void*)(
struct RData*)iseq;
3185fill_standard_fds(
void)
3187 int f0, f1, f2, fds[2];
3189 f0 = fstat(0, &buf) == -1 &&
errno == EBADF;
3190 f1 = fstat(1, &buf) == -1 &&
errno == EBADF;
3191 f2 = fstat(2, &buf) == -1 &&
errno == EBADF;
3193 if (pipe(fds) == 0) {
3202 if (pipe(fds) == 0) {
3204 if (f1 && fds[1] != 1)
3206 if (f2 && fds[1] != 2)
3208 if (fds[1] != 1 && fds[1] != 2)
3218 rb_w32_sysinit(argc, argv);
3220 if (*argc >= 0 && *argv) {
3221 origarg.argc = *argc;
3222 origarg.argv = *argv;
3224 fill_standard_fds();
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define RUBY_EXTERN
Declaration of externally visible global variables.
#define PATH_SEP_CHAR
Identical to PATH_SEP, except it is of type char.
VALUE rb_define_module(const char *name)
Defines a top-level module.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ISSPACE
Old name of rb_isspace.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define STRNCASECMP
Old name of st_locale_insensitive_strncasecmp.
#define TOLOWER
Old name of rb_tolower.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
#define NIL_P
Old name of RB_NIL_P.
#define scan_oct(s, l, e)
Old name of ruby_scan_oct.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ISALNUM
Old name of rb_isalnum.
#define rb_str_new4
Old name of rb_str_new_frozen.
void ruby_script(const char *name)
Sets the current script name to this value.
void ruby_set_argv(int argc, char **argv)
Sets argv that ruby understands.
void ruby_set_script_name(VALUE name)
Sets the current script name to this value.
void ruby_init_loadpath(void)
Sets up $LOAD_PATH.
void * ruby_process_options(int argc, char **argv)
Identical to ruby_options(), except it raises ruby-level exceptions on failure.
void ruby_prog_init(void)
Defines built-in variables.
void ruby_incpush(const char *path)
Appends the given path to the end of the load path.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNameError
NameError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
VALUE rb_eSecurityError
SecurityError exception.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_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 "defaultexternal" 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.
struct pm_options pm_options_t
The options that can be passed to the parser.
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.
struct pm_parser pm_parser_t
The parser used to parse Ruby source.
#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.
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.
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.