Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ruby.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 ruby.c -
4
5 $Author: tmm1 $
6 created at: Tue Aug 10 12:47:31 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12 **********************************************************************/
13
14 #ifdef __CYGWIN__
15 #include <windows.h>
16 #include <sys/cygwin.h>
17 #endif
18 #include "ruby/ruby.h"
19 #include "ruby/encoding.h"
20 #include "internal.h"
21 #include "eval_intern.h"
22 #include "dln.h"
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <ctype.h>
26
27 #ifdef __hpux
28 #include <sys/pstat.h>
29 #endif
30 #if defined(LOAD_RELATIVE) && defined(HAVE_DLADDR)
31 #include <dlfcn.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #if defined(HAVE_FCNTL_H)
38 #include <fcntl.h>
39 #elif defined(HAVE_SYS_FCNTL_H)
40 #include <sys/fcntl.h>
41 #endif
42 #ifdef HAVE_SYS_PARAM_H
43 # include <sys/param.h>
44 #endif
45 #ifndef MAXPATHLEN
46 # define MAXPATHLEN 1024
47 #endif
48
49 #include "ruby/util.h"
50
51 #ifndef HAVE_STDLIB_H
52 char *getenv();
53 #endif
54
55 #ifndef DISABLE_RUBYGEMS
56 # define DISABLE_RUBYGEMS 0
57 #endif
58 #if DISABLE_RUBYGEMS
59 #define DEFAULT_RUBYGEMS_ENABLED "disabled"
60 #else
61 #define DEFAULT_RUBYGEMS_ENABLED "enabled"
62 #endif
63
64 #define DISABLE_BIT(bit) (1U << disable_##bit)
65 enum disable_flag_bits {
66 disable_gems,
67 disable_rubyopt,
68 disable_flag_count
69 };
70
71 #define DUMP_BIT(bit) (1U << dump_##bit)
72 enum dump_flag_bits {
73 dump_version,
74 dump_version_v,
75 dump_copyright,
76 dump_usage,
77 dump_help,
78 dump_yydebug,
79 dump_syntax,
80 dump_parsetree,
81 dump_parsetree_with_comment,
82 dump_insns,
83 dump_flag_count
84 };
85
86 struct cmdline_options {
87 int sflag, xflag;
88 int do_loop, do_print;
89 int do_line, do_split;
90 int do_search;
91 unsigned int disable;
92 int verbose;
93 int safe_level;
94 unsigned int setids;
95 unsigned int dump;
96 const char *script;
97 VALUE script_name;
98 VALUE e_script;
99 struct {
100 struct {
101 VALUE name;
102 int index;
103 } enc;
104 } src, ext, intern;
105 VALUE req_list;
106 };
107
108 static void init_ids(struct cmdline_options *);
109
110 #define src_encoding_index GET_VM()->src_encoding_index
111
112 static struct cmdline_options *
113 cmdline_options_init(struct cmdline_options *opt)
114 {
115 MEMZERO(opt, *opt, 1);
116 init_ids(opt);
117 opt->src.enc.index = src_encoding_index;
118 opt->ext.enc.index = -1;
119 opt->intern.enc.index = -1;
120 #if DISABLE_RUBYGEMS
121 opt->disable |= DISABLE_BIT(gems);
122 #endif
123 return opt;
124 }
125
126 static NODE *load_file(VALUE, VALUE, int, struct cmdline_options *);
127 static void forbid_setid(const char *, struct cmdline_options *);
128 #define forbid_setid(s) forbid_setid((s), opt)
129
130 static struct {
131 int argc;
132 char **argv;
133 } origarg;
134
135 static void
136 usage(const char *name, int help)
137 {
138 /* This message really ought to be max 23 lines.
139 * Removed -h because the user already knows that option. Others? */
140
141 struct message {
142 const char *str;
143 unsigned short namelen, secondlen;
144 };
145 #define M(shortopt, longopt, desc) { \
146 shortopt " " longopt " " desc, \
147 (unsigned short)sizeof(shortopt), \
148 (unsigned short)sizeof(longopt), \
149 }
150 static const struct message usage_msg[] = {
151 M("-0[octal]", "", "specify record separator (\\0, if no argument)"),
152 M("-a", "", "autosplit mode with -n or -p (splits $_ into $F)"),
153 M("-c", "", "check syntax only"),
154 M("-Cdirectory", "", "cd to directory before executing your script"),
155 M("-d", ", --debug", "set debugging flags (set $DEBUG to true)"),
156 M("-e 'command'", "", "one line of script. Several -e's allowed. Omit [programfile]"),
157 M("-Eex[:in]", ", --encoding=ex[:in]", "specify the default external and internal character encodings"),
158 M("-Fpattern", "", "split() pattern for autosplit (-a)"),
159 M("-i[extension]", "", "edit ARGV files in place (make backup if extension supplied)"),
160 M("-Idirectory", "", "specify $LOAD_PATH directory (may be used more than once)"),
161 M("-l", "", "enable line ending processing"),
162 M("-n", "", "assume 'while gets(); ... end' loop around your script"),
163 M("-p", "", "assume loop like -n but print line also like sed"),
164 M("-rlibrary", "", "require the library before executing your script"),
165 M("-s", "", "enable some switch parsing for switches after script name"),
166 M("-S", "", "look for the script using PATH environment variable"),
167 M("-T[level=1]", "", "turn on tainting checks"),
168 M("-v", ", --verbose", "print version number, then turn on verbose mode"),
169 M("-w", "", "turn warnings on for your script"),
170 M("-W[level=2]", "", "set warning level; 0=silence, 1=medium, 2=verbose"),
171 M("-x[directory]", "", "strip off text before #!ruby line and perhaps cd to directory"),
172 M("-h", "", "show this message, --help for more info"),
173 };
174 static const struct message help_msg[] = {
175 M("--copyright", "", "print the copyright"),
176 M("--enable=feature[,...]", ", --disable=feature[,...]",
177 "enable or disable features"),
178 M("--external-encoding=encoding", ", --internal-encoding=encoding",
179 "specify the default external or internal character encoding"),
180 M("--version", "", "print the version"),
181 M("--help", "", "show this message, -h for short message"),
182 };
183 static const struct message features[] = {
184 M("gems", "", "rubygems (default: "DEFAULT_RUBYGEMS_ENABLED")"),
185 M("rubyopt", "", "RUBYOPT environment variable (default: enabled)"),
186 };
187 int i, w = 16, num = numberof(usage_msg) - (help ? 1 : 0);
188 #define SHOW(m) do { \
189 int wrap = help && (m).namelen + (m).secondlen - 2 > w; \
190 printf(" %.*s%-*.*s%-*s%s\n", (m).namelen-1, (m).str, \
191 (wrap ? 0 : w - (m).namelen + 1), \
192 (help ? (m).secondlen-1 : 0), (m).str + (m).namelen, \
193 (wrap ? w + 3 : 0), (wrap ? "\n" : ""), \
194 (m).str + (m).namelen + (m).secondlen); \
195 } while (0)
196
197 printf("Usage: %s [switches] [--] [programfile] [arguments]\n", name);
198 for (i = 0; i < num; ++i)
199 SHOW(usage_msg[i]);
200
201 if (!help) return;
202
203 for (i = 0; i < numberof(help_msg); ++i)
204 SHOW(help_msg[i]);
205 puts("Features:");
206 for (i = 0; i < numberof(features); ++i)
207 SHOW(features[i]);
208 }
209
210 #ifdef MANGLED_PATH
211 static VALUE
212 rubylib_mangled_path(const char *s, unsigned int l)
213 {
214 static char *newp, *oldp;
215 static int newl, oldl, notfound;
216 char *ptr;
217 VALUE ret;
218
219 if (!newp && !notfound) {
220 newp = getenv("RUBYLIB_PREFIX");
221 if (newp) {
222 oldp = newp = strdup(newp);
223 while (*newp && !ISSPACE(*newp) && *newp != ';') {
224 newp = CharNext(newp); /* Skip digits. */
225 }
226 oldl = newp - oldp;
227 while (*newp && (ISSPACE(*newp) || *newp == ';')) {
228 newp = CharNext(newp); /* Skip whitespace. */
229 }
230 newl = strlen(newp);
231 if (newl == 0 || oldl == 0) {
232 rb_fatal("malformed RUBYLIB_PREFIX");
233 }
234 translit_char(newp, '\\', '/');
235 }
236 else {
237 notfound = 1;
238 }
239 }
240 if (!newp || l < oldl || STRNCASECMP(oldp, s, oldl) != 0) {
241 return rb_str_new(s, l);
242 }
243 ret = rb_str_new(0, l + newl - oldl);
244 ptr = RSTRING_PTR(ret);
245 memcpy(ptr, newp, newl);
246 memcpy(ptr + newl, s + oldl, l - oldl);
247 ptr[l + newl - oldl] = 0;
248 return ret;
249 }
250 #else
251 #define rubylib_mangled_path rb_str_new
252 #endif
253
254 static void
255 push_include(const char *path, VALUE (*filter)(VALUE))
256 {
257 const char sep = PATH_SEP_CHAR;
258 const char *p, *s;
259 VALUE load_path = GET_VM()->load_path;
260
261 p = path;
262 while (*p) {
263 while (*p == sep)
264 p++;
265 if (!*p) break;
266 for (s = p; *s && *s != sep; s = CharNext(s));
267 rb_ary_push(load_path, (*filter)(rubylib_mangled_path(p, s - p)));
268 p = s;
269 }
270 }
271
272 #ifdef __CYGWIN__
273 static void
274 push_include_cygwin(const char *path, VALUE (*filter)(VALUE))
275 {
276 const char *p, *s;
277 char rubylib[FILENAME_MAX];
278 VALUE buf = 0;
279
280 p = path;
281 while (*p) {
282 unsigned int len;
283 while (*p == ';')
284 p++;
285 if (!*p) break;
286 for (s = p; *s && *s != ';'; s = CharNext(s));
287 len = s - p;
288 if (*s) {
289 if (!buf) {
290 buf = rb_str_new(p, len);
291 p = RSTRING_PTR(buf);
292 }
293 else {
294 rb_str_resize(buf, len);
295 p = strncpy(RSTRING_PTR(buf), p, len);
296 }
297 }
298 #ifdef HAVE_CYGWIN_CONV_PATH
299 #define CONV_TO_POSIX_PATH(p, lib) \
300 cygwin_conv_path(CCP_WIN_A_TO_POSIX|CCP_RELATIVE, (p), (lib), sizeof(lib))
301 #else
302 #define CONV_TO_POSIX_PATH(p, lib) \
303 cygwin_conv_to_posix_path((p), (lib))
304 #endif
305 if (CONV_TO_POSIX_PATH(p, rubylib) == 0)
306 p = rubylib;
307 push_include(p, filter);
308 if (!*s) break;
309 p = s + 1;
310 }
311 }
312
313 #define push_include push_include_cygwin
314 #endif
315
316 void
317 ruby_push_include(const char *path, VALUE (*filter)(VALUE))
318 {
319 if (path == 0)
320 return;
321 push_include(path, filter);
322 }
323
324 static VALUE
325 identical_path(VALUE path)
326 {
327 return path;
328 }
329 static VALUE
330 locale_path(VALUE path)
331 {
332 rb_enc_associate(path, rb_locale_encoding());
333 return path;
334 }
335
336 void
337 ruby_incpush(const char *path)
338 {
339 ruby_push_include(path, locale_path);
340 }
341
342 static VALUE
343 expand_include_path(VALUE path)
344 {
345 char *p = RSTRING_PTR(path);
346 if (!p)
347 return path;
348 if (*p == '.' && p[1] == '/')
349 return path;
350 return rb_file_expand_path(path, Qnil);
351 }
352
353 void
354 ruby_incpush_expand(const char *path)
355 {
356 ruby_push_include(path, expand_include_path);
357 }
358
359 #if defined _WIN32 || defined __CYGWIN__
360 static HMODULE libruby;
361
362 BOOL WINAPI
363 DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
364 {
365 if (reason == DLL_PROCESS_ATTACH)
366 libruby = dll;
367 return TRUE;
368 }
369
370 HANDLE
371 rb_libruby_handle(void)
372 {
373 return libruby;
374 }
375 #endif
376
377 void ruby_init_loadpath_safe(int safe_level);
378
379 void
380 ruby_init_loadpath(void)
381 {
382 ruby_init_loadpath_safe(0);
383 }
384
385 void
386 ruby_init_loadpath_safe(int safe_level)
387 {
388 VALUE load_path;
389 ID id_initial_load_path_mark;
390 extern const char ruby_initial_load_paths[];
391 const char *paths = ruby_initial_load_paths;
392 #if defined LOAD_RELATIVE
393 # if defined HAVE_DLADDR || defined HAVE_CYGWIN_CONV_PATH
394 # define VARIABLE_LIBPATH 1
395 # else
396 # define VARIABLE_LIBPATH 0
397 # endif
398 # if VARIABLE_LIBPATH
399 char *libpath;
400 VALUE sopath;
401 # else
402 char libpath[MAXPATHLEN + 1];
403 # endif
404 size_t baselen;
405 char *p;
406
407 #if defined _WIN32 || defined __CYGWIN__
408 # if VARIABLE_LIBPATH
409 sopath = rb_str_new(0, MAXPATHLEN);
410 libpath = RSTRING_PTR(sopath);
411 GetModuleFileName(libruby, libpath, MAXPATHLEN);
412 # else
413 GetModuleFileName(libruby, libpath, sizeof libpath);
414 # endif
415 #elif defined(__EMX__)
416 _execname(libpath, sizeof(libpath) - 1);
417 #elif defined(HAVE_DLADDR)
418 Dl_info dli;
419 if (dladdr((void *)(VALUE)expand_include_path, &dli)) {
420 char fbuf[MAXPATHLEN];
421 char *f = dln_find_file_r(dli.dli_fname, getenv(PATH_ENV), fbuf, sizeof(fbuf));
422 VALUE fname = rb_str_new_cstr(f ? f : dli.dli_fname);
423 rb_str_freeze(fname);
424 sopath = rb_realpath_internal(Qnil, fname, 1);
425 }
426 else {
427 sopath = rb_str_new(0, 0);
428 }
429 libpath = RSTRING_PTR(sopath);
430 #endif
431
432 #if !VARIABLE_LIBPATH
433 libpath[sizeof(libpath) - 1] = '\0';
434 #endif
435 #if defined DOSISH
436 translit_char(libpath, '\\', '/');
437 #elif defined __CYGWIN__
438 {
439 # if VARIABLE_LIBPATH
440 const int win_to_posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE;
441 size_t newsize = cygwin_conv_path(win_to_posix, libpath, 0, 0);
442 if (newsize > 0) {
443 VALUE rubylib = rb_str_new(0, newsize);
444 p = RSTRING_PTR(rubylib);
445 if (cygwin_conv_path(win_to_posix, libpath, p, newsize) == 0) {
446 rb_str_resize(sopath, 0);
447 sopath = rubylib;
448 libpath = p;
449 }
450 }
451 # else
452 char rubylib[FILENAME_MAX];
453 cygwin_conv_to_posix_path(libpath, rubylib);
454 strncpy(libpath, rubylib, sizeof(libpath));
455 # endif
456 }
457 #endif
458 p = strrchr(libpath, '/');
459 if (p) {
460 static const char bindir[] = "/bin";
461 #ifdef LIBDIR_BASENAME
462 static const char libdir[] = "/"LIBDIR_BASENAME;
463 #else
464 static const char libdir[] = "/lib";
465 #endif
466 const ptrdiff_t bindir_len = (ptrdiff_t)sizeof(bindir) - 1;
467 const ptrdiff_t libdir_len = (ptrdiff_t)sizeof(libdir) - 1;
468
469 #ifdef ENABLE_MULTIARCH
470 char *p2 = NULL;
471
472 multiarch:
473 #endif
474 if (p - libpath >= bindir_len && !STRNCASECMP(p - bindir_len, bindir, bindir_len)) {
475 p -= bindir_len;
476 }
477 else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) {
478 p -= libdir_len;
479 }
480 #ifdef ENABLE_MULTIARCH
481 else if (p2) {
482 p = p2;
483 }
484 else {
485 p2 = p;
486 p = rb_enc_path_last_separator(libpath, p, rb_ascii8bit_encoding());
487 if (p) goto multiarch;
488 p = p2;
489 }
490 #endif
491 *p = 0;
492 }
493 #if !VARIABLE_LIBPATH
494 else {
495 strlcpy(libpath, ".", sizeof(libpath));
496 p = libpath + 1;
497 }
498 baselen = p - libpath;
499 #define PREFIX_PATH() rb_str_new(libpath, baselen)
500 #else
501 baselen = p - libpath;
502 rb_str_resize(sopath, baselen);
503 libpath = RSTRING_PTR(sopath);
504 #define PREFIX_PATH() sopath
505 #endif
506
507 #define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen)
508
509 #define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len))
510 #else
511 extern const char ruby_exec_prefix[];
512 const size_t exec_prefix_len = strlen(ruby_exec_prefix);
513 #define RUBY_RELATIVE(path, len) rubylib_mangled_path((path), (len))
514 #define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len)
515 #endif
516 load_path = GET_VM()->load_path;
517
518 if (safe_level == 0) {
519 #ifdef MANGLED_PATH
520 rubylib_mangled_path("", 0);
521 #endif
522 ruby_push_include(getenv("RUBYLIB"), identical_path);
523 }
524
525 id_initial_load_path_mark = rb_intern_const("@gem_prelude_index");
526 while (*paths) {
527 size_t len = strlen(paths);
528 VALUE path = RUBY_RELATIVE(paths, len);
529 rb_ivar_set(path, id_initial_load_path_mark, path);
530 rb_ary_push(load_path, path);
531 paths += len + 1;
532 }
533
534 rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), rb_obj_freeze(PREFIX_PATH()));
535 }
536
537
538 static void
539 add_modules(VALUE *req_list, const char *mod)
540 {
541 VALUE list = *req_list;
542 VALUE feature;
543
544 if (!list) {
545 *req_list = list = rb_ary_new();
546 RBASIC_CLEAR_CLASS(list);
547 }
548 feature = rb_str_new2(mod);
549 RBASIC_CLEAR_CLASS(feature);
550 rb_ary_push(list, feature);
551 }
552
553 static void
554 require_libraries(VALUE *req_list)
555 {
556 VALUE list = *req_list;
557 VALUE self = rb_vm_top_self();
558 ID require;
559 rb_thread_t *th = GET_THREAD();
560 rb_encoding *extenc = rb_default_external_encoding();
561 int prev_parse_in_eval = th->parse_in_eval;
562 th->parse_in_eval = 0;
563
564 CONST_ID(require, "require");
565 while (list && RARRAY_LEN(list) > 0) {
566 VALUE feature = rb_ary_shift(list);
567 rb_enc_associate(feature, extenc);
568 RBASIC_SET_CLASS_RAW(feature, rb_cString);
569 OBJ_FREEZE(feature);
570 rb_funcall2(self, require, 1, &feature);
571 }
572 *req_list = 0;
573
574 th->parse_in_eval = prev_parse_in_eval;
575 }
576
577 static rb_env_t*
578 toplevel_context(VALUE toplevel_binding)
579 {
580 rb_env_t *env;
581 rb_binding_t *bind;
582
583 GetBindingPtr(toplevel_binding, bind);
584 GetEnvPtr(bind->env, env);
585 return env;
586 }
587
588 static void
589 process_sflag(int *sflag)
590 {
591 if (*sflag > 0) {
592 long n;
593 const VALUE *args;
594 VALUE argv = rb_argv;
595
596 n = RARRAY_LEN(argv);
597 args = RARRAY_CONST_PTR(argv);
598 while (n > 0) {
599 VALUE v = *args++;
600 char *s = StringValuePtr(v);
601 char *p;
602 int hyphen = FALSE;
603
604 if (s[0] != '-')
605 break;
606 n--;
607 if (s[1] == '-' && s[2] == '\0')
608 break;
609
610 v = Qtrue;
611 /* check if valid name before replacing - with _ */
612 for (p = s + 1; *p; p++) {
613 if (*p == '=') {
614 *p++ = '\0';
615 v = rb_str_new2(p);
616 break;
617 }
618 if (*p == '-') {
619 hyphen = TRUE;
620 }
621 else if (*p != '_' && !ISALNUM(*p)) {
622 VALUE name_error[2];
623 name_error[0] =
624 rb_str_new2("invalid name for global variable - ");
625 if (!(p = strchr(p, '='))) {
626 rb_str_cat2(name_error[0], s);
627 }
628 else {
629 rb_str_cat(name_error[0], s, p - s);
630 }
631 name_error[1] = args[-1];
632 rb_exc_raise(rb_class_new_instance(2, name_error, rb_eNameError));
633 }
634 }
635 s[0] = '$';
636 if (hyphen) {
637 for (p = s + 1; *p; ++p) {
638 if (*p == '-')
639 *p = '_';
640 }
641 }
642 rb_gv_set(s, v);
643 }
644 n = RARRAY_LEN(argv) - n;
645 while (n--) {
646 rb_ary_shift(argv);
647 }
648 *sflag = -1;
649 }
650 }
651
652 static long proc_options(long argc, char **argv, struct cmdline_options *opt, int envopt);
653
654 static void
655 moreswitches(const char *s, struct cmdline_options *opt, int envopt)
656 {
657 long argc, i, len;
658 char **argv, *p;
659 const char *ap = 0;
660 VALUE argstr, argary;
661
662 while (ISSPACE(*s)) s++;
663 if (!*s) return;
664 argstr = rb_str_tmp_new((len = strlen(s)) + 2);
665 argary = rb_str_tmp_new(0);
666
667 p = RSTRING_PTR(argstr);
668 *p++ = ' ';
669 memcpy(p, s, len + 1);
670 ap = 0;
671 rb_str_cat(argary, (char *)&ap, sizeof(ap));
672 while (*p) {
673 ap = p;
674 rb_str_cat(argary, (char *)&ap, sizeof(ap));
675 while (*p && !ISSPACE(*p)) ++p;
676 if (!*p) break;
677 *p++ = '\0';
678 while (ISSPACE(*p)) ++p;
679 }
680 argc = RSTRING_LEN(argary) / sizeof(ap);
681 ap = 0;
682 rb_str_cat(argary, (char *)&ap, sizeof(ap));
683 argv = (char **)RSTRING_PTR(argary);
684
685 while ((i = proc_options(argc, argv, opt, envopt)) > 1 && (argc -= i) > 0) {
686 argv += i;
687 if (**argv != '-') {
688 *--*argv = '-';
689 }
690 if ((*argv)[1]) {
691 ++argc;
692 --argv;
693 }
694 }
695
696 /* get rid of GC */
697 rb_str_resize(argary, 0);
698 rb_str_resize(argstr, 0);
699 }
700
701 #define NAME_MATCH_P(name, str, len) \
702 ((len) < (int)sizeof(name) && strncmp((str), (name), (len)) == 0)
703
704 #define UNSET_WHEN(name, bit, str, len) \
705 if (NAME_MATCH_P((name), (str), (len))) { \
706 *(unsigned int *)arg &= ~(bit); \
707 return; \
708 }
709
710 #define SET_WHEN(name, bit, str, len) \
711 if (NAME_MATCH_P((name), (str), (len))) { \
712 *(unsigned int *)arg |= (bit); \
713 return; \
714 }
715
716 static void
717 enable_option(const char *str, int len, void *arg)
718 {
719 #define UNSET_WHEN_DISABLE(bit) UNSET_WHEN(#bit, DISABLE_BIT(bit), str, len)
720 UNSET_WHEN_DISABLE(gems);
721 UNSET_WHEN_DISABLE(rubyopt);
722 if (NAME_MATCH_P("all", str, len)) {
723 *(unsigned int *)arg = 0U;
724 return;
725 }
726 rb_warn("unknown argument for --enable: `%.*s'", len, str);
727 }
728
729 static void
730 disable_option(const char *str, int len, void *arg)
731 {
732 #define SET_WHEN_DISABLE(bit) SET_WHEN(#bit, DISABLE_BIT(bit), str, len)
733 SET_WHEN_DISABLE(gems);
734 SET_WHEN_DISABLE(rubyopt);
735 if (NAME_MATCH_P("all", str, len)) {
736 *(unsigned int *)arg = ~0U;
737 return;
738 }
739 rb_warn("unknown argument for --disable: `%.*s'", len, str);
740 }
741
742 static void
743 dump_option(const char *str, int len, void *arg)
744 {
745 #define SET_WHEN_DUMP(bit) SET_WHEN(#bit, DUMP_BIT(bit), str, len)
746 SET_WHEN_DUMP(version);
747 SET_WHEN_DUMP(copyright);
748 SET_WHEN_DUMP(usage);
749 SET_WHEN_DUMP(help);
750 SET_WHEN_DUMP(yydebug);
751 SET_WHEN_DUMP(syntax);
752 SET_WHEN_DUMP(parsetree);
753 SET_WHEN_DUMP(parsetree_with_comment);
754 SET_WHEN_DUMP(insns);
755 rb_warn("don't know how to dump `%.*s',", len, str);
756 rb_warn("but only [version, copyright, usage, yydebug, syntax, parsetree, parsetree_with_comment, insns].");
757 }
758
759 static void
760 set_option_encoding_once(const char *type, VALUE *name, const char *e, long elen)
761 {
762 VALUE ename;
763
764 if (!elen) elen = strlen(e);
765 ename = rb_str_new(e, elen);
766
767 if (*name &&
768 rb_funcall(ename, rb_intern("casecmp"), 1, *name) != INT2FIX(0)) {
769 rb_raise(rb_eRuntimeError,
770 "%s already set to %"PRIsVALUE, type, *name);
771 }
772 *name = ename;
773 }
774
775 #define set_internal_encoding_once(opt, e, elen) \
776 set_option_encoding_once("default_internal", &(opt)->intern.enc.name, (e), (elen))
777 #define set_external_encoding_once(opt, e, elen) \
778 set_option_encoding_once("default_external", &(opt)->ext.enc.name, (e), (elen))
779 #define set_source_encoding_once(opt, e, elen) \
780 set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen))
781
782 static long
783 proc_options(long argc, char **argv, struct cmdline_options *opt, int envopt)
784 {
785 long n, argc0 = argc;
786 const char *s;
787
788 if (argc == 0)
789 return 0;
790
791 for (argc--, argv++; argc > 0; argc--, argv++) {
792 const char *const arg = argv[0];
793 if (!arg || arg[0] != '-' || !arg[1])
794 break;
795
796 s = arg + 1;
797 reswitch:
798 switch (*s) {
799 case 'a':
800 if (envopt) goto noenvopt;
801 opt->do_split = TRUE;
802 s++;
803 goto reswitch;
804
805 case 'p':
806 if (envopt) goto noenvopt;
807 opt->do_print = TRUE;
808 /* through */
809 case 'n':
810 if (envopt) goto noenvopt;
811 opt->do_loop = TRUE;
812 s++;
813 goto reswitch;
814
815 case 'd':
816 ruby_debug = Qtrue;
817 ruby_verbose = Qtrue;
818 s++;
819 goto reswitch;
820
821 case 'y':
822 if (envopt) goto noenvopt;
823 opt->dump |= DUMP_BIT(yydebug);
824 s++;
825 goto reswitch;
826
827 case 'v':
828 if (opt->verbose) {
829 s++;
830 goto reswitch;
831 }
832 opt->dump |= DUMP_BIT(version_v);
833 opt->verbose = 1;
834 case 'w':
835 ruby_verbose = Qtrue;
836 s++;
837 goto reswitch;
838
839 case 'W':
840 {
841 size_t numlen;
842 int v = 2; /* -W as -W2 */
843
844 if (*++s) {
845 v = scan_oct(s, 1, &numlen);
846 if (numlen == 0)
847 v = 1;
848 s += numlen;
849 }
850 switch (v) {
851 case 0:
852 ruby_verbose = Qnil;
853 break;
854 case 1:
855 ruby_verbose = Qfalse;
856 break;
857 default:
858 ruby_verbose = Qtrue;
859 break;
860 }
861 }
862 goto reswitch;
863
864 case 'c':
865 if (envopt) goto noenvopt;
866 opt->dump |= DUMP_BIT(syntax);
867 s++;
868 goto reswitch;
869
870 case 's':
871 if (envopt) goto noenvopt;
872 forbid_setid("-s");
873 if (!opt->sflag) opt->sflag = 1;
874 s++;
875 goto reswitch;
876
877 case 'h':
878 if (envopt) goto noenvopt;
879 opt->dump |= DUMP_BIT(usage);
880 goto switch_end;
881
882 case 'l':
883 if (envopt) goto noenvopt;
884 opt->do_line = TRUE;
885 rb_output_rs = rb_rs;
886 s++;
887 goto reswitch;
888
889 case 'S':
890 if (envopt) goto noenvopt;
891 forbid_setid("-S");
892 opt->do_search = TRUE;
893 s++;
894 goto reswitch;
895
896 case 'e':
897 if (envopt) goto noenvopt;
898 forbid_setid("-e");
899 if (!*++s) {
900 s = argv[1];
901 argc--, argv++;
902 }
903 if (!s) {
904 rb_raise(rb_eRuntimeError, "no code specified for -e");
905 }
906 if (!opt->e_script) {
907 opt->e_script = rb_str_new(0, 0);
908 if (opt->script == 0)
909 opt->script = "-e";
910 }
911 rb_str_cat2(opt->e_script, s);
912 rb_str_cat2(opt->e_script, "\n");
913 break;
914
915 case 'r':
916 forbid_setid("-r");
917 if (*++s) {
918 add_modules(&opt->req_list, s);
919 }
920 else if (argv[1]) {
921 add_modules(&opt->req_list, argv[1]);
922 argc--, argv++;
923 }
924 break;
925
926 case 'i':
927 if (envopt) goto noenvopt;
928 forbid_setid("-i");
929 ruby_set_inplace_mode(s + 1);
930 break;
931
932 case 'x':
933 if (envopt) goto noenvopt;
934 opt->xflag = TRUE;
935 s++;
936 if (*s && chdir(s) < 0) {
937 rb_fatal("Can't chdir to %s", s);
938 }
939 break;
940
941 case 'C':
942 case 'X':
943 if (envopt) goto noenvopt;
944 s++;
945 if (!*s) {
946 s = argv[1];
947 argc--, argv++;
948 }
949 if (!s || !*s) {
950 rb_fatal("Can't chdir");
951 }
952 if (chdir(s) < 0) {
953 rb_fatal("Can't chdir to %s", s);
954 }
955 break;
956
957 case 'F':
958 if (envopt) goto noenvopt;
959 if (*++s) {
960 rb_fs = rb_reg_new(s, strlen(s), 0);
961 }
962 break;
963
964 case 'E':
965 if (!*++s && (!--argc || !(s = *++argv))) {
966 rb_raise(rb_eRuntimeError, "missing argument for -E");
967 }
968 goto encoding;
969
970 case 'U':
971 set_internal_encoding_once(opt, "UTF-8", 0);
972 ++s;
973 goto reswitch;
974
975 case 'K':
976 if (*++s) {
977 const char *enc_name = 0;
978 switch (*s) {
979 case 'E': case 'e':
980 enc_name = "EUC-JP";
981 break;
982 case 'S': case 's':
983 enc_name = "Windows-31J";
984 break;
985 case 'U': case 'u':
986 enc_name = "UTF-8";
987 break;
988 case 'N': case 'n': case 'A': case 'a':
989 enc_name = "ASCII-8BIT";
990 break;
991 }
992 if (enc_name) {
993 opt->src.enc.name = rb_str_new2(enc_name);
994 if (!opt->ext.enc.name)
995 opt->ext.enc.name = opt->src.enc.name;
996 }
997 s++;
998 }
999 goto reswitch;
1000
1001 case 'T':
1002 {
1003 size_t numlen;
1004 int v = 1;
1005
1006 if (*++s) {
1007 v = scan_oct(s, 2, &numlen);
1008 if (numlen == 0)
1009 v = 1;
1010 s += numlen;
1011 }
1012 if (v > opt->safe_level) opt->safe_level = v;
1013 }
1014 goto reswitch;
1015
1016 case 'I':
1017 forbid_setid("-I");
1018 if (*++s)
1019 ruby_incpush_expand(s);
1020 else if (argv[1]) {
1021 ruby_incpush_expand(argv[1]);
1022 argc--, argv++;
1023 }
1024 break;
1025
1026 case '0':
1027 if (envopt) goto noenvopt;
1028 {
1029 size_t numlen;
1030 int v;
1031 char c;
1032
1033 v = scan_oct(s, 4, &numlen);
1034 s += numlen;
1035 if (v > 0377)
1036 rb_rs = Qnil;
1037 else if (v == 0 && numlen >= 2) {
1038 rb_rs = rb_str_new2("\n\n");
1039 }
1040 else {
1041 c = v & 0xff;
1042 rb_rs = rb_str_new(&c, 1);
1043 }
1044 }
1045 goto reswitch;
1046
1047 case '-':
1048 if (!s[1] || (s[1] == '\r' && !s[2])) {
1049 argc--, argv++;
1050 goto switch_end;
1051 }
1052 s++;
1053
1054 # define is_option_end(c, allow_hyphen) \
1055 (!(c) || ((allow_hyphen) && (c) == '-') || (c) == '=')
1056 # define check_envopt(name, allow_envopt) \
1057 (((allow_envopt) || !envopt) ? (void)0 : \
1058 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
1059 # define need_argument(name, s, needs_arg) \
1060 ((*(s)++ ? !*(s) : (!--argc || !((s) = *++argv))) && (needs_arg) ? \
1061 rb_raise(rb_eRuntimeError, "missing argument for --" name) \
1062 : (void)0)
1063 # define is_option_with_arg(name, allow_hyphen, allow_envopt) \
1064 is_option_with_optarg(name, allow_hyphen, allow_envopt, Qtrue)
1065 # define is_option_with_optarg(name, allow_hyphen, allow_envopt, needs_arg) \
1066 (strncmp((name), s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], (allow_hyphen)) ? \
1067 (check_envopt(name, (allow_envopt)), s += n, \
1068 need_argument(name, s, needs_arg), 1) : 0)
1069
1070 if (strcmp("copyright", s) == 0) {
1071 if (envopt) goto noenvopt_long;
1072 opt->dump |= DUMP_BIT(copyright);
1073 }
1074 else if (strcmp("debug", s) == 0) {
1075 ruby_debug = Qtrue;
1076 ruby_verbose = Qtrue;
1077 }
1078 else if (is_option_with_arg("enable", Qtrue, Qtrue)) {
1079 ruby_each_words(s, enable_option, &opt->disable);
1080 }
1081 else if (is_option_with_arg("disable", Qtrue, Qtrue)) {
1082 ruby_each_words(s, disable_option, &opt->disable);
1083 }
1084 else if (is_option_with_arg("encoding", Qfalse, Qtrue)) {
1085 char *p;
1086 encoding:
1087 do {
1088 # define set_encoding_part(type) \
1089 if (!(p = strchr(s, ':'))) { \
1090 set_##type##_encoding_once(opt, s, 0); \
1091 break; \
1092 } \
1093 else if (p > s) { \
1094 set_##type##_encoding_once(opt, s, p-s); \
1095 }
1096 set_encoding_part(external);
1097 if (!*(s = ++p)) break;
1098 set_encoding_part(internal);
1099 if (!*(s = ++p)) break;
1100 #if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1101 set_encoding_part(source);
1102 if (!*(s = ++p)) break;
1103 #endif
1104 rb_raise(rb_eRuntimeError, "extra argument for %s: %s",
1105 (arg[1] == '-' ? "--encoding" : "-E"), s);
1106 # undef set_encoding_part
1107 } while (0);
1108 }
1109 else if (is_option_with_arg("internal-encoding", Qfalse, Qtrue)) {
1110 set_internal_encoding_once(opt, s, 0);
1111 }
1112 else if (is_option_with_arg("external-encoding", Qfalse, Qtrue)) {
1113 set_external_encoding_once(opt, s, 0);
1114 }
1115 #if defined ALLOW_DEFAULT_SOURCE_ENCODING && ALLOW_DEFAULT_SOURCE_ENCODING
1116 else if (is_option_with_arg("source-encoding", Qfalse, Qtrue)) {
1117 set_source_encoding_once(opt, s, 0);
1118 }
1119 #endif
1120 else if (strcmp("version", s) == 0) {
1121 if (envopt) goto noenvopt_long;
1122 opt->dump |= DUMP_BIT(version);
1123 }
1124 else if (strcmp("verbose", s) == 0) {
1125 opt->verbose = 1;
1126 ruby_verbose = Qtrue;
1127 }
1128 else if (strcmp("yydebug", s) == 0) {
1129 if (envopt) goto noenvopt_long;
1130 opt->dump |= DUMP_BIT(yydebug);
1131 }
1132 else if (is_option_with_arg("dump", Qfalse, Qfalse)) {
1133 ruby_each_words(s, dump_option, &opt->dump);
1134 }
1135 else if (strcmp("help", s) == 0) {
1136 if (envopt) goto noenvopt_long;
1137 opt->dump |= DUMP_BIT(help);
1138 goto switch_end;
1139 }
1140 else {
1141 rb_raise(rb_eRuntimeError,
1142 "invalid option --%s (-h will show valid options)", s);
1143 }
1144 break;
1145
1146 case '\r':
1147 if (!s[1])
1148 break;
1149
1150 default:
1151 {
1152 if (ISPRINT(*s)) {
1153 rb_raise(rb_eRuntimeError,
1154 "invalid option -%c (-h will show valid options)",
1155 (int)(unsigned char)*s);
1156 }
1157 else {
1158 rb_raise(rb_eRuntimeError,
1159 "invalid option -\\x%02X (-h will show valid options)",
1160 (int)(unsigned char)*s);
1161 }
1162 }
1163 goto switch_end;
1164
1165 noenvopt:
1166 /* "EIdvwWrKU" only */
1167 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: -%c", *s);
1168 break;
1169
1170 noenvopt_long:
1171 rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --%s", s);
1172 break;
1173
1174 case 0:
1175 break;
1176 # undef is_option_end
1177 # undef check_envopt
1178 # undef need_argument
1179 # undef is_option_with_arg
1180 # undef is_option_with_optarg
1181 }
1182 }
1183
1184 switch_end:
1185 return argc0 - argc;
1186 }
1187
1188 static void
1189 ruby_init_prelude(void)
1190 {
1191 Init_prelude();
1192 rb_const_remove(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"));
1193 }
1194
1195 static int
1196 opt_enc_index(VALUE enc_name)
1197 {
1198 const char *s = RSTRING_PTR(enc_name);
1199 int i = rb_enc_find_index(s);
1200
1201 if (i < 0) {
1202 rb_raise(rb_eRuntimeError, "unknown encoding name - %s", s);
1203 }
1204 else if (rb_enc_dummy_p(rb_enc_from_index(i))) {
1205 rb_raise(rb_eRuntimeError, "dummy encoding is not acceptable - %s ", s);
1206 }
1207 return i;
1208 }
1209
1210 #define rb_progname (GET_VM()->progname)
1211 #define rb_orig_progname (GET_VM()->orig_progname)
1212 VALUE rb_argv0;
1213
1214 static VALUE
1215 false_value(void)
1216 {
1217 return Qfalse;
1218 }
1219
1220 static VALUE
1221 true_value(void)
1222 {
1223 return Qtrue;
1224 }
1225
1226 #define rb_define_readonly_boolean(name, val) \
1227 rb_define_virtual_variable((name), (val) ? true_value : false_value, 0)
1228
1229 static VALUE
1230 uscore_get(void)
1231 {
1232 VALUE line;
1233
1234 line = rb_lastline_get();
1235 if (!RB_TYPE_P(line, T_STRING)) {
1236 rb_raise(rb_eTypeError, "$_ value need to be String (%s given)",
1237 NIL_P(line) ? "nil" : rb_obj_classname(line));
1238 }
1239 return line;
1240 }
1241
1242 /*
1243 * call-seq:
1244 * sub(pattern, replacement) -> $_
1245 * sub(pattern) {|...| block } -> $_
1246 *
1247 * Equivalent to <code>$_.sub(<i>args</i>)</code>, except that
1248 * <code>$_</code> will be updated if substitution occurs.
1249 * Available only when -p/-n command line option specified.
1250 */
1251
1252 static VALUE
1253 rb_f_sub(int argc, VALUE *argv)
1254 {
1255 VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("sub"), argc, argv);
1256 rb_lastline_set(str);
1257 return str;
1258 }
1259
1260 /*
1261 * call-seq:
1262 * gsub(pattern, replacement) -> $_
1263 * gsub(pattern) {|...| block } -> $_
1264 *
1265 * Equivalent to <code>$_.gsub...</code>, except that <code>$_</code>
1266 * will be updated if substitution occurs.
1267 * Available only when -p/-n command line option specified.
1268 *
1269 */
1270
1271 static VALUE
1272 rb_f_gsub(int argc, VALUE *argv)
1273 {
1274 VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("gsub"), argc, argv);
1275 rb_lastline_set(str);
1276 return str;
1277 }
1278
1279 /*
1280 * call-seq:
1281 * chop -> $_
1282 *
1283 * Equivalent to <code>($_.dup).chop!</code>, except <code>nil</code>
1284 * is never returned. See <code>String#chop!</code>.
1285 * Available only when -p/-n command line option specified.
1286 *
1287 */
1288
1289 static VALUE
1290 rb_f_chop(void)
1291 {
1292 VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chop"), 0, 0);
1293 rb_lastline_set(str);
1294 return str;
1295 }
1296
1297
1298 /*
1299 * call-seq:
1300 * chomp -> $_
1301 * chomp(string) -> $_
1302 *
1303 * Equivalent to <code>$_ = $_.chomp(<em>string</em>)</code>. See
1304 * <code>String#chomp</code>.
1305 * Available only when -p/-n command line option specified.
1306 *
1307 */
1308
1309 static VALUE
1310 rb_f_chomp(int argc, VALUE *argv)
1311 {
1312 VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chomp"), argc, argv);
1313 rb_lastline_set(str);
1314 return str;
1315 }
1316
1317 /* blank function in dmyext.c or generated by enc/make_encmake.rb */
1318 extern void Init_enc(void);
1319
1320 static VALUE
1321 process_options(int argc, char **argv, struct cmdline_options *opt)
1322 {
1323 NODE *tree = 0;
1324 VALUE parser;
1325 VALUE iseq;
1326 rb_encoding *enc, *lenc;
1327 const char *s;
1328 char fbuf[MAXPATHLEN];
1329 int i = (int)proc_options(argc, argv, opt, 0);
1330 rb_thread_t *th = GET_THREAD();
1331 VALUE toplevel_binding = Qundef;
1332
1333 argc -= i;
1334 argv += i;
1335
1336 if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) {
1337 usage(origarg.argv[0], (opt->dump & DUMP_BIT(help)));
1338 return Qtrue;
1339 }
1340
1341 if (!(opt->disable & DISABLE_BIT(rubyopt)) &&
1342 opt->safe_level == 0 && (s = getenv("RUBYOPT"))) {
1343 VALUE src_enc_name = opt->src.enc.name;
1344 VALUE ext_enc_name = opt->ext.enc.name;
1345 VALUE int_enc_name = opt->intern.enc.name;
1346
1347 opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
1348 moreswitches(s, opt, 1);
1349 if (src_enc_name)
1350 opt->src.enc.name = src_enc_name;
1351 if (ext_enc_name)
1352 opt->ext.enc.name = ext_enc_name;
1353 if (int_enc_name)
1354 opt->intern.enc.name = int_enc_name;
1355 }
1356
1357 if (opt->src.enc.name)
1358 rb_warning("-K is specified; it is for 1.8 compatibility and may cause odd behavior");
1359
1360 if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
1361 ruby_show_version();
1362 if (opt->dump & DUMP_BIT(version)) return Qtrue;
1363 }
1364 if (opt->dump & DUMP_BIT(copyright)) {
1365 ruby_show_copyright();
1366 }
1367
1368 if (opt->safe_level >= 4) {
1369 OBJ_TAINT(rb_argv);
1370 OBJ_TAINT(GET_VM()->load_path);
1371 }
1372
1373 if (!opt->e_script) {
1374 if (argc == 0) { /* no more args */
1375 if (opt->verbose)
1376 return Qtrue;
1377 opt->script = "-";
1378 }
1379 else {
1380 opt->script = argv[0];
1381 if (!opt->script || opt->script[0] == '\0') {
1382 opt->script = "-";
1383 }
1384 else if (opt->do_search) {
1385 char *path = getenv("RUBYPATH");
1386
1387 opt->script = 0;
1388 if (path) {
1389 opt->script = dln_find_file_r(argv[0], path, fbuf, sizeof(fbuf));
1390 }
1391 if (!opt->script) {
1392 opt->script = dln_find_file_r(argv[0], getenv(PATH_ENV), fbuf, sizeof(fbuf));
1393 }
1394 if (!opt->script)
1395 opt->script = argv[0];
1396 }
1397 argc--;
1398 argv++;
1399 }
1400 }
1401
1402 opt->script_name = rb_str_new_cstr(opt->script);
1403 opt->script = RSTRING_PTR(opt->script_name);
1404 #if defined DOSISH || defined __CYGWIN__
1405 translit_char(RSTRING_PTR(opt->script_name), '\\', '/');
1406 #endif
1407
1408 ruby_gc_set_params(opt->safe_level);
1409 ruby_init_loadpath_safe(opt->safe_level);
1410 Init_enc();
1411 rb_enc_find_index("encdb");
1412 lenc = rb_locale_encoding();
1413 rb_enc_associate(rb_progname, lenc);
1414 rb_obj_freeze(rb_progname);
1415 parser = rb_parser_new();
1416 if (opt->dump & DUMP_BIT(yydebug)) {
1417 rb_parser_set_yydebug(parser, Qtrue);
1418 }
1419 if (opt->ext.enc.name != 0) {
1420 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
1421 }
1422 if (opt->intern.enc.name != 0) {
1423 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
1424 }
1425 if (opt->src.enc.name != 0) {
1426 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
1427 src_encoding_index = opt->src.enc.index;
1428 }
1429 if (opt->ext.enc.index >= 0) {
1430 enc = rb_enc_from_index(opt->ext.enc.index);
1431 }
1432 else {
1433 enc = lenc;
1434 }
1435 rb_enc_set_default_external(rb_enc_from_encoding(enc));
1436 if (opt->intern.enc.index >= 0) {
1437 enc = rb_enc_from_index(opt->intern.enc.index);
1438 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
1439 opt->intern.enc.index = -1;
1440 }
1441 rb_enc_associate(opt->script_name, lenc);
1442 rb_obj_freeze(opt->script_name);
1443 {
1444 long i;
1445 VALUE load_path = GET_VM()->load_path;
1446 for (i = 0; i < RARRAY_LEN(load_path); ++i) {
1447 RARRAY_ASET(load_path, i,
1448 rb_enc_associate(rb_str_dup(RARRAY_AREF(load_path, i)), lenc));
1449 }
1450 }
1451 Init_ext(); /* load statically linked extensions before rubygems */
1452 if (!(opt->disable & DISABLE_BIT(gems))) {
1453 rb_define_module("Gem");
1454 }
1455 ruby_init_prelude();
1456 ruby_set_argv(argc, argv);
1457 process_sflag(&opt->sflag);
1458
1459 toplevel_binding = rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING"));
1460
1461 #define PREPARE_PARSE_MAIN(expr) do { \
1462 rb_env_t *env = toplevel_context(toplevel_binding); \
1463 th->parse_in_eval--; \
1464 th->base_block = &env->block; \
1465 expr; \
1466 th->parse_in_eval++; \
1467 th->base_block = 0; \
1468 } while (0)
1469
1470 if (opt->e_script) {
1471 VALUE progname = rb_progname;
1472 rb_encoding *eenc;
1473 if (opt->src.enc.index >= 0) {
1474 eenc = rb_enc_from_index(opt->src.enc.index);
1475 }
1476 else {
1477 eenc = lenc;
1478 }
1479 rb_enc_associate(opt->e_script, eenc);
1480 ruby_set_script_name(opt->script_name);
1481 require_libraries(&opt->req_list);
1482 ruby_set_script_name(progname);
1483
1484 PREPARE_PARSE_MAIN({
1485 tree = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
1486 });
1487 }
1488 else {
1489 if (opt->script[0] == '-' && !opt->script[1]) {
1490 forbid_setid("program input from stdin");
1491 }
1492
1493 PREPARE_PARSE_MAIN({
1494 tree = load_file(parser, opt->script_name, 1, opt);
1495 });
1496 }
1497 ruby_set_script_name(opt->script_name);
1498 if (opt->dump & DUMP_BIT(yydebug)) return Qtrue;
1499
1500 if (opt->ext.enc.index >= 0) {
1501 enc = rb_enc_from_index(opt->ext.enc.index);
1502 }
1503 else {
1504 enc = lenc;
1505 }
1506 rb_enc_set_default_external(rb_enc_from_encoding(enc));
1507 if (opt->intern.enc.index >= 0) {
1508 /* Set in the shebang line */
1509 enc = rb_enc_from_index(opt->intern.enc.index);
1510 rb_enc_set_default_internal(rb_enc_from_encoding(enc));
1511 }
1512 else if (!rb_default_internal_encoding())
1513 /* Freeze default_internal */
1514 rb_enc_set_default_internal(Qnil);
1515 rb_stdio_set_default_encoding();
1516
1517 if (!tree) return Qfalse;
1518
1519 process_sflag(&opt->sflag);
1520 opt->xflag = 0;
1521
1522 if (opt->safe_level >= 4) {
1523 FL_UNSET(rb_argv, FL_TAINT);
1524 FL_UNSET(GET_VM()->load_path, FL_TAINT);
1525 }
1526
1527 if (opt->dump & DUMP_BIT(syntax)) {
1528 printf("Syntax OK\n");
1529 return Qtrue;
1530 }
1531
1532 if (opt->do_print) {
1533 PREPARE_PARSE_MAIN({
1534 tree = rb_parser_append_print(parser, tree);
1535 });
1536 }
1537 if (opt->do_loop) {
1538 PREPARE_PARSE_MAIN({
1539 tree = rb_parser_while_loop(parser, tree, opt->do_line, opt->do_split);
1540 });
1541 rb_define_global_function("sub", rb_f_sub, -1);
1542 rb_define_global_function("gsub", rb_f_gsub, -1);
1543 rb_define_global_function("chop", rb_f_chop, 0);
1544 rb_define_global_function("chomp", rb_f_chomp, -1);
1545 }
1546
1547 if (opt->dump & DUMP_BIT(parsetree) || opt->dump & DUMP_BIT(parsetree_with_comment)) {
1548 rb_io_write(rb_stdout, rb_parser_dump_tree(tree, opt->dump & DUMP_BIT(parsetree_with_comment)));
1549 rb_io_flush(rb_stdout);
1550 return Qtrue;
1551 }
1552
1553 PREPARE_PARSE_MAIN({
1554 VALUE path = Qnil;
1555 if (!opt->e_script && strcmp(opt->script, "-")) {
1556 path = rb_realpath_internal(Qnil, opt->script_name, 1);
1557 }
1558 iseq = rb_iseq_new_main(tree, opt->script_name, path);
1559 });
1560
1561 if (opt->dump & DUMP_BIT(insns)) {
1562 rb_io_write(rb_stdout, rb_iseq_disasm(iseq));
1563 rb_io_flush(rb_stdout);
1564 return Qtrue;
1565 }
1566
1567 rb_define_readonly_boolean("$-p", opt->do_print);
1568 rb_define_readonly_boolean("$-l", opt->do_line);
1569 rb_define_readonly_boolean("$-a", opt->do_split);
1570
1571 rb_set_safe_level(opt->safe_level);
1572
1573 return iseq;
1574 }
1575
1576 struct load_file_arg {
1577 VALUE parser;
1578 VALUE fname;
1579 int script;
1580 struct cmdline_options *opt;
1581 };
1582
1583 static VALUE
1584 load_file_internal(VALUE arg)
1585 {
1586 extern VALUE rb_stdin;
1587 struct load_file_arg *argp = (struct load_file_arg *)arg;
1588 VALUE parser = argp->parser;
1589 VALUE orig_fname = argp->fname;
1590 VALUE fname_v = rb_str_encode_ospath(orig_fname);
1591 const char *fname = StringValueCStr(fname_v);
1592 int script = argp->script;
1593 struct cmdline_options *opt = argp->opt;
1594 VALUE f;
1595 int line_start = 1;
1596 NODE *tree = 0;
1597 rb_encoding *enc;
1598 ID set_encoding;
1599 int xflag = 0;
1600
1601 if (strcmp(fname, "-") == 0) {
1602 f = rb_stdin;
1603 }
1604 else {
1605 int fd, mode = O_RDONLY;
1606 #if defined DOSISH || defined __CYGWIN__
1607 {
1608 const char *ext = strrchr(fname, '.');
1609 if (ext && STRCASECMP(ext, ".exe") == 0) {
1610 mode |= O_BINARY;
1611 xflag = 1;
1612 }
1613 }
1614 #endif
1615 if ((fd = rb_cloexec_open(fname, mode, 0)) < 0) {
1616 rb_load_fail(fname_v, strerror(errno));
1617 }
1618 rb_update_max_fd(fd);
1619 #if !defined DOSISH && !defined __CYGWIN__
1620 {
1621 struct stat st;
1622 if (fstat(fd, &st) != 0)
1623 rb_load_fail(fname_v, strerror(errno));
1624 if (S_ISDIR(st.st_mode)) {
1625 errno = EISDIR;
1626 rb_load_fail(fname_v, strerror(EISDIR));
1627 }
1628 }
1629 #endif
1630 f = rb_io_fdopen(fd, mode, fname);
1631 }
1632
1633 CONST_ID(set_encoding, "set_encoding");
1634 if (script) {
1635 VALUE c = 1; /* something not nil */
1636 VALUE line;
1637 char *p;
1638 int no_src_enc = !opt->src.enc.name;
1639 int no_ext_enc = !opt->ext.enc.name;
1640 int no_int_enc = !opt->intern.enc.name;
1641
1642 enc = rb_ascii8bit_encoding();
1643 rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
1644
1645 if (xflag || opt->xflag) {
1646 line_start--;
1647 search_shebang:
1648 forbid_setid("-x");
1649 opt->xflag = FALSE;
1650 while (!NIL_P(line = rb_io_gets(f))) {
1651 line_start++;
1652 if (RSTRING_LEN(line) > 2
1653 && RSTRING_PTR(line)[0] == '#'
1654 && RSTRING_PTR(line)[1] == '!') {
1655 if ((p = strstr(RSTRING_PTR(line), "ruby")) != 0) {
1656 goto start_read;
1657 }
1658 }
1659 }
1660 rb_loaderror("no Ruby script found in input");
1661 }
1662
1663 c = rb_io_getbyte(f);
1664 if (c == INT2FIX('#')) {
1665 c = rb_io_getbyte(f);
1666 if (c == INT2FIX('!')) {
1667 line = rb_io_gets(f);
1668 if (NIL_P(line))
1669 return 0;
1670
1671 if ((p = strstr(RSTRING_PTR(line), "ruby")) == 0) {
1672 /* not ruby script, assume -x flag */
1673 goto search_shebang;
1674 }
1675
1676 start_read:
1677 p += 4;
1678 RSTRING_PTR(line)[RSTRING_LEN(line) - 1] = '\0';
1679 if (RSTRING_PTR(line)[RSTRING_LEN(line) - 2] == '\r')
1680 RSTRING_PTR(line)[RSTRING_LEN(line) - 2] = '\0';
1681 if ((p = strstr(p, " -")) != 0) {
1682 moreswitches(p + 1, opt, 0);
1683 }
1684
1685 /* push back shebang for pragma may exist in next line */
1686 rb_io_ungetbyte(f, rb_str_new2("!\n"));
1687 }
1688 else if (!NIL_P(c)) {
1689 rb_io_ungetbyte(f, c);
1690 }
1691 rb_io_ungetbyte(f, INT2FIX('#'));
1692 if (no_src_enc && opt->src.enc.name) {
1693 opt->src.enc.index = opt_enc_index(opt->src.enc.name);
1694 src_encoding_index = opt->src.enc.index;
1695 }
1696 if (no_ext_enc && opt->ext.enc.name) {
1697 opt->ext.enc.index = opt_enc_index(opt->ext.enc.name);
1698 }
1699 if (no_int_enc && opt->intern.enc.name) {
1700 opt->intern.enc.index = opt_enc_index(opt->intern.enc.name);
1701 }
1702 }
1703 else if (!NIL_P(c)) {
1704 rb_io_ungetbyte(f, c);
1705 }
1706 else {
1707 if (f != rb_stdin) rb_io_close(f);
1708 f = Qnil;
1709 }
1710 ruby_set_script_name(opt->script_name);
1711 require_libraries(&opt->req_list); /* Why here? unnatural */
1712 }
1713 if (opt->src.enc.index >= 0) {
1714 enc = rb_enc_from_index(opt->src.enc.index);
1715 }
1716 else if (f == rb_stdin) {
1717 enc = rb_locale_encoding();
1718 }
1719 else {
1720 enc = rb_utf8_encoding();
1721 }
1722 if (NIL_P(f)) {
1723 f = rb_str_new(0, 0);
1724 rb_enc_associate(f, enc);
1725 return (VALUE)rb_parser_compile_string_path(parser, orig_fname, f, line_start);
1726 }
1727 rb_funcall(f, set_encoding, 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
1728 tree = rb_parser_compile_file_path(parser, orig_fname, f, line_start);
1729 rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser));
1730 if (script && tree && rb_parser_end_seen_p(parser)) {
1731 /*
1732 * DATA is a File that contains the data section of the executed file.
1733 * To create a data section use <tt>__END__</tt>:
1734 *
1735 * $ cat t.rb
1736 * puts DATA.gets
1737 * __END__
1738 * hello world!
1739 *
1740 * $ ruby t.rb
1741 * hello world!
1742 */
1743 rb_define_global_const("DATA", f);
1744 }
1745 else if (f != rb_stdin) {
1746 rb_io_close(f);
1747 }
1748 return (VALUE)tree;
1749 }
1750
1751 static VALUE
1752 restore_lineno(VALUE lineno)
1753 {
1754 return rb_gv_set("$.", lineno);
1755 }
1756
1757 static NODE *
1758 load_file(VALUE parser, VALUE fname, int script, struct cmdline_options *opt)
1759 {
1760 struct load_file_arg arg;
1761 arg.parser = parser;
1762 arg.fname = fname;
1763 arg.script = script;
1764 arg.opt = opt;
1765 return (NODE *)rb_ensure(load_file_internal, (VALUE)&arg, restore_lineno, rb_gv_get("$."));
1766 }
1767
1768 void *
1769 rb_load_file(const char *fname)
1770 {
1771 VALUE fname_v = rb_str_new_cstr(fname);
1772 return rb_load_file_str(fname_v);
1773 }
1774
1775 void *
1776 rb_load_file_str(VALUE fname_v)
1777 {
1778 struct cmdline_options opt;
1779
1780 return load_file(rb_parser_new(), fname_v, 0, cmdline_options_init(&opt));
1781 }
1782
1783 /*
1784 * call-seq:
1785 * Process.argv0 -> frozen_string
1786 *
1787 * Returns the name of the script being executed. The value is not
1788 * affected by assigning a new value to $0.
1789 *
1790 * This method first appeared in Ruby 2.1 to serve as a global
1791 * variable free means to get the script name.
1792 */
1793
1794 static VALUE
1795 proc_argv0(VALUE process)
1796 {
1797 return rb_orig_progname;
1798 }
1799
1800 /*
1801 * call-seq:
1802 * Process.setproctitle(string) -> string
1803 *
1804 * Sets the process title that appears on the ps(1) command. Not
1805 * necessarily effective on all platforms. No exception will be
1806 * raised regardless of the result, nor will NotImplementedError be
1807 * raised even if the platform does not support the feature.
1808 *
1809 * Calling this method does not affect the value of $0.
1810 *
1811 * Process.setproctitle('myapp: worker #%d' % worker_id)
1812 *
1813 * This method first appeared in Ruby 2.1 to serve as a global
1814 * variable free means to change the process title.
1815 */
1816
1817 static VALUE
1818 proc_setproctitle(VALUE process, VALUE title)
1819 {
1820 StringValue(title);
1821
1822 setproctitle("%.*s", RSTRING_LENINT(title), RSTRING_PTR(title));
1823
1824 return title;
1825 }
1826
1827 static void
1828 set_arg0(VALUE val, ID id)
1829 {
1830 if (origarg.argv == 0)
1831 rb_raise(rb_eRuntimeError, "$0 not initialized");
1832
1833 rb_progname = rb_str_new_frozen(proc_setproctitle(rb_mProcess, val));
1834 }
1835
1841 void
1842 ruby_script(const char *name)
1843 {
1844 if (name) {
1845 rb_orig_progname = rb_progname = rb_external_str_new(name, strlen(name));
1846 rb_vm_set_progname(rb_progname);
1847 }
1848 }
1849
1854 void
1855 ruby_set_script_name(VALUE name)
1856 {
1857 rb_orig_progname = rb_progname = rb_str_dup(name);
1858 rb_vm_set_progname(rb_progname);
1859 }
1860
1861 static void
1862 init_ids(struct cmdline_options *opt)
1863 {
1864 rb_uid_t uid = getuid();
1865 rb_uid_t euid = geteuid();
1866 rb_gid_t gid = getgid();
1867 rb_gid_t egid = getegid();
1868
1869 if (uid != euid) opt->setids |= 1;
1870 if (egid != gid) opt->setids |= 2;
1871 if (uid && opt->setids) {
1872 if (opt->safe_level < 1) opt->safe_level = 1;
1873 }
1874 }
1875
1876 #undef forbid_setid
1877 static void
1878 forbid_setid(const char *s, struct cmdline_options *opt)
1879 {
1880 if (opt->setids & 1)
1881 rb_raise(rb_eSecurityError, "no %s allowed while running setuid", s);
1882 if (opt->setids & 2)
1883 rb_raise(rb_eSecurityError, "no %s allowed while running setgid", s);
1884 if (opt->safe_level > 0)
1885 rb_raise(rb_eSecurityError, "no %s allowed in tainted mode", s);
1886 }
1887
1888 static void
1889 verbose_setter(VALUE val, ID id, void *data)
1890 {
1891 VALUE *variable = data;
1892 *variable = RTEST(val) ? Qtrue : val;
1893 }
1894
1895 static VALUE
1896 opt_W_getter(ID id, void *data)
1897 {
1898 VALUE *variable = data;
1899 switch (*variable) {
1900 case Qnil:
1901 return INT2FIX(0);
1902 case Qfalse:
1903 return INT2FIX(1);
1904 case Qtrue:
1905 return INT2FIX(2);
1906 default:
1907 return Qnil;
1908 }
1909 }
1910
1912 void
1913 ruby_prog_init(void)
1914 {
1915 rb_define_hooked_variable("$VERBOSE", &ruby_verbose, 0, verbose_setter);
1916 rb_define_hooked_variable("$-v", &ruby_verbose, 0, verbose_setter);
1917 rb_define_hooked_variable("$-w", &ruby_verbose, 0, verbose_setter);
1918 rb_define_hooked_variable("$-W", &ruby_verbose, opt_W_getter, rb_gvar_readonly_setter);
1919 rb_define_variable("$DEBUG", &ruby_debug);
1920 rb_define_variable("$-d", &ruby_debug);
1921
1922 rb_define_hooked_variable("$0", &rb_progname, 0, set_arg0);
1923 rb_define_hooked_variable("$PROGRAM_NAME", &rb_progname, 0, set_arg0);
1924
1925 rb_define_module_function(rb_mProcess, "argv0", proc_argv0, 0);
1926 rb_define_module_function(rb_mProcess, "setproctitle", proc_setproctitle, 1);
1927
1928 /*
1929 * ARGV contains the command line arguments used to run ruby with the
1930 * first value containing the name of the executable.
1931 *
1932 * A library like OptionParser can be used to process command-line
1933 * arguments.
1934 */
1935 rb_define_global_const("ARGV", rb_argv);
1936 }
1937
1938 void
1939 ruby_set_argv(int argc, char **argv)
1940 {
1941 int i;
1942 VALUE av = rb_argv;
1943
1944 #if defined(USE_DLN_A_OUT)
1945 if (origarg.argv)
1946 dln_argv0 = origarg.argv[0];
1947 else
1948 dln_argv0 = argv[0];
1949 #endif
1950 rb_ary_clear(av);
1951 for (i = 0; i < argc; i++) {
1952 VALUE arg = rb_external_str_new_cstr(argv[i]);
1953
1954 OBJ_FREEZE(arg);
1955 rb_ary_push(av, arg);
1956 }
1957 }
1958
1959 void *
1960 ruby_process_options(int argc, char **argv)
1961 {
1962 struct cmdline_options opt;
1963 VALUE iseq;
1964 const char *script_name = (argc > 0 && argv[0]) ? argv[0] : "ruby";
1965
1966 ruby_script(script_name); /* for the time being */
1967 rb_argv0 = rb_str_new4(rb_progname);
1968 rb_gc_register_mark_object(rb_argv0);
1969 iseq = process_options(argc, argv, cmdline_options_init(&opt));
1970
1971 #ifndef HAVE_SETPROCTITLE
1972 {
1973 extern void ruby_init_setproctitle(int argc, char *argv[]);
1974 ruby_init_setproctitle(argc, argv);
1975 }
1976 #endif
1977
1978 return (void*)(struct RData*)iseq;
1979 }
1980
1981 static void
1982 fill_standard_fds(void)
1983 {
1984 int f0, f1, f2, fds[2];
1985 struct stat buf;
1986 f0 = fstat(0, &buf) == -1 && errno == EBADF;
1987 f1 = fstat(1, &buf) == -1 && errno == EBADF;
1988 f2 = fstat(2, &buf) == -1 && errno == EBADF;
1989 if (f0) {
1990 if (pipe(fds) == 0) {
1991 close(fds[1]);
1992 if (fds[0] != 0) {
1993 dup2(fds[0], 0);
1994 close(fds[0]);
1995 }
1996 }
1997 }
1998 if (f1 || f2) {
1999 if (pipe(fds) == 0) {
2000 close(fds[0]);
2001 if (f1 && fds[1] != 1)
2002 dup2(fds[1], 1);
2003 if (f2 && fds[1] != 2)
2004 dup2(fds[1], 2);
2005 if (fds[1] != 1 && fds[1] != 2)
2006 close(fds[1]);
2007 }
2008 }
2009 }
2010
2017 void
2018 ruby_sysinit(int *argc, char ***argv)
2019 {
2020 #if defined(_WIN32)
2021 void rb_w32_sysinit(int *argc, char ***argv);
2022 rb_w32_sysinit(argc, argv);
2023 #endif
2024 origarg.argc = *argc;
2025 origarg.argv = *argv;
2026 #if defined(USE_DLN_A_OUT)
2027 dln_argv0 = origarg.argv[0];
2028 #endif
2029 fill_standard_fds();
2030 }
STRNCASECMP
#define STRNCASECMP(s1, s2, n)
Definition: ruby.h:1784
rb_cString
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1583
rb_define_global_const
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2236
RBASIC_CLEAR_CLASS
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:607
ruby_init_prelude
static void ruby_init_prelude(void)
Definition: ruby.c:1189
process_sflag
static void process_sflag(int *sflag)
Definition: ruby.c:589
rb_mProcess
RUBY_EXTERN VALUE rb_mProcess
Definition: ruby.h:1548
rb_parser_dump_tree
VALUE rb_parser_dump_tree(NODE *node, int comment)
Definition: node.c:894
load_file_internal
static VALUE load_file_internal(VALUE arg)
Definition: ruby.c:1584
rb_str_new4
#define rb_str_new4
Definition: intern.h:842
cmdline_options::sflag
int sflag
Definition: ruby.c:87
yydebug
#define yydebug
Definition: ripper.c:405
restore_lineno
static VALUE restore_lineno(VALUE lineno)
Definition: ruby.c:1752
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:878
FALSE
#define FALSE
Definition: nkf.h:174
disable_flag_bits
disable_flag_bits
Definition: ruby.c:65
dump_option
static void dump_option(const char *str, int len, void *arg)
Definition: ruby.c:743
strlen
size_t strlen(const char *)
CharNext
#define CharNext(p)
Definition: eval_intern.h:254
rb_update_max_fd
void rb_update_max_fd(int fd)
Definition: io.c:183
dln.h
scan_oct
#define scan_oct(s, l, e)
Definition: util.h:50
rb_orig_progname
#define rb_orig_progname
Definition: ruby.c:1211
getuid
rb_uid_t getuid(void)
Definition: win32.c:2494
load_file_arg::script
int script
Definition: ruby.c:1579
proc_argv0
static VALUE proc_argv0(VALUE process)
Definition: ruby.c:1795
ruby_set_inplace_mode
void ruby_set_inplace_mode(const char *)
Definition: io.c:11606
ruby.h
dump_version_v
Definition: ruby.c:74
MAXPATHLEN
#define MAXPATHLEN
Definition: ruby.c:46
FL_TAINT
#define FL_TAINT
Definition: ruby.h:1137
rb_cloexec_open
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Definition: io.c:228
rb_str_cat
VALUE rb_str_cat(VALUE, const char *, long)
Definition: string.c:2140
ruby_each_words
void ruby_each_words(const char *, void(*)(const char *, int, void *), void *)
Definition: util.c:3767
Qtrue
#define Qtrue
Definition: ruby.h:426
ruby_show_version
void ruby_show_version(void)
Prints the version information of the CRuby interpreter to stdout.
Definition: version.c:78
rb_io_flush
VALUE rb_io_flush(VALUE)
Definition: io.c:1510
rb_ary_shift
VALUE rb_ary_shift(VALUE ary)
Definition: array.c:991
rb_parser_end_seen_p
VALUE rb_parser_end_seen_p(VALUE vparser)
Definition: ripper.c:17604
dump_usage
Definition: ruby.c:76
eval_intern.h
disable_rubyopt
Definition: ruby.c:67
cmdline_options::req_list
VALUE req_list
Definition: ruby.c:105
rb_enc_from_encoding
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:102
rb_f_chop
static VALUE rb_f_chop(void)
Definition: ruby.c:1290
origarg
static struct @149 origarg
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:548
rb_stdin
RUBY_EXTERN VALUE rb_stdin
Definition: ruby.h:1627
ruby_show_copyright
void ruby_show_copyright(void)
Prints the copyright notice of the CRuby interpreter to stdout and exits this process successfully...
Definition: version.c:88
rb_default_internal_encoding
rb_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1436
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:896
cmdline_options::xflag
int xflag
Definition: ruby.c:87
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:775
add_modules
static void add_modules(VALUE *req_list, const char *mod)
Definition: ruby.c:539
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
ruby_init_loadpath_safe
void ruby_init_loadpath_safe(int safe_level)
Definition: ruby.c:386
expand_include_path
static VALUE expand_include_path(VALUE path)
Definition: ruby.c:343
rb_enc_set_default_external
void rb_enc_set_default_external(VALUE encoding)
Definition: encoding.c:1403
rb_enc_associate
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:826
rb_io_getbyte
VALUE rb_io_getbyte(VALUE)
Definition: io.c:3772
rb_ary_clear
VALUE rb_ary_clear(VALUE ary)
Definition: array.c:3381
dump_flag_bits
dump_flag_bits
Definition: ruby.c:72
dln_find_file_r
#define dln_find_file_r
Definition: win32.c:76
require_libraries
static void require_libraries(VALUE *req_list)
Definition: ruby.c:554
SHOW
#define SHOW(m)
M
#define M(shortopt, longopt, desc)
Init_ext
void Init_ext(void)
Definition: dmyext.c:2
disable_option
static void disable_option(const char *str, int len, void *arg)
Definition: ruby.c:730
rb_eSecurityError
VALUE rb_eSecurityError
Definition: error.c:557
dump_help
Definition: ruby.c:77
ruby_exec_prefix
const char ruby_exec_prefix[]
Definition: loadpath.c:59
dump_syntax
Definition: ruby.c:79
PREPARE_PARSE_MAIN
#define PREPARE_PARSE_MAIN(expr)
GetEnvPtr
#define GetEnvPtr(obj, ptr)
Definition: vm_core.h:710
verbose_setter
static void verbose_setter(VALUE val, ID id, void *data)
Definition: ruby.c:1889
rb_io_write
VALUE rb_io_write(VALUE, VALUE)
Definition: io.c:1427
cmdline_options::do_split
int do_split
Definition: ruby.c:89
rb_f_sub
static VALUE rb_f_sub(int argc, VALUE *argv)
Definition: ruby.c:1253
Init_enc
void Init_enc(void)
Definition: dmyext.c:7
rb_define_global_function
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1684
rb_binding_t::env
VALUE env
Definition: vm_core.h:727
dump_insns
Definition: ruby.c:82
rb_thread_struct
Definition: vm_core.h:524
rb_utf8_encoding
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1242
rb_load_fail
void rb_load_fail(VALUE path, const char *err)
Definition: error.c:2068
rb_argv
#define rb_argv
Definition: intern.h:681
rb_str_tmp_new
VALUE rb_str_tmp_new(long)
Definition: string.c:919
rb_loaderror
void rb_loaderror(const char *fmt,...)
Definition: error.c:1879
RData
Definition: ruby.h:957
disable_gems
Definition: ruby.c:66
load_file_arg::opt
struct cmdline_options * opt
Definition: ruby.c:1580
dump_copyright
Definition: ruby.c:75
set_option_encoding_once
static void set_option_encoding_once(const char *type, VALUE *name, const char *e, long elen)
Definition: ruby.c:760
rb_external_str_new_cstr
VALUE rb_external_str_new_cstr(const char *)
Definition: string.c:713
rb_parser_set_yydebug
VALUE rb_parser_set_yydebug(VALUE self, VALUE flag)
Definition: ripper.c:17649
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:406
toplevel_context
static rb_env_t * toplevel_context(VALUE toplevel_binding)
Definition: ruby.c:578
rb_gv_set
VALUE rb_gv_set(const char *, VALUE)
Definition: variable.c:810
ruby_incpush
void ruby_incpush(const char *path)
Definition: ruby.c:337
RNode
Definition: node.h:239
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Definition: eval.c:567
RBASIC_SET_CLASS_RAW
#define RBASIC_SET_CLASS_RAW(obj, cls)
Definition: internal.h:608
getegid
rb_gid_t getegid(void)
Definition: win32.c:2515
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1664
rb_eNameError
VALUE rb_eNameError
Definition: error.c:553
set_external_encoding_once
#define set_external_encoding_once(opt, e, elen)
Definition: ruby.c:777
fill_standard_fds
static void fill_standard_fds(void)
Definition: ruby.c:1982
rb_gv_get
VALUE rb_gv_get(const char *)
Definition: variable.c:819
MEMZERO
#define MEMZERO(p, type, n)
Definition: ruby.h:1351
rb_set_safe_level
void rb_set_safe_level(int)
Definition: safe.c:49
enable_option
static void enable_option(const char *str, int len, void *arg)
Definition: ruby.c:717
rb_lastline_get
VALUE rb_lastline_get(void)
Definition: vm.c:959
rb_default_external_encoding
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1351
rb_output_rs
RUBY_EXTERN VALUE rb_output_rs
Definition: intern.h:520
PREFIX_PATH
#define PREFIX_PATH()
load_file
static NODE * load_file(VALUE, VALUE, int, struct cmdline_options *)
Definition: ruby.c:1758
rb_parser_compile_string
NODE * rb_parser_compile_string(volatile VALUE vparser, const char *f, VALUE s, int line)
Definition: ripper.c:12074
push_include
static void push_include(const char *path, VALUE(*filter)(VALUE))
Definition: ruby.c:255
rb_iseq_new_main
VALUE rb_iseq_new_main(NODE *node, VALUE path, VALUE absolute_path)
Definition: iseq.c:428
rb_class_new_instance
VALUE rb_class_new_instance(int, VALUE *, VALUE)
Definition: object.c:1855
rb_str_encode_ospath
VALUE rb_str_encode_ospath(VALUE path)
Definition: file.c:231
is_option_with_arg
#define is_option_with_arg(name, allow_hyphen, allow_envopt)
val
#define val
init_ids
static void init_ids(struct cmdline_options *)
Definition: ruby.c:1862
RUBY_RELATIVE
#define RUBY_RELATIVE(path, len)
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1553
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:547
DWORD
IUnknown DWORD
Definition: win32ole.c:149
rb_parser_encoding
VALUE rb_parser_encoding(VALUE vparser)
Definition: ripper.c:17619
SET_WHEN_DUMP
#define SET_WHEN_DUMP(bit)
strstr
RUBY_EXTERN char * strstr(const char *, const char *)
Definition: strstr.c:8
GetBindingPtr
#define GetBindingPtr(obj, ptr)
Definition: vm_core.h:723
false_value
static VALUE false_value(void)
Definition: ruby.c:1215
rb_str_cat2
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:2159
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:495
moreswitches
static void moreswitches(const char *s, struct cmdline_options *opt, int envopt)
Definition: ruby.c:655
cmdline_options::name
VALUE name
Definition: ruby.c:101
cmdline_options::setids
unsigned int setids
Definition: ruby.c:94
usage
static void usage(const char *name, int help)
Definition: ruby.c:136
NIL_P
#define NIL_P(v)
Definition: ruby.h:438
enc_name
static VALUE enc_name(VALUE self)
Definition: encoding.c:1079
cmdline_options::do_search
int do_search
Definition: ruby.c:90
ruby_incpush_expand
void ruby_incpush_expand(const char *path)
Definition: ruby.c:354
UNSET_WHEN_DISABLE
#define UNSET_WHEN_DISABLE(bit)
DEFAULT_RUBYGEMS_ENABLED
#define DEFAULT_RUBYGEMS_ENABLED
Definition: ruby.c:61
unistd.h
ISALNUM
#define ISALNUM(c)
Definition: ruby.h:1773
rb_lastline_set
void rb_lastline_set(VALUE)
Definition: vm.c:965
DISABLE_BIT
#define DISABLE_BIT(bit)
Definition: ruby.c:64
rb_parser_append_print
NODE * rb_parser_append_print(VALUE vparser, NODE *node)
Definition: ripper.c:16612
argc
int argc
Definition: ruby.c:131
Qfalse
#define Qfalse
Definition: ruby.h:425
rb_external_str_new
VALUE rb_external_str_new(const char *, long)
Definition: string.c:707
disable_flag_count
Definition: ruby.c:68
rb_fs
RUBY_EXTERN VALUE rb_fs
Definition: intern.h:516
rb_gc_register_mark_object
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:4920
rb_str_new2
#define rb_str_new2
Definition: intern.h:840
OBJ_FREEZE
#define OBJ_FREEZE(x)
Definition: ruby.h:1186
ruby_push_include
void ruby_push_include(const char *path, VALUE(*filter)(VALUE))
Definition: ruby.c:317
rb_vm_top_self
VALUE rb_vm_top_self()
Definition: vm.c:2826
rb_parser_while_loop
NODE * rb_parser_while_loop(VALUE vparser, NODE *node, int chop, int split)
Definition: ripper.c:16644
U
Definition: util.c:792
encoding.h
numberof
#define numberof(array)
Definition: etc.c:595
dump_parsetree_with_comment
Definition: ruby.c:81
rb_str_resize
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2025
ruby_set_script_name
void ruby_set_script_name(VALUE name)
Sets the current script name to this value.
Definition: ruby.c:1855
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1880
cmdline_options
Definition: ruby.c:86
load_file_arg::fname
VALUE fname
Definition: ruby.c:1578
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:841
rb_define_module_function
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1670
rb_io_fdopen
VALUE rb_io_fdopen(int, int, const char *)
Definition: io.c:7219
RARRAY_CONST_PTR
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:886
rb_reg_new
VALUE rb_reg_new(const char *, long, int)
Definition: re.c:2542
DUMP_BIT
#define DUMP_BIT(bit)
Definition: ruby.c:71
errno
int errno
util.h
TRUE
#define TRUE
Definition: nkf.h:175
rb_io_gets
VALUE rb_io_gets(VALUE)
Definition: io.c:3187
cmdline_options::do_print
int do_print
Definition: ruby.c:88
cmdline_options::intern
struct cmdline_options::@150 intern
Init_prelude
void Init_prelude(void)
rb_w32_sysinit
void rb_w32_sysinit(int *argc, char ***argv)
Definition: win32.c:749
rb_fatal
void rb_fatal(const char *fmt,...)
Definition: error.c:1911
ruby_prog_init
void ruby_prog_init(void)
Defines built-in variables.
Definition: ruby.c:1913
rb_progname
#define rb_progname
Definition: ruby.c:1210
rubylib_mangled_path
#define rubylib_mangled_path
Definition: ruby.c:251
strdup
#define strdup(s)
Definition: util.h:67
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1133
S_ISDIR
#define S_ISDIR(m)
Definition: dir.c:1289
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
dump_yydebug
Definition: ruby.c:78
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:137
internal.h
ID
unsigned long ID
Definition: ruby.h:89
PATH_ENV
#define PATH_ENV
Definition: defines.h:280
Qnil
#define Qnil
Definition: ruby.h:427
rb_const_set
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2160
rb_argv0
VALUE rb_argv0
Definition: ruby.c:1212
type
int type
Definition: tcltklib.c:112
rb_io_close
VALUE rb_io_close(VALUE)
Definition: io.c:4292
OBJ_TAINT
#define OBJ_TAINT(x)
Definition: ruby.h:1177
VALUE
unsigned long VALUE
Definition: ruby.h:88
geteuid
rb_uid_t geteuid(void)
Definition: win32.c:2501
set_encoding_part
#define set_encoding_part(type)
rb_locale_encoding
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1294
rb_funcall2
#define rb_funcall2
Definition: ruby.h:1456
strchr
char * strchr(char *, char)
getenv
char * getenv()
opt_enc_index
static int opt_enc_index(VALUE enc_name)
Definition: ruby.c:1196
cmdline_options::script_name
VALUE script_name
Definition: ruby.c:97
NAME_MATCH_P
#define NAME_MATCH_P(name, str, len)
Definition: ruby.c:701
rb_enc_dummy_p
static int rb_enc_dummy_p(rb_encoding *enc)
Definition: encoding.h:245
strlcpy
RUBY_EXTERN size_t strlcpy(char *, const char *, size_t)
Definition: strlcpy.c:44
cmdline_options::script
const char * script
Definition: ruby.c:96
rb_ensure
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:839
rb_str_new_cstr
VALUE rb_str_new_cstr(const char *)
Definition: string.c:560
cmdline_options::verbose
int verbose
Definition: ruby.c:92
uscore_get
static VALUE uscore_get(void)
Definition: ruby.c:1230
rb_rs
RUBY_EXTERN VALUE rb_rs
Definition: intern.h:518
cmdline_options::index
int index
Definition: ruby.c:102
rb_io_ungetbyte
VALUE rb_io_ungetbyte(VALUE, VALUE)
Definition: io.c:3833
ruby_gc_set_params
void ruby_gc_set_params(int safe_level)
Definition: gc.c:5733
rb_str_dup
VALUE rb_str_dup(VALUE)
Definition: string.c:1062
forbid_setid
#define forbid_setid(s)
Definition: ruby.c:128
cmdline_options_init
static struct cmdline_options * cmdline_options_init(struct cmdline_options *opt)
Definition: ruby.c:113
rb_define_hooked_variable
void rb_define_hooked_variable(const char *, VALUE *, VALUE(*)(ANYARGS), void(*)(ANYARGS))
Definition: variable.c:585
FL_UNSET
#define FL_UNSET(x, f)
Definition: ruby.h:1173
dump_version
Definition: ruby.c:73
proc_setproctitle
static VALUE proc_setproctitle(VALUE process, VALUE title)
Definition: ruby.c:1818
rb_str_freeze
VALUE rb_str_freeze(VALUE)
Definition: string.c:1968
opt_W_getter
static VALUE opt_W_getter(ID id, void *data)
Definition: ruby.c:1896
StringValueCStr
#define StringValueCStr(v)
Definition: ruby.h:541
identical_path
static VALUE identical_path(VALUE path)
Definition: ruby.c:325
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:845
RARRAY_ASET
#define RARRAY_ASET(a, i, v)
Definition: ruby.h:902
set_source_encoding_once
#define set_source_encoding_once(opt, e, elen)
Definition: ruby.c:779
rb_funcall_passing_block
VALUE rb_funcall_passing_block(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:827
f
#define f
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:231
SET_WHEN_DISABLE
#define SET_WHEN_DISABLE(bit)
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: ruby.h:901
ruby_process_options
void * ruby_process_options(int argc, char **argv)
Definition: ruby.c:1960
ruby_script
void ruby_script(const char *name)
Sets the current script name to this value.
Definition: ruby.c:1842
rb_file_expand_path
VALUE rb_file_expand_path(VALUE fname, VALUE dname)
Definition: file.c:3421
PATH_SEP_CHAR
#define PATH_SEP_CHAR
Definition: defines.h:278
cmdline_options::enc
struct cmdline_options::@150::@151 enc
ruby_initial_load_paths
const char ruby_initial_load_paths[]
Definition: loadpath.c:62
rb_f_gsub
static VALUE rb_f_gsub(int argc, VALUE *argv)
Definition: ruby.c:1272
ruby_init_loadpath
void ruby_init_loadpath(void)
Definition: ruby.c:380
rb_enc_set_default_internal
void rb_enc_set_default_internal(VALUE encoding)
Definition: encoding.c:1488
rb_gvar_readonly_setter
void rb_gvar_readonly_setter(VALUE val, ID id, void *data, struct rb_global_variable *gvar)
strerror
RUBY_EXTERN char * strerror(int)
Definition: strerror.c:11
ruby_set_argv
void ruby_set_argv(int argc, char **argv)
Definition: ruby.c:1939
RTEST
#define RTEST(v)
Definition: ruby.h:437
T_STRING
#define T_STRING
Definition: ruby.h:482
list
struct rb_encoding_entry * list
Definition: encoding.c:47
rb_define_variable
void rb_define_variable(const char *, VALUE *)
Definition: variable.c:604
rb_binding_t
Definition: vm_core.h:726
set_internal_encoding_once
#define set_internal_encoding_once(opt, e, elen)
Definition: ruby.c:775
ISPRINT
#define ISPRINT(c)
Definition: ruby.h:1768
cmdline_options::safe_level
int safe_level
Definition: ruby.c:93
cmdline_options::dump
unsigned int dump
Definition: ruby.c:95
src_encoding_index
#define src_encoding_index
Definition: ruby.c:110
rb_vm_set_progname
void rb_vm_set_progname(VALUE filename)
Definition: vm.c:2779
rb_parser_new
VALUE rb_parser_new(void)
Definition: ripper.c:17590
rb_realpath_internal
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict)
Definition: file.c:3620
rb_enc_path_last_separator
char * rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc)
Definition: file.c:2894
ruby_debug
#define ruby_debug
Definition: ruby.h:1476
name
const char * name
Definition: nkf.c:208
cmdline_options::src
struct cmdline_options::@150 src
StringValuePtr
#define StringValuePtr(v)
Definition: ruby.h:540
ruby_sysinit
void ruby_sysinit(int *argc, char ***argv)
Initializes the process for ruby(1).
Definition: ruby.c:2018
rb_load_file_str
void * rb_load_file_str(VALUE fname_v)
Definition: ruby.c:1776
dup2
RUBY_EXTERN int dup2(int, int)
Definition: dup2.c:27
cmdline_options::e_script
VALUE e_script
Definition: ruby.c:98
cmdline_options::do_loop
int do_loop
Definition: ruby.c:88
rb_str_new_frozen
VALUE rb_str_new_frozen(VALUE)
Definition: string.c:833
STRCASECMP
#define STRCASECMP(s1, s2)
Definition: ruby.h:1783
rb_parser_compile_string_path
NODE * rb_parser_compile_string_path(volatile VALUE vparser, VALUE f, VALUE s, int line)
Definition: ripper.c:12080
load_file_arg
Definition: ruby.c:1576
rb_ascii8bit_encoding
rb_encoding * rb_ascii8bit_encoding(void)
Definition: encoding.c:1227
getgid
rb_gid_t getgid(void)
Definition: win32.c:2508
rb_warning
void rb_warning(const char *fmt,...)
Definition: error.c:236
rb_enc_find_index
int rb_enc_find_index(const char *name)
Definition: encoding.c:684
RSTRING_LENINT
#define RSTRING_LENINT(str)
Definition: ruby.h:853
cmdline_options::ext
struct cmdline_options::@150 ext
CONST_ID
#define CONST_ID(var, str)
Definition: ruby.h:1428
load_file_arg::parser
VALUE parser
Definition: ruby.c:1577
rb_stdout
RUBY_EXTERN VALUE rb_stdout
Definition: ruby.h:1627
version
static void version(void)
Definition: nkf.c:898
rb_intern_const
#define rb_intern_const(str)
Definition: ruby.h:1442
rb_obj_freeze
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1076
cmdline_options::do_line
int do_line
Definition: ruby.c:89
true_value
static VALUE true_value(void)
Definition: ruby.c:1221
rb_f_chomp
static VALUE rb_f_chomp(int argc, VALUE *argv)
Definition: ruby.c:1310
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:746
rb_stdio_set_default_encoding
void rb_stdio_set_default_encoding(void)
Definition: io.c:10558
rb_intern
#define rb_intern(str)
fstat
#define fstat(fd, st)
Definition: win32.h:214
stat
#define stat(path, st)
Definition: win32.h:213
mod
#define mod(x, y)
Definition: date_strftime.c:28
rb_const_remove
VALUE rb_const_remove(VALUE, ID)
Definition: variable.c:1942
env
#define env
NULL
#define NULL
Definition: _sdbm.c:103
Qundef
#define Qundef
Definition: ruby.h:428
rb_define_readonly_boolean
#define rb_define_readonly_boolean(name, val)
Definition: ruby.c:1226
locale_path
static VALUE locale_path(VALUE path)
Definition: ruby.c:330
setproctitle
RUBY_EXTERN void setproctitle(const char *fmt,...)
Definition: setproctitle.c:135
process_options
static VALUE process_options(int argc, char **argv, struct cmdline_options *opt)
Definition: ruby.c:1321
GET_THREAD
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:927
ruby_verbose
#define ruby_verbose
Definition: ruby.h:1475
rb_iseq_disasm
VALUE rb_iseq_disasm(VALUE self)
Definition: iseq.c:1378
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:223
rb_parser_compile_file_path
NODE * rb_parser_compile_file_path(volatile VALUE vparser, VALUE fname, VALUE file, int start)
Definition: ripper.c:12121
O_BINARY
#define O_BINARY
Definition: _sdbm.c:89
strrchr
char * strrchr(const char *, const char)
argv
char ** argv
Definition: ruby.c:132
ISSPACE
#define ISSPACE(c)
Definition: ruby.h:1770
StringValue
#define StringValue(v)
Definition: ruby.h:539
rb_thread_struct::parse_in_eval
int parse_in_eval
Thread-local state of evaluation context.
Definition: vm_core.h:602
dump_flag_count
Definition: ruby.c:83
rb_enc_from_index
rb_encoding * rb_enc_from_index(int index)
Definition: encoding.c:590
proc_options
static long proc_options(long argc, char **argv, struct cmdline_options *opt, int envopt)
Definition: ruby.c:783
OnigEncodingTypeST
Definition: oniguruma.h:158
rb_load_file
void * rb_load_file(const char *fname)
Definition: ruby.c:1769
dump_parsetree
Definition: ruby.c:80
rb_str_new
VALUE rb_str_new(const char *, long)
Definition: string.c:534
set_arg0
static void set_arg0(VALUE val, ID id)
Definition: ruby.c:1828
cmdline_options::disable
unsigned int disable
Definition: ruby.c:91
rb_env_t
Definition: vm_core.h:713
GET_VM
#define GET_VM()
Definition: vm_core.h:920