mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:07:44 +00:00
jp: Port to LibMain :^)
This commit is contained in:
parent
c3733bea92
commit
b0659b50dc
2 changed files with 15 additions and 26 deletions
|
@ -82,6 +82,7 @@ target_link_libraries(grep LibRegex)
|
||||||
target_link_libraries(gunzip LibCompress)
|
target_link_libraries(gunzip LibCompress)
|
||||||
target_link_libraries(gzip LibCompress)
|
target_link_libraries(gzip LibCompress)
|
||||||
target_link_libraries(id LibMain)
|
target_link_libraries(id LibMain)
|
||||||
|
target_link_libraries(jp LibMain)
|
||||||
target_link_libraries(js LibJS LibLine LibMain)
|
target_link_libraries(js LibJS LibLine LibMain)
|
||||||
target_link_libraries(keymap LibKeyboard LibMain)
|
target_link_libraries(keymap LibKeyboard LibMain)
|
||||||
target_link_libraries(logout LibMain)
|
target_link_libraries(logout LibMain)
|
||||||
|
|
|
@ -5,14 +5,14 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include <AK/ByteBuffer.h>
|
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <AK/JsonValue.h>
|
#include <AK/JsonValue.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <stdio.h>
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static void print(const JsonValue& value, int spaces_per_indent, int indent = 0, bool use_color = true);
|
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(" ");
|
out(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
if (pledge("stdio rpath", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio rpath"));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* path = nullptr;
|
StringView path;
|
||||||
int spaces_in_indent = 4;
|
int spaces_in_indent = 4;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
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_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);
|
args_parser.add_positional_argument(path, "Path to JSON file", "path", Core::ArgsParser::Required::No);
|
||||||
VERIFY(spaces_in_indent >= 0);
|
VERIFY(spaces_in_indent >= 0);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(arguments);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pledge("stdio", nullptr) < 0) {
|
if (path == nullptr)
|
||||||
perror("pledge");
|
path = "/dev/stdin"sv;
|
||||||
return 1;
|
|
||||||
}
|
auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
|
||||||
|
|
||||||
|
TRY(Core::System::pledge("stdio"));
|
||||||
|
|
||||||
auto file_contents = file->read_all();
|
auto file_contents = file->read_all();
|
||||||
auto json = JsonValue::from_string(file_contents);
|
auto json = TRY(JsonValue::from_string(file_contents));
|
||||||
if (json.is_error()) {
|
|
||||||
warnln("Couldn't parse {} as JSON", path);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
print(json.value(), spaces_in_indent, 0, isatty(STDOUT_FILENO));
|
print(json, spaces_in_indent, 0, isatty(STDOUT_FILENO));
|
||||||
outln();
|
outln();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue