From 29f1563294ac1ab19aa252f3fd5fca94c4f88516 Mon Sep 17 00:00:00 2001
From: "Arnold D. Robbins" <arnold@skeeve.com>
Date: Sun, 27 May 2018 18:43:20 +0300
Subject: [PATCH 061/289] Bug fix to extract.awk. Rerun and update files.

---
 awklib/ChangeLog                |   4 +
 awklib/eg/network/stoxpred.awk  |  29 ++
 awklib/eg/prog/extract.awk      |  11 +-
 awklib/eg/prog/indirectcall.awk |  45 +++
 awklib/extract.awk              |  10 +-
 doc/ChangeLog                   |   8 +
 doc/gawk.info                   | 492 ++++++++++++++++----------------
 doc/gawk.texi                   |  29 +-
 doc/gawktexi.in                 |  29 +-
 9 files changed, 390 insertions(+), 267 deletions(-)

diff --git a/awklib/eg/network/stoxpred.awk b/awklib/eg/network/stoxpred.awk
index 62744c14..aa1fbe9f 100644
--- a/awklib/eg/network/stoxpred.awk
+++ b/awklib/eg/network/stoxpred.awk
@@ -1,3 +1,32 @@
+BEGIN {
+  Init()
+  ReadQuotes()
+  CleanUp()
+  Prediction()
+  Report()
+  SendMail()
+}
+function Init() {
+  if (ARGC != 1) {
+    print "STOXPRED - daily stock share prediction"
+    print "IN:\n    no parameters, nothing on stdin"
+    print "PARAM:\n    -v Proxy=MyProxy -v ProxyPort=80"
+    print "OUT:\n    commented predictions as email"
+    print "JK 09.10.2000"
+    exit
+  }
+  # Remember ticker symbols from Dow Jones Industrial Index
+  StockCount = split("AA GE JNJ MSFT AXP GM JPM PG BA HD KO \
+    SBC C HON MCD T CAT HWP MMM UTX DD IBM MO WMT DIS INTC \
+    MRK XOM EK IP", name);
+  # Remember the current date as the end of the time series
+  day   = strftime("%d")
+  month = strftime("%m")
+  year  = strftime("%Y")
+  if (Proxy     == "")  Proxy     = "chart.yahoo.com"
+  if (ProxyPort ==  0)  ProxyPort = 80
+  YahooData = "/inet/tcp/0/" Proxy "/" ProxyPort
+}
 function ReadQuotes() {
   # Retrieve historical data for each ticker symbol
   FS = ","
diff --git a/awklib/eg/prog/extract.awk b/awklib/eg/prog/extract.awk
index f5dfcf40..ff598e8e 100644
--- a/awklib/eg/prog/extract.awk
+++ b/awklib/eg/prog/extract.awk
@@ -30,7 +30,7 @@ BEGIN    { IGNORECASE = 1 }
     }
     if ($3 != curfile) {
         if (curfile != "")
-            close(curfile)
+            filelist[curfile]++     # save to close later
         curfile = $3
     }
 
@@ -60,14 +60,13 @@ BEGIN    { IGNORECASE = 1 }
         print join(a, 1, n, SUBSEP) > curfile
     }
 }
+END {
+    for (f in filelist)
+        close(filelist[f])
+}
 function unexpected_eof()
 {
     printf("extract: %s:%d: unexpected EOF or error\n",
                      FILENAME, FNR) > "/dev/stderr"
     exit 1
 }
