1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

jp: Port to LibMain :^)

This commit is contained in:
Andreas Kling 2021-12-02 23:40:42 +01:00
parent c3733bea92
commit b0659b50dc
2 changed files with 15 additions and 26 deletions

View file

@ -5,14 +5,14 @@
*/
#include <AK/Assertions.h>
#include <AK/ByteBuffer.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <stdio.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <unistd.h>
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<int> 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;