Commit 58461a29 authored by jan.koester's avatar jan.koester
Browse files

gnu func ext

parent 8e42e174
Loading
Loading
Loading
Loading
+12 −7
Changes for parser.cpp: 12 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
#include <cstring>
#include <cctype>
#include <set>
#include <regex>

namespace fs = std::filesystem;
using namespace makeplus;
@@ -1441,13 +1442,17 @@ std::vector<std::string> Parser::split_func_args(const std::string& args, int ma
std::string Parser::func_shell(const std::string& cmd) const {
    std::string modified_cmd = cmd;

    // Emulation für GNU printf '%b\n':
    // Wir ersetzen 'printf '%b\n'' durch ein echo -e oder übersetzen die Escapes direkt,
    // damit der an die Shell übergebene C-Code syntaktisch korrekt ankommt.
    size_t pos = 0;
    while ((pos = modified_cmd.find("printf '%b\\n'", pos)) != std::string::npos) {
        modified_cmd.replace(pos, 13, "printf '%s\\n'"); // Changed 12 to 13
        pos += 13;                                       // Advance by the correct length
    try {
        // 1. Verallgemeinerung für printf mit '%b' -> '%s'
        std::regex printf_b_regex(R"(printf\s+(['"])([^'"]*?)%b([^'"]*?)\1)");
        modified_cmd = std::regex_replace(modified_cmd, printf_b_regex, "printf $1$2%s$3$1");

        // 2. Ersetze literal übergebenes '\n' innerhalb von printf-Args durch echte Leerzeichen/Zeilenumbrüche,
        //    damit der Compiler keine "-Werror,-Wextra-tokens" Fehler wirft.
        std::regex literal_newline_regex(R"(\\n)");
        modified_cmd = std::regex_replace(modified_cmd, literal_newline_regex, "\n");
    } catch (const std::regex_error& e) {
        // Fallback falls Regex fehlschlägt
    }

    FILE* pipe = popen(modified_cmd.c_str(), "r");