summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-12-01 14:54:00 +0330
committerAndreas Kling <kling@serenityos.org>2020-12-29 16:55:43 +0100
commit97c5a78d27e9dddc12e6b20430892d11016cb6dd (patch)
treea0c48515e6d26d0e0f600d360a040c0e99e3cbdc
parentaf28a8ad117924f00b227418a2d4a3040bc47dad (diff)
downloadserenity-97c5a78d27e9dddc12e6b20430892d11016cb6dd.zip
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/*" ```
-rw-r--r--Shell/Builtin.cpp17
-rw-r--r--Shell/Shell.h1
2 files changed, 18 insertions, 0 deletions
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<const char*> globs;
+ Core::ArgsParser parser;
+ parser.add_positional_argument(globs, "Globs to resolve", "glob");
+
+ if (!parser.parse(argc, const_cast<char**>(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) \