From b0659b50dce890dbf29b7cb8f0239ee6391a9f03 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 2 Dec 2021 23:40:42 +0100 Subject: [PATCH] jp: Port to LibMain :^) --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/jp.cpp | 40 +++++++++++-------------------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index fb5ea82721..edc37d59e4 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -82,6 +82,7 @@ target_link_libraries(grep LibRegex) target_link_libraries(gunzip LibCompress) target_link_libraries(gzip LibCompress) target_link_libraries(id LibMain) +target_link_libraries(jp LibMain) target_link_libraries(js LibJS LibLine LibMain) target_link_libraries(keymap LibKeyboard LibMain) target_link_libraries(logout LibMain) diff --git a/Userland/Utilities/jp.cpp b/Userland/Utilities/jp.cpp index 7ab6099e9c..99612c1adb 100644 --- a/Userland/Utilities/jp.cpp +++ b/Userland/Utilities/jp.cpp @@ -5,14 +5,14 @@ */ #include -#include #include #include #include #include #include #include -#include +#include +#include #include static void print(const JsonValue& value, int spaces_per_indent, int indent = 0, bool use_color = true); @@ -22,14 +22,11 @@ static void print_indent(int indent, int spaces_per_indent) out(" "); } -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio rpath")); - const char* path = nullptr; + StringView path; int spaces_in_indent = 4; Core::ArgsParser args_parser; @@ -37,28 +34,19 @@ int main(int argc, char** argv) args_parser.add_option(spaces_in_indent, "Indent size", "indent-size", 'i', "spaces_in_indent"); args_parser.add_positional_argument(path, "Path to JSON file", "path", Core::ArgsParser::Required::No); VERIFY(spaces_in_indent >= 0); - args_parser.parse(argc, argv); - if (path == nullptr) - path = "/dev/stdin"; - auto file = Core::File::construct(path); - if (!file->open(Core::OpenMode::ReadOnly)) { - warnln("Couldn't open {} for reading: {}", path, file->error_string()); - return 1; - } + args_parser.parse(arguments); - if (pledge("stdio", nullptr) < 0) { - perror("pledge"); - return 1; - } + if (path == nullptr) + path = "/dev/stdin"sv; + + auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); + + TRY(Core::System::pledge("stdio")); auto file_contents = file->read_all(); - auto json = JsonValue::from_string(file_contents); - if (json.is_error()) { - warnln("Couldn't parse {} as JSON", path); - return 1; - } + auto json = TRY(JsonValue::from_string(file_contents)); - print(json.value(), spaces_in_indent, 0, isatty(STDOUT_FILENO)); + print(json, spaces_in_indent, 0, isatty(STDOUT_FILENO)); outln(); return 0;