Switch to using strlen in concat3Fn. Make sure uses-variables are local to improve purity of makeCWrapper. Refactor

This commit is contained in:
Tobias Bergkvist 2021-08-26 14:36:30 +02:00
parent dcba4171d4
commit b58c857bfb

View file

@ -36,6 +36,7 @@ makeDocumentedCWrapper() {
# ARGS: same as makeBinaryWrapper
makeCWrapper() {
local argv0 n params cmd main flagsBefore flags
local uses_prefix uses_suffix uses_concat3
local executable=$(escapeStringLiteral "$1")
local params=("$@")
@ -77,22 +78,19 @@ makeCWrapper() {
printf "%s\n" " #error makeCWrapper did not understand argument ${p}"
fi
done
[ -z ${flagsBefore+"1"} ] || {
flagsBefore=("$flagsBefore")
main="$main"$'\n'$(addFlags $flagsBefore)$'\n'$'\n'
}
[ -z "$flagsBefore" ] || main="$main"${main:+$'\n'}$(addFlags $flagsBefore)$'\n'$'\n'
main="$main argv[0] = \"${argv0:-${executable}}\";"$'\n'
main="$main return execv(\"${executable}\", argv);"$'\n'
printf "%s\n" "#include <unistd.h>"
printf "%s\n" "#include <stdlib.h>"
[ -z "$uses_concat3" ] || printf "%s\n" "#include <string.h>"
[ -z "$uses_concat3" ] || printf "\n%s\n" "$(concat3Fn)"
[ -z "$uses_prefix" ] || printf "\n%s\n" "$(setEnvPrefixFn)"
[ -z "$uses_suffix" ] || printf "\n%s\n" "$(setEnvSuffixFn)"
printf "\n%s" "int main(int argc, char **argv) {"
printf "\n%s" "$main"
printf "%s" "}"
printf "%s\n" "}"
}
addFlags() {
@ -100,7 +98,7 @@ addFlags() {
local var="argv_tmp"
flags=("$@")
for ((n = 0; n < ${#flags[*]}; n += 1)); do
flag=$(escapeStringLiteral "${flags[((n))]}")
flag=$(escapeStringLiteral "${flags[$n]}")
result="$result $var[$((n+1))] = \"$flag\";"$'\n'
done
printf " %s\n" "char **$var = malloc(sizeof(*$var) * ($((n+1)) + argc));"
@ -169,9 +167,9 @@ escapeStringLiteral() {
concat3Fn() {
printf "%s\n" 'char *concat3(char *x, char *y, char *z) {'
printf "%s\n" ' int xn = 0; while(x[++xn]);'
printf "%s\n" ' int yn = 0; while(y[++yn]);'
printf "%s\n" ' int zn = 0; while(z[++zn]);'
printf "%s\n" ' int xn = strlen(x);'
printf "%s\n" ' int yn = strlen(y);'
printf "%s\n" ' int zn = strlen(z);'
printf "%s\n" ' char *res = malloc(sizeof(*res)*(xn + yn + zn + 1));'
printf "%s\n" ' for (int i = 0; i < xn; ++i) res[i] = x[i];'
printf "%s\n" ' for (int i = 0; i < yn; ++i) res[xn+i] = y[i];'