Ruby  2.1.10p492(2016-04-01revision54464)
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)
69 };
70 
71 #define DUMP_BIT(bit) (1U << dump_##bit)
84 };
85 
87  int sflag, xflag;
90  int do_search;
91  unsigned int disable;
92  int verbose;
94  unsigned int setids;
95  unsigned int dump;
96  const char *script;
99  struct {
100  struct {
102  int index;
103  } enc;
104  } src, ext, intern;
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 *
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
326 {
327  return path;
328 }
329 static VALUE
331 {
333  return path;
334 }
335 
336 void
337 ruby_incpush(const char *path)
338 {
340 }
341 
342 static VALUE
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 {
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
381 {
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;
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
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 
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();
547  }
548  feature = rb_str_new2(mod);
549  RBASIC_CLEAR_CLASS(feature);
550  rb_ary_push(list, feature);
551 }
552 
553 static void
555 {
556  VALUE list = *req_list;
557  VALUE self = rb_vm_top_self();
558  ID require;
559  rb_thread_t *th = GET_THREAD();
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);
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];
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--) {
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)
747  SET_WHEN_DUMP(copyright);
749  SET_WHEN_DUMP(help);
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)) {
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;
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':
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:
856  break;
857  default:
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;
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)
1020  else if (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)) {
1080  }
1081  else if (is_option_with_arg("disable", Qtrue, Qtrue)) {
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 {
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)) {
1154  "invalid option -%c (-h will show valid options)",
1155  (int)(unsigned char)*s);
1156  }
1157  else {
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
1190 {
1191  Init_prelude();
1192  rb_const_remove(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"));
1193 }
1194 
1195 static int
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)
1213 
1214 static VALUE
1216 {
1217  return Qfalse;
1218 }
1219 
1220 static VALUE
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
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
1254 {
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
1273 {
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
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
1311 {
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))) {
1362  if (opt->dump & DUMP_BIT(version)) return Qtrue;
1363  }
1364  if (opt->dump & DUMP_BIT(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 
1410  Init_enc();
1411  rb_enc_find_index("encdb");
1412  lenc = rb_locale_encoding();
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  }
1436  if (opt->intern.enc.index >= 0) {
1437  enc = rb_enc_from_index(opt->intern.enc.index);
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  }
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);
1481  require_libraries(&opt->req_list);
1482  ruby_set_script_name(progname);
1483 
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 
1494  tree = load_file(parser, opt->script_name, 1, opt);
1495  });
1496  }
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  }
1507  if (opt->intern.enc.index >= 0) {
1508  /* Set in the shebang line */
1509  enc = rb_enc_from_index(opt->intern.enc.index);
1511  }
1512  else if (!rb_default_internal_encoding())
1513  /* Freeze default_internal */
1516 
1517  if (!tree) return Qfalse;
1518 
1519  process_sflag(&opt->sflag);
1520  opt->xflag = 0;
1521 
1522  if (opt->safe_level >= 4) {
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) {
1534  tree = rb_parser_append_print(parser, tree);
1535  });
1536  }
1537  if (opt->do_loop) {
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);
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)));
1550  return Qtrue;
1551  }
1552 
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)) {
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 
1572 
1573  return iseq;
1574 }
1575 
1579  int script;
1581 };
1582 
1583 static VALUE
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  }
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
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 *
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
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
1819 {
1820  StringValue(title);
1821 
1822  setproctitle("%.*s", RSTRING_LENINT(title), RSTRING_PTR(title));
1823 
1824  return title;
1825 }
1826 
1827 static void
1829 {
1830  if (origarg.argv == 0)
1831  rb_raise(rb_eRuntimeError, "$0 not initialized");
1832 
1834 }
1835 
1841 void
1842 ruby_script(const char *name)
1843 {
1844  if (name) {
1847  }
1848 }
1849 
1854 void
1856 {
1859 }
1860 
1861 static void
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
1914 {
1919  rb_define_variable("$DEBUG", &ruby_debug);
1920  rb_define_variable("$-d", &ruby_debug);
1921 
1923  rb_define_hooked_variable("$PROGRAM_NAME", &rb_progname, 0, set_arg0);
1924 
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  */
1936 }
1937 
1938 void
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++) {
1953 
1954  OBJ_FREEZE(arg);
1955  rb_ary_push(av, arg);
1956  }
1957 }
1958 
1959 void *
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 */
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
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);
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
2030 }
#define STRNCASECMP(s1, s2, n)
Definition: ruby.h:1792
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1591
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2239
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:609
static void ruby_init_prelude(void)
Definition: ruby.c:1189
static void process_sflag(int *sflag)
Definition: ruby.c:589
RUBY_EXTERN VALUE rb_mProcess
Definition: ruby.h:1556
VALUE rb_parser_dump_tree(NODE *node, int comment)
Definition: node.c:894
static VALUE load_file_internal(VALUE arg)
Definition: ruby.c:1584
#define rb_str_new4
Definition: intern.h:842
int sflag
Definition: ruby.c:87
#define yydebug
Definition: ripper.c:405
static VALUE restore_lineno(VALUE lineno)
Definition: ruby.c:1752
#define RARRAY_LEN(a)
Definition: ruby.h:878
#define FALSE
Definition: nkf.h:174
disable_flag_bits
Definition: ruby.c:65
static void dump_option(const char *str, int len, void *arg)
Definition: ruby.c:743
size_t strlen(const char *)
#define CharNext(p)
Definition: eval_intern.h:254
void rb_update_max_fd(int fd)
Definition: io.c:183
#define scan_oct(s, l, e)
Definition: util.h:50
#define rb_orig_progname
Definition: ruby.c:1211
rb_uid_t getuid(void)
Definition: win32.c:2498
int script
Definition: ruby.c:1579
static VALUE proc_argv0(VALUE process)
Definition: ruby.c:1795
void ruby_set_inplace_mode(const char *)
Definition: io.c:11636
#define MAXPATHLEN
Definition: ruby.c:46
#define FL_TAINT
Definition: ruby.h:1137
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Definition: io.c:228
VALUE rb_str_cat(VALUE, const char *, long)
Definition: string.c:2139
void ruby_each_words(const char *, void(*)(const char *, int, void *), void *)
Definition: util.c:3771
#define Qtrue
Definition: ruby.h:426
void ruby_show_version(void)
Prints the version information of the CRuby interpreter to stdout.
Definition: version.c:78
VALUE rb_io_flush(VALUE)
Definition: io.c:1510
VALUE rb_ary_shift(VALUE ary)
Definition: array.c:995
VALUE rb_parser_end_seen_p(VALUE vparser)
Definition: ripper.c:17645
VALUE req_list
Definition: ruby.c:105
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:102
static VALUE rb_f_chop(void)
Definition: ruby.c:1290
static struct @149 origarg
VALUE rb_eTypeError
Definition: error.c:548
RUBY_EXTERN VALUE rb_stdin
Definition: ruby.h:1635
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_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1451
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:900
int xflag
Definition: ruby.c:87
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:781
static void add_modules(VALUE *req_list, const char *mod)
Definition: ruby.c:539
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
void ruby_init_loadpath_safe(int safe_level)
Definition: ruby.c:386
static VALUE expand_include_path(VALUE path)
Definition: ruby.c:343
void rb_enc_set_default_external(VALUE encoding)
Definition: encoding.c:1418
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:826
VALUE rb_io_getbyte(VALUE)
Definition: io.c:3795
VALUE rb_ary_clear(VALUE ary)
Definition: array.c:3392
dump_flag_bits
Definition: ruby.c:72
#define dln_find_file_r
Definition: win32.c:76
static void require_libraries(VALUE *req_list)
Definition: ruby.c:554
#define SHOW(m)
#define M(shortopt, longopt, desc)
void Init_ext(void)
Definition: dmyext.c:2
static void disable_option(const char *str, int len, void *arg)
Definition: ruby.c:730
VALUE rb_eSecurityError
Definition: error.c:557
Definition: ruby.c:77
const char ruby_exec_prefix[]
Definition: loadpath.c:59
#define PREPARE_PARSE_MAIN(expr)
#define GetEnvPtr(obj, ptr)
Definition: vm_core.h:710
static void verbose_setter(VALUE val, ID id, void *data)
Definition: ruby.c:1889
VALUE rb_io_write(VALUE, VALUE)
Definition: io.c:1427
int do_split
Definition: ruby.c:89
static VALUE rb_f_sub(int argc, VALUE *argv)
Definition: ruby.c:1253
void Init_enc(void)
Definition: dmyext.c:7
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1675
VALUE env
Definition: vm_core.h:727
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1257
void rb_load_fail(VALUE path, const char *err)
Definition: error.c:2068
#define rb_argv
Definition: intern.h:681
VALUE rb_str_tmp_new(long)
Definition: string.c:919
void rb_loaderror(const char *fmt,...)
Definition: error.c:1879
Definition: ruby.h:957
struct cmdline_options * opt
Definition: ruby.c:1580
static void set_option_encoding_once(const char *type, VALUE *name, const char *e, long elen)
Definition: ruby.c:760
VALUE rb_external_str_new_cstr(const char *)
Definition: string.c:713
VALUE rb_parser_set_yydebug(VALUE self, VALUE flag)
Definition: ripper.c:17690
const char * rb_obj_classname(VALUE)
Definition: variable.c:406
static rb_env_t * toplevel_context(VALUE toplevel_binding)
Definition: ruby.c:578
VALUE rb_gv_set(const char *, VALUE)
Definition: variable.c:810
void ruby_incpush(const char *path)
Definition: ruby.c:337
Definition: node.h:239
void rb_exc_raise(VALUE mesg)
Definition: eval.c:567
#define RBASIC_SET_CLASS_RAW(obj, cls)
Definition: internal.h:610
rb_gid_t getegid(void)
Definition: win32.c:2519
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1672
VALUE rb_eNameError
Definition: error.c:553
#define set_external_encoding_once(opt, e, elen)
Definition: ruby.c:777
static void fill_standard_fds(void)
Definition: ruby.c:1982
VALUE rb_gv_get(const char *)
Definition: variable.c:819
#define MEMZERO(p, type, n)
Definition: ruby.h:1359
void rb_set_safe_level(int)
Definition: safe.c:49
static void enable_option(const char *str, int len, void *arg)
Definition: ruby.c:717
VALUE rb_lastline_get(void)
Definition: vm.c:959
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1366
RUBY_EXTERN VALUE rb_output_rs
Definition: intern.h:520
#define PREFIX_PATH()
static NODE * load_file(VALUE, VALUE, int, struct cmdline_options *)
Definition: ruby.c:1758
NODE * rb_parser_compile_string(volatile VALUE vparser, const char *f, VALUE s, int line)
Definition: ripper.c:12094
static void push_include(const char *path, VALUE(*filter)(VALUE))
Definition: ruby.c:255
VALUE rb_iseq_new_main(NODE *node, VALUE path, VALUE absolute_path)
Definition: iseq.c:428
VALUE rb_class_new_instance(int, VALUE *, VALUE)
Definition: object.c:1857
VALUE rb_str_encode_ospath(VALUE path)
Definition: file.c:232
#define is_option_with_arg(name, allow_hyphen, allow_envopt)
#define val
static void init_ids(struct cmdline_options *)
Definition: ruby.c:1862
#define RUBY_RELATIVE(path, len)
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1561
VALUE rb_eRuntimeError
Definition: error.c:547
IUnknown DWORD
Definition: win32ole.c:149
VALUE rb_parser_encoding(VALUE vparser)
Definition: ripper.c:17660
#define SET_WHEN_DUMP(bit)
RUBY_EXTERN char * strstr(const char *, const char *)
Definition: strstr.c:8
#define GetBindingPtr(obj, ptr)
Definition: vm_core.h:723
static VALUE false_value(void)
Definition: ruby.c:1215
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:2158
VALUE rb_ary_new(void)
Definition: array.c:499
static void moreswitches(const char *s, struct cmdline_options *opt, int envopt)
Definition: ruby.c:655
VALUE name
Definition: ruby.c:101
unsigned int setids
Definition: ruby.c:94
static void usage(const char *name, int help)
Definition: ruby.c:136
#define NIL_P(v)
Definition: ruby.h:438
static VALUE enc_name(VALUE self)
Definition: encoding.c:1079
int do_search
Definition: ruby.c:90
void ruby_incpush_expand(const char *path)
Definition: ruby.c:354
#define UNSET_WHEN_DISABLE(bit)
#define DEFAULT_RUBYGEMS_ENABLED
Definition: ruby.c:61
#define ISALNUM(c)
Definition: ruby.h:1781
void rb_lastline_set(VALUE)
Definition: vm.c:965
#define DISABLE_BIT(bit)
Definition: ruby.c:64
NODE * rb_parser_append_print(VALUE vparser, NODE *node)
Definition: ripper.c:16653
int argc
Definition: ruby.c:131
#define Qfalse
Definition: ruby.h:425
VALUE rb_external_str_new(const char *, long)
Definition: string.c:707
RUBY_EXTERN VALUE rb_fs
Definition: intern.h:516
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:4923
#define rb_str_new2
Definition: intern.h:840
#define OBJ_FREEZE(x)
Definition: ruby.h:1194
void ruby_push_include(const char *path, VALUE(*filter)(VALUE))
Definition: ruby.c:317
VALUE rb_vm_top_self()
Definition: vm.c:2834
NODE * rb_parser_while_loop(VALUE vparser, NODE *node, int chop, int split)
Definition: ripper.c:16685
Definition: util.c:796
#define numberof(array)
Definition: etc.c:602
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2024
void ruby_set_script_name(VALUE name)
Sets the current script name to this value.
Definition: ruby.c:1855
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1880
VALUE fname
Definition: ruby.c:1578
#define RSTRING_LEN(str)
Definition: ruby.h:841
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1661
VALUE rb_io_fdopen(int, int, const char *)
Definition: io.c:7247
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:886
VALUE rb_reg_new(const char *, long, int)
Definition: re.c:2574
#define DUMP_BIT(bit)
Definition: ruby.c:71
int errno
#define TRUE
Definition: nkf.h:175
VALUE rb_io_gets(VALUE)
Definition: io.c:3189
int do_print
Definition: ruby.c:88
struct cmdline_options::@150 intern
void Init_prelude(void)
void rb_w32_sysinit(int *argc, char ***argv)
Definition: win32.c:753
void rb_fatal(const char *fmt,...)
Definition: error.c:1911
void ruby_prog_init(void)
Defines built-in variables.
Definition: ruby.c:1913
#define rb_progname
Definition: ruby.c:1210
#define rubylib_mangled_path
Definition: ruby.c:251
#define strdup(s)
Definition: util.h:67
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1133
#define S_ISDIR(m)
Definition: dir.c:1289
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
#define PRIsVALUE
Definition: ruby.h:137
unsigned long ID
Definition: ruby.h:89
#define PATH_ENV
Definition: defines.h:280
#define Qnil
Definition: ruby.h:427
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2163
VALUE rb_argv0
Definition: ruby.c:1212
int type
Definition: tcltklib.c:112
VALUE rb_io_close(VALUE)
Definition: io.c:4315
#define OBJ_TAINT(x)
Definition: ruby.h:1184
unsigned long VALUE
Definition: ruby.h:88
rb_uid_t geteuid(void)
Definition: win32.c:2505
#define set_encoding_part(type)
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1309
#define rb_funcall2
Definition: ruby.h:1464
char * strchr(char *, char)
char * getenv()
static int opt_enc_index(VALUE enc_name)
Definition: ruby.c:1196
VALUE script_name
Definition: ruby.c:97
#define NAME_MATCH_P(name, str, len)
Definition: ruby.c:701
static int rb_enc_dummy_p(rb_encoding *enc)
Definition: encoding.h:245
RUBY_EXTERN size_t strlcpy(char *, const char *, size_t)
Definition: strlcpy.c:44
const char * script
Definition: ruby.c:96
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:839
VALUE rb_str_new_cstr(const char *)
Definition: string.c:560
int verbose
Definition: ruby.c:92
static VALUE uscore_get(void)
Definition: ruby.c:1230
RUBY_EXTERN VALUE rb_rs
Definition: intern.h:518
VALUE rb_io_ungetbyte(VALUE, VALUE)
Definition: io.c:3856
void ruby_gc_set_params(int safe_level)
Definition: gc.c:5736
VALUE rb_str_dup(VALUE)
Definition: string.c:1062
#define forbid_setid(s)
Definition: ruby.c:128
static struct cmdline_options * cmdline_options_init(struct cmdline_options *opt)
Definition: ruby.c:113
void rb_define_hooked_variable(const char *, VALUE *, VALUE(*)(ANYARGS), void(*)(ANYARGS))
Definition: variable.c:585
#define FL_UNSET(x, f)
Definition: ruby.h:1177
static VALUE proc_setproctitle(VALUE process, VALUE title)
Definition: ruby.c:1818
VALUE rb_str_freeze(VALUE)
Definition: string.c:1967
static VALUE opt_W_getter(ID id, void *data)
Definition: ruby.c:1896
#define StringValueCStr(v)
Definition: ruby.h:541
static VALUE identical_path(VALUE path)
Definition: ruby.c:325
#define RSTRING_PTR(str)
Definition: ruby.h:845
#define RARRAY_ASET(a, i, v)
Definition: ruby.h:902
#define set_source_encoding_once(opt, e, elen)
Definition: ruby.c:779
VALUE rb_funcall_passing_block(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:833
#define f
#define INT2FIX(i)
Definition: ruby.h:231
#define SET_WHEN_DISABLE(bit)
#define RARRAY_AREF(a, i)
Definition: ruby.h:901
void * ruby_process_options(int argc, char **argv)
Definition: ruby.c:1960
void ruby_script(const char *name)
Sets the current script name to this value.
Definition: ruby.c:1842
VALUE rb_file_expand_path(VALUE fname, VALUE dname)
Definition: file.c:3422
#define PATH_SEP_CHAR
Definition: defines.h:278
struct cmdline_options::@150::@151 enc
const char ruby_initial_load_paths[]
Definition: loadpath.c:62
static VALUE rb_f_gsub(int argc, VALUE *argv)
Definition: ruby.c:1272
void ruby_init_loadpath(void)
Definition: ruby.c:380
void rb_enc_set_default_internal(VALUE encoding)
Definition: encoding.c:1503
void rb_gvar_readonly_setter(VALUE val, ID id, void *data, struct rb_global_variable *gvar)
RUBY_EXTERN char * strerror(int)
Definition: strerror.c:11
void ruby_set_argv(int argc, char **argv)
Definition: ruby.c:1939
#define RTEST(v)
Definition: ruby.h:437
#define T_STRING
Definition: ruby.h:482
struct rb_encoding_entry * list
Definition: encoding.c:47
void rb_define_variable(const char *, VALUE *)
Definition: variable.c:604
#define set_internal_encoding_once(opt, e, elen)
Definition: ruby.c:775
#define ISPRINT(c)
Definition: ruby.h:1776
int safe_level
Definition: ruby.c:93
unsigned int dump
Definition: ruby.c:95
#define src_encoding_index
Definition: ruby.c:110
void rb_vm_set_progname(VALUE filename)
Definition: vm.c:2787
VALUE rb_parser_new(void)
Definition: ripper.c:17631
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict)
Definition: file.c:3621
char * rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc)
Definition: file.c:2895
#define ruby_debug
Definition: ruby.h:1484
const char * name
Definition: nkf.c:208
struct cmdline_options::@150 src
#define StringValuePtr(v)
Definition: ruby.h:540
void ruby_sysinit(int *argc, char ***argv)
Initializes the process for ruby(1).
Definition: ruby.c:2018
void * rb_load_file_str(VALUE fname_v)
Definition: ruby.c:1776
RUBY_EXTERN int dup2(int, int)
Definition: dup2.c:27
VALUE e_script
Definition: ruby.c:98
int do_loop
Definition: ruby.c:88
VALUE rb_str_new_frozen(VALUE)
Definition: string.c:833
#define STRCASECMP(s1, s2)
Definition: ruby.h:1791
NODE * rb_parser_compile_string_path(volatile VALUE vparser, VALUE f, VALUE s, int line)
Definition: ripper.c:12100
rb_encoding * rb_ascii8bit_encoding(void)
Definition: encoding.c:1242
rb_gid_t getgid(void)
Definition: win32.c:2512
void rb_warning(const char *fmt,...)
Definition: error.c:236
int rb_enc_find_index(const char *name)
Definition: encoding.c:684
#define RSTRING_LENINT(str)
Definition: ruby.h:853
struct cmdline_options::@150 ext
#define CONST_ID(var, str)
Definition: ruby.h:1436
VALUE parser
Definition: ruby.c:1577
RUBY_EXTERN VALUE rb_stdout
Definition: ruby.h:1635
static void version(void)
Definition: nkf.c:898
#define rb_intern_const(str)
Definition: ruby.h:1450
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1070
int do_line
Definition: ruby.c:89
static VALUE true_value(void)
Definition: ruby.c:1221
static VALUE rb_f_chomp(int argc, VALUE *argv)
Definition: ruby.c:1310
VALUE rb_define_module(const char *name)
Definition: class.c:727
void rb_stdio_set_default_encoding(void)
Definition: io.c:10586
#define rb_intern(str)
#define fstat(fd, st)
Definition: win32.h:214
#define stat(path, st)
Definition: win32.h:213
#define mod(x, y)
Definition: date_strftime.c:28
VALUE rb_const_remove(VALUE, ID)
Definition: variable.c:1942
#define env
#define NULL
Definition: _sdbm.c:102
#define Qundef
Definition: ruby.h:428
#define rb_define_readonly_boolean(name, val)
Definition: ruby.c:1226
static VALUE locale_path(VALUE path)
Definition: ruby.c:330
RUBY_EXTERN void setproctitle(const char *fmt,...)
Definition: setproctitle.c:135
static VALUE process_options(int argc, char **argv, struct cmdline_options *opt)
Definition: ruby.c:1321
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:929
#define ruby_verbose
Definition: ruby.h:1483
VALUE rb_iseq_disasm(VALUE self)
Definition: iseq.c:1379
void rb_warn(const char *fmt,...)
Definition: error.c:223
NODE * rb_parser_compile_file_path(volatile VALUE vparser, VALUE fname, VALUE file, int start)
Definition: ripper.c:12141
#define O_BINARY
Definition: _sdbm.c:88
char * strrchr(const char *, const char)
char ** argv
Definition: ruby.c:132
#define ISSPACE(c)
Definition: ruby.h:1778
#define StringValue(v)
Definition: ruby.h:539
int parse_in_eval
Thread-local state of evaluation context.
Definition: vm_core.h:602
rb_encoding * rb_enc_from_index(int index)
Definition: encoding.c:590
static long proc_options(long argc, char **argv, struct cmdline_options *opt, int envopt)
Definition: ruby.c:783
void * rb_load_file(const char *fname)
Definition: ruby.c:1769
VALUE rb_str_new(const char *, long)
Definition: string.c:534
static void set_arg0(VALUE val, ID id)
Definition: ruby.c:1828
unsigned int disable
Definition: ruby.c:91
#define GET_VM()
Definition: vm_core.h:922