From 6199944350088dba9744462f0b973c4789750efd Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Wed, 23 Nov 2022 21:22:00 -0500 Subject: [PATCH] script parameters, move shift to the environment --- builtins.cpp | 5 +++-- command.cpp | 2 +- environment.cpp | 22 +++++++++++++++++++--- environment.h | 4 +++- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/builtins.cpp b/builtins.cpp index 5dbdaaf..784ac0f 100644 --- a/builtins.cpp +++ b/builtins.cpp @@ -188,7 +188,8 @@ int builtin_shift(Environment &env, const std::vector &tokens, cons } if (n == 0) return 0; - + env.shift(n); +#if 0 auto argv = load_argv(env); if (argv.empty()) return 0; @@ -199,7 +200,7 @@ int builtin_shift(Environment &env, const std::vector &tokens, cons } while (--n); env.set_argv(argv); - +#endif return 0; } diff --git a/command.cpp b/command.cpp index ce8ad7d..c773942 100644 --- a/command.cpp +++ b/command.cpp @@ -400,7 +400,7 @@ int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) { // scripts run with an isolated environment. Environment new_env = env.subshell_environment(); new_env.set("command", path); - new_env.set_argv(path, p.arguments); + new_env.set_argv(p.arguments); try { return read_file(new_env, path, newfds); diff --git a/environment.cpp b/environment.cpp index 2b602c7..e8636d7 100644 --- a/environment.cpp +++ b/environment.cpp @@ -112,15 +112,17 @@ namespace { set_common(k, std::to_string(value), exported); } +#if 0 void Environment::set_argv(const std::string &argv0, const std::vector& argv) { set_common("0", argv0, false); set_argv(argv); } +#endif void Environment::set_argv(const std::vector& argv) { - _pound = argv.size(); - set_common("#", std::to_string(argv.size()), false); + _pound = argv.size() - 1; + set_common("#", std::to_string(_pound), false); - int n = 1; + int n = 0; for (const auto &s : argv) { set_common(std::to_string(n++), s, false); } @@ -143,8 +145,22 @@ namespace { } p.pop_back(); set_common("parameters", p, false); + } + void Environment::shift(int n) { + if (n < 0) return; + if (_pound < 1) return; + std::vector argv; + argv.push_back(get("0")); + + for (int i = 1 + n; i <= _pound; ++i) { + argv.push_back(get(std::to_string(i))); + } + for (int i = 0; i < n; ++i) { + unset(std::to_string(_pound - i)); + } + set_argv(argv); } diff --git a/environment.h b/environment.h index f3cbfe2..fe87193 100644 --- a/environment.h +++ b/environment.h @@ -59,9 +59,11 @@ public: Environment subshell_environment(); - void set_argv(const std::string &argv0, const std::vector& argv); + // void set_argv(const std::string &argv0, const std::vector& argv); void set_argv(const std::vector& argv); + void shift(int n); + void set(const std::string &k, const std::string &value, bool exported = false); void set(const std::string &k, long l, bool exported = false); void unset(const std::string &k);