X-Git-Url: https://git.draconx.ca/gitweb/dxcommon.git/blobdiff_plain/43e5c4ebc6411fcc47a637ddeb22ac02bb5561ee..HEAD:/scripts/gen-strtab.awk diff --git a/scripts/gen-strtab.awk b/scripts/gen-strtab.awk index 2265bb5..2a56fd6 100755 --- a/scripts/gen-strtab.awk +++ b/scripts/gen-strtab.awk @@ -1,6 +1,6 @@ #!/bin/awk -f # -# Copyright © 2021, 2023 Nick Bowler +# Copyright © 2021, 2023-2024 Nick Bowler # # Generate a C string table based on an input string specification file. # @@ -80,6 +80,10 @@ END { } BEGIN { + # Check if "\\\\" in substitutions gives just one backslash. + bs = "x"; sub(/x/, "\\\\", bs); + bs = (length(bs) == 1 ? "\\\\" : "\\"); + opts["zero"] = 1 opts["macro"] = 0 collected = ident = "" @@ -107,32 +111,29 @@ sub(/^@/, "", $0) { } sub(/^[&]/, "") { - if (ident) { - finish_string_input(strings, ident, collected) - vars[num_vars++] = ident + if (ident != "") { + finish_string_input(strings, ident, collected); + vars[num_vars++] = ident; } - current_l10n = !sub(/^[&]/, "", $1); - startline = NR - ident = $1 + current_l10n = !sub(/^[&]/, ""); + startline = NR; + ident = $1; - $1 = "" - collected = "" + collected = ""; + sub(/^[^ \t]*/, ""); } -ident { - sub(/^[ \t]*/, "") - if (collected) { - collected = collected "\n" $0 - } else { - collected = $0 - } +ident != "" { + sub(/^[ \t]*/, ""); - endline = NR + sep = collected != "" ? "\n" : ""; + collected = collected sep $0; + endline = NR; } END { - if (ident) { + if (ident != "") { finish_string_input(strings, ident, collected) vars[num_vars++] = ident } @@ -157,7 +158,7 @@ END { for (i = 0; i < count; i++) { s = sorted_strings[i] - gsub(/\\\\/, "\2\2", s) + gsub(/\\\\/, "\2", s) if ((n = index(strtab "\1", s "\1")) > 0) { offsets[sorted_strings[i]] = real_length(substr(strtab, 1, n-1)); if (!(sorted_strings[i] in nol10n)) @@ -173,7 +174,7 @@ END { } } - gsub(/\2/, "\\", strtab); + gsub("\2", bs bs, strtab); n = split(strtab, split_strtab, "\1"); for (i = 1; i <= n; i++) { printf("\t%4s ", i > !!opts["zero"] ? "\"\\0\"" : ""); @@ -206,24 +207,25 @@ END { # strings[ident] = val. function finish_string_input(strings, ident, val, n, tmpval) { - gsub(/\\\\/, "\1\1", val) - val = val (endline > startline ? "\n" : "") - gsub(/\\\n/, "", val) + gsub(/\\\\/, "\2", val); + if (endline > startline) + val = val "\n"; + gsub(/\\\n/, "", val); tmpval = "" while ((n = match(val, /\\[^abtnvfr]/)) > 0) { - tmpval = tmpval substr(val, 1, n-1) - val = substr(val, n+1) + tmpval = tmpval substr(val, 1, n-1); + val = substr(val, n+1); } - tmpval = tmpval val + tmpval = tmpval val; # Escape special characters - gsub(/"/, "\\\"", tmpval) - gsub(/\t/, "\\t", tmpval) - gsub(/\n/, "\\n", tmpval) - gsub(/\1/, "\\", tmpval) + gsub(/"/, bs"\"", tmpval); + gsub(/\t/, bs"t", tmpval); + gsub(/\n/, bs"n", tmpval); + gsub("\2", bs bs, tmpval); - strings[ident] = tmpval + strings[ident] = tmpval; if (!current_l10n) { nol10n[tmpval] = 1; } @@ -232,7 +234,7 @@ function finish_string_input(strings, ident, val, n, tmpval) function real_length(s, t) { t = length(s) - return t - gsub(/\\.|\2\2/, "&", s) + return t - gsub(/\\./, "&", s) } # bucketsort(dst, src) @@ -241,25 +243,28 @@ function real_length(s, t) # placing them into dst[0] ... dst[n]. # # Returns the number of elements. -function bucketsort(dst, src, buckets, max, count, i, t) +function bucketsort(dst, src, max, count, i, t) { + # Note: ULTRIX 4.5 nawk does not support local array parameters + split("", bucketsort_buckets); + for (t in src) { i = length(src[t]) if (i > max) { max = i } - buckets[i]++ + bucketsort_buckets[i]++ } for (i = max; i > 0; i--) { - if (i in buckets) { - t = buckets[i] - buckets[i] = count + if (i in bucketsort_buckets) { + t = bucketsort_buckets[i] + bucketsort_buckets[i] = count count += t } } for (t in src) { i = length(t = src[t]) - dst[buckets[i]++] = t + dst[bucketsort_buckets[i]++] = t } return count