From db886fe18bad881c1e1064780937c75e92e52b5f Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 21 Feb 2023 15:14:41 +0330 Subject: [PATCH] Userland+AK: Stop using getopt() for ArgsParser This commit moves the implementation of getopt into AK, and converts its API to understand and use StringView instead of char*. Everything else is caught in the crossfire of making Option::accept_value() take a StringView instead of a char const*. With this, we must now pass a Span to ArgsParser::parse(), applications using LibMain are unaffected, but anything not using that or taking its own argc/argv has to construct a Vector for this method. --- AK/CMakeLists.txt | 7 +- AK/OptionParser.cpp | 283 +++++++++++++ AK/OptionParser.h | 84 ++++ .../CodeGenerators/LibEDID/GeneratePnpIDs.cpp | 2 +- .../LibWeb/BindingsGenerator/main.cpp | 4 +- Tests/Kernel/crash.cpp | 9 +- Tests/Kernel/stress-truncate.cpp | 7 +- Tests/Kernel/stress-writeread.cpp | 7 +- Tests/LibCore/TestLibCoreArgsParser.cpp | 96 ++--- Tests/LibJS/test262-runner.cpp | 7 +- Userland/Libraries/LibC/getopt.cpp | 387 +++--------------- Userland/Libraries/LibCore/ArgsParser.cpp | 144 ++++--- Userland/Libraries/LibCore/ArgsParser.h | 29 +- .../LibTest/JavaScriptTestRunnerMain.cpp | 9 +- Userland/Libraries/LibTest/TestMain.cpp | 8 +- Userland/Libraries/LibTest/TestSuite.cpp | 4 +- Userland/Libraries/LibTest/TestSuite.h | 2 +- Userland/Shell/Builtin.cpp | 64 +-- Userland/Utilities/cp.cpp | 6 +- Userland/Utilities/cut.cpp | 8 +- Userland/Utilities/du.cpp | 5 +- Userland/Utilities/grep.cpp | 6 +- Userland/Utilities/gron.cpp | 4 +- Userland/Utilities/mount.cpp | 2 +- Userland/Utilities/mv.cpp | 2 +- Userland/Utilities/nl.cpp | 8 +- Userland/Utilities/pidof.cpp | 2 +- Userland/Utilities/pro.cpp | 6 +- Userland/Utilities/profile.cpp | 2 +- Userland/Utilities/readelf.cpp | 2 +- Userland/Utilities/rm.cpp | 2 +- Userland/Utilities/run-tests.cpp | 2 +- Userland/Utilities/strings.cpp | 3 +- Userland/Utilities/syscall.cpp | 2 +- Userland/Utilities/sysctl.cpp | 2 +- Userland/Utilities/test-fuzz.cpp | 2 +- Userland/Utilities/test-unveil.cpp | 9 +- Userland/Utilities/top.cpp | 3 +- Userland/Utilities/tr.cpp | 6 +- Userland/Utilities/truncate.cpp | 6 +- Userland/Utilities/tt.cpp | 2 +- Userland/Utilities/ttfdisasm.cpp | 2 +- Userland/Utilities/wasm.cpp | 10 +- 43 files changed, 673 insertions(+), 584 deletions(-) create mode 100644 AK/OptionParser.cpp create mode 100644 AK/OptionParser.h diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index 72e513c60a9..5d6539ffbe0 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -2,9 +2,9 @@ set(AK_SOURCES Assertions.cpp Base64.cpp CircularBuffer.cpp + DOSPackedTime.cpp DeprecatedFlyString.cpp DeprecatedString.cpp - DOSPackedTime.cpp Error.cpp FloatingPointStringConversions.cpp FlyString.cpp @@ -16,10 +16,10 @@ set(AK_SOURCES JsonParser.cpp JsonPath.cpp JsonValue.cpp - kmalloc.cpp LexicalPath.cpp MemoryStream.cpp NumberFormat.cpp + OptionParser.cpp Random.cpp StackInfo.cpp Stream.cpp @@ -32,10 +32,11 @@ set(AK_SOURCES Time.cpp URL.cpp URLParser.cpp + UUID.cpp Utf16View.cpp Utf32View.cpp Utf8View.cpp - UUID.cpp + kmalloc.cpp ) # AK sources are included from many different places, such as the Kernel, LibC, and Loader list(TRANSFORM AK_SOURCES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/") diff --git a/AK/OptionParser.cpp b/AK/OptionParser.cpp new file mode 100644 index 00000000000..681e24fa26e --- /dev/null +++ b/AK/OptionParser.cpp @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2023, Ali Mohammad Pur + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace AK { + +void OptionParser::reset_state() +{ + m_arg_index = 0; + m_consumed_args = 0; + m_index_into_multioption_argument = 0; + m_stop_on_first_non_option = false; +} + +OptionParser::GetOptResult OptionParser::getopt(Span args, StringView short_options, Span