From 552badf29f3aa5e9b76d02716f93f37b94ba8353 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 23 Feb 2008 21:13:05 +0000 Subject: [PATCH] * debug.c (ruby_set_debug_option): separated ruby_each_words(). * util.c (ruby_each_words): extracted from ruby_set_debug_option(). * ruby.c (proc_options): generalized enable/disable options. * ruby.c (ruby_init_gems): take enabled flag. [ruby-core:14840] * ruby.c (process_options): added --disable-rubyopt flag. * include/ruby/util.h (ruby_each_words): prototype. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15590 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 14 ++++++++++ debug.c | 37 ++++++++++++-------------- include/ruby/util.h | 2 ++ ruby.c | 64 +++++++++++++++++++++++++++++++++++++++------ util.c | 18 +++++++++++++ 5 files changed, 106 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index b29f7d3d0b..beac994ff8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Sun Feb 24 06:13:02 2008 Nobuyoshi Nakada + + * debug.c (ruby_set_debug_option): separated ruby_each_words(). + + * util.c (ruby_each_words): extracted from ruby_set_debug_option(). + + * ruby.c (proc_options): generalized enable/disable options. + + * ruby.c (ruby_init_gems): take enabled flag. [ruby-core:14840] + + * ruby.c (process_options): added --disable-rubyopt flag. + + * include/ruby/util.h (ruby_each_words): prototype. + Sun Feb 24 05:25:26 2008 Nobuyoshi Nakada * ruby.c (proc_options): check if argument for -E exists. diff --git a/debug.c b/debug.c index 7ca9e3aae5..be90698b82 100644 --- a/debug.c +++ b/debug.c @@ -11,6 +11,7 @@ #include "ruby/ruby.h" #include "ruby/encoding.h" +#include "ruby/util.h" #include "debug.h" #include "vm_core.h" @@ -121,31 +122,25 @@ ruby_debug_breakpoint(void) } #ifdef RUBY_DEBUG_ENV -#include - -void -ruby_set_debug_option(const char *str) +static void +set_debug_option(const char *str, int len, void *arg) { - const char *end; - int len; - - if (!str) return; - for (; *str; str = end) { - while (ISSPACE(*str) || *str == ',') str++; - if (!*str) break; - end = str; - while (*end && !ISSPACE(*end) && *end != ',') end++; - len = end - str; -#define SET_WHEN(name, var) \ +#define SET_WHEN(name, var) do { \ if (len == sizeof(name) - 1 && \ strncmp(str, name, len) == 0) { \ extern int ruby_##var; \ ruby_##var = 1; \ - continue; \ - } - SET_WHEN("gc_stress", gc_stress); - SET_WHEN("core", enable_coredump); - fprintf(stderr, "unexpected debug option: %.*s\n", len, str); - } + return; \ + } \ + } while (0) + SET_WHEN("gc_stress", gc_stress); + SET_WHEN("core", enable_coredump); + fprintf(stderr, "unexpected debug option: %.*s\n", len, str); +} + +void +ruby_set_debug_option(const char *str) +{ + ruby_each_words(str, set_debug_option, 0); } #endif diff --git a/include/ruby/util.h b/include/ruby/util.h index ca9867a37b..e6e6c95ca4 100644 --- a/include/ruby/util.h +++ b/include/ruby/util.h @@ -70,6 +70,8 @@ double ruby_strtod(const char *, char **); #undef strtod #define strtod(s,e) ruby_strtod(s,e) +void ruby_each_words(const char *, void (*)(const char*, int, void*), void *); + #if defined(__cplusplus) #if 0 { /* satisfy cc-mode */ diff --git a/ruby.c b/ruby.c index 04a4fe413c..5975b308d4 100644 --- a/ruby.c +++ b/ruby.c @@ -65,6 +65,12 @@ VALUE rb_parser_set_yydebug(VALUE, VALUE); char *ruby_inplace_mode = 0; +#define DISABLE_BIT(bit) (1U << disable_##bit) +enum disable_flag_bits { + disable_gems, + disable_rubyopt, +}; + struct cmdline_options { int sflag, xflag; int do_loop, do_print; @@ -73,7 +79,7 @@ struct cmdline_options { int usage; int version; int copyright; - int disable_gems; + int disable; int verbose; int yydebug; char *script; @@ -132,7 +138,8 @@ usage(const char *name) "-w turn warnings on for your script", "-W[level] set warning level; 0=silence, 1=medium, 2=verbose (default)", "-x[directory] strip off text before #!ruby line and perhaps cd to directory", - "--disable-gems disable gem libraries", + "--enable/--disable-FEATURE --enable/--disable=FEATURE enable/disable FEATUREs", + " gems: gem libraries, rubyopt: RUBYOPT env", "--copyright print the copyright", "--version print the version", NULL @@ -536,6 +543,34 @@ moreswitches(const char *s, struct cmdline_options *opt) return (char *)s; } +#define UNSET_WHEN(bit) \ + if (len < sizeof(#bit) && strncmp(str, #bit, len) == 0) { \ + *(unsigned int *)arg &= ~DISABLE_BIT(bit); \ + return; \ + } + +#define SET_WHEN(bit) \ + if (len < sizeof(#bit) && strncmp(str, #bit, len) == 0) { \ + *(unsigned int *)arg |= DISABLE_BIT(bit); \ + return; \ + } + +static void +enable_option(const char *str, int len, void *arg) +{ + UNSET_WHEN(gems); + UNSET_WHEN(rubyopt); + rb_warn("unknown argument for --enable: `%.*s'", len, str); +} + +static void +disable_option(const char *str, int len, void *arg) +{ + SET_WHEN(gems); + SET_WHEN(rubyopt); + rb_warn("unknown argument for --disable: `%.*s'", len, str); +} + static int proc_options(int argc, char **argv, struct cmdline_options *opt) { @@ -792,8 +827,20 @@ proc_options(int argc, char **argv, struct cmdline_options *opt) ruby_debug = Qtrue; ruby_verbose = Qtrue; } - else if (strcmp("disable-gems", s) == 0) - opt->disable_gems = 1; + else if (strncmp("enable", s, n = 6) == 0 && + (!s[n] || s[n] == '-' || s[n] == '=')) { + if (!(s += n + 1)[-1] && (!--argc || !(s = *++argv))) { + rb_raise(rb_eRuntimeError, "missing argument for --enable"); + } + ruby_each_words(s, enable_option, &opt->disable); + } + else if (strncmp("disable", s, n = 7) == 0 && + (!s[n] || s[n] == '-' || s[n] == '=')) { + if (!(s += n + 1)[-1] && (!--argc || !(s = *++argv))) { + rb_raise(rb_eRuntimeError, "missing argument for --disable"); + } + ruby_each_words(s, disable_option, &opt->disable); + } else if (strncmp("encoding", s, n = 8) == 0 && (!s[n] || s[n] == '=')) { s += n; if (!*s++) { @@ -854,11 +901,11 @@ proc_options(int argc, char **argv, struct cmdline_options *opt) void Init_prelude(void); static void -ruby_init_gems(struct cmdline_options *opt) +ruby_init_gems(int enable) { VALUE gem; gem = rb_define_module("Gem"); - rb_const_set(gem, rb_intern("Enable"), opt->disable_gems ? Qfalse : Qtrue); + rb_const_set(gem, rb_intern("Enable"), enable ? Qtrue : Qfalse); Init_prelude(); } @@ -895,7 +942,8 @@ process_options(VALUE arg) argc -= i; argv += i; - if (rb_safe_level() == 0 && (s = getenv("RUBYOPT"))) { + if (!(opt->disable & DISABLE_BIT(rubyopt)) && + rb_safe_level() == 0 && (s = getenv("RUBYOPT"))) { VALUE src_enc_name = opt->src.enc.name; VALUE ext_enc_name = opt->ext.enc.name; @@ -990,7 +1038,7 @@ process_options(VALUE arg) process_sflag(opt); ruby_init_loadpath(); - ruby_init_gems(opt); + ruby_init_gems(!(opt->disable && DISABLE_BIT(gems))); parser = rb_parser_new(); if (opt->yydebug) rb_parser_set_yydebug(parser, Qtrue); if (opt->ext.enc.name != 0) { diff --git a/util.c b/util.c index 136ff1b213..fc8874c942 100644 --- a/util.c +++ b/util.c @@ -3936,6 +3936,24 @@ ret1: *rve = s; return s0; } + +void +ruby_each_words(const char *str, void (*func)(const char*, int, void*), void *arg) +{ + const char *end; + int len; + + if (!str) return; + for (; *str; str = end) { + while (ISSPACE(*str) || *str == ',') str++; + if (!*str) break; + end = str; + while (*end && !ISSPACE(*end) && *end != ',') end++; + len = end - str; + (*func)(str, len, arg); + } +} + #ifdef __cplusplus } #endif