summaryrefslogtreecommitdiff
path: root/Userland/BuggieBox
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2022-09-27 05:53:51 +0300
committerAndrew Kaster <andrewdkaster@gmail.com>2022-11-26 12:41:47 -0700
commit35efdb17f9c3856d1c241d9f63619a4d1174a509 (patch)
treedbc3f80ca939044b36933714261ba7414b857618 /Userland/BuggieBox
parentbc0d56fa747e95218c4258ead8a52cd0cd3df16d (diff)
downloadserenity-35efdb17f9c3856d1c241d9f63619a4d1174a509.zip
Userland: Add the BuggieBox program
This will be our alternative to what is known as BusyBox for Linux distributions. Co-Authored-By: Tim Schumacher <timschumi@gmx.de>
Diffstat (limited to 'Userland/BuggieBox')
-rw-r--r--Userland/BuggieBox/CMakeLists.txt51
-rw-r--r--Userland/BuggieBox/main.cpp111
2 files changed, 162 insertions, 0 deletions
diff --git a/Userland/BuggieBox/CMakeLists.txt b/Userland/BuggieBox/CMakeLists.txt
new file mode 100644
index 0000000000..200daa5142
--- /dev/null
+++ b/Userland/BuggieBox/CMakeLists.txt
@@ -0,0 +1,51 @@
+serenity_component(
+ BuggieBox
+ REQUIRED
+ TARGETS BuggieBox
+)
+
+function (buggiebox_utility src)
+ get_filename_component(utility ${src} NAME_WE)
+ target_sources(BuggieBox PRIVATE ${src})
+ set_source_files_properties(${src} PROPERTIES COMPILE_DEFINITIONS "serenity_main=${utility}_main")
+endfunction()
+
+set(utility_srcs
+ ../Utilities/cat.cpp
+ ../Utilities/checksum.cpp
+ ../Utilities/chmod.cpp
+ ../Utilities/chown.cpp
+ ../Utilities/cp.cpp
+ ../Utilities/df.cpp
+ ../Utilities/env.cpp
+ ../Utilities/file.cpp
+ ../Utilities/find.cpp
+ ../Utilities/id.cpp
+ ../Utilities/less.cpp
+ ../Utilities/ln.cpp
+ ../Utilities/ls.cpp
+ ../Utilities/lsblk.cpp
+ ../Utilities/mkdir.cpp
+ ../Utilities/mknod.cpp
+ ../Utilities/mount.cpp
+ ../Utilities/mv.cpp
+ ../Utilities/ps.cpp
+ ../Utilities/rm.cpp
+ ../Utilities/rmdir.cpp
+ ../Utilities/tail.cpp
+ ../Utilities/tree.cpp
+ ../Utilities/umount.cpp
+ ../Utilities/uname.cpp
+ ../Utilities/uniq.cpp
+)
+
+serenity_bin(BuggieBox)
+target_sources(BuggieBox PRIVATE main.cpp)
+target_link_libraries(BuggieBox PRIVATE LibMain LibShell LibCompress LibCore LibCrypto LibGfx LibLine LibRegex)
+
+foreach(file IN LISTS utility_srcs)
+ buggiebox_utility(${file})
+endforeach()
+
+target_sources(BuggieBox PRIVATE ../Shell/main.cpp)
+set_source_files_properties( ../Shell/main.cpp PROPERTIES COMPILE_DEFINITIONS "serenity_main=sh_main")
diff --git a/Userland/BuggieBox/main.cpp b/Userland/BuggieBox/main.cpp
new file mode 100644
index 0000000000..d951d7ea3b
--- /dev/null
+++ b/Userland/BuggieBox/main.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ * Copyright (c) 2022, Tim Schumacher <timschumi@gmx.de>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/LexicalPath.h>
+#include <LibMain/Main.h>
+#include <Userland/Shell/Shell.h>
+
+#define ENUMERATE_UTILITIES(E) \
+ E(cat) \
+ E(checksum) \
+ E(chmod) \
+ E(chown) \
+ E(cp) \
+ E(df) \
+ E(env) \
+ E(file) \
+ E(find) \
+ E(id) \
+ E(less) \
+ E(ln) \
+ E(ls) \
+ E(lsblk) \
+ E(mkdir) \
+ E(mknod) \
+ E(mount) \
+ E(mv) \
+ E(ps) \
+ E(rm) \
+ E(sh) \
+ E(rmdir) \
+ E(tail) \
+ E(tree) \
+ E(umount) \
+ E(uname) \
+ E(uniq)
+
+// Declare the entrypoints of all the tools that we delegate to.
+// Relying on `decltype(serenity_main)` ensures that we always stay consistent with the `serenity_main` signature.
+#define DECLARE_ENTRYPOINT(name) decltype(serenity_main) name##_main;
+ENUMERATE_UTILITIES(DECLARE_ENTRYPOINT)
+#undef DECLARE_ENTRYPOINT
+
+static void fail()
+{
+ out(stderr, "Direct running of BuggieBox was detected without finding a proper requested utility.\n");
+ out(stderr, "The following programs are supported: uname, env, lsblk, file, df, mount, umount, mkdir, ");
+ out(stderr, "rmdir, rm, chown, chmod, cp, ln, ls, mv, cat, md5sum, sha1sum, sha256sum, sha512sum, sh, uniq, id, tail, ");
+ out(stderr, "find, less, mknod, ps\n");
+ out(stderr, "To use one of these included utilities, create a symbolic link with the target being this binary, and ensure the basename");
+ out(stderr, "is included within.\n");
+}
+
+struct Runner {
+ StringView name;
+ ErrorOr<int> (*func)(Main::Arguments arguments) = nullptr;
+};
+
+static constexpr Runner s_runners[] = {
+#define RUNNER_ENTRY(name) { #name##sv, name##_main },
+ ENUMERATE_UTILITIES(RUNNER_ENTRY)
+#undef RUNNER_ENTRY
+
+ // Some tools have additional aliases.
+ { "md5sum"sv, checksum_main },
+ { "sha1sum"sv, checksum_main },
+ { "sha256sum"sv, checksum_main },
+ { "sha512sum"sv, checksum_main },
+ { "Shell"sv, sh_main },
+};
+
+static ErrorOr<int> run_program(Main::Arguments arguments, LexicalPath const& runbase, bool& found_runner)
+{
+ for (auto& runner : s_runners) {
+ if (runbase.basename() == runner.name) {
+ found_runner = true;
+ return runner.func(arguments);
+ }
+ }
+ return 0;
+}
+
+static ErrorOr<int> buggiebox_main(Main::Arguments arguments)
+{
+ if (arguments.argc == 0) {
+ fail();
+ return 1;
+ }
+ bool found_runner = false;
+ LexicalPath runbase { arguments.strings[0] };
+ auto result = TRY(run_program(arguments, runbase, found_runner));
+ if (!found_runner)
+ fail();
+ return result;
+}
+
+ErrorOr<int> serenity_main(Main::Arguments arguments)
+{
+ LexicalPath runbase { arguments.strings[0] };
+ if (runbase.basename() == "BuggieBox"sv) {
+ Main::Arguments utility_arguments = arguments;
+ utility_arguments.argc--;
+ utility_arguments.argv++;
+ utility_arguments.strings = arguments.strings.slice(1);
+ return buggiebox_main(utility_arguments);
+ }
+ return buggiebox_main(arguments);
+}