mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 14:17:34 +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
43 changed files with 122 additions and 145 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue