1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- /*
- * Copyright (c) 2020, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "AST.h"
- #include "Shell.h"
- #include "Shell/Formatter.h"
- #include <AK/LexicalPath.h>
- #include <AK/ScopeGuard.h>
- #include <LibCore/ArgsParser.h>
- #include <LibCore/EventLoop.h>
- #include <LibCore/File.h>
- #include <errno.h>
- #include <inttypes.h>
- #include <limits.h>
- #include <signal.h>
- #include <sys/wait.h>
- #include <unistd.h>
- extern char** environ;
- namespace Shell {
- int Shell::builtin_dump(int argc, const char** argv)
- {
- if (argc != 2)
- return 1;
- Parser { argv[1] }.parse()->dump(0);
- return 0;
- }
- int Shell::builtin_alias(int argc, const char** argv)
- {
- Vector<const char*> arguments;
- Core::ArgsParser parser;
- parser.add_positional_argument(arguments, "List of name[=values]'s", "name[=value]", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (arguments.is_empty()) {
- for (auto& alias : m_aliases)
- printf("%s=%s\n", escape_token(alias.key).characters(), escape_token(alias.value).characters());
- return 0;
- }
- bool fail = false;
- for (auto& argument : arguments) {
- auto parts = String { argument }.split_limit('=', 2, true);
- if (parts.size() == 1) {
- auto alias = m_aliases.get(parts[0]);
- if (alias.has_value()) {
- printf("%s=%s\n", escape_token(parts[0]).characters(), escape_token(alias.value()).characters());
- } else {
- fail = true;
- }
- } else {
- m_aliases.set(parts[0], parts[1]);
- add_entry_to_cache(parts[0]);
- }
- }
- return fail ? 1 : 0;
- }
- int Shell::builtin_bg(int argc, const char** argv)
- {
- int job_id = -1;
- bool is_pid = false;
- Core::ArgsParser parser;
- parser.add_positional_argument(Core::ArgsParser::Arg {
- .help_string = "Job ID or Jobspec to run in background",
- .name = "job-id",
- .min_values = 0,
- .max_values = 1,
- .accept_value = [&](const String& value) -> bool {
- // Check if it's a pid (i.e. literal integer)
- if (auto number = value.to_uint(); number.has_value()) {
- job_id = number.value();
- is_pid = true;
- return true;
- }
- // Check if it's a jobspec
- if (auto id = resolve_job_spec(value); id.has_value()) {
- job_id = id.value();
- is_pid = false;
- return true;
- }
- return false;
- } });
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (job_id == -1 && !jobs.is_empty())
- job_id = find_last_job_id();
- auto* job = const_cast<Job*>(find_job(job_id, is_pid));
- if (!job) {
- if (job_id == -1) {
- warnln("bg: No current job");
- } else {
- warnln("bg: Job with id/pid {} not found", job_id);
- }
- return 1;
- }
- job->set_running_in_background(true);
- job->set_should_announce_exit(true);
- job->set_shell_did_continue(true);
- dbgln("Resuming {} ({})", job->pid(), job->cmd());
- warnln("Resuming job {} - {}", job->job_id(), job->cmd());
- // Try using the PGID, but if that fails, just use the PID.
- if (killpg(job->pgid(), SIGCONT) < 0) {
- if (kill(job->pid(), SIGCONT) < 0) {
- perror("kill");
- return 1;
- }
- }
- return 0;
- }
- int Shell::builtin_type(int argc, const char** argv)
- {
- Vector<const char*> commands;
- bool dont_show_function_source = false;
- Core::ArgsParser parser;
- parser.set_general_help("Display information about commands.");
- parser.add_positional_argument(commands, "Command(s) to list info about", "command");
- parser.add_option(dont_show_function_source, "Do not show functions source.", "no-fn-source", 'f');
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- bool something_not_found = false;
- for (auto& command : commands) {
- // check if it is an alias
- if (auto alias = m_aliases.get(command); alias.has_value()) {
- printf("%s is aliased to `%s`\n", escape_token(command).characters(), escape_token(alias.value()).characters());
- continue;
- }
- // check if it is a function
- if (auto function = m_functions.get(command); function.has_value()) {
- auto fn = function.value();
- printf("%s is a function\n", command);
- if (!dont_show_function_source) {
- StringBuilder builder;
- builder.append(fn.name);
- builder.append("(");
- for (size_t i = 0; i < fn.arguments.size(); i++) {
- builder.append(fn.arguments[i]);
- if (!(i == fn.arguments.size() - 1))
- builder.append(" ");
- }
- builder.append(") {\n");
- if (fn.body) {
- auto formatter = Formatter(*fn.body);
- builder.append(formatter.format());
- printf("%s\n}\n", builder.build().characters());
- } else {
- printf("%s\n}\n", builder.build().characters());
- }
- }
- continue;
- }
- // check if its a builtin
- if (has_builtin(command)) {
- printf("%s is a shell builtin\n", command);
- continue;
- }
- // check if its an executable in PATH
- auto fullpath = Core::find_executable_in_path(command);
- if (!fullpath.is_null()) {
- printf("%s is %s\n", command, escape_token(fullpath).characters());
- continue;
- }
- something_not_found = true;
- printf("type: %s not found\n", command);
- }
- if (something_not_found)
- return 1;
- else
- return 0;
- }
- int Shell::builtin_cd(int argc, const char** argv)
- {
- const char* arg_path = nullptr;
- Core::ArgsParser parser;
- parser.add_positional_argument(arg_path, "Path to change to", "path", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- String new_path;
- if (!arg_path) {
- new_path = home;
- } else {
- if (strcmp(arg_path, "-") == 0) {
- char* oldpwd = getenv("OLDPWD");
- if (oldpwd == nullptr)
- return 1;
- new_path = oldpwd;
- } else {
- new_path = arg_path;
- }
- }
- auto real_path = Core::File::real_path_for(new_path);
- if (real_path.is_empty()) {
- warnln("Invalid path '{}'", new_path);
- return 1;
- }
- if (cd_history.is_empty() || cd_history.last() != real_path)
- cd_history.enqueue(real_path);
- const char* path = real_path.characters();
- int rc = chdir(path);
- if (rc < 0) {
- if (errno == ENOTDIR) {
- warnln("Not a directory: {}", path);
- } else {
- warnln("chdir({}) failed: {}", path, strerror(errno));
- }
- return 1;
- }
- setenv("OLDPWD", cwd.characters(), 1);
- cwd = real_path;
- setenv("PWD", cwd.characters(), 1);
- return 0;
- }
- int Shell::builtin_cdh(int argc, const char** argv)
- {
- int index = -1;
- Core::ArgsParser parser;
- parser.add_positional_argument(index, "Index of the cd history entry (leave out for a list)", "index", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (index == -1) {
- if (cd_history.is_empty()) {
- warnln("cdh: no history available");
- return 0;
- }
- for (ssize_t i = cd_history.size() - 1; i >= 0; --i)
- printf("%lu: %s\n", cd_history.size() - i, cd_history.at(i).characters());
- return 0;
- }
- if (index < 1 || (size_t)index > cd_history.size()) {
- warnln("cdh: history index out of bounds: {} not in (0, {})", index, cd_history.size());
- return 1;
- }
- const char* path = cd_history.at(cd_history.size() - index).characters();
- const char* cd_args[] = { "cd", path, nullptr };
- return Shell::builtin_cd(2, cd_args);
- }
- int Shell::builtin_dirs(int argc, const char** argv)
- {
- // The first directory in the stack is ALWAYS the current directory
- directory_stack.at(0) = cwd.characters();
- bool clear = false;
- bool print = false;
- bool number_when_printing = false;
- char separator = ' ';
- Vector<const char*> paths;
- Core::ArgsParser parser;
- parser.add_option(clear, "Clear the directory stack", "clear", 'c');
- parser.add_option(print, "Print directory entries one per line", "print", 'p');
- parser.add_option(number_when_printing, "Number the directories in the stack when printing", "number", 'v');
- parser.add_positional_argument(paths, "Extra paths to put on the stack", "path", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- // -v implies -p
- print = print || number_when_printing;
- if (print) {
- if (!paths.is_empty()) {
- warnln("dirs: 'print' and 'number' are not allowed when any path is specified");
- return 1;
- }
- separator = '\n';
- }
- if (clear) {
- for (size_t i = 1; i < directory_stack.size(); i++)
- directory_stack.remove(i);
- }
- for (auto& path : paths)
- directory_stack.append(path);
- if (print || (!clear && paths.is_empty())) {
- int index = 0;
- for (auto& directory : directory_stack) {
- if (number_when_printing)
- printf("%d ", index++);
- print_path(directory);
- fputc(separator, stdout);
- }
- }
- return 0;
- }
- int Shell::builtin_exec(int argc, const char** argv)
- {
- if (argc < 2) {
- warnln("Shell: No command given to exec");
- return 1;
- }
- Vector<const char*> argv_vector;
- argv_vector.append(argv + 1, argc - 1);
- argv_vector.append(nullptr);
- execute_process(move(argv_vector));
- }
- int Shell::builtin_exit(int argc, const char** argv)
- {
- int exit_code = 0;
- Core::ArgsParser parser;
- parser.add_positional_argument(exit_code, "Exit code", "code", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv)))
- return 1;
- if (m_is_interactive) {
- if (!jobs.is_empty()) {
- if (!m_should_ignore_jobs_on_next_exit) {
- warnln("Shell: You have {} active job{}, run 'exit' again to really exit.", jobs.size(), jobs.size() > 1 ? "s" : "");
- m_should_ignore_jobs_on_next_exit = true;
- return 1;
- }
- }
- }
- stop_all_jobs();
- if (m_is_interactive) {
- m_editor->save_history(get_history_path());
- printf("Good-bye!\n");
- }
- exit(exit_code);
- return 0;
- }
- int Shell::builtin_export(int argc, const char** argv)
- {
- Vector<const char*> vars;
- Core::ArgsParser parser;
- parser.add_positional_argument(vars, "List of variable[=value]'s", "values", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (vars.is_empty()) {
- for (size_t i = 0; environ[i]; ++i)
- puts(environ[i]);
- return 0;
- }
- for (auto& value : vars) {
- auto parts = String { value }.split_limit('=', 2);
- if (parts.size() == 1) {
- auto value = lookup_local_variable(parts[0]);
- if (value) {
- auto values = value->resolve_as_list(*this);
- StringBuilder builder;
- builder.join(" ", values);
- parts.append(builder.to_string());
- } else {
- // Ignore the export.
- continue;
- }
- }
- int setenv_return = setenv(parts[0].characters(), parts[1].characters(), 1);
- if (setenv_return != 0) {
- perror("setenv");
- return 1;
- }
- if (parts[0] == "PATH")
- cache_path();
- }
- return 0;
- }
- int Shell::builtin_glob(int argc, const char** argv)
- {
- Vector<const char*> globs;
- Core::ArgsParser parser;
- parser.add_positional_argument(globs, "Globs to resolve", "glob");
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- for (auto& glob : globs) {
- for (auto& expanded : expand_globs(glob, cwd))
- outln("{}", expanded);
- }
- return 0;
- }
- int Shell::builtin_fg(int argc, const char** argv)
- {
- int job_id = -1;
- bool is_pid = false;
- Core::ArgsParser parser;
- parser.add_positional_argument(Core::ArgsParser::Arg {
- .help_string = "Job ID or Jobspec to bring to foreground",
- .name = "job-id",
- .min_values = 0,
- .max_values = 1,
- .accept_value = [&](const String& value) -> bool {
- // Check if it's a pid (i.e. literal integer)
- if (auto number = value.to_uint(); number.has_value()) {
- job_id = number.value();
- is_pid = true;
- return true;
- }
- // Check if it's a jobspec
- if (auto id = resolve_job_spec(value); id.has_value()) {
- job_id = id.value();
- is_pid = false;
- return true;
- }
- return false;
- } });
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (job_id == -1 && !jobs.is_empty())
- job_id = find_last_job_id();
- RefPtr<Job> job = find_job(job_id, is_pid);
- if (!job) {
- if (job_id == -1) {
- warnln("fg: No current job");
- } else {
- warnln("fg: Job with id/pid {} not found", job_id);
- }
- return 1;
- }
- job->set_running_in_background(false);
- job->set_shell_did_continue(true);
- dbgln("Resuming {} ({})", job->pid(), job->cmd());
- warnln("Resuming job {} - {}", job->job_id(), job->cmd());
- tcsetpgrp(STDOUT_FILENO, job->pgid());
- tcsetpgrp(STDIN_FILENO, job->pgid());
- // Try using the PGID, but if that fails, just use the PID.
- if (killpg(job->pgid(), SIGCONT) < 0) {
- if (kill(job->pid(), SIGCONT) < 0) {
- perror("kill");
- return 1;
- }
- }
- block_on_job(job);
- if (job->exited())
- return job->exit_code();
- else
- return 0;
- }
- int Shell::builtin_disown(int argc, const char** argv)
- {
- Vector<int> job_ids;
- Vector<bool> id_is_pid;
- Core::ArgsParser parser;
- parser.add_positional_argument(Core::ArgsParser::Arg {
- .help_string = "Job IDs or Jobspecs to disown",
- .name = "job-id",
- .min_values = 0,
- .max_values = INT_MAX,
- .accept_value = [&](const String& value) -> bool {
- // Check if it's a pid (i.e. literal integer)
- if (auto number = value.to_uint(); number.has_value()) {
- job_ids.append(number.value());
- id_is_pid.append(true);
- return true;
- }
- // Check if it's a jobspec
- if (auto id = resolve_job_spec(value); id.has_value()) {
- job_ids.append(id.value());
- id_is_pid.append(false);
- return true;
- }
- return false;
- } });
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (job_ids.is_empty()) {
- job_ids.append(find_last_job_id());
- id_is_pid.append(false);
- }
- Vector<const Job*> jobs_to_disown;
- for (size_t i = 0; i < job_ids.size(); ++i) {
- auto id = job_ids[i];
- auto is_pid = id_is_pid[i];
- auto job = find_job(id, is_pid);
- if (!job)
- warnln("disown: Job with id/pid {} not found", id);
- else
- jobs_to_disown.append(job);
- }
- if (jobs_to_disown.is_empty()) {
- if (job_ids.is_empty())
- warnln("disown: No current job");
- // An error message has already been printed about the nonexistence of each listed job.
- return 1;
- }
- for (auto job : jobs_to_disown) {
- job->deactivate();
- if (!job->is_running_in_background())
- warnln("disown warning: Job {} is currently not running, 'kill -{} {}' to make it continue", job->job_id(), SIGCONT, job->pid());
- jobs.remove(job->pid());
- }
- return 0;
- }
- int Shell::builtin_history(int, const char**)
- {
- for (size_t i = 0; i < m_editor->history().size(); ++i) {
- printf("%6zu %s\n", i, m_editor->history()[i].entry.characters());
- }
- return 0;
- }
- int Shell::builtin_jobs(int argc, const char** argv)
- {
- bool list = false, show_pid = false;
- Core::ArgsParser parser;
- parser.add_option(list, "List all information about jobs", "list", 'l');
- parser.add_option(show_pid, "Display the PID of the jobs", "pid", 'p');
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- Job::PrintStatusMode mode = Job::PrintStatusMode::Basic;
- if (show_pid)
- mode = Job::PrintStatusMode::OnlyPID;
- if (list)
- mode = Job::PrintStatusMode::ListAll;
- for (auto& it : jobs) {
- if (!it.value->print_status(mode))
- return 1;
- }
- return 0;
- }
- int Shell::builtin_popd(int argc, const char** argv)
- {
- if (directory_stack.size() <= 1) {
- warnln("Shell: popd: directory stack empty");
- return 1;
- }
- bool should_not_switch = false;
- String path = directory_stack.take_last();
- Core::ArgsParser parser;
- parser.add_option(should_not_switch, "Do not switch dirs", "no-switch", 'n');
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- bool should_switch = !should_not_switch;
- // When no arguments are given, popd removes the top directory from the stack and performs a cd to the new top directory.
- if (argc == 1) {
- int rc = chdir(path.characters());
- if (rc < 0) {
- warnln("chdir({}) failed: {}", path, strerror(errno));
- return 1;
- }
- cwd = path;
- return 0;
- }
- LexicalPath lexical_path(path.characters());
- if (!lexical_path.is_valid()) {
- warnln("LexicalPath failed to canonicalize '{}'", path);
- return 1;
- }
- const char* real_path = lexical_path.string().characters();
- struct stat st;
- int rc = stat(real_path, &st);
- if (rc < 0) {
- warnln("stat({}) failed: {}", real_path, strerror(errno));
- return 1;
- }
- if (!S_ISDIR(st.st_mode)) {
- warnln("Not a directory: {}", real_path);
- return 1;
- }
- if (should_switch) {
- int rc = chdir(real_path);
- if (rc < 0) {
- warnln("chdir({}) failed: {}", real_path, strerror(errno));
- return 1;
- }
- cwd = lexical_path.string();
- }
- return 0;
- }
- int Shell::builtin_pushd(int argc, const char** argv)
- {
- StringBuilder path_builder;
- bool should_switch = true;
- // From the BASH reference manual: https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html
- // With no arguments, pushd exchanges the top two directories and makes the new top the current directory.
- if (argc == 1) {
- if (directory_stack.size() < 2) {
- warnln("pushd: no other directory");
- return 1;
- }
- String dir1 = directory_stack.take_first();
- String dir2 = directory_stack.take_first();
- directory_stack.insert(0, dir2);
- directory_stack.insert(1, dir1);
- int rc = chdir(dir2.characters());
- if (rc < 0) {
- warnln("chdir({}) failed: {}", dir2, strerror(errno));
- return 1;
- }
- cwd = dir2;
- return 0;
- }
- // Let's assume the user's typed in 'pushd <dir>'
- if (argc == 2) {
- directory_stack.append(cwd.characters());
- if (argv[1][0] == '/') {
- path_builder.append(argv[1]);
- } else {
- path_builder.appendff("{}/{}", cwd, argv[1]);
- }
- } else if (argc == 3) {
- directory_stack.append(cwd.characters());
- for (int i = 1; i < argc; i++) {
- const char* arg = argv[i];
- if (arg[0] != '-') {
- if (arg[0] == '/') {
- path_builder.append(arg);
- } else
- path_builder.appendff("{}/{}", cwd, arg);
- }
- if (!strcmp(arg, "-n"))
- should_switch = false;
- }
- }
- LexicalPath lexical_path(path_builder.to_string());
- if (!lexical_path.is_valid()) {
- warnln("LexicalPath failed to canonicalize '{}'", path_builder.string_view());
- return 1;
- }
- const char* real_path = lexical_path.string().characters();
- struct stat st;
- int rc = stat(real_path, &st);
- if (rc < 0) {
- warnln("stat({}) failed: {}", real_path, strerror(errno));
- return 1;
- }
- if (!S_ISDIR(st.st_mode)) {
- warnln("Not a directory: {}", real_path);
- return 1;
- }
- if (should_switch) {
- int rc = chdir(real_path);
- if (rc < 0) {
- warnln("chdir({}) failed: {}", real_path, strerror(errno));
- return 1;
- }
- cwd = lexical_path.string();
- }
- return 0;
- }
- int Shell::builtin_pwd(int, const char**)
- {
- print_path(cwd);
- fputc('\n', stdout);
- return 0;
- }
- int Shell::builtin_setopt(int argc, const char** argv)
- {
- if (argc == 1) {
- #define __ENUMERATE_SHELL_OPTION(name, default_, description) \
- if (options.name) \
- warnln("{}", #name);
- ENUMERATE_SHELL_OPTIONS();
- #undef __ENUMERATE_SHELL_OPTION
- }
- Core::ArgsParser parser;
- #define __ENUMERATE_SHELL_OPTION(name, default_, description) \
- bool name = false; \
- bool not_##name = false; \
- parser.add_option(name, "Enable: " description, #name, '\0'); \
- parser.add_option(not_##name, "Disable: " description, "no_" #name, '\0');
- ENUMERATE_SHELL_OPTIONS();
- #undef __ENUMERATE_SHELL_OPTION
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- #define __ENUMERATE_SHELL_OPTION(name, default_, description) \
- if (name) \
- options.name = true; \
- if (not_##name) \
- options.name = false;
- ENUMERATE_SHELL_OPTIONS();
- #undef __ENUMERATE_SHELL_OPTION
- return 0;
- }
- int Shell::builtin_shift(int argc, const char** argv)
- {
- int count = 1;
- Core::ArgsParser parser;
- parser.add_positional_argument(count, "Shift count", "count", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (count < 1)
- return 0;
- auto argv_ = lookup_local_variable("ARGV");
- if (!argv_) {
- warnln("shift: ARGV is unset");
- return 1;
- }
- if (!argv_->is_list())
- argv_ = adopt_ref(*new AST::ListValue({ argv_.release_nonnull() }));
- auto& values = static_cast<AST::ListValue*>(argv_.ptr())->values();
- if ((size_t)count > values.size()) {
- warnln("shift: shift count must not be greater than {}", values.size());
- return 1;
- }
- for (auto i = 0; i < count; ++i)
- values.take_first();
- return 0;
- }
- int Shell::builtin_source(int argc, const char** argv)
- {
- const char* file_to_source = nullptr;
- Vector<const char*> args;
- Core::ArgsParser parser;
- parser.add_positional_argument(file_to_source, "File to read commands from", "path");
- parser.add_positional_argument(args, "ARGV for the sourced file", "args", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv)))
- return 1;
- Vector<String> string_argv;
- for (auto& arg : args)
- string_argv.append(arg);
- auto previous_argv = lookup_local_variable("ARGV");
- ScopeGuard guard { [&] {
- if (!args.is_empty())
- set_local_variable("ARGV", move(previous_argv));
- } };
- if (!args.is_empty())
- set_local_variable("ARGV", AST::create<AST::ListValue>(move(string_argv)));
- if (!run_file(file_to_source, true))
- return 126;
- return 0;
- }
- int Shell::builtin_time(int argc, const char** argv)
- {
- Vector<const char*> args;
- Core::ArgsParser parser;
- parser.set_stop_on_first_non_option(true);
- parser.add_positional_argument(args, "Command to execute with arguments", "command", Core::ArgsParser::Required::Yes);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- AST::Command command;
- for (auto& arg : args)
- command.argv.append(arg);
- auto commands = expand_aliases({ move(command) });
- Core::ElapsedTimer timer;
- int exit_code = 1;
- timer.start();
- for (auto& job : run_commands(commands)) {
- block_on_job(job);
- exit_code = job.exit_code();
- }
- warnln("Time: {} ms", timer.elapsed());
- return exit_code;
- }
- int Shell::builtin_umask(int argc, const char** argv)
- {
- const char* mask_text = nullptr;
- Core::ArgsParser parser;
- parser.add_positional_argument(mask_text, "New mask (omit to get current mask)", "octal-mask", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (!mask_text) {
- mode_t old_mask = umask(0);
- printf("%#o\n", old_mask);
- umask(old_mask);
- return 0;
- }
- unsigned mask;
- int matches = sscanf(mask_text, "%o", &mask);
- if (matches == 1) {
- umask(mask);
- return 0;
- }
- warnln("umask: Invalid mask '{}'", mask_text);
- return 1;
- }
- int Shell::builtin_wait(int argc, const char** argv)
- {
- Vector<int> job_ids;
- Vector<bool> id_is_pid;
- Core::ArgsParser parser;
- parser.add_positional_argument(Core::ArgsParser::Arg {
- .help_string = "Job IDs or Jobspecs to wait for",
- .name = "job-id",
- .min_values = 0,
- .max_values = INT_MAX,
- .accept_value = [&](const String& value) -> bool {
- // Check if it's a pid (i.e. literal integer)
- if (auto number = value.to_uint(); number.has_value()) {
- job_ids.append(number.value());
- id_is_pid.append(true);
- return true;
- }
- // Check if it's a jobspec
- if (auto id = resolve_job_spec(value); id.has_value()) {
- job_ids.append(id.value());
- id_is_pid.append(false);
- return true;
- }
- return false;
- } });
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- Vector<NonnullRefPtr<Job>> jobs_to_wait_for;
- for (size_t i = 0; i < job_ids.size(); ++i) {
- auto id = job_ids[i];
- auto is_pid = id_is_pid[i];
- auto job = find_job(id, is_pid);
- if (!job)
- warnln("wait: Job with id/pid {} not found", id);
- else
- jobs_to_wait_for.append(*job);
- }
- if (job_ids.is_empty()) {
- for (const auto& it : jobs)
- jobs_to_wait_for.append(it.value);
- }
- for (auto& job : jobs_to_wait_for) {
- job->set_running_in_background(false);
- block_on_job(job);
- }
- return 0;
- }
- int Shell::builtin_unset(int argc, const char** argv)
- {
- Vector<const char*> vars;
- Core::ArgsParser parser;
- parser.add_positional_argument(vars, "List of variables", "variables", Core::ArgsParser::Required::Yes);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- for (auto& value : vars) {
- if (lookup_local_variable(value)) {
- unset_local_variable(value);
- } else {
- unsetenv(value);
- }
- }
- return 0;
- }
- int Shell::builtin_not(int argc, const char** argv)
- {
- // FIXME: Use ArgsParser when it can collect unrelated -arguments too.
- if (argc == 1)
- return 1;
- AST::Command command;
- for (size_t i = 1; i < (size_t)argc; ++i)
- command.argv.append(argv[i]);
- auto commands = expand_aliases({ move(command) });
- int exit_code = 1;
- auto found_a_job = false;
- for (auto& job : run_commands(commands)) {
- found_a_job = true;
- block_on_job(job);
- exit_code = job.exit_code();
- }
- // In case it was a function.
- if (!found_a_job)
- exit_code = last_return_code;
- 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())
- return false;
- if (!has_builtin(command.argv.first()))
- return false;
- Vector<const char*> argv;
- for (auto& arg : command.argv)
- argv.append(arg.characters());
- argv.append(nullptr);
- StringView name = command.argv.first();
- SavedFileDescriptors fds { rewirings };
- for (auto& rewiring : rewirings) {
- int rc = dup2(rewiring.old_fd, rewiring.new_fd);
- if (rc < 0) {
- perror("dup2(run)");
- return false;
- }
- }
- Core::EventLoop loop;
- setup_signals();
- #define __ENUMERATE_SHELL_BUILTIN(builtin) \
- if (name == #builtin) { \
- retval = builtin_##builtin(argv.size() - 1, argv.data()); \
- if (!has_error(ShellError::None)) \
- raise_error(m_error, m_error_description, command.position); \
- fflush(stdout); \
- fflush(stderr); \
- return true; \
- }
- ENUMERATE_SHELL_BUILTINS();
- #undef __ENUMERATE_SHELL_BUILTIN
- return false;
- }
- bool Shell::has_builtin(const StringView& name) const
- {
- #define __ENUMERATE_SHELL_BUILTIN(builtin) \
- if (name == #builtin) { \
- return true; \
- }
- ENUMERATE_SHELL_BUILTINS();
- #undef __ENUMERATE_SHELL_BUILTIN
- return false;
- }
- }
|