gawk: Read Timeout

1 
1 4.11 Reading Input with a Timeout
1 =================================
1 
1 This minor node describes a feature that is specific to 'gawk'.
1 
1    You may specify a timeout in milliseconds for reading input from the
1 keyboard, a pipe, or two-way communication, including TCP/IP sockets.
1 This can be done on a per-input, per-command, or per-connection basis,
1 by setting a special element in the 'PROCINFO' array (⇒Auto-set):
1 
1      PROCINFO["input_name", "READ_TIMEOUT"] = TIMEOUT IN MILLISECONDS
1 
1    When set, this causes 'gawk' to time out and return failure if no
1 data is available to read within the specified timeout period.  For
1 example, a TCP client can decide to give up on receiving any response
1 from the server after a certain amount of time:
1 
1      Service = "/inet/tcp/0/localhost/daytime"
1      PROCINFO[Service, "READ_TIMEOUT"] = 100
1      if ((Service |& getline) > 0)
1          print $0
1      else if (ERRNO != "")
1          print ERRNO
1 
1    Here is how to read interactively from the user(1) without waiting
1 for more than five seconds:
1 
1      PROCINFO["/dev/stdin", "READ_TIMEOUT"] = 5000
1      while ((getline < "/dev/stdin") > 0)
1          print $0
1 
1    'gawk' terminates the read operation if input does not arrive after
1 waiting for the timeout period, returns failure, and sets 'ERRNO' to an
1 appropriate string value.  A negative or zero value for the timeout is
1 the same as specifying no timeout at all.
1 
1    A timeout can also be set for reading from the keyboard in the
1 implicit loop that reads input records and matches them against
1 patterns, like so:
1 
1      $ gawk 'BEGIN { PROCINFO["-", "READ_TIMEOUT"] = 5000 }
1      > { print "You entered: " $0 }'
1      gawk
1      -| You entered: gawk
1 
1    In this case, failure to respond within five seconds results in the
1 following error message:
1 
1      error-> gawk: cmd. line:2: (FILENAME=- FNR=1) fatal: error reading input file `-': Connection timed out
1 
1    The timeout can be set or changed at any time, and will take effect
1 on the next attempt to read from the input device.  In the following
1 example, we start with a timeout value of one second, and progressively
1 reduce it by one-tenth of a second until we wait indefinitely for the
1 input to arrive:
1 
1      PROCINFO[Service, "READ_TIMEOUT"] = 1000
1      while ((Service |& getline) > 0) {
1          print $0
1          PROCINFO[Service, "READ_TIMEOUT"] -= 100
1      }
1 
1      NOTE: You should not assume that the read operation will block
1      exactly after the tenth record has been printed.  It is possible
1      that 'gawk' will read and buffer more than one record's worth of
1      data the first time.  Because of this, changing the value of
1      timeout like in the preceding example is not very useful.
1 
1    If the 'PROCINFO' element is not present and the 'GAWK_READ_TIMEOUT'
1 environment variable exists, 'gawk' uses its value to initialize the
1 timeout value.  The exclusive use of the environment variable to specify
1 timeout has the disadvantage of not being able to control it on a
1 per-command or per-connection basis.
1 
1    'gawk' considers a timeout event to be an error even though the
1 attempt to read from the underlying device may succeed in a later
1 attempt.  This is a limitation, and it also means that you cannot use
11 this to multiplex input from two or more sources.  ⇒Retrying
 Input for a way to enable later I/O attempts to succeed.
1 
1    Assigning a timeout value prevents read operations from blocking
1 indefinitely.  But bear in mind that there are other ways 'gawk' can
1 stall waiting for an input device to be ready.  A network client can
1 sometimes take a long time to establish a connection before it can start
1 reading any data, or the attempt to open a FIFO special file for reading
1 can block indefinitely until some other process opens it for writing.
1 
1    ---------- Footnotes ----------
1 
1    (1) This assumes that standard input is the keyboard.
1