mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
LibCore+Everywhere: Remove ArgsParser::add*(char const*&)
This is not guaranteed to always work correctly as ArgsParser deals in StringViews and might have a non-properly-null-terminated string as a value. As a bonus, using StringView (and DeprecatedString where necessary) leads to nicer looking code too :^)
This commit is contained in:
parent
60908adcbe
commit
500044906d
Notes:
sideshowbarker
2024-07-17 01:27:18 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/500044906d Pull-request: https://github.com/SerenityOS/serenity/pull/17674 Reviewed-by: https://github.com/AtkinsSJ ✅ Reviewed-by: https://github.com/kleinesfilmroellchen
43 changed files with 122 additions and 145 deletions
|
@ -18,7 +18,7 @@ int main(int argc, char** argv)
|
|||
for (auto i = 0; i < argc; ++i)
|
||||
arguments.append({ argv[i], strlen(argv[i]) });
|
||||
|
||||
char const* target = nullptr;
|
||||
DeprecatedString target;
|
||||
int max_file_size = 1024 * 1024;
|
||||
int count = 1024;
|
||||
|
||||
|
@ -28,7 +28,7 @@ int main(int argc, char** argv)
|
|||
args_parser.add_positional_argument(target, "Target file path", "target");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
int fd = creat(target, 0666);
|
||||
int fd = creat(target.characters(), 0666);
|
||||
if (fd < 0) {
|
||||
perror("Couldn't create target file");
|
||||
return EXIT_FAILURE;
|
||||
|
@ -38,13 +38,13 @@ int main(int argc, char** argv)
|
|||
for (int i = 0; i < count; i++) {
|
||||
auto new_file_size = AK::get_random<uint64_t>() % (max_file_size + 1);
|
||||
printf("(%d/%d)\tTruncating to %" PRIu64 " bytes...\n", i + 1, count, new_file_size);
|
||||
if (truncate(target, new_file_size) < 0) {
|
||||
if (truncate(target.characters(), new_file_size) < 0) {
|
||||
perror("Couldn't truncate target file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (unlink(target) < 0) {
|
||||
if (unlink(target.characters()) < 0) {
|
||||
perror("Couldn't remove target file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ int main(int argc, char** argv)
|
|||
for (auto i = 0; i < argc; ++i)
|
||||
arguments.append({ argv[i], strlen(argv[i]) });
|
||||
|
||||
char const* target = nullptr;
|
||||
DeprecatedString target;
|
||||
int min_block_offset = 0;
|
||||
int block_length = 2048;
|
||||
int block_size = 512;
|
||||
|
@ -96,7 +96,7 @@ int main(int argc, char** argv)
|
|||
}
|
||||
auto buffer = buffer_result.release_value();
|
||||
|
||||
int fd = open(target, O_CREAT | O_RDWR, 0666);
|
||||
int fd = open(target.characters(), O_CREAT | O_RDWR, 0666);
|
||||
if (fd < 0) {
|
||||
perror("Couldn't create target file");
|
||||
return EXIT_FAILURE;
|
||||
|
|
|
@ -305,7 +305,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
StringView per_file_location;
|
||||
StringView pass_through_parameters;
|
||||
StringView runner_command = "test262-runner"sv;
|
||||
char const* test_directory = nullptr;
|
||||
StringView test_directory;
|
||||
bool dont_print_progress = false;
|
||||
bool dont_disable_core_dump = false;
|
||||
|
||||
|
|
|
@ -173,14 +173,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
bool display_notifications = false;
|
||||
char const* name = nullptr;
|
||||
StringView name;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(display_notifications, "Display notifications", "display-notifications", 'd');
|
||||
args_parser.add_option(name, "Applet name used by WindowServer.ini to set the applet order", "name", 'n', "name");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (name == nullptr)
|
||||
name = "Network";
|
||||
if (name.is_empty())
|
||||
name = "Network"sv;
|
||||
|
||||
auto window = TRY(GUI::Window::try_create());
|
||||
window->set_title(name);
|
||||
|
|
|
@ -28,7 +28,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
Config::pledge_domain("FontEditor");
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath"));
|
||||
|
||||
char const* path = nullptr;
|
||||
StringView path;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "The font file for editing.", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -42,7 +42,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto font_editor = TRY(window->set_main_widget<FontEditor::MainWidget>());
|
||||
TRY(font_editor->initialize_menubar(*window));
|
||||
|
||||
if (path) {
|
||||
if (!path.is_empty()) {
|
||||
TRY(font_editor->open_file(path));
|
||||
} else {
|
||||
auto mutable_font = TRY(TRY(Gfx::BitmapFont::try_load_from_file("/res/fonts/KaticaRegular10.font"))->unmasked_character_set());
|
||||
|
|
|
@ -51,7 +51,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
auto app_icon = GUI::Icon::default_icon("filetype-image"sv);
|
||||
|
||||
char const* path = nullptr;
|
||||
StringView path;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "The image file to be displayed.", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -68,7 +68,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto main_toolbar = TRY(toolbar_container->try_add<GUI::Toolbar>());
|
||||
|
||||
auto widget = TRY(root_widget->try_add<ViewWidget>());
|
||||
if (path) {
|
||||
if (!path.is_empty()) {
|
||||
widget->set_path(path);
|
||||
}
|
||||
widget->on_scale_change = [&](float scale) {
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
char const* file_path = nullptr;
|
||||
StringView file_path;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(file_path, "PDF file to open", "path", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -45,7 +45,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->show();
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
if (file_path) {
|
||||
if (!file_path.is_empty()) {
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, file_path);
|
||||
if (response.is_error())
|
||||
return 1;
|
||||
|
|
|
@ -26,7 +26,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto app = TRY(GUI::Application::try_create(arguments));
|
||||
Config::pledge_domain("PixelPaint");
|
||||
|
||||
char const* image_file = nullptr;
|
||||
StringView image_file;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(image_file, "Image file to open", "path", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -72,7 +72,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
window->show();
|
||||
|
||||
if (image_file) {
|
||||
if (!image_file.is_empty()) {
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, image_file);
|
||||
if (response.is_error())
|
||||
return 1;
|
||||
|
|
|
@ -26,15 +26,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
auto app = TRY(GUI::Application::try_create(arguments));
|
||||
|
||||
char const* filename = nullptr;
|
||||
StringView filename;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(filename, "File to read from", "file", Core::ArgsParser::Required::No);
|
||||
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (filename) {
|
||||
if (!Core::DeprecatedFile::exists({ filename, strlen(filename) }) || Core::DeprecatedFile::is_directory(filename)) {
|
||||
if (!filename.is_empty()) {
|
||||
if (!Core::DeprecatedFile::exists(filename) || Core::DeprecatedFile::is_directory(filename)) {
|
||||
warnln("File does not exist or is a directory: {}", filename);
|
||||
return 1;
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->resize(640, 480);
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
auto spreadsheet_widget = TRY(window->set_main_widget<Spreadsheet::SpreadsheetWidget>(*window, NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename == nullptr));
|
||||
auto spreadsheet_widget = TRY(window->set_main_widget<Spreadsheet::SpreadsheetWidget>(*window, NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename.is_empty()));
|
||||
|
||||
spreadsheet_widget->initialize_menubar(*window);
|
||||
spreadsheet_widget->update_window_title();
|
||||
|
@ -64,7 +64,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
window->show();
|
||||
|
||||
if (filename) {
|
||||
if (!filename.is_empty()) {
|
||||
auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, filename));
|
||||
spreadsheet_widget->load_file(file.filename(), file.stream());
|
||||
}
|
||||
|
|
|
@ -251,7 +251,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
Config::pledge_domain("Terminal");
|
||||
|
||||
char const* command_to_execute = nullptr;
|
||||
StringView command_to_execute;
|
||||
bool keep_open = false;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
|
@ -260,7 +260,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (keep_open && !command_to_execute) {
|
||||
if (keep_open && command_to_execute.is_empty()) {
|
||||
warnln("Option -k can only be used in combination with -e.");
|
||||
return 1;
|
||||
}
|
||||
|
@ -273,7 +273,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
if (shell_pid == 0) {
|
||||
close(ptm_fd);
|
||||
if (command_to_execute)
|
||||
if (!command_to_execute.is_empty())
|
||||
TRY(run_command(command_to_execute, keep_open));
|
||||
else
|
||||
TRY(run_command(Config::read_string("Terminal"sv, "Startup"sv, "Command"sv, ""sv), false));
|
||||
|
|
|
@ -27,7 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
app->set_config_domain(TRY("TextEditor"_string));
|
||||
|
||||
auto preview_mode = "auto"sv;
|
||||
char const* file_to_edit = nullptr;
|
||||
StringView file_to_edit;
|
||||
Core::ArgsParser parser;
|
||||
parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode");
|
||||
parser.add_positional_argument(file_to_edit, "File to edit, with optional starting line and column number", "file[:line[:column]]", Core::ArgsParser::Required::No);
|
||||
|
@ -73,8 +73,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->show();
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
if (file_to_edit) {
|
||||
auto filename = TRY(String::from_utf8(StringView(file_to_edit, strlen(file_to_edit))));
|
||||
if (!file_to_edit.is_empty()) {
|
||||
auto filename = TRY(String::from_utf8(file_to_edit));
|
||||
FileArgument parsed_argument(filename);
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, parsed_argument.filename().to_deprecated_string());
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/GMLPlayground.md") }));
|
||||
TRY(Desktop::Launcher::seal_allowlist());
|
||||
|
||||
char const* path = nullptr;
|
||||
StringView path;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "GML file to edit", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
|
|
@ -54,7 +54,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(notify_make_not_available());
|
||||
}
|
||||
|
||||
char const* path_argument = nullptr;
|
||||
StringView path_argument;
|
||||
bool mode_coredump = false;
|
||||
pid_t pid_to_debug = -1;
|
||||
Core::ArgsParser args_parser;
|
||||
|
|
|
@ -47,13 +47,13 @@ static bool generate_profile(pid_t& pid);
|
|||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
int pid = 0;
|
||||
char const* perfcore_file_arg = nullptr;
|
||||
StringView perfcore_file_arg;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(pid, "PID to profile", "pid", 'p', "PID");
|
||||
args_parser.add_positional_argument(perfcore_file_arg, "Path of perfcore file", "perfcore-file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (pid && perfcore_file_arg) {
|
||||
if (pid && !perfcore_file_arg.is_empty()) {
|
||||
warnln("-p/--pid option and perfcore-file argument must not be used together!");
|
||||
return 1;
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-profiler"sv));
|
||||
|
||||
DeprecatedString perfcore_file;
|
||||
if (!perfcore_file_arg) {
|
||||
if (perfcore_file_arg.is_empty()) {
|
||||
if (!generate_profile(pid))
|
||||
return 0;
|
||||
perfcore_file = DeprecatedString::formatted("/proc/{}/perf_events", pid);
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
char const* file_to_open = nullptr;
|
||||
StringView file_to_open;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(file_to_open, "Path to SQL script or DB", "file", Core::ArgsParser::Required::No);
|
||||
|
@ -39,7 +39,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
return GUI::Window::CloseRequestDecision::StayOpen;
|
||||
};
|
||||
|
||||
if (file_to_open) {
|
||||
if (!file_to_open.is_empty()) {
|
||||
auto path = LexicalPath(file_to_open);
|
||||
main_widget->open_script_from_file(path);
|
||||
} else {
|
||||
|
|
|
@ -406,24 +406,6 @@ void ArgsParser::add_option(bool& value, char const* help_string, char const* lo
|
|||
add_option(move(option));
|
||||
}
|
||||
|
||||
void ArgsParser::add_option(char const*& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode)
|
||||
{
|
||||
Option option {
|
||||
OptionArgumentMode::Required,
|
||||
help_string,
|
||||
long_name,
|
||||
short_name,
|
||||
value_name,
|
||||
[&value](StringView s) {
|
||||
VERIFY(s.length() == strlen(s.characters_without_null_termination()));
|
||||
value = s.characters_without_null_termination();
|
||||
return true;
|
||||
},
|
||||
hide_mode,
|
||||
};
|
||||
add_option(move(option));
|
||||
}
|
||||
|
||||
void ArgsParser::add_option(DeprecatedString& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode)
|
||||
{
|
||||
Option option {
|
||||
|
@ -589,22 +571,6 @@ void ArgsParser::add_positional_argument(Arg&& arg)
|
|||
m_positional_args.append(move(arg));
|
||||
}
|
||||
|
||||
void ArgsParser::add_positional_argument(char const*& value, char const* help_string, char const* name, Required required)
|
||||
{
|
||||
Arg arg {
|
||||
help_string,
|
||||
name,
|
||||
required == Required::Yes ? 1 : 0,
|
||||
1,
|
||||
[&value](StringView s) {
|
||||
VERIFY(s.length() == strlen(s.characters_without_null_termination()));
|
||||
value = s.characters_without_null_termination();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
add_positional_argument(move(arg));
|
||||
}
|
||||
|
||||
void ArgsParser::add_positional_argument(DeprecatedString& value, char const* help_string, char const* name, Required required)
|
||||
{
|
||||
Arg arg {
|
||||
|
|
|
@ -87,7 +87,6 @@ public:
|
|||
void add_option(Option&&);
|
||||
void add_ignored(char const* long_name, char short_name, OptionHideMode hide_mode = OptionHideMode::None);
|
||||
void add_option(bool& value, char const* help_string, char const* long_name, char short_name, OptionHideMode hide_mode = OptionHideMode::None);
|
||||
void add_option(char const*& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
|
||||
void add_option(DeprecatedString& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
|
||||
void add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
|
||||
template<Integral I>
|
||||
|
@ -101,7 +100,6 @@ public:
|
|||
void add_option(Vector<DeprecatedString>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, OptionHideMode hide_mode = OptionHideMode::None);
|
||||
|
||||
void add_positional_argument(Arg&&);
|
||||
void add_positional_argument(char const*& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(DeprecatedString& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(StringView& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
void add_positional_argument(int& value, char const* help_string, char const* name, Required required = Required::Yes);
|
||||
|
|
|
@ -94,7 +94,7 @@ int main(int argc, char** argv)
|
|||
#endif
|
||||
bool print_json = false;
|
||||
bool per_file = false;
|
||||
char const* specified_test_root = nullptr;
|
||||
StringView specified_test_root;
|
||||
DeprecatedString common_path;
|
||||
DeprecatedString test_glob;
|
||||
|
||||
|
@ -143,7 +143,7 @@ int main(int argc, char** argv)
|
|||
|
||||
DeprecatedString test_root;
|
||||
|
||||
if (specified_test_root) {
|
||||
if (!specified_test_root.is_empty()) {
|
||||
test_root = DeprecatedString { specified_test_root };
|
||||
} else {
|
||||
#ifdef AK_OS_SERENITY
|
||||
|
|
|
@ -65,7 +65,7 @@ int TestSuite::main(DeprecatedString const& suite_name, Span<StringView> argumen
|
|||
bool do_tests_only = getenv("TESTS_ONLY") != nullptr;
|
||||
bool do_benchmarks_only = false;
|
||||
bool do_list_cases = false;
|
||||
char const* search_string = "*";
|
||||
StringView search_string = "*"sv;
|
||||
|
||||
args_parser.add_option(do_tests_only, "Only run tests.", "tests", 0);
|
||||
args_parser.add_option(do_benchmarks_only, "Only run benchmarks.", "bench", 0);
|
||||
|
|
|
@ -86,7 +86,7 @@ static void run_command(int ptm_fd, DeprecatedString command)
|
|||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
int port = 23;
|
||||
char const* command = "";
|
||||
StringView command = ""sv;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(port, "Port to listen on", nullptr, 'p', "port");
|
||||
|
|
|
@ -350,7 +350,7 @@ ErrorOr<int> Shell::builtin_type(Main::Arguments arguments)
|
|||
|
||||
ErrorOr<int> Shell::builtin_cd(Main::Arguments arguments)
|
||||
{
|
||||
char const* arg_path = nullptr;
|
||||
StringView arg_path;
|
||||
|
||||
Core::ArgsParser parser;
|
||||
parser.add_positional_argument(arg_path, "Path to change to", "path", Core::ArgsParser::Required::No);
|
||||
|
@ -360,10 +360,10 @@ ErrorOr<int> Shell::builtin_cd(Main::Arguments arguments)
|
|||
|
||||
DeprecatedString new_path;
|
||||
|
||||
if (!arg_path) {
|
||||
if (arg_path.is_empty()) {
|
||||
new_path = home;
|
||||
} else {
|
||||
if (strcmp(arg_path, "-") == 0) {
|
||||
if (arg_path == "-"sv) {
|
||||
char* oldpwd = getenv("OLDPWD");
|
||||
if (oldpwd == nullptr)
|
||||
return 1;
|
||||
|
@ -1044,7 +1044,7 @@ ErrorOr<int> Shell::builtin_time(Main::Arguments arguments)
|
|||
|
||||
ErrorOr<int> Shell::builtin_umask(Main::Arguments arguments)
|
||||
{
|
||||
char const* mask_text = nullptr;
|
||||
StringView mask_text;
|
||||
|
||||
Core::ArgsParser parser;
|
||||
parser.add_positional_argument(mask_text, "New mask (omit to get current mask)", "octal-mask", Core::ArgsParser::Required::No);
|
||||
|
@ -1052,16 +1052,29 @@ ErrorOr<int> Shell::builtin_umask(Main::Arguments arguments)
|
|||
if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
|
||||
return 1;
|
||||
|
||||
if (!mask_text) {
|
||||
if (mask_text.is_empty()) {
|
||||
mode_t old_mask = umask(0);
|
||||
printf("%#o\n", old_mask);
|
||||
umask(old_mask);
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned mask;
|
||||
int matches = sscanf(mask_text, "%o", &mask);
|
||||
if (matches == 1) {
|
||||
unsigned mask = 0;
|
||||
auto matches = true;
|
||||
|
||||
// FIXME: There's currently no way to parse an StringView as an octal integer,
|
||||
// when that becomes available, replace this code.
|
||||
for (auto byte : mask_text.bytes()) {
|
||||
if (isspace(byte))
|
||||
continue;
|
||||
if (!is_ascii_octal_digit(byte)) {
|
||||
matches = false;
|
||||
break;
|
||||
}
|
||||
|
||||
mask = (mask << 3) + (byte - '0');
|
||||
}
|
||||
if (matches) {
|
||||
umask(mask);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -167,7 +167,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
StringView file_to_read_from = {};
|
||||
Vector<StringView> script_args;
|
||||
bool skip_rc_files = false;
|
||||
char const* format = nullptr;
|
||||
StringView format;
|
||||
bool should_format_live = false;
|
||||
bool keep_open = false;
|
||||
bool posix_mode = false;
|
||||
|
@ -185,7 +185,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
parser.set_stop_on_first_non_option(true);
|
||||
parser.parse(arguments);
|
||||
|
||||
if (format) {
|
||||
if (!format.is_empty()) {
|
||||
auto file = TRY(Core::DeprecatedFile::open(format, Core::OpenMode::ReadOnly));
|
||||
|
||||
initialize(posix_mode);
|
||||
|
|
|
@ -15,14 +15,14 @@
|
|||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
Vector<DeprecatedString> paths;
|
||||
char const* opt_algorithm = nullptr;
|
||||
StringView opt_algorithm;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(opt_algorithm, "Checksum algorithm (default 'crc32', use 'list' to list available algorithms)", "algorithm", '\0', nullptr);
|
||||
args_parser.add_positional_argument(paths, "File", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
auto algorithm = (opt_algorithm == nullptr) ? "crc32" : DeprecatedString(opt_algorithm).to_lowercase();
|
||||
auto algorithm = opt_algorithm.is_empty() ? "crc32" : DeprecatedString(opt_algorithm).to_lowercase();
|
||||
|
||||
auto available_algorithms = Vector<DeprecatedString> { "crc32", "adler32" };
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
bool print_iso_8601 = false;
|
||||
bool print_rfc_3339 = false;
|
||||
bool print_rfc_5322 = false;
|
||||
char const* set_date = nullptr;
|
||||
StringView set_date;
|
||||
StringView format_string;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
|
@ -31,8 +31,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
args_parser.add_positional_argument(format_string, "Custom format to print the date in", "format-string", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (set_date != nullptr) {
|
||||
auto number = DeprecatedString(set_date).to_uint();
|
||||
if (!set_date.is_empty()) {
|
||||
auto number = set_date.to_uint();
|
||||
|
||||
if (!number.has_value()) {
|
||||
warnln("date: Invalid timestamp value");
|
||||
|
|
|
@ -20,14 +20,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Core::System::unveil("/etc/", "rwc"));
|
||||
TRY(Core::System::unveil("/bin/rm", "x"));
|
||||
|
||||
char const* groupname = nullptr;
|
||||
DeprecatedString groupname;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(groupname, "Group name", "group");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
setgrent();
|
||||
auto* g = getgrnam(groupname);
|
||||
auto* g = getgrnam(groupname.characters());
|
||||
|
||||
// Check if the group exists
|
||||
if (!g) {
|
||||
|
|
|
@ -49,7 +49,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
bool verbose = false;
|
||||
bool should_close = false;
|
||||
bool udp_mode = false;
|
||||
char const* target = nullptr;
|
||||
DeprecatedString target;
|
||||
int port = 0;
|
||||
int maximum_tcp_receive_buffer_size_input = -1;
|
||||
|
||||
|
@ -96,8 +96,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = htons(port);
|
||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
if (target) {
|
||||
if (inet_pton(AF_INET, target, &sa.sin_addr) <= 0) {
|
||||
if (!target.is_empty()) {
|
||||
if (inet_pton(AF_INET, target.characters(), &sa.sin_addr) <= 0) {
|
||||
perror("inet_pton");
|
||||
return 1;
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Core::System::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)));
|
||||
TRY(Core::System::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)));
|
||||
|
||||
auto* hostent = gethostbyname(target);
|
||||
auto* hostent = gethostbyname(target.characters());
|
||||
if (!hostent) {
|
||||
warnln("Socket::connect: Unable to resolve '{}'", target);
|
||||
return 1;
|
||||
|
|
|
@ -22,7 +22,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
{
|
||||
NumberStyle number_style = NumberNonEmptyLines;
|
||||
int increment = 1;
|
||||
char const* separator = " ";
|
||||
StringView separator = " "sv;
|
||||
int start_number = 1;
|
||||
int number_width = 6;
|
||||
Vector<DeprecatedString> files;
|
||||
|
|
|
@ -107,7 +107,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
// Leap seconds smearing NTP servers:
|
||||
// - time.facebook.com , https://engineering.fb.com/production-engineering/ntp-service/ , sine-smears over 18 hours
|
||||
// - time.google.com , https://developers.google.com/time/smear , linear-smears over 24 hours
|
||||
char const* host = "time.google.com";
|
||||
DeprecatedString host = "time.google.com"sv;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(adjust_time, "Gradually adjust system time (requires root)", "adjust", 'a');
|
||||
args_parser.add_option(set_time, "Immediately set system time (requires root)", "set", 's');
|
||||
|
@ -128,7 +128,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Core::System::pledge("stdio inet unix rpath"));
|
||||
}
|
||||
|
||||
auto* hostent = gethostbyname(host);
|
||||
auto* hostent = gethostbyname(host.characters());
|
||||
if (!hostent) {
|
||||
warnln("Lookup failed for '{}'", host);
|
||||
return 1;
|
||||
|
|
|
@ -21,7 +21,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
|
||||
bool case_insensitive = false;
|
||||
bool invert_match = false;
|
||||
char const* pattern = nullptr;
|
||||
StringView pattern;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i');
|
||||
|
|
|
@ -45,8 +45,8 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
bool single_shot = false;
|
||||
char const* omit_pid_value = nullptr;
|
||||
char const* process_name = nullptr;
|
||||
StringView omit_pid_value;
|
||||
StringView process_name;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(single_shot, "Only return one pid", nullptr, 's');
|
||||
|
@ -56,11 +56,11 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
args_parser.parse(args);
|
||||
|
||||
pid_t pid_to_omit = 0;
|
||||
if (omit_pid_value) {
|
||||
if (!strcmp(omit_pid_value, "%PPID")) {
|
||||
if (!omit_pid_value.is_empty()) {
|
||||
if (omit_pid_value == "%PPID"sv) {
|
||||
pid_to_omit = getppid();
|
||||
} else {
|
||||
auto number = StringView { omit_pid_value, strlen(omit_pid_value) }.to_uint();
|
||||
auto number = omit_pid_value.to_uint();
|
||||
if (!number.has_value()) {
|
||||
warnln("Invalid value for -o");
|
||||
args_parser.print_usage(stderr, args.strings[0]);
|
||||
|
@ -69,5 +69,5 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
pid_to_omit = number.value();
|
||||
}
|
||||
}
|
||||
return pid_of(process_name, single_shot, omit_pid_value != nullptr, pid_to_omit);
|
||||
return pid_of(process_name, single_shot, !omit_pid_value.is_empty(), pid_to_omit);
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ static Optional<size_t> count;
|
|||
static uint32_t total_ms;
|
||||
static int min_ms;
|
||||
static int max_ms;
|
||||
static char const* host;
|
||||
static DeprecatedString host;
|
||||
static int payload_size = -1;
|
||||
// variable part of header can be 0 to 40 bytes
|
||||
// https://datatracker.ietf.org/doc/html/rfc791#section-3.1
|
||||
|
@ -88,7 +88,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
TRY(Core::System::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)));
|
||||
|
||||
auto* hostent = gethostbyname(host);
|
||||
auto* hostent = gethostbyname(host.characters());
|
||||
if (!hostent) {
|
||||
warnln("Lookup failed for '{}'", host);
|
||||
return 1;
|
||||
|
|
|
@ -24,7 +24,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
|
||||
bool case_insensitive = false;
|
||||
bool echo = false;
|
||||
char const* pattern = nullptr;
|
||||
StringView pattern;
|
||||
int signal = SIGTERM;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
|
|
|
@ -18,7 +18,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TRY(Core::System::unveil("/proc", "r"));
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
char const* pid;
|
||||
StringView pid;
|
||||
static bool extended = false;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
|
|
|
@ -151,7 +151,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
bool save_at_provided_name = false;
|
||||
bool should_follow_url = false;
|
||||
bool verbose_output = false;
|
||||
char const* data = nullptr;
|
||||
StringView data;
|
||||
StringView proxy_spec;
|
||||
DeprecatedString method = "GET";
|
||||
StringView method_override;
|
||||
|
@ -209,7 +209,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
if (!method_override.is_empty()) {
|
||||
method = method_override;
|
||||
} else if (data) {
|
||||
} else if (!data.is_empty()) {
|
||||
method = "POST";
|
||||
// FIXME: Content-Type?
|
||||
}
|
||||
|
@ -399,7 +399,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
request->stream_into(output_stream);
|
||||
};
|
||||
|
||||
request = protocol_client->start_request(method, url, request_headers, data ? StringView { data, strlen(data) }.bytes() : ReadonlyBytes {}, proxy_data);
|
||||
request = protocol_client->start_request(method, url, request_headers, data.bytes(), proxy_data);
|
||||
setup_request();
|
||||
|
||||
dbgln("started request with id {}", request->id());
|
||||
|
|
|
@ -14,7 +14,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
{
|
||||
TRY(Core::System::pledge("stdio rpath"));
|
||||
|
||||
char const* path;
|
||||
DeprecatedString path;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.set_general_help(
|
||||
|
@ -22,7 +22,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
args_parser.add_positional_argument(path, "Path to resolve", "path");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
char* value = realpath(path, nullptr);
|
||||
char* value = realpath(path.characters(), nullptr);
|
||||
if (value == nullptr) {
|
||||
perror("realpath");
|
||||
return 1;
|
||||
|
|
|
@ -315,7 +315,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
bool print_all_output = false;
|
||||
bool run_benchmarks = false;
|
||||
bool run_skipped_tests = false;
|
||||
char const* specified_test_root = nullptr;
|
||||
StringView specified_test_root;
|
||||
DeprecatedString test_glob;
|
||||
DeprecatedString exclude_pattern;
|
||||
DeprecatedString config_file;
|
||||
|
@ -360,7 +360,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
DeprecatedString test_root;
|
||||
|
||||
if (specified_test_root) {
|
||||
if (!specified_test_root.is_empty()) {
|
||||
test_root = DeprecatedString { specified_test_root };
|
||||
} else {
|
||||
test_root = "/usr/Tests";
|
||||
|
|
|
@ -819,8 +819,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
Vector<StringView> child_argv;
|
||||
|
||||
StringView output_filename;
|
||||
char const* exclude_syscalls_option = nullptr;
|
||||
char const* include_syscalls_option = nullptr;
|
||||
StringView exclude_syscalls_option;
|
||||
StringView include_syscalls_option;
|
||||
HashTable<StringView> exclude_syscalls;
|
||||
HashTable<StringView> include_syscalls;
|
||||
|
||||
|
@ -840,9 +840,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
? TRY(Core::File::standard_error())
|
||||
: TRY(Core::File::open(output_filename, Core::File::OpenMode::Write));
|
||||
|
||||
auto parse_syscalls = [](char const* option, auto& hash_table) {
|
||||
if (option != nullptr) {
|
||||
for (auto syscall : StringView { option, strlen(option) }.split_view(','))
|
||||
auto parse_syscalls = [](StringView option, auto& hash_table) {
|
||||
if (!option.is_empty()) {
|
||||
for (auto syscall : option.split_view(','))
|
||||
hash_table.set(syscall);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -25,7 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
{
|
||||
TRY(Core::System::pledge("stdio id inet unix"));
|
||||
|
||||
char const* host_name;
|
||||
DeprecatedString host_name;
|
||||
int max_hops = 30;
|
||||
int max_retries = 3;
|
||||
int echo_timeout = 5;
|
||||
|
@ -45,7 +45,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
return Error::from_string_literal("Invalid maximum retries amount");
|
||||
}
|
||||
|
||||
auto* hostent = gethostbyname(host_name);
|
||||
auto* hostent = gethostbyname(host_name.characters());
|
||||
if (!hostent) {
|
||||
warnln("Lookup failed for '{}'", host_name);
|
||||
return 1;
|
||||
|
|
|
@ -24,7 +24,7 @@ static int kill_test();
|
|||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
char const* test_name = "n";
|
||||
StringView test_name = "n"sv;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.set_general_help(
|
||||
|
@ -33,21 +33,21 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
args_parser.add_positional_argument(test_name, "Test to run (m = mutex, d = detached, p = priority, s = stack size, t = simple thread test, x = set stack, k = kill, nothing = join race)", "test-name", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (*test_name == 'm')
|
||||
if (test_name[0] == 'm')
|
||||
return mutex_test();
|
||||
if (*test_name == 'd')
|
||||
if (test_name[0] == 'd')
|
||||
return detached_test();
|
||||
if (*test_name == 'p')
|
||||
if (test_name[0] == 'p')
|
||||
return priority_test();
|
||||
if (*test_name == 's')
|
||||
if (test_name[0] == 's')
|
||||
return stack_size_test();
|
||||
if (*test_name == 't')
|
||||
if (test_name[0] == 't')
|
||||
return staying_alive_test();
|
||||
if (*test_name == 'x')
|
||||
if (test_name[0] == 'x')
|
||||
return set_stack_test();
|
||||
if (*test_name == 'k')
|
||||
if (test_name[0] == 'k')
|
||||
return kill_test();
|
||||
if (*test_name != 'n') {
|
||||
if (test_name[0] != 'n') {
|
||||
args_parser.print_usage(stdout, arguments.strings[0]);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
{
|
||||
TRY(Core::System::pledge("stdio wpath rpath cpath chown"));
|
||||
|
||||
char const* home_path = nullptr;
|
||||
StringView home_path;
|
||||
int uid = 0;
|
||||
int gid = USERS_GID;
|
||||
bool create_home_dir = false;
|
||||
|
@ -108,7 +108,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
|
||||
DeprecatedString home;
|
||||
if (!home_path)
|
||||
if (home_path.is_empty())
|
||||
home = DeprecatedString::formatted("/home/{}", username);
|
||||
else
|
||||
home = home_path;
|
||||
|
|
|
@ -14,13 +14,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
{
|
||||
TRY(Core::System::pledge("stdio rpath"));
|
||||
|
||||
char const* filename = nullptr;
|
||||
StringView filename;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(filename, "Name of executable", "executable");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
auto fullpath = Core::DeprecatedFile::resolve_executable_from_environment({ filename, strlen(filename) });
|
||||
auto fullpath = Core::DeprecatedFile::resolve_executable_from_environment(filename);
|
||||
if (!fullpath.has_value()) {
|
||||
warnln("no '{}' in path", filename);
|
||||
return 1;
|
||||
|
|
|
@ -44,10 +44,10 @@ ErrorOr<int> serenity_main(Main::Arguments main_arguments)
|
|||
|
||||
StringView placeholder;
|
||||
bool split_with_nulls = false;
|
||||
char const* specified_delimiter = "\n";
|
||||
DeprecatedString specified_delimiter = "\n"sv;
|
||||
Vector<DeprecatedString> arguments;
|
||||
bool verbose = false;
|
||||
char const* file_to_read = "-";
|
||||
DeprecatedString file_to_read = "-"sv;
|
||||
int max_lines_for_one_command = 0;
|
||||
int max_bytes_for_one_command = ARG_MAX;
|
||||
|
||||
|
@ -67,12 +67,12 @@ ErrorOr<int> serenity_main(Main::Arguments main_arguments)
|
|||
size_t max_bytes = min(ARG_MAX, max_bytes_for_one_command);
|
||||
size_t max_lines = max(max_lines_for_one_command, 0);
|
||||
|
||||
if (!split_with_nulls && strlen(specified_delimiter) > 1) {
|
||||
if (!split_with_nulls && specified_delimiter.length() > 1) {
|
||||
warnln("xargs: the delimiter must be a single byte");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char entry_separator = split_with_nulls ? '\0' : *specified_delimiter;
|
||||
char entry_separator = split_with_nulls ? '\0' : specified_delimiter[0];
|
||||
|
||||
if (!placeholder.is_empty())
|
||||
max_lines = 1;
|
||||
|
@ -87,7 +87,7 @@ ErrorOr<int> serenity_main(Main::Arguments main_arguments)
|
|||
|
||||
if ("-"sv != file_to_read) {
|
||||
// A file was specified, try to open it.
|
||||
fp = fopen(file_to_read, "re");
|
||||
fp = fopen(file_to_read.characters(), "re");
|
||||
if (!fp) {
|
||||
perror("fopen");
|
||||
return 1;
|
||||
|
|
|
@ -13,12 +13,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
{
|
||||
TRY(Core::System::pledge("stdio"));
|
||||
|
||||
char const* string = "yes";
|
||||
StringView string = "yes"sv;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(string, "String to output (defaults to 'yes')", "string", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
for (;;)
|
||||
puts(string);
|
||||
outln("{}", string);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue