123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704 |
- /*
- * Copyright (c) 2020-2021, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "AST.h"
- #include "Formatter.h"
- #include "PosixParser.h"
- #include "Shell.h"
- #include <AK/DeprecatedString.h>
- #include <AK/LexicalPath.h>
- #include <AK/ScopeGuard.h>
- #include <AK/Statistics.h>
- #include <LibCore/ArgsParser.h>
- #include <LibCore/DeprecatedFile.h>
- #include <LibCore/EventLoop.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_noop(int, char const**)
- {
- return 0;
- }
- int Shell::builtin_dump(int argc, char const** argv)
- {
- bool posix = false;
- StringView source;
- Core::ArgsParser parser;
- parser.add_positional_argument(source, "Shell code to parse and dump", "source");
- parser.add_option(posix, "Use the POSIX parser", "posix", 'p');
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- (posix ? Posix::Parser { source }.parse() : Parser { source }.parse())->dump(0);
- return 0;
- }
- enum FollowSymlinks {
- Yes,
- No
- };
- static Vector<DeprecatedString> find_matching_executables_in_path(StringView filename, FollowSymlinks follow_symlinks = FollowSymlinks::No)
- {
- // Edge cases in which there are guaranteed no solutions
- if (filename.is_empty() || filename.contains('/'))
- return {};
- char const* path_str = getenv("PATH");
- auto path = DEFAULT_PATH_SV;
- if (path_str != nullptr) // maybe && *path_str
- path = { path_str, strlen(path_str) };
- Vector<DeprecatedString> executables;
- auto directories = path.split_view(':');
- for (auto directory : directories) {
- auto file = DeprecatedString::formatted("{}/{}", directory, filename);
- if (follow_symlinks == FollowSymlinks::Yes) {
- auto path_or_error = Core::DeprecatedFile::read_link(file);
- if (!path_or_error.is_error())
- file = path_or_error.release_value();
- }
- if (access(file.characters(), X_OK) == 0)
- executables.append(move(file));
- }
- return executables;
- }
- int Shell::builtin_where(int argc, char const** argv)
- {
- Vector<StringView> arguments;
- bool do_only_path_search { false };
- bool do_follow_symlinks { false };
- bool do_print_only_type { false };
- Core::ArgsParser parser;
- parser.add_positional_argument(arguments, "List of shell builtins, aliases or executables", "arguments");
- parser.add_option(do_only_path_search, "Search only for executables in the PATH environment variable", "path-only", 'p');
- parser.add_option(do_follow_symlinks, "Follow symlinks and print the symlink free path", "follow-symlink", 's');
- parser.add_option(do_print_only_type, "Print the argument type instead of a human readable description", "type", 'w');
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- auto const lookup_alias = [do_only_path_search, &m_aliases = this->m_aliases](StringView alias) -> Optional<DeprecatedString> {
- if (do_only_path_search)
- return {};
- return m_aliases.get(alias);
- };
- auto const lookup_builtin = [do_only_path_search](StringView builtin) -> Optional<DeprecatedString> {
- if (do_only_path_search)
- return {};
- for (auto const& _builtin : builtin_names) {
- if (_builtin == builtin) {
- return builtin;
- }
- }
- return {};
- };
- bool at_least_one_succeded { false };
- for (auto const& argument : arguments) {
- auto const alias = lookup_alias(argument);
- if (alias.has_value()) {
- if (do_print_only_type)
- outln("{}: alias", argument);
- else
- outln("{}: aliased to {}", argument, alias.value());
- at_least_one_succeded = true;
- }
- auto const builtin = lookup_builtin(argument);
- if (builtin.has_value()) {
- if (do_print_only_type)
- outln("{}: builtin", builtin.value());
- else
- outln("{}: shell built-in command", builtin.value());
- at_least_one_succeded = true;
- }
- auto const executables = find_matching_executables_in_path(argument, do_follow_symlinks ? FollowSymlinks::Yes : FollowSymlinks::No);
- for (auto const& path : executables) {
- if (do_print_only_type)
- outln("{}: command", argument);
- else
- outln(path);
- at_least_one_succeded = true;
- }
- if (!at_least_one_succeded)
- warnln("{} not found", argument);
- }
- return at_least_one_succeded ? 0 : 1;
- }
- int Shell::builtin_alias(int argc, char const** argv)
- {
- Vector<DeprecatedString> 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 = argument.split_limit('=', 2, SplitBehavior::KeepEmpty);
- 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({ RunnablePath::Kind::Alias, parts[0] });
- }
- }
- return fail ? 1 : 0;
- }
- int Shell::builtin_unalias(int argc, char const** argv)
- {
- bool remove_all { false };
- Vector<DeprecatedString> arguments;
- Core::ArgsParser parser;
- parser.set_general_help("Remove alias from the list of aliases");
- parser.add_option(remove_all, "Remove all aliases", nullptr, 'a');
- parser.add_positional_argument(arguments, "List of aliases to remove", "alias", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (remove_all) {
- m_aliases.clear();
- cache_path();
- return 0;
- }
- if (arguments.is_empty()) {
- warnln("unalias: not enough arguments");
- parser.print_usage(stderr, argv[0]);
- return 1;
- }
- bool failed { false };
- for (auto& argument : arguments) {
- if (!m_aliases.contains(argument)) {
- warnln("unalias: {}: alias not found", argument);
- failed = true;
- continue;
- }
- m_aliases.remove(argument);
- remove_entry_from_cache(argument);
- }
- return failed ? 1 : 0;
- }
- int Shell::builtin_bg(int argc, char const** 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 = [&](auto value_ptr) -> bool {
- StringView value { value_ptr, strlen(value_ptr) };
- // 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, char const** argv)
- {
- Vector<DeprecatedString> 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.characters());
- 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"sv);
- if (fn.body) {
- auto formatter = Formatter(*fn.body);
- builder.append(formatter.format());
- printf("%s\n}\n", builder.to_deprecated_string().characters());
- } else {
- printf("%s\n}\n", builder.to_deprecated_string().characters());
- }
- }
- continue;
- }
- // check if its a builtin
- if (has_builtin(command)) {
- printf("%s is a shell builtin\n", command.characters());
- continue;
- }
- // check if its an executable in PATH
- auto fullpath = Core::DeprecatedFile::resolve_executable_from_environment(command);
- if (fullpath.has_value()) {
- printf("%s is %s\n", command.characters(), escape_token(fullpath.release_value()).characters());
- continue;
- }
- something_not_found = true;
- printf("type: %s not found\n", command.characters());
- }
- if (something_not_found)
- return 1;
- else
- return 0;
- }
- int Shell::builtin_cd(int argc, char const** argv)
- {
- char const* 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;
- DeprecatedString 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::DeprecatedFile::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);
- auto path_relative_to_current_directory = LexicalPath::relative_path(real_path, cwd);
- if (path_relative_to_current_directory.is_empty())
- path_relative_to_current_directory = real_path;
- char const* path = path_relative_to_current_directory.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 = move(real_path);
- setenv("PWD", cwd.characters(), 1);
- return 0;
- }
- int Shell::builtin_cdh(int argc, char const** 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("%zu: %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;
- }
- char const* path = cd_history.at(cd_history.size() - index).characters();
- char const* cd_args[] = { "cd", path, nullptr };
- return Shell::builtin_cd(2, cd_args);
- }
- int Shell::builtin_dirs(int argc, char const** 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<DeprecatedString> 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, char const** argv)
- {
- if (argc < 2) {
- warnln("Shell: No command given to exec");
- return 1;
- }
- Vector<char const*> argv_vector;
- argv_vector.append(argv + 1, argc - 1);
- argv_vector.append(nullptr);
- execute_process(move(argv_vector));
- }
- int Shell::builtin_exit(int argc, char const** 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), Core::ArgsParser::FailureBehavior::PrintUsage))
- 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);
- }
- int Shell::builtin_export(int argc, char const** argv)
- {
- Vector<DeprecatedString> 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 = value.split_limit('=', 2);
- if (parts.size() == 1) {
- auto value = lookup_local_variable(parts[0]);
- if (value) {
- auto values = const_cast<AST::Value&>(*value).resolve_as_list(*this);
- StringBuilder builder;
- builder.join(' ', values);
- parts.append(builder.to_deprecated_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, char const** argv)
- {
- Vector<DeprecatedString> 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, char const** 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 = [&](auto const* value_ptr) -> bool {
- StringView value { value_ptr, strlen(value_ptr) };
- // 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, char const** 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 = [&](auto const* value_ptr) -> bool {
- StringView value { value_ptr, strlen(value_ptr) };
- // 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<Job const*> 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, char const**)
- {
- for (size_t i = 0; i < m_editor->history().size(); ++i) {
- printf("%6zu %s\n", i + 1, m_editor->history()[i].entry.characters());
- }
- return 0;
- }
- int Shell::builtin_jobs(int argc, char const** 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, char const** argv)
- {
- if (directory_stack.size() <= 1) {
- warnln("Shell: popd: directory stack empty");
- return 1;
- }
- bool should_not_switch = false;
- 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;
- auto popped_path = directory_stack.take_last();
- if (should_not_switch)
- return 0;
- auto new_path = LexicalPath::canonicalized_path(popped_path);
- if (chdir(new_path.characters()) < 0) {
- warnln("chdir({}) failed: {}", new_path, strerror(errno));
- return 1;
- }
- cwd = new_path;
- return 0;
- }
- int Shell::builtin_pushd(int argc, char const** 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;
- }
- DeprecatedString dir1 = directory_stack.take_first();
- DeprecatedString 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], strlen(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++) {
- char const* arg = argv[i];
- if (arg[0] != '-') {
- if (arg[0] == '/') {
- path_builder.append({ arg, strlen(arg) });
- } else
- path_builder.appendff("{}/{}", cwd, arg);
- }
- if (!strcmp(arg, "-n"))
- should_switch = false;
- }
- }
- auto real_path = LexicalPath::canonicalized_path(path_builder.to_deprecated_string());
- struct stat st;
- int rc = stat(real_path.characters(), &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.characters());
- if (rc < 0) {
- warnln("chdir({}) failed: {}", real_path, strerror(errno));
- return 1;
- }
- cwd = real_path;
- }
- return 0;
- }
- int Shell::builtin_pwd(int, char const**)
- {
- print_path(cwd);
- fputc('\n', stdout);
- return 0;
- }
- int Shell::builtin_setopt(int argc, char const** 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, char const** 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"sv);
- if (!argv_) {
- warnln("shift: ARGV is unset");
- return 1;
- }
- if (!argv_->is_list())
- argv_ = adopt_ref(*new AST::ListValue({ const_cast<AST::Value&>(*argv_) }));
- auto& values = const_cast<AST::ListValue*>(static_cast<AST::ListValue const*>(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)
- (void)values.take_first();
- return 0;
- }
- int Shell::builtin_source(int argc, char const** argv)
- {
- char const* file_to_source = nullptr;
- Vector<DeprecatedString> 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;
- auto previous_argv = lookup_local_variable("ARGV"sv);
- ScopeGuard guard { [&] {
- if (!args.is_empty())
- set_local_variable("ARGV", const_cast<AST::Value&>(*previous_argv));
- } };
- if (!args.is_empty())
- set_local_variable("ARGV", AST::make_ref_counted<AST::ListValue>(move(args)));
- if (!run_file(file_to_source, true))
- return 126;
- return 0;
- }
- int Shell::builtin_time(int argc, char const** argv)
- {
- AST::Command command;
- int number_of_iterations = 1;
- Core::ArgsParser parser;
- parser.add_option(number_of_iterations, "Number of iterations", "iterations", 'n', "iterations");
- parser.set_stop_on_first_non_option(true);
- parser.add_positional_argument(command.argv, "Command to execute with arguments", "command", Core::ArgsParser::Required::Yes);
- if (!parser.parse(argc, const_cast<char**>(argv), Core::ArgsParser::FailureBehavior::PrintUsage))
- return 1;
- if (number_of_iterations < 1)
- return 1;
- auto commands = expand_aliases({ move(command) });
- AK::Statistics iteration_times;
- int exit_code = 1;
- for (int i = 0; i < number_of_iterations; ++i) {
- auto timer = Core::ElapsedTimer::start_new();
- for (auto& job : run_commands(commands)) {
- block_on_job(job);
- exit_code = job.exit_code();
- }
- iteration_times.add(static_cast<float>(timer.elapsed()));
- }
- if (number_of_iterations == 1) {
- warnln("Time: {} ms", iteration_times.values().first());
- } else {
- AK::Statistics iteration_times_excluding_first;
- for (size_t i = 1; i < iteration_times.size(); i++)
- iteration_times_excluding_first.add(iteration_times.values()[i]);
- warnln("Timing report: {} ms", iteration_times.sum());
- warnln("==============");
- warnln("Command: {}", DeprecatedString::join(' ', Span<char const*>(argv, argc)));
- warnln("Average time: {:.2} ms (median: {}, stddev: {:.2}, min: {}, max:{})",
- iteration_times.average(), iteration_times.median(),
- iteration_times.standard_deviation(),
- iteration_times.min(), iteration_times.max());
- warnln("Excluding first: {:.2} ms (median: {}, stddev: {:.2}, min: {}, max:{})",
- iteration_times_excluding_first.average(), iteration_times_excluding_first.median(),
- iteration_times_excluding_first.standard_deviation(),
- iteration_times_excluding_first.min(), iteration_times_excluding_first.max());
- }
- return exit_code;
- }
- int Shell::builtin_umask(int argc, char const** argv)
- {
- char const* 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, char const** 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 = [&](auto const* value_ptr) -> bool {
- StringView value { value_ptr, strlen(value_ptr) };
- // 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 (auto const& 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, char const** argv)
- {
- Vector<DeprecatedString> 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;
- bool did_touch_path = false;
- for (auto& value : vars) {
- if (!did_touch_path && value == "PATH"sv)
- did_touch_path = true;
- if (lookup_local_variable(value)) {
- unset_local_variable(value);
- } else {
- unsetenv(value.characters());
- }
- }
- if (did_touch_path)
- cache_path();
- return 0;
- }
- int Shell::builtin_not(int argc, char const** 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.value_or(0);
- return exit_code == 0 ? 1 : 0;
- }
- int Shell::builtin_kill(int argc, char const** argv)
- {
- // Simply translate the arguments and pass them to `kill'
- Vector<DeprecatedString> replaced_values;
- auto kill_path = Core::DeprecatedFile::resolve_executable_from_environment("kill"sv);
- if (!kill_path.has_value()) {
- warnln("kill: `kill' not found in PATH");
- return 126;
- }
- replaced_values.append(kill_path.release_value());
- for (auto i = 1; i < argc; ++i) {
- if (auto job_id = resolve_job_spec({ argv[i], strlen(argv[1]) }); job_id.has_value()) {
- auto job = find_job(job_id.value());
- if (job) {
- replaced_values.append(DeprecatedString::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;
- auto job_result = run_command(command);
- if (job_result.is_error()) {
- warnln("kill: Failed to run {}: {}", command.argv.first(), job_result.error());
- return exit_code;
- }
- if (auto job = job_result.release_value()) {
- block_on_job(job);
- exit_code = job->exit_code();
- }
- return exit_code;
- }
- bool Shell::run_builtin(const AST::Command& command, NonnullRefPtrVector<AST::Rewiring> const& rewirings, int& retval)
- {
- if (command.argv.is_empty())
- return false;
- if (!has_builtin(command.argv.first()))
- return false;
- Vector<char const*> 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();
- if (name == ":"sv)
- name = "noop"sv;
- #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;
- }
- int Shell::builtin_argsparser_parse(int argc, char const** argv)
- {
- // argsparser_parse
- // --add-option variable [--type (bool | string | i32 | u32 | double | size)] --help-string "" --long-name "" --short-name "" [--value-name "" <if not --type bool>] --list
- // --add-positional-argument variable [--type (bool | string | i32 | u32 | double | size)] ([--min n] [--max n] | [--required]) --help-string "" --value-name ""
- // [--general-help ""]
- // [--stop-on-first-non-option]
- // --
- // $args_to_parse
- Core::ArgsParser parser;
- Core::ArgsParser user_parser;
- Vector<char const*> arguments;
- Variant<Core::ArgsParser::Option, Core::ArgsParser::Arg, Empty> current;
- DeprecatedString current_variable;
- // if max > 1 or min < 1, or explicit `--list`.
- bool treat_arg_as_list = false;
- enum class Type {
- Bool,
- String,
- I32,
- U32,
- Double,
- Size,
- };
- auto type = Type::String;
- auto try_convert = [](StringView value, Type type) -> Optional<RefPtr<AST::Value>> {
- switch (type) {
- case Type::Bool:
- return AST::make_ref_counted<AST::StringValue>("true");
- case Type::String:
- return AST::make_ref_counted<AST::StringValue>(value);
- case Type::I32:
- if (auto number = value.to_int(); number.has_value())
- return AST::make_ref_counted<AST::StringValue>(DeprecatedString::number(*number));
- warnln("Invalid value for type i32: {}", value);
- return {};
- case Type::U32:
- case Type::Size:
- if (auto number = value.to_uint(); number.has_value())
- return AST::make_ref_counted<AST::StringValue>(DeprecatedString::number(*number));
- warnln("Invalid value for type u32|size: {}", value);
- return {};
- case Type::Double: {
- DeprecatedString string = value;
- char* endptr = nullptr;
- auto number = strtod(string.characters(), &endptr);
- if (endptr != string.characters() + string.length()) {
- warnln("Invalid value for type double: {}", value);
- return {};
- }
- return AST::make_ref_counted<AST::StringValue>(DeprecatedString::number(number));
- }
- default:
- VERIFY_NOT_REACHED();
- }
- };
- auto enlist = [&](auto name, auto value) -> NonnullRefPtr<AST::Value> {
- auto variable = lookup_local_variable(name);
- if (variable) {
- auto list = const_cast<AST::Value&>(*variable).resolve_as_list(*this);
- auto new_value = value->resolve_as_string(*this);
- list.append(move(new_value));
- return make_ref_counted<AST::ListValue>(move(list));
- }
- return *value;
- };
- auto commit = [&] {
- return current.visit(
- [&](Core::ArgsParser::Option& option) {
- if (!option.long_name && !option.short_name) {
- warnln("Defined option must have at least one of --long-name or --short-name");
- return false;
- }
- option.accept_value = [&, current_variable, treat_arg_as_list, type](auto value) {
- auto result = try_convert({ value, strlen(value) }, type);
- if (result.has_value()) {
- auto value = result.release_value();
- if (treat_arg_as_list)
- value = enlist(current_variable, move(value));
- this->set_local_variable(current_variable, move(value), true);
- return true;
- }
- return false;
- };
- user_parser.add_option(move(option));
- type = Type::String;
- treat_arg_as_list = false;
- return true;
- },
- [&](Core::ArgsParser::Arg& arg) {
- if (!arg.name) {
- warnln("Defined positional argument must have a name");
- return false;
- }
- arg.accept_value = [&, current_variable, treat_arg_as_list, type](auto value) {
- auto result = try_convert({ value, strlen(value) }, type);
- if (result.has_value()) {
- auto value = result.release_value();
- if (treat_arg_as_list)
- value = enlist(current_variable, move(value));
- this->set_local_variable(current_variable, move(value), true);
- return true;
- }
- return false;
- };
- user_parser.add_positional_argument(move(arg));
- type = Type::String;
- treat_arg_as_list = false;
- return true;
- },
- [&](Empty) {
- return true;
- });
- };
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::None,
- .help_string = "Stop processing arguments after a non-argument parameter is seen",
- .long_name = "stop-on-first-non-option",
- .accept_value = [&](auto) {
- user_parser.set_stop_on_first_non_option(true);
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Set the general help string for the parser",
- .long_name = "general-help",
- .value_name = "string",
- .accept_value = [&](auto value) {
- user_parser.set_general_help(value);
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Start describing an option",
- .long_name = "add-option",
- .value_name = "variable-name",
- .accept_value = [&](auto name) {
- if (!commit())
- return false;
- current = Core::ArgsParser::Option {};
- current_variable = name;
- if (current_variable.is_empty() || !all_of(current_variable, [](auto ch) { return ch == '_' || isalnum(ch); })) {
- warnln("Option variable name must be a valid identifier");
- return false;
- }
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::None,
- .help_string = "Accept multiple of the current option being given",
- .long_name = "list",
- .accept_value = [&](auto) {
- if (!current.has<Core::ArgsParser::Option>()) {
- warnln("Must be defining an option to use --list");
- return false;
- }
- treat_arg_as_list = true;
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Define the type of the option or argument being described",
- .long_name = "type",
- .value_name = "type",
- .accept_value = [&](auto name) {
- if (current.has<Empty>()) {
- warnln("Must be defining an argument or option to use --type");
- return false;
- }
- StringView ty { name, strlen(name) };
- if (ty == "bool") {
- if (auto option = current.get_pointer<Core::ArgsParser::Option>()) {
- if (option->value_name != nullptr) {
- warnln("Type 'bool' does not apply to options with a value (value name is set to {})", option->value_name);
- return false;
- }
- }
- type = Type::Bool;
- } else if (ty == "string") {
- type = Type::String;
- } else if (ty == "i32") {
- type = Type::I32;
- } else if (ty == "u32") {
- type = Type::U32;
- } else if (ty == "double") {
- type = Type::Double;
- } else if (ty == "size") {
- type = Type::Size;
- } else {
- warnln("Invalid type '{}', expected one of bool | string | i32 | u32 | double | size", ty);
- return false;
- }
- if (type == Type::Bool)
- set_local_variable(current_variable, make_ref_counted<AST::StringValue>("false"), true);
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Set the help string of the option or argument being defined",
- .long_name = "help-string",
- .value_name = "string",
- .accept_value = [&](auto value) {
- return current.visit(
- [](Empty) {
- warnln("Must be defining an option or argument to use --help-string");
- return false;
- },
- [&](auto& option) {
- option.help_string = value;
- return true;
- });
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Set the long name of the option being defined",
- .long_name = "long-name",
- .value_name = "name",
- .accept_value = [&](auto value) {
- auto option = current.get_pointer<Core::ArgsParser::Option>();
- if (!option) {
- warnln("Must be defining an option to use --long-name");
- return false;
- }
- if (option->long_name) {
- warnln("Repeated application of --long-name is not allowed, current option has long name set to \"{}\"", option->long_name);
- return false;
- }
- option->long_name = value;
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Set the short name of the option being defined",
- .long_name = "short-name",
- .value_name = "char",
- .accept_value = [&](auto value) {
- auto option = current.get_pointer<Core::ArgsParser::Option>();
- if (!option) {
- warnln("Must be defining an option to use --short-name");
- return false;
- }
- if (strlen(value) != 1) {
- warnln("Option short name ('{}') must be exactly one character long", value);
- return false;
- }
- if (option->short_name) {
- warnln("Repeated application of --short-name is not allowed, current option has short name set to '{}'", option->short_name);
- return false;
- }
- option->short_name = value[0];
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Set the value name of the option being defined",
- .long_name = "value-name",
- .value_name = "string",
- .accept_value = [&](auto value) {
- return current.visit(
- [](Empty) {
- warnln("Must be defining an option or a positional argument to use --value-name");
- return false;
- },
- [&](Core::ArgsParser::Option& option) {
- if (option.value_name) {
- warnln("Repeated application of --value-name is not allowed, current option has value name set to \"{}\"", option.value_name);
- return false;
- }
- if (type == Type::Bool) {
- warnln("Options of type bool cannot have a value name");
- return false;
- }
- option.value_name = value;
- return true;
- },
- [&](Core::ArgsParser::Arg& arg) {
- if (arg.name) {
- warnln("Repeated application of --value-name is not allowed, current argument has value name set to \"{}\"", arg.name);
- return false;
- }
- arg.name = value;
- return true;
- });
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Start describing a positional argument",
- .long_name = "add-positional-argument",
- .value_name = "variable",
- .accept_value = [&](auto value) {
- if (!commit())
- return false;
- current = Core::ArgsParser::Arg {};
- current_variable = value;
- if (current_variable.is_empty() || !all_of(current_variable, [](auto ch) { return ch == '_' || isalnum(ch); })) {
- warnln("Argument variable name must be a valid identifier");
- return false;
- }
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Set the minimum required number of positional arguments for the argument being described",
- .long_name = "min",
- .value_name = "n",
- .accept_value = [&](auto value) {
- auto arg = current.get_pointer<Core::ArgsParser::Arg>();
- if (!arg) {
- warnln("Must be describing a positional argument to use --min");
- return false;
- }
- auto number = StringView { value, strlen(value) }.to_uint();
- if (!number.has_value()) {
- warnln("Invalid value for --min: '{}', expected a non-negative number", value);
- return false;
- }
- if (static_cast<unsigned>(arg->max_values) < *number) {
- warnln("Invalid value for --min: {}, min must not be larger than max ({})", *number, arg->max_values);
- return false;
- }
- arg->min_values = *number;
- treat_arg_as_list = arg->max_values > 1 || arg->min_values < 1;
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
- .help_string = "Set the maximum required number of positional arguments for the argument being described",
- .long_name = "max",
- .value_name = "n",
- .accept_value = [&](auto value) {
- auto arg = current.get_pointer<Core::ArgsParser::Arg>();
- if (!arg) {
- warnln("Must be describing a positional argument to use --max");
- return false;
- }
- auto number = StringView { value, strlen(value) }.to_uint();
- if (!number.has_value()) {
- warnln("Invalid value for --max: '{}', expected a non-negative number", value);
- return false;
- }
- if (static_cast<unsigned>(arg->min_values) > *number) {
- warnln("Invalid value for --max: {}, max must not be smaller than min ({})", *number, arg->min_values);
- return false;
- }
- arg->max_values = *number;
- treat_arg_as_list = arg->max_values > 1 || arg->min_values < 1;
- return true;
- },
- });
- parser.add_option(Core::ArgsParser::Option {
- .argument_mode = Core::ArgsParser::OptionArgumentMode::None,
- .help_string = "Mark the positional argument being described as required (shorthand for --min 1)",
- .long_name = "required",
- .accept_value = [&](auto) {
- auto arg = current.get_pointer<Core::ArgsParser::Arg>();
- if (!arg) {
- warnln("Must be describing a positional argument to use --required");
- return false;
- }
- arg->min_values = 1;
- if (arg->max_values < arg->min_values)
- arg->max_values = 1;
- treat_arg_as_list = arg->max_values > 1 || arg->min_values < 1;
- return true;
- },
- });
- parser.add_positional_argument(arguments, "Arguments to parse via the described ArgsParser configuration", "arg", Core::ArgsParser::Required::No);
- if (!parser.parse(argc, const_cast<char* const*>(argv), Core::ArgsParser::FailureBehavior::Ignore))
- return 2;
- if (!commit())
- return 2;
- if (!user_parser.parse(static_cast<int>(arguments.size()), const_cast<char* const*>(arguments.data()), Core::ArgsParser::FailureBehavior::Ignore))
- return 1;
- return 0;
- }
- bool Shell::has_builtin(StringView name) const
- {
- if (name == ":"sv)
- return true;
- #define __ENUMERATE_SHELL_BUILTIN(builtin) \
- if (name == #builtin) { \
- return true; \
- }
- ENUMERATE_SHELL_BUILTINS();
- #undef __ENUMERATE_SHELL_BUILTIN
- return false;
- }
- }
|