]> git.draconx.ca Git - dxcommon.git/blobdiff - scripts/gen-tree.awk
Import getline helper from cdecl99.
[dxcommon.git] / scripts / gen-tree.awk
index c461e5d1b97ec211575241408a54d03d4f63530a..1334bf8dfe9611ad83bdfce79fcb99020af25f32 100755 (executable)
@@ -93,6 +93,10 @@ END {
 }
 
 BEGIN {
+  # Check if "\\\\" in substitutions gives just one backslash.
+  bs = "x"; sub(/x/, "\\\\", bs);
+  bs = (length(bs) == 1 ? "\\\\" : "\\");
+
   opts["strtab"] = 1;
 
   depth = max_depth = 0;
@@ -161,12 +165,12 @@ indent > 0 {
   level_count[depth]++;
 }
 
-indent == 0 && tree_identifier {
+indent == 0 && tree_identifier != "" {
   trees[tree_identifier] = format_items();
   tree_identifier = "";
 }
 END {
-  if (tree_identifier)
+  if (tree_identifier != "")
     trees[tree_identifier] = format_items()
 }
 indent == 0 { tree_identifier = $1 }
@@ -187,7 +191,7 @@ END {
       }
     }
 
-    gsub(/\1/, "\"\n\t\"\\0\" \"", entry_strtab);
+    gsub("\1", "\"\n\t\"" bs "0\" \"", entry_strtab);
     sub(/^"/, "", entry_strtab);
     sub(/\n[^\n]*$/, ";", entry_strtab);
     print "\nstatic const char tree_strtab[] =" entry_strtab
@@ -221,7 +225,7 @@ function format_items(s, i)
     depth--;
   }
 
-  for (i = 2; tree_items[i]; i++) {
+  for (i = 2; tree_items[i] != ""; i++) {
     level_count[i] += level_count[i-1];
   }
 
@@ -230,7 +234,7 @@ function format_items(s, i)
     delete subtree_depth[i];
   }
 
-  for (i = 1; tree_items[i]; i++) {
+  for (i = 1; tree_items[i] != ""; i++) {
     s = s tree_items[i];
     delete tree_items[i];
     delete level_count[i];
@@ -246,25 +250,28 @@ function format_items(s, i)
 # 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