[type/__dma] Use EQS to split config lines

This commit is contained in:
Dennis Camera 2020-06-11 18:07:28 +02:00
parent 5513485097
commit b293c42b5a
1 changed files with 17 additions and 14 deletions

View File

@ -130,19 +130,20 @@ function comment_line(line) {
function empty_line(line) { return line ~ /^[ \t]*$/ } function empty_line(line) { return line ~ /^[ \t]*$/ }
function is_word(s) { return s ~ /^[A-Z_]+$/ } # "looks like a plausible word" function is_word(s) { return s ~ /^[A-Z_]+$/ } # "looks like a plausible word"
function first(line, sep) { function first(line, sep_re) {
# returns the part of the line until sep is found # returns the part of the line until sep is found
# (or the whole line if sep is not found) # (or the whole line if sep is not found)
if (!sep) sep = SUBSEP if (!sep_re) sep_re = "[" SUBSEP "]"
return index(line, sep) ? substr(line, 1, index(line, sep) - 1) : line match(line, sep_re)
return RSTART ? substr(line, 1, RSTART - 1) : line
} }
function rest(line, sep) { function rest(line, sep_re) {
# returns the part of the line after the first occurrence of sep is found. # returns the part of the line after the first occurrence of sep is found.
# (or nothing if sep is not found) # (or nothing if sep is not found)
if (!sep) sep = SUBSEP if (!sep_re) sep_re = "[" SUBSEP "]"
if (index(line, sep)) if (match(line, sep_re))
return substr(line, index(line, sep) + 1) return substr(line, RSTART + RLENGTH + 1)
} }
function conf_pop(word, value) { function conf_pop(word, value) {
@ -188,13 +189,15 @@ function print_confs(word, value) {
} }
BEGIN { BEGIN {
EQS = /[ \t]/ # copied from dma/conf.c
# read the "should" state into the `conf` array. # read the "should" state into the `conf` array.
while (getline < "/dev/stdin") { while (getline < "/dev/stdin") {
word = first($0, " ") word = first($0, EQS)
if ((word in conf)) if ((word in conf))
conf[word] = conf[word] SUBSEP rest($0, " ") conf[word] = conf[word] SUBSEP rest($0, EQS)
else else
conf[word] = rest($0, " ") conf[word] = rest($0, EQS)
} }
} }
@ -203,10 +206,10 @@ BEGIN {
NR == FNR { NR == FNR {
if (comment_line($0)) { if (comment_line($0)) {
# comment line # comment line
word = first(substr($0, comment_line($0) + 1), " ") word = first(substr($0, comment_line($0) + 1), /[ ]/)
if (is_word(word)) last_occ["#" word] = FNR if (is_word(word)) last_occ["#" word] = FNR
} else { } else {
word = first($0, " ") word = first($0, EQS)
if (is_word(word)) last_occ[word] = FNR if (is_word(word)) last_occ[word] = FNR
} }
} }
@ -247,8 +250,8 @@ NR > FNR {
} }
} }
} else { } else {
word = first($0, " ") word = first($0, EQS)
value = rest($0, " ") value = rest($0, EQS)
sub(/[ \t]*\#.*$/, "", value) # ignore comments in value sub(/[ \t]*\#.*$/, "", value) # ignore comments in value
if ((word in conf) && value == first(conf[word])) { if ((word in conf) && value == first(conf[word])) {