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