Utilities: Read positional arguments as Strings not char*s

This is a pretty trivial change so they're all batched together.
This commit is contained in:
Sam Atkins 2022-04-02 16:48:05 +01:00 committed by Andreas Kling
parent 84b67754c0
commit f0aba519c3
18 changed files with 46 additions and 52 deletions

View file

@ -13,7 +13,7 @@
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Vector<char const*> paths;
Vector<String> paths;
char const* opt_algorithm = nullptr;
Core::ArgsParser args_parser;
@ -43,7 +43,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool fail = false;
for (auto& path : paths) {
auto filepath = (StringView(path) == "-") ? "/dev/stdin" : path;
auto filepath = (path == "-") ? "/dev/stdin" : path;
auto file = Core::File::construct(filepath);
if (!file->open(Core::OpenMode::ReadOnly)) {
warnln("{}: {}: {}", arguments.strings[0], path, file->error_string());

View file

@ -25,7 +25,7 @@ struct Options {
static Options parse_options(Main::Arguments arguments)
{
char const* type = "text/plain";
Vector<char const*> text;
Vector<String> text;
bool clear = false;
Core::ArgsParser args_parser;

View file

@ -101,7 +101,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio"));
Vector<char const*> text;
Vector<String> text;
bool no_trailing_newline = false;
bool should_interpret_backslash_escapes = false;

View file

@ -141,7 +141,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));
Vector<char const*> paths;
Vector<String> paths;
bool flag_mime_only = false;
Core::ArgsParser args_parser;
@ -155,7 +155,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
for (auto path : paths) {
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
if (file_or_error.is_error()) {
perror(path);
perror(path.characters());
all_ok = false;
continue;
}

View file

@ -53,7 +53,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
#endif
bool inplace = false;
Vector<char const*> files;
Vector<String> files;
Core::ArgsParser args_parser;
args_parser.set_general_help("Format GML files.");

View file

@ -39,11 +39,11 @@ ErrorOr<int> serenity_main(Main::Arguments args)
String program_name = AK::LexicalPath::basename(args.strings[0]);
Vector<char const*> files;
Vector<String> files;
bool recursive = (program_name == "rgrep"sv);
bool use_ere = (program_name == "egrep"sv);
Vector<char const*> patterns;
Vector<String> patterns;
BinaryFileMode binary_mode { BinaryFileMode::Binary };
bool case_insensitive = false;
bool line_numbers = false;

View file

@ -35,7 +35,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::unveil(nullptr, nullptr));
TRY(Core::System::pledge("stdio rpath"));
Vector<char const*> usernames;
Vector<String> usernames;
Core::ArgsParser args_parser;
args_parser.set_general_help("Print group memberships for each username or, if no username is specified, for the current process.");
@ -48,7 +48,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
for (auto username : usernames) {
auto result = Core::Account::from_name(username, Core::Account::Read::PasswdOnly);
auto result = Core::Account::from_name(username.characters(), Core::Account::Read::PasswdOnly);
if (result.is_error()) {
warnln("{} '{}'", result.error(), username);
continue;

View file

@ -24,7 +24,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
int byte_count = -1;
bool never_print_filenames = false;
bool always_print_filenames = false;
Vector<char const*> files;
Vector<String> files;
Core::ArgsParser args_parser;
args_parser.set_general_help("Print the beginning ('head') of a file.");

View file

@ -22,7 +22,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool create_parents = false;
String mode_string;
Vector<char const*> directories;
Vector<String> directories;
Core::ArgsParser args_parser;
args_parser.add_option(create_parents, "Create parent directories if they don't exist", "parents", 'p');

View file

@ -24,7 +24,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool force = false;
bool verbose = false;
Vector<char const*> paths;
Vector<String> paths;
Core::ArgsParser args_parser;
args_parser.add_option(force, "Force", "force", 'f');
@ -41,7 +41,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
struct stat st;
int rc = lstat(original_new_path, &st);
int rc = lstat(original_new_path.characters(), &st);
if (rc != 0 && errno != ENOENT) {
perror("lstat");
return 1;
@ -57,14 +57,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
for (auto& old_path : paths) {
String combined_new_path;
char const* new_path = original_new_path;
auto new_path = original_new_path;
if (S_ISDIR(st.st_mode)) {
auto old_basename = LexicalPath::basename(old_path);
combined_new_path = String::formatted("{}/{}", original_new_path, old_basename);
new_path = combined_new_path.characters();
}
rc = rename(old_path, new_path);
rc = rename(old_path.characters(), new_path.characters());
if (rc < 0) {
if (errno == EXDEV) {
auto result = Core::File::copy_file_or_directory(
@ -77,7 +77,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
warnln("mv: could not move '{}': {}", old_path, static_cast<Error const&>(result.error()));
return 1;
}
rc = unlink(old_path);
rc = unlink(old_path.characters());
if (rc < 0)
warnln("mv: unlink '{}': {}", old_path, strerror(errno));
} else {

View file

@ -25,7 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
char const* separator = " ";
int start_number = 1;
int number_width = 6;
Vector<char const*> files;
Vector<String> files;
Core::ArgsParser args_parser;
@ -60,7 +60,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Vector<FILE*> file_pointers;
if (!files.is_empty()) {
for (auto& file : files) {
FILE* file_pointer = fopen(file, "r");
FILE* file_pointer = fopen(file.characters(), "r");
if (!file_pointer) {
warnln("Failed to open {}: {}", file, strerror(errno));
continue;

View file

@ -6,7 +6,6 @@
#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <bits/posix1_lim.h>
@ -19,7 +18,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
static bool flag_most_posix = false;
static bool flag_portability = false;
static bool flag_empty_name_and_leading_dash = false;
Vector<char const*> paths;
Vector<String> paths;
Core::ArgsParser args_parser;
args_parser.add_option(flag_most_posix, "Check for most POSIX systems", nullptr, 'p');
@ -34,31 +33,30 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
for (auto& path : paths) {
auto str_path = String(path);
unsigned long path_max = flag_most_posix ? _POSIX_PATH_MAX : pathconf(str_path.characters(), _PC_PATH_MAX);
unsigned long name_max = flag_most_posix ? _POSIX_NAME_MAX : pathconf(str_path.characters(), _PC_NAME_MAX);
unsigned long path_max = flag_most_posix ? _POSIX_PATH_MAX : pathconf(path.characters(), _PC_PATH_MAX);
unsigned long name_max = flag_most_posix ? _POSIX_NAME_MAX : pathconf(path.characters(), _PC_NAME_MAX);
if (str_path.length() > path_max) {
warnln("Limit {} exceeded by length {} of filename '{}'", path_max, str_path.length(), str_path);
if (path.length() > path_max) {
warnln("Limit {} exceeded by length {} of filename '{}'", path_max, path.length(), path);
fail = true;
continue;
}
if (flag_most_posix) {
// POSIX portable filename character set (a-z A-Z 0-9 . _ -)
for (long unsigned i = 0; i < str_path.length(); ++i) {
for (long unsigned i = 0; i < path.length(); ++i) {
auto c = path[i];
if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && c != '/' && c != '.' && c != '-' && c != '_') {
warnln("Non-portable character '{}' in filename '{}'", path[i], str_path);
warnln("Non-portable character '{}' in filename '{}'", path[i], path);
fail = true;
continue;
}
}
} else {
struct stat st;
if (lstat(str_path.characters(), &st) < 0) {
if (lstat(path.characters(), &st) < 0) {
if (errno != ENOENT) {
warnln("Directory is not searchable '{}'", str_path);
warnln("Directory is not searchable '{}'", path);
fail = true;
continue;
}
@ -66,17 +64,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
if (flag_empty_name_and_leading_dash) {
if (str_path.is_empty()) {
if (path.is_empty()) {
warnln("Empty filename");
fail = true;
continue;
}
}
for (auto& component : str_path.split('/')) {
for (auto& component : path.split('/')) {
if (flag_empty_name_and_leading_dash) {
if (component.starts_with('-')) {
warnln("Leading '-' in a component of filename '{}'", str_path);
warnln("Leading '-' in a component of filename '{}'", path);
fail = true;
break;
}

View file

@ -15,7 +15,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio cpath"));
Vector<char const*> paths;
Vector<String> paths;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(paths, "Directories to remove", "paths");
@ -23,7 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
int status = 0;
for (auto path : paths) {
int rc = rmdir(path);
int rc = rmdir(path.characters());
if (rc < 0) {
perror("rmdir");
status = 1;

View file

@ -34,7 +34,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool no_auto_compress = false;
StringView archive_file;
char const* directory = nullptr;
Vector<char const*> paths;
Vector<String> paths;
Core::ArgsParser args_parser;
args_parser.add_option(create, "Create archive", "create", 'c');

View file

@ -12,13 +12,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
static bool file_exists(char const* path)
static bool file_exists(String const& path)
{
struct stat st;
int rc = stat(path, &st);
int rc = stat(path.characters(), &st);
if (rc < 0) {
if (errno == ENOENT)
return false;
@ -34,7 +32,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath cpath fattr"));
Vector<char const*> paths;
Vector<String> paths;
Core::ArgsParser args_parser;
args_parser.set_general_help("Create a file, or update its mtime (time of last modification).");

View file

@ -106,7 +106,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath tty"));
Vector<char const*> directories;
Vector<String> directories;
Core::ArgsParser args_parser;
args_parser.add_option(flag_show_hidden_files, "Show hidden files", "all", 'a');
@ -124,7 +124,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
print_directory_tree(".", 0, "");
puts("");
} else {
for (char const* directory : directories) {
for (auto const& directory : directories) {
print_directory_tree(directory, 0, "");
puts("");
}

View file

@ -90,7 +90,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));
Vector<char const*> file_specifiers;
Vector<String> file_specifiers;
Core::ArgsParser args_parser;
args_parser.add_option(g_output_line, "Output line count", "lines", 'l');

View file

@ -28,7 +28,7 @@ bool read_items(FILE* fp, char entry_separator, Function<Decision(StringView)>);
class ParsedInitialArguments {
public:
ParsedInitialArguments(Vector<char const*>&, StringView placeholder);
ParsedInitialArguments(Vector<String>&, StringView placeholder);
void for_each_joined_argument(StringView, Function<void(String const&)>) const;
@ -45,7 +45,7 @@ ErrorOr<int> serenity_main(Main::Arguments main_arguments)
char const* placeholder = nullptr;
bool split_with_nulls = false;
char const* specified_delimiter = "\n";
Vector<char const*> arguments;
Vector<String> arguments;
bool verbose = false;
char const* file_to_read = "-";
int max_lines_for_one_command = 0;
@ -238,18 +238,16 @@ bool run_command(Vector<char*>&& child_argv, bool verbose, bool is_stdin, int de
return true;
}
ParsedInitialArguments::ParsedInitialArguments(Vector<char const*>& arguments, StringView placeholder)
ParsedInitialArguments::ParsedInitialArguments(Vector<String>& arguments, StringView placeholder)
{
m_all_parts.ensure_capacity(arguments.size());
bool some_argument_has_placeholder = false;
for (auto argument : arguments) {
StringView arg { argument };
for (auto arg : arguments) {
if (placeholder.is_empty()) {
m_all_parts.append({ arg });
} else {
auto parts = arg.split_view(placeholder, true);
auto parts = arg.view().split_view(placeholder, true);
some_argument_has_placeholder = some_argument_has_placeholder || parts.size() > 1;
m_all_parts.append(move(parts));
}