From 608addf75a6283d6db9467dc27272a9e28fe4670 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 6 Feb 2025 17:27:18 +0100 Subject: Add argsplit to support multiple arguments in CXXFLAGS, CFLAGS and LDFLAGS. --- src/ctor.h | 2 +- src/tools.cc | 10 +++---- src/util.cc | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 3 ++ 4 files changed, 102 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ctor.h b/src/ctor.h index 27706c7..6cb46a5 100644 --- a/src/ctor.h +++ b/src/ctor.h @@ -191,7 +191,7 @@ using asm_flag = ctor::flag; using c_flags = std::vector; using cxx_flags = std::vector; -using ld_flags= std::vector; +using ld_flags = std::vector; using ar_flags = std::vector; using asm_flags = std::vector; diff --git a/src/tools.cc b/src/tools.cc index f6cb437..dfabdff 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -441,7 +441,7 @@ std::vector cxx_option(ctor::cxx_opt opt, const std::string& arg, } return {"-D" + arg}; case ctor::cxx_opt::custom: - return {arg}; + return argsplit(arg); } std::cerr << "Unsupported compiler option.\n"; @@ -488,7 +488,7 @@ std::vector c_option(ctor::c_opt opt, const std::string& arg, } return {"-D" + arg}; case ctor::c_opt::custom: - return {arg}; + return argsplit(arg); } std::cerr << "Unsupported compiler option.\n"; @@ -521,7 +521,7 @@ std::vector ld_option(ctor::ld_opt opt, const std::string& arg, case ctor::ld_opt::position_independent_executable: return {"-fPIE"}; case ctor::ld_opt::custom: - return {arg}; + return argsplit(arg); } std::cerr << "Unsupported compiler option.\n"; @@ -542,7 +542,7 @@ std::vector ar_option(ctor::ar_opt opt, const std::string& arg, case ctor::ar_opt::output: return {arg}; case ctor::ar_opt::custom: - return {arg}; + return argsplit(arg); } std::cerr << "Unsupported compiler option.\n"; @@ -555,7 +555,7 @@ std::vector asm_option(ctor::asm_opt opt, const std::string& arg, switch(opt) { case ctor::asm_opt::custom: - return {arg}; + return argsplit(arg); } std::cerr << "Unsupported compiler option.\n"; diff --git a/src/util.cc b/src/util.cc index dbd4c3c..6fc650a 100644 --- a/src/util.cc +++ b/src/util.cc @@ -6,6 +6,7 @@ #include #include #include +#include std::string to_lower(const std::string& str) { @@ -184,3 +185,95 @@ std::string locate(const std::string& prog, return {}; } + +std::vector argsplit(const std::string& str) +{ + enum class state + { + normal, + in_quot, + in_apotrophe, + } state{state::normal}; + bool esc{false}; + + std::string token; + std::vector tokens; + for(auto c : str) + { + switch(state) + { + case state::normal: + if(esc) + { + esc = false; + } + else + { + if(c == ' ') + { + tokens.push_back(token); + token.clear(); + continue; + } + if(c == '\\') + { + esc = true; + } + if(c == '"') + { + state = state::in_quot; + } + if(c == '\'') + { + state = state::in_apotrophe; + } + } + + token += c; + break; + case state::in_quot: + if(esc) + { + esc = false; + } + else + { + if(c == '\\') + { + esc = true; + } + if(c == '"') + { + state = state::normal; + } + } + + token += c; + break; + case state::in_apotrophe: + if(esc) + { + esc = false; + } + else + { + if(c == '\\') + { + esc = true; + } + if(c == '\'') + { + state = state::normal; + } + } + + token += c; + break; + } + } + if(!token.empty()) + { + tokens.push_back(token); + } + return tokens; +} diff --git a/src/util.h b/src/util.h index 0a90049..8b41014 100644 --- a/src/util.h +++ b/src/util.h @@ -28,3 +28,6 @@ std::vector get_paths(const std::string& path_env = std::getenv("PA std::string locate(const std::string& app, const std::vector& paths, const std::string& arch = {}); + +//! Splits string into tokens adhering to quotations " and ' +std::vector argsplit(const std::string& str); -- cgit v1.2.3