diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
commit | be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97 (patch) | |
tree | 9754ff1ca740f6346cf8483ec915d4054bc5da2d /web/server/h2o/libh2o/deps/mruby-env | |
parent | Initial commit. (diff) | |
download | netdata-upstream.tar.xz netdata-upstream.zip |
Adding upstream version 1.44.3.upstream/1.44.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'web/server/h2o/libh2o/deps/mruby-env')
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/.gitignore | 1 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/.travis.yml | 2 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/README.md | 52 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/mrbgem.rake | 4 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/mrblib/env.rb | 16 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/run_test.rb | 27 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/src/env.c | 243 | ||||
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby-env/test/env_test.rb | 121 |
8 files changed, 466 insertions, 0 deletions
diff --git a/web/server/h2o/libh2o/deps/mruby-env/.gitignore b/web/server/h2o/libh2o/deps/mruby-env/.gitignore new file mode 100644 index 00000000..ceeb05b4 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/.gitignore @@ -0,0 +1 @@ +/tmp diff --git a/web/server/h2o/libh2o/deps/mruby-env/.travis.yml b/web/server/h2o/libh2o/deps/mruby-env/.travis.yml new file mode 100644 index 00000000..ffe22728 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/.travis.yml @@ -0,0 +1,2 @@ +script: + - "ruby run_test.rb all test" diff --git a/web/server/h2o/libh2o/deps/mruby-env/README.md b/web/server/h2o/libh2o/deps/mruby-env/README.md new file mode 100644 index 00000000..b6db2248 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/README.md @@ -0,0 +1,52 @@ +mruby-env +========= + +mruby-env provides "ENV" object, which is a hash-like accessor +for environment variables. + +### Methods + +``` +[] []= clear delete has_key? include? inspect key? keys +member? size store to_a to_hash to_s values +``` + + +### To build: + +Prerequisites: + + * mruby + * libc + + activate GEMs in *build_config.rb* + * conf.gem :github => 'iij/mruby-env' + ruby ./minirake + +### To run the tests: + + ruby ./minirake test + + +## License + +Copyright (c) 2012 Internet Initiative Japan Inc. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + diff --git a/web/server/h2o/libh2o/deps/mruby-env/mrbgem.rake b/web/server/h2o/libh2o/deps/mruby-env/mrbgem.rake new file mode 100644 index 00000000..03249be2 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-env') do |spec| + spec.license = 'MIT' + spec.authors = 'Internet Initiative Japan Inc.' +end diff --git a/web/server/h2o/libh2o/deps/mruby-env/mrblib/env.rb b/web/server/h2o/libh2o/deps/mruby-env/mrblib/env.rb new file mode 100644 index 00000000..67f9811a --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/mrblib/env.rb @@ -0,0 +1,16 @@ +class << ENV + alias include? has_key? + alias key? has_key? + alias member? has_key? + + def clear + self.keys.each { |k| self.delete(k) } + self + end + + def delete(key) + old = self[key] + self[key] = nil + old + end +end diff --git a/web/server/h2o/libh2o/deps/mruby-env/run_test.rb b/web/server/h2o/libh2o/deps/mruby-env/run_test.rb new file mode 100644 index 00000000..eefc9143 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/run_test.rb @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# +# mrbgems test runner +# + +if __FILE__ == $0 + repository, dir = 'https://github.com/mruby/mruby.git', 'tmp/mruby' + build_args = ARGV + + Dir.mkdir 'tmp' unless File.exist?('tmp') + unless File.exist?(dir) + system "git clone #{repository} #{dir}" + end + + exit system(%Q[cd #{dir}; MRUBY_CONFIG=#{File.expand_path __FILE__} ruby minirake #{build_args.join(' ')}]) +end + +MRuby::Build.new do |conf| + toolchain :gcc + conf.gembox 'default' + + conf.gem :core => 'mruby-time' + conf.gem :github => 'iij/mruby-io' + conf.gem :github => 'iij/mruby-mtest' + + conf.gem File.expand_path(File.dirname(__FILE__)) +end diff --git a/web/server/h2o/libh2o/deps/mruby-env/src/env.c b/web/server/h2o/libh2o/deps/mruby-env/src/env.c new file mode 100644 index 00000000..0395e1bc --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/src/env.c @@ -0,0 +1,243 @@ +/* +** env.c - ENV is a Hash-like accessor for environment variables. +** +*/ + +#include "mruby.h" +#include "mruby/hash.h" +#include "mruby/khash.h" +#include "mruby/class.h" +#include "mruby/array.h" +#include "mruby/string.h" +#include "mruby/variable.h" +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#ifdef _WIN32 +int +unsetenv(const char* name) +{ + int r; + char* p = malloc(strlen(name) + 2); + if (!p) return -1; + strcpy(p, name); + strcat(p, "="); + r = _putenv(p); + free(p); + return r; +} + +int +setenv(const char* name, const char* value, int overwrite) +{ + int r; + char* p = malloc(strlen(name) + strlen(value) + 2); + if (!p) return -1; + strcpy(p, name); + strcat(p, "="); + strcat(p, value); + r = _putenv(p); + free(p); + return r; +} +#define environ _environ +#else +extern char **environ; +#endif + + +mrb_value +mrb_env_aget(mrb_state *mrb, mrb_value self) +{ + mrb_value key; + const char *cname, *cvalue; + + mrb_get_args(mrb, "S", &key); + cname = mrb_string_value_cstr(mrb, &key); + cvalue = getenv(cname); + if (cvalue != NULL) { + return mrb_str_new_cstr(mrb, cvalue); + } else { + return mrb_nil_value(); + } +} + +mrb_value +mrb_env_has_key(mrb_state *mrb, mrb_value self) +{ + mrb_value name; + const char *key; + mrb_get_args(mrb, "S", &name); + key = mrb_str_to_cstr(mrb, name); + if (getenv(key) != NULL) { + return mrb_true_value(); + } else { + return mrb_false_value(); + } +} + +mrb_value +mrb_env_keys(mrb_state *mrb, mrb_value self) +{ + int i; + mrb_value ary; + + ary = mrb_ary_new(mrb); + for (i = 0; environ[i] != NULL; i++) { + char *str = strchr(environ[i], '='); + if (str != NULL) { + int len = str - environ[i]; + mrb_ary_push(mrb, ary, mrb_str_new(mrb, environ[i], len)); + } + } + + return ary; +} + +mrb_value +mrb_env_values(mrb_state *mrb, mrb_value self) +{ + int i; + mrb_value ary; + + ary = mrb_ary_new(mrb); + for (i = 0; environ[i] != NULL; i++) { + char *str = strchr(environ[i], '='); + if (str) { + int len; + str++; + len = strlen(str); + mrb_ary_push(mrb, ary, mrb_str_new(mrb, str, len)); + } + } + + return ary; +} + +static mrb_value +mrb_env_size(mrb_state *mrb, mrb_value self) +{ + int i; + + for (i = 0; environ[i] != NULL; i++) + ; + + return mrb_fixnum_value(i); +} + +static mrb_value +mrb_env_to_hash(mrb_state *mrb, mrb_value self) +{ + int i; + mrb_value hash; + + hash = mrb_hash_new(mrb); + for (i = 0; environ[i] != NULL; i++) { + char *str = strchr(environ[i], '='); + if (str != NULL) { + mrb_value val; + int ai = mrb_gc_arena_save(mrb); + int len = str - environ[i]; + mrb_value key = mrb_str_new(mrb, environ[i], len); + str++; + val = mrb_str_new(mrb, str, strlen(str)); + mrb_hash_set(mrb, hash, key, val); + mrb_gc_arena_restore(mrb, ai); + } + } + + return hash; +} + +static mrb_value +mrb_env_to_a(mrb_state *mrb, mrb_value self) +{ + int i; + mrb_value ary; + + ary = mrb_ary_new(mrb); + for (i = 0; environ[i] != NULL; i++) { + char *str = strchr(environ[i], '='); + if (str != NULL) { + int ai = mrb_gc_arena_save(mrb); + mrb_value elem = mrb_ary_new(mrb); + int len = str - environ[i]; + mrb_ary_push(mrb, elem, mrb_str_new(mrb, environ[i], len)); + str++; + mrb_ary_push(mrb, elem, mrb_str_new(mrb, str, strlen(str))); + mrb_ary_push(mrb, ary, elem); + mrb_gc_arena_restore(mrb, ai); + } + } + + return ary; +} + +static mrb_value +mrb_env_inspect(mrb_state *mrb, mrb_value self) +{ + mrb_value hash = mrb_env_to_hash(mrb, self); + return mrb_funcall(mrb, hash, "inspect", 0); +} + +static mrb_value +mrb_env_to_s(mrb_state *mrb, mrb_value self) +{ + return mrb_str_new_cstr(mrb, "ENV"); +} + +static mrb_value +mrb_env_aset(mrb_state *mrb, mrb_value self) +{ + mrb_value name, value; + const char *cname, *cvalue; + + mrb_get_args(mrb, "So", &name, &value); + cname = mrb_string_value_cstr(mrb, &name); + + if (mrb_nil_p(value)) { + if (unsetenv(cname) != 0) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't delete environment variable"); + } + } else { + mrb_convert_type(mrb, value, MRB_TT_STRING, "String", "to_str"); + cvalue = mrb_string_value_cstr(mrb, &value); + if (setenv(cname, cvalue, 1) != 0) { + mrb_raise(mrb, E_RUNTIME_ERROR, "can't change environment variable"); + } + } + return value; +} + +void +mrb_mruby_env_gem_init(mrb_state *mrb) +{ + struct RObject *e; + + e = (struct RObject*) mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb->object_class); +#if defined(MRUBY_RELEASE_NO) && MRUBY_RELEASE_NO >= 10000 + mrb_include_module(mrb, (struct RClass*)e, mrb_module_get(mrb, "Enumerable")); +#else + mrb_include_module(mrb, (struct RClass*)e, mrb_class_get(mrb, "Enumerable")); +#endif + + mrb_define_singleton_method(mrb, e,"[]", mrb_env_aget, MRB_ARGS_REQ(1)); + mrb_define_singleton_method(mrb, e,"[]=", mrb_env_aset, MRB_ARGS_REQ(2)); + mrb_define_singleton_method(mrb, e,"has_key?", mrb_env_has_key, MRB_ARGS_REQ(1)); + mrb_define_singleton_method(mrb, e,"inspect", mrb_env_inspect, MRB_ARGS_NONE()); + mrb_define_singleton_method(mrb, e,"keys", mrb_env_keys, MRB_ARGS_NONE()); + mrb_define_singleton_method(mrb, e,"size", mrb_env_size, MRB_ARGS_NONE()); + mrb_define_singleton_method(mrb, e,"store", mrb_env_aset, MRB_ARGS_REQ(2)); + mrb_define_singleton_method(mrb, e,"to_a", mrb_env_to_a, MRB_ARGS_NONE()); + mrb_define_singleton_method(mrb, e,"to_hash", mrb_env_to_hash, MRB_ARGS_NONE()); + mrb_define_singleton_method(mrb, e,"to_s", mrb_env_to_s, MRB_ARGS_NONE()); + mrb_define_singleton_method(mrb, e,"values", mrb_env_values, MRB_ARGS_NONE()); + + mrb_define_global_const(mrb, "ENV", mrb_obj_value(e)); +} + +void +mrb_mruby_env_gem_final(mrb_state *mrb) +{ +} diff --git a/web/server/h2o/libh2o/deps/mruby-env/test/env_test.rb b/web/server/h2o/libh2o/deps/mruby-env/test/env_test.rb new file mode 100644 index 00000000..2aa3dab2 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby-env/test/env_test.rb @@ -0,0 +1,121 @@ +## +# ENV test +# + +if Object.const_defined?(:MTest) + class ENVTest < MTest::Unit::TestCase + def test_env_class + assert_equal(Object, ENV.class) + end + + def setup + @env_hash = ENV.to_hash + ENV.clear + end + + def teardown + ENV.clear + @env_hash.each do |k, v| + ENV[k] = v + end + end + + def set_dummy_env + ENV['FOO'] = 'bar' + end + + def test_size_empty + assert_equal(0, ENV.size) + end + + def test_keys_empty + assert_empty(ENV.keys) + end + + def test_values_empty + assert_empty(ENV.values) + end + + def test_env_to_s_empty + assert_equal("ENV", ENV.to_s) + end + + def test_env_inspect_empty + assert_equal("{}", ENV.inspect) + end + + def test_env_to_hash_empty + assert_equal({}, ENV.to_hash) + end + + def test_env_get_val + set_dummy_env + assert_equal('bar', ENV['FOO']) + end + + def test_env_keys + set_dummy_env + assert_equal(['FOO'], ENV.keys) + end + + def test_env_values + set_dummy_env + assert_equal(['bar'], ENV.values) + end + + def test_env_to_s + set_dummy_env + assert_equal("ENV", ENV.to_s) + end + + def test_env_has_key + set_dummy_env + assert_true ENV.has_key?("FOO") + assert_false ENV.has_key?("BAR") + end + + def test_env_inspect + set_dummy_env + assert_equal("{\"FOO\"=>\"bar\"}", ENV.inspect) + end + + def test_env_delete + set_dummy_env + old = ENV['FOO'] + ret = ENV.delete('FOO') + assert_equal(0, ENV.size) + assert_equal(old, ret) + assert_equal(nil, ENV.delete('nosuchenv')) + end + + def test_env_subst_nil + set_dummy_env + ENV['FOO'] = nil + assert_equal(0, ENV.size) + end + + def test_env_store + ENV['a'] = 'b' + assert_equal 'b', ENV['a'] + + ENV['a'] = 'c' + assert_equal 'c', ENV['a'] + + ENV['a'] = nil + assert_equal nil, ENV['a'] + + ENV['b'] = nil + assert_equal nil, ENV['b'] + assert_equal 0, ENV.size + end + end + + if $ok_test + MTest::Unit.new.mrbtest + else + MTest::Unit.new.run + end +else + $asserts << "test skip of mruby-env/test/env_test.rb" if $asserts +end + |