]> git.draconx.ca Git - dxcommon.git/commitdiff
Avoid local array parameters in awk scripts.
authorNick Bowler <nbowler@draconx.ca>
Sun, 3 Dec 2023 05:21:49 +0000 (00:21 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sun, 3 Dec 2023 06:07:51 +0000 (01:07 -0500)
ULTRIX 4.5 nawk appears to not support local array parameters on user-
defined functions (it is OK for array arguments passed by the caller).
Things seems to work on the first call but on the second call the array
is not empty and other weird behaviours occur.

Using a global array instead seems OK and avoids the problem.

scripts/gen-options.awk
scripts/gen-strtab.awk
scripts/gen-tree.awk

index c6d361cd89159956c1ef71f948abcd7a845c4bbc..f2cdfe7825ab6f600334a7bb76fbe32408b6f1a4 100755 (executable)
@@ -458,25 +458,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
@@ -505,15 +508,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
 }
index c5b02c6d3840fef82f2bbaa7a9353312b986c95c..6ec06be8b526c469c2f094babf6fb0bee2867b3c 100755 (executable)
@@ -246,25 +246,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
index 55a7d5c4e6bebe3bc02bdbfa867f43702907c0e7..413f156707c96b1f3578aca519ba40b105eb9335 100755 (executable)
@@ -250,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