1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:07: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:
Ali Mohammad Pur 2023-03-01 00:11:43 +03:30 committed by Andreas Kling
parent 60908adcbe
commit 500044906d
43 changed files with 122 additions and 145 deletions

View file

@ -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;
}

View file

@ -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);