Strengthen LinuxCNC switchkins remap generation

This commit is contained in:
cnc
2026-06-02 06:46:54 +08:00
parent f5013ec745
commit 87fcf74135
9 changed files with 2284 additions and 123 deletions

View File

@@ -75,6 +75,10 @@ read_ini_switchkins_config() {
line = $0
sub(/[[:space:]]*[#;].*$/, "", line)
line = trim(line)
if (line ~ /^\[[^][]+\]$/) {
section = tolower(substr(line, 2, length(line) - 2))
next
}
if (line !~ /=/) {
next
}
@@ -85,17 +89,17 @@ read_ini_switchkins_config() {
sub(/^[^=]*=[[:space:]]*/, "", value)
value = trim(value)
if (key == "machine") {
if (section == "emc" && key == "machine") {
print "machine\t" value
} else if (key == "kinematics") {
} else if (section == "kins" && key == "kinematics") {
print "kinematics\t" value
} else if (key == "halfile") {
} else if (section == "hal" && key == "halfile") {
print "halfile\t" value
} else if (key == "postgui_halfile") {
} else if (section == "hal" && key == "postgui_halfile") {
print "postgui_halfile\t" value
} else if (key == "subroutine_path") {
} else if (section == "rs274ngc" && key == "subroutine_path") {
print "subroutine_path\t" value
} else if (key == "remap") {
} else if (section == "rs274ngc" && key == "remap") {
lower = tolower(value)
for (mcode = 428; mcode <= 430; mcode++) {
if (lower ~ "m" mcode "([^0-9]|$)" &&
@@ -110,24 +114,34 @@ read_ini_switchkins_config() {
' "$file"
}
resolved_config_path=
resolve_config_path() {
local config_dir=$1
local path=$2
if [[ "$path" = /* ]]; then
printf '%s\n' "$path"
resolved_config_path=$path
else
printf '%s\n' "$config_dir/$path"
resolved_config_path=$config_dir/$path
fi
}
resolved_linuxcnc_path=
declare -A relative_linuxcnc_path_cache=()
relative_linuxcnc_path() {
local path=$1
path=$(cd "${path%/*}" && pwd)/${path##*/}
if [[ "$path" != "$linuxcnc_root/"* ]]; then
echo "resolved LinuxCNC source path escapes LinuxCNC root: $path" >&2
if [[ -n "${relative_linuxcnc_path_cache[$path]+x}" ]]; then
resolved_linuxcnc_path=${relative_linuxcnc_path_cache[$path]}
return 0
fi
local absolute_path
absolute_path=$(cd "${path%/*}" && pwd)/${path##*/}
if [[ "$absolute_path" != "$linuxcnc_root/"* ]]; then
echo "resolved LinuxCNC source path escapes LinuxCNC root: $absolute_path" >&2
exit 1
fi
printf '%s\n' "${path#"$linuxcnc_root/"}"
resolved_linuxcnc_path=${absolute_path#"$linuxcnc_root/"}
relative_linuxcnc_path_cache[$path]=$resolved_linuxcnc_path
}
declare -A config_dir_cache=()
@@ -159,6 +173,10 @@ while IFS=: read -r manifest_group manifest_path _manifest_note; do
case "$manifest_group" in
config)
validate_manifest_source_path "$manifest_group" "$manifest_path"
if [[ "$manifest_path" != *.ini && "$manifest_path" != *.hal ]]; then
echo "LinuxCNC config source must be an INI or HAL file in $manifest: $manifest_path" >&2
exit 1
fi
if [[ -n "${manifest_config_sources[$manifest_path]:-}" ]]; then
echo "duplicate LinuxCNC config source in $manifest: $manifest_path" >&2
exit 1
@@ -190,24 +208,28 @@ while IFS=: read -r manifest_group manifest_path _manifest_note; do
esac
done <"$manifest"
resolved_config_dir=
config_dir_for() {
local config=$1
if [[ -z "${config_dir_cache[$config]:-}" ]]; then
config_dir_cache[$config]=$(cd "${config%/*}" && pwd)
fi
printf '%s\n' "${config_dir_cache[$config]}"
resolved_config_dir=${config_dir_cache[$config]}
}
resolved_remap_source=
remap_source_for_ngc() {
local config=$1
local ngc_name=$2
local subroutine_paths=${3:-}
resolved_remap_source=
if [[ "$ngc_name" != *.ngc ]]; then
ngc_name="$ngc_name.ngc"
fi
local config_dir
config_dir=$(config_dir_for "$config")
config_dir_for "$config"
config_dir=$resolved_config_dir
if [[ -z "$subroutine_paths" ]]; then
subroutine_paths="."
fi
@@ -220,9 +242,11 @@ remap_source_for_ngc() {
for path_part in "${path_parts[@]}"; do
[[ -n "$path_part" ]] || continue
local candidate_dir
candidate_dir=$(resolve_config_path "$config_dir" "$path_part")
resolve_config_path "$config_dir" "$path_part"
candidate_dir=$resolved_config_path
if [[ -f "$candidate_dir/$ngc_name" ]]; then
relative_linuxcnc_path "$candidate_dir/$ngc_name"
resolved_remap_source=$resolved_linuxcnc_path
return 0
fi
done
@@ -231,9 +255,11 @@ remap_source_for_ngc() {
return 1
}
resolved_config_source=
config_source_for_halfile() {
local config=$1
local halfile=$2
resolved_config_source=
[[ -n "$halfile" ]] || return 1
[[ "$halfile" == LIB:* ]] && return 1
@@ -241,24 +267,34 @@ config_source_for_halfile() {
[[ -n "$hal_path" ]] || return 1
local config_dir
config_dir=$(config_dir_for "$config")
config_dir_for "$config"
config_dir=$resolved_config_dir
local resolved
resolved=$(resolve_config_path "$config_dir" "$hal_path")
resolve_config_path "$config_dir" "$hal_path"
resolved=$resolved_config_path
if [[ ! -f "$resolved" ]]; then
echo "missing LinuxCNC HAL source referenced by ${config#"$linuxcnc_root/"}: $halfile" >&2
exit 1
fi
relative_linuxcnc_path "$resolved"
resolved_config_source=$resolved_linuxcnc_path
}
resolved_kinstype=
declare -A kinstype_cache=()
kinstype_for_remap_source() {
local source
source=$1
resolved_kinstype=
if [[ -z "$source" ]]; then
printf '%s\n' -1
resolved_kinstype=-1
return 0
fi
awk '
if [[ -n "${kinstype_cache[$source]+x}" ]]; then
resolved_kinstype=${kinstype_cache[$source]}
return 0
fi
resolved_kinstype=$(awk '
/^[[:space:]]*#<kinstype>[[:space:]]*=/ {
value = $0
sub(/^[[:space:]]*#<kinstype>[[:space:]]*=[[:space:]]*/, "", value)
@@ -266,7 +302,8 @@ kinstype_for_remap_source() {
print value
exit
}
' "$linuxcnc_root/$source"
' "$linuxcnc_root/$source")
kinstype_cache[$source]=$resolved_kinstype
}
require_remap_source() {
@@ -324,13 +361,6 @@ resolved_remap_count=0
unique_config_source_count=0
unique_resolved_remap_count=0
escape_generated_string() {
local value=$1
value=${value//\\/\\\\}
value=${value//\"/\\\"}
printf '%s\n' "$value"
}
add_alias() {
local alias=$1
local m428=$2
@@ -338,7 +368,8 @@ add_alias() {
local m430=$4
alias=${alias,,}
alias=${alias//[[:space:]]/}
alias=$(escape_generated_string "$alias")
alias=${alias//\\/\\\\}
alias=${alias//\"/\\\"}
[[ -n "$alias" ]] || return 0
local types="$m428,$m429,$m430"
@@ -376,18 +407,21 @@ add_case() {
seen_cases[$key]=1
local cpp_field=$field
local cpp_value=$value
cpp_field=$(escape_generated_string "$cpp_field")
cpp_value=$(escape_generated_string "$cpp_value")
cpp_field=${cpp_field//\\/\\\\}
cpp_field=${cpp_field//\"/\\\"}
cpp_value=${cpp_value//\\/\\\\}
cpp_value=${cpp_value//\"/\\\"}
case_entries+=(" {\"$cpp_field\", \"$cpp_value\", $m428, $m429, $m430},")
json_case_entries+=("{\"field\":\"$cpp_field\",\"value\":\"$cpp_value\",\"m428\":$m428,\"m429\":$m429,\"m430\":$m430}")
}
trimmed_machine_label=
trim_machine_label() {
local machine=$1
machine=${machine%%(*}
machine="${machine#"${machine%%[![:space:]]*}"}"
machine="${machine%"${machine##*[![:space:]]}"}"
printf '%s\n' "$machine"
trimmed_machine_label=$machine
}
add_kinematics_variants() {
@@ -444,9 +478,8 @@ add_machine_aliases() {
local m430=$4
[[ -n "$machine" ]] || return 0
add_alias "$machine" "$m428" "$m429" "$m430"
local trimmed_machine
trimmed_machine=$(trim_machine_label "$machine")
add_alias "$trimmed_machine" "$m428" "$m429" "$m430"
trim_machine_label "$machine"
add_alias "$trimmed_machine_label" "$m428" "$m429" "$m430"
}
add_kinematics_aliases() {
@@ -472,9 +505,8 @@ add_switchkins_cases() {
if [[ -n "$machine" ]]; then
add_case "switchkins" "$machine" "$m428" "$m429" "$m430"
local trimmed_machine
trimmed_machine=$(trim_machine_label "$machine")
add_case "switchkins" "$trimmed_machine" "$m428" "$m429" "$m430"
trim_machine_label "$machine"
add_case "switchkins" "$trimmed_machine_label" "$m428" "$m429" "$m430"
fi
if [[ -n "$kinematics" ]]; then
@@ -509,6 +541,19 @@ add_halfile_cases() {
add_halfile_case_fields "HALFILE" "$halfile" "$m428" "$m429" "$m430"
}
add_halfile_source_cases() {
local source=$1
local m428=$2
local m429=$3
local m430=$4
[[ -n "$source" ]] || return 0
add_case "halFile" "$source" "$m428" "$m429" "$m430"
add_case "halfile" "$source" "$m428" "$m429" "$m430"
add_case "hal_file" "$source" "$m428" "$m429" "$m430"
add_case "HALFILE" "$source" "$m428" "$m429" "$m430"
}
add_postgui_halfile_cases() {
local halfile=$1
local m428=$2
@@ -518,11 +563,39 @@ add_postgui_halfile_cases() {
[[ "$halfile" == LIB:* ]] && return 0
add_halfile_case_fields "postguiHalFile" "$halfile" "$m428" "$m429" "$m430"
add_halfile_case_fields "postguihalfile" "$halfile" "$m428" "$m429" "$m430"
add_halfile_case_fields "postgui_halfile" "$halfile" "$m428" "$m429" "$m430"
add_halfile_case_fields "postgui_hal_file" "$halfile" "$m428" "$m429" "$m430"
add_halfile_case_fields "POSTGUI_HALFILE" "$halfile" "$m428" "$m429" "$m430"
}
add_postgui_halfile_source_cases() {
local source=$1
local m428=$2
local m429=$3
local m430=$4
[[ -n "$source" ]] || return 0
add_case "postguiHalFile" "$source" "$m428" "$m429" "$m430"
add_case "postguihalfile" "$source" "$m428" "$m429" "$m430"
add_case "postgui_halfile" "$source" "$m428" "$m429" "$m430"
add_case "postgui_hal_file" "$source" "$m428" "$m429" "$m430"
add_case "POSTGUI_HALFILE" "$source" "$m428" "$m429" "$m430"
}
add_subroutine_path_cases() {
local subroutine_path=$1
local m428=$2
local m429=$3
local m430=$4
[[ -n "$subroutine_path" ]] || return 0
add_case "subroutinePath" "$subroutine_path" "$m428" "$m429" "$m430"
add_case "subroutinepath" "$subroutine_path" "$m428" "$m429" "$m430"
add_case "subroutine_path" "$subroutine_path" "$m428" "$m429" "$m430"
add_case "SUBROUTINE_PATH" "$subroutine_path" "$m428" "$m429" "$m430"
}
while IFS=: read -r group path _note; do
[[ "$group" == "config" && "$path" == *.ini ]] || continue
@@ -531,9 +604,6 @@ while IFS=: read -r group path _note; do
echo "missing LinuxCNC config source: $path" >&2
exit 1
}
if ! grep -Eiq 'REMAP[[:space:]]*=[[:space:]]*M(428|429|430)([^0-9]|$)' "$config"; then
continue
fi
machine=
kinematics=
@@ -571,6 +641,9 @@ while IFS=: read -r group path _note; do
;;
esac
done < <(read_ini_switchkins_config "$config")
if [[ -z "$remap_ngc_428" && -z "$remap_ngc_429" && -z "$remap_ngc_430" ]]; then
continue
fi
if [[ -z "$subroutine_paths" ]]; then
subroutine_paths="."
fi
@@ -579,27 +652,36 @@ while IFS=: read -r group path _note; do
remap_source_429=
remap_source_430=
if [[ -n "$remap_ngc_428" ]]; then
remap_source_428=$(remap_source_for_ngc "$config" "$remap_ngc_428" "$subroutine_paths" || true)
if remap_source_for_ngc "$config" "$remap_ngc_428" "$subroutine_paths"; then
remap_source_428=$resolved_remap_source
fi
require_remap_source "$path" 428 "$remap_ngc_428" "$remap_source_428"
record_remap_source "$path" 428 "$remap_source_428"
resolved_remap_count=$((resolved_remap_count + 1))
fi
if [[ -n "$remap_ngc_429" ]]; then
remap_source_429=$(remap_source_for_ngc "$config" "$remap_ngc_429" "$subroutine_paths" || true)
if remap_source_for_ngc "$config" "$remap_ngc_429" "$subroutine_paths"; then
remap_source_429=$resolved_remap_source
fi
require_remap_source "$path" 429 "$remap_ngc_429" "$remap_source_429"
record_remap_source "$path" 429 "$remap_source_429"
resolved_remap_count=$((resolved_remap_count + 1))
fi
if [[ -n "$remap_ngc_430" ]]; then
remap_source_430=$(remap_source_for_ngc "$config" "$remap_ngc_430" "$subroutine_paths" || true)
if remap_source_for_ngc "$config" "$remap_ngc_430" "$subroutine_paths"; then
remap_source_430=$resolved_remap_source
fi
require_remap_source "$path" 430 "$remap_ngc_430" "$remap_source_430"
record_remap_source "$path" 430 "$remap_source_430"
resolved_remap_count=$((resolved_remap_count + 1))
fi
m428=$(kinstype_for_remap_source "$remap_source_428")
m429=$(kinstype_for_remap_source "$remap_source_429")
m430=$(kinstype_for_remap_source "$remap_source_430")
kinstype_for_remap_source "$remap_source_428"
m428=$resolved_kinstype
kinstype_for_remap_source "$remap_source_429"
m429=$resolved_kinstype
kinstype_for_remap_source "$remap_source_430"
m430=$resolved_kinstype
[[ -z "$remap_ngc_428" ]] || require_kinstype "$path" 428 "$remap_source_428" "$m428"
[[ -z "$remap_ngc_429" ]] || require_kinstype "$path" 429 "$remap_source_429" "$m429"
[[ -z "$remap_ngc_430" ]] || require_kinstype "$path" 430 "$remap_source_430" "$m430"
@@ -615,10 +697,13 @@ while IFS=: read -r group path _note; do
add_case "config" "$path" "$m428" "$m429" "$m430"
add_case "configPath" "$path" "$m428" "$m429" "$m430"
add_case "configpath" "$path" "$m428" "$m429" "$m430"
add_case "config_path" "$path" "$m428" "$m429" "$m430"
add_case "ini" "$path" "$m428" "$m429" "$m430"
add_case "iniFile" "$path" "$m428" "$m429" "$m430"
add_case "inifile" "$path" "$m428" "$m429" "$m430"
add_case "iniFileName" "$path" "$m428" "$m429" "$m430"
add_case "inifilename" "$path" "$m428" "$m429" "$m430"
add_case "ini_file" "$path" "$m428" "$m429" "$m430"
add_case "ini_file_name" "$path" "$m428" "$m429" "$m430"
add_case "INI_FILE_NAME" "$path" "$m428" "$m429" "$m430"
@@ -633,15 +718,21 @@ while IFS=: read -r group path _note; do
add_machine_aliases "$machine" "$m428" "$m429" "$m430"
add_kinematics_aliases "$kinematics" "$m428" "$m429" "$m430"
while IFS= read -r halfile; do
if hal_source=$(config_source_for_halfile "$config" "$halfile"); then
if config_source_for_halfile "$config" "$halfile"; then
hal_source=$resolved_config_source
record_config_source "$path" "$hal_source" "HALFILE"
add_alias "$hal_source" "$m428" "$m429" "$m430"
add_halfile_source_cases "$hal_source" "$m428" "$m429" "$m430"
fi
add_halfile_cases "$halfile" "$m428" "$m429" "$m430"
add_halfile_aliases "$halfile" "$m428" "$m429" "$m430"
done <<<"$halfiles"
while IFS= read -r halfile; do
if hal_source=$(config_source_for_halfile "$config" "$halfile"); then
if config_source_for_halfile "$config" "$halfile"; then
hal_source=$resolved_config_source
record_config_source "$path" "$hal_source" "POSTGUI_HALFILE"
add_alias "$hal_source" "$m428" "$m429" "$m430"
add_postgui_halfile_source_cases "$hal_source" "$m428" "$m429" "$m430"
fi
add_postgui_halfile_cases "$halfile" "$m428" "$m429" "$m430"
add_halfile_aliases "$halfile" "$m428" "$m429" "$m430"
@@ -650,13 +741,17 @@ while IFS=: read -r group path _note; do
for remap_source in "$remap_source_428" "$remap_source_429" "$remap_source_430"; do
if [[ -n "$remap_source" ]]; then
remap_dir=${remap_source%/*}
add_alias "$remap_source" "$m428" "$m429" "$m430"
add_alias "$remap_dir" "$m428" "$m429" "$m430"
add_alias "${remap_dir%/*}" "$m428" "$m429" "$m430"
add_alias "${remap_dir%/remap_subs}" "$m428" "$m429" "$m430"
remap_parent=${remap_dir%/remap_subs}
add_alias "${remap_parent##*/}" "$m428" "$m429" "$m430"
add_case "remap" "$remap_source" "$m428" "$m429" "$m430"
add_case "remap" "$remap_dir" "$m428" "$m429" "$m430"
add_case "remap" "$remap_parent" "$m428" "$m429" "$m430"
add_case "remap" "${remap_parent##*/}" "$m428" "$m429" "$m430"
add_subroutine_path_cases "$remap_dir" "$m428" "$m429" "$m430"
fi
done
done <"$manifest"
@@ -739,7 +834,7 @@ write_config_cases() {
cat <<'EOF'
// Generated by ./generate-linuxcnc-switchkins-remap-table.sh --config-cases.
// Source: LinuxCNC INI config path, MACHINE, KINEMATICS, non-LIB HALFILE/POSTGUI_HALFILE,
// and adjacent remap_subs entries for M428/M429/M430 #<kinstype> assignments.
// SUBROUTINE_PATH-resolved remap_subs entries, and adjacent M428/M429/M430 #<kinstype> assignments.
EOF
printf '%s\n' "${case_entries[@]}"