Shell: Add a 'kill' builtin that wraps the system's own

Fixes #6578.
This commit is contained in:
Ali Mohammad Pur 2021-04-23 19:09:13 +04:30 committed by Linus Groh
parent 95055d3a38
commit 0d2602c900
Notes: sideshowbarker 2024-07-18 19:11:21 +09:00
3 changed files with 62 additions and 1 deletions

View File

@ -1031,6 +1031,43 @@ int Shell::builtin_not(int argc, const char** argv)
return exit_code == 0 ? 1 : 0;
}
int Shell::builtin_kill(int argc, const char** argv)
{
// Simply translate the arguments and pass them to `kill'
Vector<String> replaced_values;
auto kill_path = find_in_path("kill");
if (kill_path.is_empty()) {
warnln("kill: `kill' not found in PATH");
return 126;
}
replaced_values.append(kill_path);
for (auto i = 1; i < argc; ++i) {
if (auto job_id = resolve_job_spec(argv[i]); job_id.has_value()) {
auto job = find_job(job_id.value());
if (job) {
replaced_values.append(String::number(job->pid()));
} else {
warnln("kill: Job with pid {} not found", job_id.value());
return 1;
}
} else {
replaced_values.append(argv[i]);
}
}
// Now just run `kill'
AST::Command command;
command.argv = move(replaced_values);
command.position = m_source_position.has_value() ? m_source_position->position : Optional<AST::Position> {};
auto exit_code = 1;
if (auto job = run_command(command)) {
block_on_job(job);
exit_code = job->exit_code();
}
return exit_code;
}
bool Shell::run_builtin(const AST::Command& command, const NonnullRefPtrVector<AST::Rewiring>& rewirings, int& retval)
{
if (command.argv.is_empty())

View File

@ -1194,6 +1194,27 @@ String Shell::unescape_token(const String& token)
return builder.build();
}
String Shell::find_in_path(const StringView& program_name)
{
String path = getenv("PATH");
if (!path.is_empty()) {
auto directories = path.split(':');
for (const auto& directory : directories) {
Core::DirIterator programs(directory.characters(), Core::DirIterator::SkipDots);
while (programs.has_next()) {
auto program = programs.next_path();
auto program_path = String::formatted("{}/{}", directory, program);
if (access(program_path.characters(), X_OK) != 0)
continue;
if (program == program_name)
return program_path;
}
}
}
return {};
}
void Shell::cache_path()
{
if (!m_is_interactive)

View File

@ -46,7 +46,8 @@
__ENUMERATE_SHELL_BUILTIN(fg) \
__ENUMERATE_SHELL_BUILTIN(bg) \
__ENUMERATE_SHELL_BUILTIN(wait) \
__ENUMERATE_SHELL_BUILTIN(dump)
__ENUMERATE_SHELL_BUILTIN(dump) \
__ENUMERATE_SHELL_BUILTIN(kill)
#define ENUMERATE_SHELL_OPTIONS() \
__ENUMERATE_SHELL_OPTION(inline_exec_keep_empty_segments, false, "Keep empty segments in inline execute $(...)") \
@ -103,6 +104,8 @@ public:
String resolve_path(String) const;
String resolve_alias(const String&) const;
static String find_in_path(const StringView& program_name);
static bool has_history_event(StringView);
RefPtr<AST::Value> get_argument(size_t);