# shellcheck shell=bash # Response files: one argument @file standing for the arguments the file holds. # # Sourced by every driver that reads its own command line (libexec/brintld, # libexec/brintcc-glibc, libexec/brintcc-musl, libexec/config.sh's libc # selection), so that each reads the line its tool would have read. It only # defines functions; the caller replaces its own arguments: # # RSP_TOOL= # rsp_expand_line "$@" # (( RSP_FILES == 0 )) || set -- ${RSP_EXPANDED[@]+"${RSP_EXPANDED[@]}"} # # The expanded line is for the driver's own decisions. What the driver hands # on keeps the caller's @file: in a guest an exec whose arguments and # environment pass 8 KiB never returns, and a response file is how a long # line stays short. A compile passes the caller's arguments to clang, which # reads the file itself; a link, whose line the driver rewrites, goes to the # linker as a response file of its own (rsp_write, libexec/link-direct.sh). # # The tokenizer is LLVM's GNU one (TokenizeGNUCommandLine, the default off # Windows and the encoding clang, CMake and Meson write): whitespace separates, # a backslash takes the next byte literally inside and outside quotes, both # quote kinds group, and an empty quoted token is dropped. As in LLVM, an @file # that does not exist stays an ordinary argument and a relative name is read # from the current directory; a pipe or a device is read like a file. Files # nest to eight levels. # awk gets the file on its stdin: as an operand a name like x=y.rsp would be # taken for an assignment and - for the driver's own stdin. # One token per line comes back from awk, so a token holding a newline cannot # be carried and is refused. A line with no @ argument is not touched. RSP_TOOL="${RSP_TOOL:-brintium}" RSP_EXPANDED=() RSP_FILES=0 rsp_expand_arg() { # $1 = argument, $2 = nesting depth local _rsp_a="$1" _rsp_d="$2" _rsp_f _rsp_out _rsp_t local -a _rsp_toks=() if [[ "$_rsp_a" != @?* || ! -e "${_rsp_a#@}" || -d "${_rsp_a#@}" ]]; then RSP_EXPANDED+=("$_rsp_a") return 0 fi _rsp_f="${_rsp_a#@}" RSP_FILES=$((RSP_FILES + 1)) if (( _rsp_d >= 8 )); then echo "$RSP_TOOL: FATAL: response files nested deeper than 8 levels at '@$_rsp_f'." >&2 exit 1 fi if _rsp_out="$(LC_ALL=C awk ' function flush() { if (tok == "") return if (index(tok, "\n")) { bad = 1; exit 3 } print tok; tok = "" } { s = $0 "\n"; n = length(s) for (i = 1; i <= n; i++) { c = substr(s, i, 1) if (esc) { tok = tok c; esc = 0; continue } if (c == "\\") { esc = 1; continue } if (q != "") { if (c == q) q = ""; else tok = tok c; continue } if (c == "\"" || c == "\047") { q = c; continue } if (c == " " || c == "\t" || c == "\r" || c == "\n") { flush(); continue } tok = tok c } } END { if (!bad) flush() } ' < "$_rsp_f")"; then :; else echo "$RSP_TOOL: FATAL: cannot expand the response file '@$_rsp_f' (unreadable, or an argument in it holds a newline, which a backslash or an open quote at the end of the file also reads as). Refusing rather than running a line this driver has not read." >&2 exit 1 fi [[ -n "$_rsp_out" ]] || return 0 mapfile -t _rsp_toks <<<"$_rsp_out" for _rsp_t in "${_rsp_toks[@]}"; do [[ -n "$_rsp_t" ]] && rsp_expand_arg "$_rsp_t" $((_rsp_d + 1)) done return 0 } # Expands the whole line into RSP_EXPANDED when any argument starts with @, and # counts the files read in RSP_FILES. RSP_FILES stays 0 when nothing was read, # and the caller then keeps its arguments as they are. rsp_expand_line() { local _rsp_arg RSP_EXPANDED=() RSP_FILES=0 for _rsp_arg in "$@"; do if [[ "$_rsp_arg" == @?* ]]; then for _rsp_arg in "$@"; do rsp_expand_arg "$_rsp_arg" 0; done return 0 fi done return 0 } # Writes its arguments to the file $1 in the encoding the tokenizer above # reads and clang writes: each argument in double quotes, \ and " escaped. # An argument holding a newline cannot be read back, so it is refused; an # empty argument is written as "" and read back as none, as clang reads it. rsp_write() { # $1 = file, then the arguments local _rsp_f="$1" _rsp_a _rsp_e shift : > "$_rsp_f" || return 1 for _rsp_a in "$@"; do if [[ "$_rsp_a" == *$'\n'* ]]; then echo "$RSP_TOOL: FATAL: cannot write an argument holding a newline to the response file '$_rsp_f'." >&2 return 1 fi _rsp_e="${_rsp_a//\\/\\\\}" _rsp_e="${_rsp_e//\"/\\\"}" printf '"%s"\n' "$_rsp_e" >> "$_rsp_f" || return 1 done }