X-Git-Url: https://git.draconx.ca/gitweb/dxcommon.git/blobdiff_plain/e599119f0492b01f1f21a8cce8d695c314dab3b1..8c45dd9fec485578a09dbf541d4209dcf77acd5f:/scripts/gen-options.awk diff --git a/scripts/gen-options.awk b/scripts/gen-options.awk index 80adcb4..450f85c 100755 --- a/scripts/gen-options.awk +++ b/scripts/gen-options.awk @@ -123,6 +123,10 @@ END { } BEGIN { + # Check if "\\\\" in substitutions gives just one backslash. + bs = "x"; sub(/x/, "\\\\", bs); + bs = (length(bs) == 1 ? "\\\\" : "\\"); + has_actions = 0 sopt_string = "" num_options = 0 @@ -177,7 +181,7 @@ $0 ~ /^-/ { # packed form is not possible w/ actions has_actions = 1; - n = split(work, a, /,[ \t]*/) + n = split(work, a, ",[ \t]*") if (n == 2) { flag = substr(a[1], 2) ", " substr(a[2], 1, length(a[2])-1) } else if (n == 1) { @@ -209,11 +213,10 @@ $0 ~ /^-/ { } # Ignore any line beginning with a # -$0 ~ /^#/ { next } +$0 ~ /^#/ { next; } -lopt { - sub(/^[ \t]*/, "") - if (!$0) { next } +NF && lopt != "" { + sub(/^[ \t]*/, ""); if (lopt in optionhelp) $0 = "\n" $0; @@ -238,7 +241,7 @@ END { lopt_strings = add_to_strtab(lopt_strings, sorted_options[i], offsets) } gsub(/[^ ]+/, "\"&", lopt_strings) - gsub(/ /, "\\0\"\n\t", lopt_strings) + gsub(/ /, bs"0\"\n\t", lopt_strings) print "static const char lopt_strings[] =" print "\t" lopt_strings "\";\n" @@ -318,8 +321,8 @@ END { help_offsets[opt] = help_pos help_pos += length(help) + 1 - gsub(/"/, "\\\"", help) - gsub(/\n/, "\\n\"\n\t \"", help) + gsub(/"/, bs"\"", help) + gsub(/\n/, bs"n\"\n\t \"", help) help = "\tPN_(\"" opt "\",\n\t \"" help "\")" } } @@ -454,25 +457,28 @@ function output_packed_macros(i, tmp, accum, max, totalbits) # 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 @@ -501,15 +507,16 @@ function to_enum(lopt) # For optimal results, strings should be added in descending length order. function add_to_strtab(strtab, str, offsets, pos) { - if ( (pos = index(strtab, str " ") - 1) < 0) { - pos = length(strtab) - if (pos) { - strtab = strtab " " str - pos++ - } else { - strtab = strtab str - } + if ( (pos = index(strtab, str " ") - 1) < 0) { + pos = length(strtab) + if (pos) { + strtab = strtab " " str + pos++ + } else { + strtab = strtab str } - offsets[str] = pos - return strtab + } + + offsets[str] = pos + return strtab }