From 97c5a78d27e9dddc12e6b20430892d11016cb6dd Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Tue, 1 Dec 2020 14:54:00 +0330 Subject: [PATCH] Shell: Add a 'glob' builtin This builtin takes a bunch of strings, resolves them as globs (in the current directory) and prints out the matching entries. Its main use is to allow dynamic glob resolution: ```sh glob "$whatever/*" ``` --- Shell/Builtin.cpp | 17 +++++++++++++++++ Shell/Shell.h | 1 + 2 files changed, 18 insertions(+) diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp index a0a7c539c4..e19e8cfce3 100644 --- a/Shell/Builtin.cpp +++ b/Shell/Builtin.cpp @@ -327,6 +327,23 @@ int Shell::builtin_export(int argc, const char** argv) return 0; } +int Shell::builtin_glob(int argc, const char** argv) +{ + Vector globs; + Core::ArgsParser parser; + parser.add_positional_argument(globs, "Globs to resolve", "glob"); + + if (!parser.parse(argc, const_cast(argv), false)) + 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; diff --git a/Shell/Shell.h b/Shell/Shell.h index eca2f47a20..f552595814 100644 --- a/Shell/Shell.h +++ b/Shell/Shell.h @@ -47,6 +47,7 @@ __ENUMERATE_SHELL_BUILTIN(pwd) \ __ENUMERATE_SHELL_BUILTIN(exit) \ __ENUMERATE_SHELL_BUILTIN(export) \ + __ENUMERATE_SHELL_BUILTIN(glob) \ __ENUMERATE_SHELL_BUILTIN(unset) \ __ENUMERATE_SHELL_BUILTIN(history) \ __ENUMERATE_SHELL_BUILTIN(umask) \