script parameters, move shift to the environment

This commit is contained in:
Kelvin Sherlock 2022-11-23 21:22:00 -05:00
parent f799eb6d81
commit 6199944350
4 changed files with 26 additions and 7 deletions

View File

@ -188,7 +188,8 @@ int builtin_shift(Environment &env, const std::vector<std::string> &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<std::string> &tokens, cons
} while (--n);
env.set_argv(argv);
#endif
return 0;
}

View File

@ -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);

View File

@ -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<std::string>& argv) {
set_common("0", argv0, false);
set_argv(argv);
}
#endif
void Environment::set_argv(const std::vector<std::string>& 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<std::string> 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);
}

View File

@ -59,9 +59,11 @@ public:
Environment subshell_environment();
void set_argv(const std::string &argv0, const std::vector<std::string>& argv);
// void set_argv(const std::string &argv0, const std::vector<std::string>& argv);
void set_argv(const std::vector<std::string>& 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);