diff --git a/generate-linuxcnc-switchkins-remap-table.sh b/generate-linuxcnc-switchkins-remap-table.sh index 130511f..394e6fc 100755 --- a/generate-linuxcnc-switchkins-remap-table.sh +++ b/generate-linuxcnc-switchkins-remap-table.sh @@ -71,6 +71,9 @@ fi linuxcnc_root=$(cd "$linuxcnc_root" && pwd) +# LinuxCNC source basis: src/hal/utils/halcmd_commands.cc do_loadusr_cmd() +# parses loadusr options with getopt("+wWin:"), and +# src/hal/utils/halcmd_completion.c lists the common -W/-Wn/-w/-iw forms. read_ini_switchkins_config() { local file=$1 awk ' @@ -79,6 +82,31 @@ read_ini_switchkins_config() { sub(/[[:space:]]*$/, "", value) return value } + function loadusr_program(value, fields, field_count, i, token) { + sub(/^.*(^|[[:space:]])loadusr[[:space:]]+/, "", value) + field_count = split(value, fields, /[[:space:]]+/) + for (i = 1; i <= field_count; i++) { + token = fields[i] + if (token == "-W" || token == "-w" || token == "-i") { + continue + } + if (token == "-Wn" || token == "-n") { + i++ + continue + } + if (token ~ /^-Wn.+/ || token ~ /^-n.+/) { + continue + } + if (token ~ /^-[wWi]+$/) { + continue + } + if (token ~ /^-/) { + continue + } + return token + } + return "" + } { line = $0 sub(/[[:space:]]*[#;].*$/, "", line) @@ -108,12 +136,7 @@ read_ini_switchkins_config() { } else if (section == "hal" && key == "halcmd") { lower = tolower(value) if (lower ~ /(^|[[:space:]])loadusr([[:space:]]|$)/) { - command = value - sub(/^.*(^|[[:space:]])loadusr[[:space:]]+/, "", command) - while (command ~ /^-[^[:space:]]+[[:space:]]+/) { - sub(/^-[^[:space:]]+[[:space:]]+/, "", command) - } - sub(/[[:space:]].*$/, "", command) + command = loadusr_program(value) if (command != "") { print "halcmd_loadusr\t" command } @@ -167,6 +190,31 @@ read_loadusr_commands() { sub(/[[:space:]]*$/, "", value) return value } + function loadusr_program(value, fields, field_count, i, token) { + sub(/^.*(^|[[:space:]])loadusr[[:space:]]+/, "", value) + field_count = split(value, fields, /[[:space:]]+/) + for (i = 1; i <= field_count; i++) { + token = fields[i] + if (token == "-W" || token == "-w" || token == "-i") { + continue + } + if (token == "-Wn" || token == "-n") { + i++ + continue + } + if (token ~ /^-Wn.+/ || token ~ /^-n.+/) { + continue + } + if (token ~ /^-[wWi]+$/) { + continue + } + if (token ~ /^-/) { + continue + } + return token + } + return "" + } { line = $0 sub(/[[:space:]]*#.*/, "", line) @@ -175,13 +223,9 @@ read_loadusr_commands() { if (lower !~ /(^|[[:space:]])loadusr([[:space:]]|$)/) { next } - sub(/^.*(^|[[:space:]])loadusr[[:space:]]+/, "", line) - while (line ~ /^-[^[:space:]]+[[:space:]]+/) { - sub(/^-[^[:space:]]+[[:space:]]+/, "", line) - } - sub(/[[:space:]].*$/, "", line) - if (line != "") { - print line + command = loadusr_program(line) + if (command != "") { + print command } } ' "$file"