-
-END {
-    if (curfile)
-        close(curfile)
-}
diff --git a/awklib/eg/prog/indirectcall.awk b/awklib/eg/prog/indirectcall.awk
index 165b022a..b2b82686 100644
--- a/awklib/eg/prog/indirectcall.awk
+++ b/awklib/eg/prog/indirectcall.awk
@@ -1,3 +1,48 @@
+# indirectcall.awk --- Demonstrate indirect function calls
+#
+# Arnold Robbins, arnold@skeeve.com, Public Domain
+# January 2009
+# average --- return the average of the values in fields $first - $last
+
+function average(first, last,   sum, i)
+{
+    sum = 0;
+    for (i = first; i <= last; i++)
+        sum += $i
+
+    return sum / (last - first + 1)
+}
+
+# sum --- return the sum of the values in fields $first - $last
+
+function sum(first, last,   ret, i)
+{
+    ret = 0;
+    for (i = first; i <= last; i++)
+        ret += $i
+
+    return ret
+}
+# For each record, print the class name and the requested statistics
+{
+    class_name = $1
+    gsub(/_/, " ", class_name)  # Replace _ with spaces
+
+    # find start
+    for (i = 1; i <= NF; i++) {
+        if ($i == "data:") {
+            start = i + 1
+            break
+        }
+    }
+
+    printf("%s:\n", class_name)
+    for (i = 2; $i != "data:"; i++) {
+        the_function = $i
+        printf("\t%s: <%s>\n", $i, @the_function(start, NF) "")
+    }
+    print ""
+}
 # num_lt --- do a numeric less than comparison
 
 function num_lt(left, right)
diff --git a/awklib/extract.awk b/awklib/extract.awk
index 2662574b..96fc9498 100644
--- a/awklib/extract.awk
+++ b/awklib/extract.awk
@@ -30,7 +30,7 @@ BEGIN    { IGNORECASE = 1 }
     }
     if ($3 != curfile) {
         if (curfile != "")
-            close(curfile)
+            filelist[curfile]++     # save to close later
         curfile = $3
     }
 
@@ -60,6 +60,10 @@ BEGIN    { IGNORECASE = 1 }
         print join(a, 1, n, SUBSEP) > curfile
     }
 }
+END {
+    for (f in filelist)
+        close(filelist[f])
+}
 function unexpected_eof()
 {
     printf("extract: %s:%d: unexpected EOF or error\n",
@@ -67,10 +71,6 @@ function unexpected_eof()
     exit 1
 }
 
-END {
-    if (curfile)
-        close(curfile)
-}
 # join.awk --- join an array into a string
 #
 # Arnold Robbins, arnold@gnu.org, Public Domain
diff --git a/doc/gawktexi.in b/doc/gawktexi.in
index b62d12cd..c645a8ec 100644
--- a/doc/gawktexi.in
+++ b/doc/gawktexi.in
@@ -20124,7 +20124,7 @@ using indirect function calls:
 @ignore
 @c file eg/prog/indirectcall.awk
 #
-# Arnold Robbins, arnold@skeeve.com, Public Domain
+# Arnold Robbins, arnold@@skeeve.com, Public Domain
 # January 2009
 @c endfile
 @end ignore
@@ -25741,7 +25741,7 @@ line.  That line is then printed to the output file:
     @}
     if ($3 != curfile) @{
         if (curfile != "")
-            close(curfile)
+            filelist[curfile]++     # save to close later
         curfile = $3
     @}
 
@@ -25785,6 +25785,26 @@ sample source file (as has been done here!) without any hassle.  The file is
 only closed when a new @value{DF} name is encountered or at the end of the
 input file.
 
+When a new @value{FN} is encountered, instead of closing the file,
+the program saves the name of the current file in @code{filelist}.
+This makes it possible to interleave the code for more than one file in
+the Texinfo input file.  (Previous versions of this program @emph{did}
+close the file. But because of the @samp{>} redirection, a file whose
+parts were not all one after the other ended up getting clobbered.)
+An @code{END} rule then closes all the open files when processing
+is finished:
+
+@example
+@c file eg/prog/extract.awk
+@group
+END @{
+    for (f in filelist)
+        close(filelist[f])
+@}
+@end group
+@c endfile
+@end example
+
 Finally, the function @code{@w{unexpected_eof()}} prints an appropriate
 error message and then exits.
 The @code{END} rule handles the final cleanup, closing the open file:
@@ -25799,11 +25819,6 @@ function unexpected_eof()
     exit 1
 @}
 @end group
-
-END @{
-    if (curfile)
-        close(curfile)
-@}
 @c endfile
 @end example
 
-- 
2.19.1

