fortune: Port to LibMain :^)

This commit is contained in:
Andreas Kling 2021-11-29 22:23:27 +01:00
parent 8ba9a54310
commit 9815ad556a
Notes: sideshowbarker 2024-07-17 23:16:53 +09:00
2 changed files with 12 additions and 26 deletions

View file

@ -74,6 +74,7 @@ target_link_libraries(echo LibMain)
target_link_libraries(expr LibRegex) target_link_libraries(expr LibRegex)
target_link_libraries(fdtdump LibDeviceTree) target_link_libraries(fdtdump LibDeviceTree)
target_link_libraries(file LibGfx LibIPC LibCompress) target_link_libraries(file LibGfx LibIPC LibCompress)
target_link_libraries(fortune LibMain)
target_link_libraries(functrace LibDebug LibX86) target_link_libraries(functrace LibDebug LibX86)
target_link_libraries(gml-format LibGUI) target_link_libraries(gml-format LibGUI)
target_link_libraries(grep LibRegex) target_link_libraries(grep LibRegex)

View file

@ -13,6 +13,8 @@
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h> #include <LibCore/DateTime.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -69,47 +71,30 @@ static Vector<Quote> parse_all(const JsonArray& array)
return quotes; return quotes;
} }
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 = "/res/fortunes.json"; const char* path = "/res/fortunes.json";
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.set_general_help("Open a fortune cookie, receive a free quote for the day!"); args_parser.set_general_help("Open a fortune cookie, receive a free quote for the day!");
args_parser.add_positional_argument(path, "Path to JSON file with quotes (/res/fortunes.json by default)", "path", Core::ArgsParser::Required::No); args_parser.add_positional_argument(path, "Path to JSON file with quotes (/res/fortunes.json by default)", "path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv); args_parser.parse(arguments);
auto file = Core::File::construct(path); auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
if (!file->open(Core::OpenMode::ReadOnly)) {
warnln("Couldn't open {} for reading: {}", path, file->error_string());
return 1;
}
if (pledge("stdio", nullptr) < 0) { TRY(Core::System::pledge("stdio"));
perror("pledge"); TRY(Core::System::unveil(nullptr, nullptr));
return 1;
}
if (unveil(nullptr, nullptr) < 0) {
perror("unveil");
return 1;
}
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()) { if (!json.is_array()) {
warnln("Couldn't parse {} as JSON", path);
return 1;
}
if (!json.value().is_array()) {
warnln("{} does not contain an array of quotes", path); warnln("{} does not contain an array of quotes", path);
return 1; return 1;
} }
const auto quotes = parse_all(json.value().as_array()); const auto quotes = parse_all(json.as_array());
if (quotes.is_empty()) { if (quotes.is_empty()) {
warnln("{} does not contain any valid quotes", path); warnln("{} does not contain any valid quotes", path);
return 1; return 1;