From a9e76295ff02e6d5f6ffe310c9d1dad7614061b0 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 28 Mar 2024 11:51:34 +0900 Subject: [PATCH] Simplify --- common/cmdline.h | 101 ----------------------------------------------- elf/cmdline.cc | 97 ++++++++++++++++++++++++++++++++++++++++++++- elf/main.cc | 1 - elf/mold.h | 3 ++ 4 files changed, 99 insertions(+), 103 deletions(-) delete mode 100644 common/cmdline.h diff --git a/common/cmdline.h b/common/cmdline.h deleted file mode 100644 index 2b449552..00000000 --- a/common/cmdline.h +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once - -#include "common.h" - -namespace mold { - -template -std::vector -read_response_file(Context &ctx, std::string_view path, i64 depth) { - if (depth > 10) - Fatal(ctx) << path << ": response file nesting too deep"; - - std::vector vec; - MappedFile *mf = must_open_file(ctx, std::string(path)); - std::string_view data((char *)mf->data, mf->size); - - while (!data.empty()) { - if (isspace(data[0])) { - data = data.substr(1); - continue; - } - - auto read_quoted = [&]() { - char quote = data[0]; - data = data.substr(1); - - std::string buf; - while (!data.empty() && data[0] != quote) { - if (data[0] == '\\' && data.size() >= 1) { - buf.append(1, data[1]); - data = data.substr(2); - } else { - buf.append(1, data[0]); - data = data.substr(1); - } - } - if (data.empty()) - Fatal(ctx) << path << ": premature end of input"; - data = data.substr(1); - return save_string(ctx, buf); - }; - - auto read_unquoted = [&] { - std::string buf; - while (!data.empty()) { - if (data[0] == '\\' && data.size() >= 1) { - buf.append(1, data[1]); - data = data.substr(2); - continue; - } - - if (!isspace(data[0])) { - buf.append(1, data[0]); - data = data.substr(1); - continue; - } - break; - } - return save_string(ctx, buf); - }; - - std::string_view tok; - if (data[0] == '\'' || data[0] == '\"') - tok = read_quoted(); - else - tok = read_unquoted(); - - if (tok.starts_with('@')) - append(vec, read_response_file(ctx, tok.substr(1), depth + 1)); - else - vec.push_back(tok); - } - return vec; -} - -// Replace "@path/to/some/text/file" with its file contents. -template -std::vector expand_response_files(Context &ctx, char **argv) { - std::vector vec; - for (i64 i = 0; argv[i]; i++) { - if (argv[i][0] == '@') - append(vec, read_response_file(ctx, argv[i] + 1, 1)); - else - vec.push_back(argv[i]); - } - return vec; -} - -static inline std::string_view string_trim(std::string_view str) { - size_t pos = str.find_first_not_of(" \t"); - if (pos == str.npos) - return ""; - str = str.substr(pos); - - pos = str.find_last_not_of(" \t"); - if (pos == str.npos) - return str; - return str.substr(0, pos + 1); -} - -} // namespace mold diff --git a/elf/cmdline.cc b/elf/cmdline.cc index 3b83927b..9e8b8000 100644 --- a/elf/cmdline.cc +++ b/elf/cmdline.cc @@ -1,5 +1,4 @@ #include "mold.h" -#include "../common/cmdline.h" #include #include @@ -210,6 +209,101 @@ Options: mold: supported targets: elf32-i386 elf64-x86-64 elf32-littlearm elf64-littleaarch64 elf32-littleriscv elf32-bigriscv elf64-littleriscv elf64-bigriscv elf32-powerpc elf64-powerpc elf64-powerpc elf64-powerpcle elf64-s390 elf64-sparc elf32-m68k elf32-sh-linux elf64-alpha elf64-loongarch elf32-loongarch mold: supported emulations: elf_i386 elf_x86_64 armelf_linux_eabi aarch64linux aarch64elf elf32lriscv elf32briscv elf64lriscv elf64briscv elf32ppc elf32ppclinux elf64ppc elf64lppc elf64_s390 elf64_sparc m68kelf shlelf_linux elf64alpha elf64loongarch elf32loongarch)"; +template +static std::vector +read_response_file(Context &ctx, std::string_view path, i64 depth) { + if (depth > 10) + Fatal(ctx) << path << ": response file nesting too deep"; + + std::vector vec; + MappedFile *mf = must_open_file(ctx, std::string(path)); + std::string_view data((char *)mf->data, mf->size); + + while (!data.empty()) { + if (isspace(data[0])) { + data = data.substr(1); + continue; + } + + auto read_quoted = [&]() { + char quote = data[0]; + data = data.substr(1); + + std::string buf; + while (!data.empty() && data[0] != quote) { + if (data[0] == '\\' && data.size() >= 1) { + buf.append(1, data[1]); + data = data.substr(2); + } else { + buf.append(1, data[0]); + data = data.substr(1); + } + } + if (data.empty()) + Fatal(ctx) << path << ": premature end of input"; + data = data.substr(1); + return save_string(ctx, buf); + }; + + auto read_unquoted = [&] { + std::string buf; + while (!data.empty()) { + if (data[0] == '\\' && data.size() >= 1) { + buf.append(1, data[1]); + data = data.substr(2); + continue; + } + + if (!isspace(data[0])) { + buf.append(1, data[0]); + data = data.substr(1); + continue; + } + break; + } + return save_string(ctx, buf); + }; + + std::string_view tok; + if (data[0] == '\'' || data[0] == '\"') + tok = read_quoted(); + else + tok = read_unquoted(); + + if (tok.starts_with('@')) + append(vec, read_response_file(ctx, tok.substr(1), depth + 1)); + else + vec.push_back(tok); + } + return vec; +} + +// Replace "@path/to/some/text/file" with its file contents. +template +std::vector +expand_response_files(Context &ctx, char **argv) { + std::vector vec; + for (i64 i = 0; argv[i]; i++) { + if (argv[i][0] == '@') + append(vec, read_response_file(ctx, argv[i] + 1, 1)); + else + vec.push_back(argv[i]); + } + return vec; +} + +static inline std::string_view string_trim(std::string_view str) { + size_t pos = str.find_first_not_of(" \t"); + if (pos == str.npos) + return ""; + str = str.substr(pos); + + pos = str.find_last_not_of(" \t"); + if (pos == str.npos) + return str; + return str.substr(0, pos + 1); +} + static std::vector add_dashes(std::string name) { // Single-letter option if (name.size() == 1) @@ -1295,6 +1389,7 @@ std::vector parse_nonpositional_args(Context &ctx) { using E = MOLD_TARGET; +template std::vector expand_response_files(Context &, char **); template std::vector parse_nonpositional_args(Context &ctx); } // namespace mold::elf diff --git a/elf/main.cc b/elf/main.cc index 733ab38e..ee800f2c 100644 --- a/elf/main.cc +++ b/elf/main.cc @@ -1,6 +1,5 @@ #include "mold.h" #include "../common/archive-file.h" -#include "../common/cmdline.h" #include "../common/output-file.h" #include diff --git a/elf/mold.h b/elf/mold.h index f578100b..77290593 100644 --- a/elf/mold.h +++ b/elf/mold.h @@ -1347,6 +1347,9 @@ void process_run_subcommand(Context &ctx, int argc, char **argv); // cmdline.cc // +template +std::vector expand_response_files(Context &ctx, char **argv); + template std::vector parse_nonpositional_args(Context &ctx);