bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

problem with gawk 3.1.3 (and 3.1.2)


From: Peter C. Vernam
Subject: problem with gawk 3.1.3 (and 3.1.2)
Date: Fri, 26 Sep 2003 12:32:55 -0400

This is most likely a bug in the documentation ("Effective awk Programming" (3rd Edition) by Arnold Robbins), but it's possible that it's a bug in the code. If it is a documentation problem, it is more a matter of omission, rather than a documentation bug (i.e., I don't believe there is any incorrect information in the book, it's just that the correct information seems to be missing).

My problem is that I can't figure out how to do error handling when using the Internetworking capability in gawk 3.1. If I try to open a connection to a host and TCP port on which there is no server listening, my gawk script exits with a fatal error. What I want to do is recognize this condition within the gawk script and then try another host or port.

Here's a simple (stripped-down) gawk script that illustrates this:

% cat smtptest.awk
BEGIN {
        if (ARGC < 1 || ARGV[1] == "")
                exit
        RS = ORS = "\r\n"
        connection = "/inet/tcp/0/" ARGV[1] "/25"
        print "opening " connection " ..."
        if ((connection |& getline) > 0) {
                print "ERRNO=" ERRNO
                print
                print "quit" |& connection
                while ((connection |& getline) > 0)
                        print
        }
        else
                print "ERRNO=" ERRNO
}


Here's my gawk version:

% gawk --version
GNU Awk 3.1.3
Copyright (C) 1989, 1991-2003 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

(This problem also occurs with gawk 3.1.2.)


Here's what I get when I run gawk with the above script and specify a host on which an SMTP daemon is listening on port 25:

% gawk -f smtptest.awk www
opening /inet/tcp/0/www/25 ...
ERRNO=0
220 www.draper.com ESMTP Sendmail 8.11.7/8.11.7; Mon, 22 Sep 2003 15:16:04 -0400 (EDT)
221 2.0.0 www.draper.com closing connection


And here's what I get when I run the script and specify a host on which there is no SMTP daemon listening on port 25:

% gawk -f smtptest.awk fs3
opening /inet/tcp/0/fs3/25 ...
gawk: smtptest.awk:16: fatal: can't open two way socket `/inet/tcp/0/fs3/25' for input/output (No such file or directory)

Note that gawk terminated immediately with this fatal error (i.e., neither 'print "ERRNO=" ERRNO' statement ever got executed).


What do I have to do to trap such an error (and not have that error message be written to stderr) so that a (much larger) gawk script can handle this condition appropriately and/or proceed to other tasks?

One reason why I think there may be a bug in the gawk implementation is that a fatal error does NOT occur for "getline <filename" when the file does not exist or is not readable, or for "command | getline" when the command does not exist or is not executable (in the first case a -1 is returned and ERRNO gets set, and in the second case a message gets written to stderr by sh). It seems reasonable that there ought to be a way to similarly handle network connection failures, but I haven't been able to figure out how (and I could find no examples of how to do this in the book by Arnold Robbins).


Peter Vernam                    Email:  address@hidden
Draper Laboratory, MS 33        Voice:  617-258-2735
555 Technology Square           Fax:    617-258-2705
Cambridge, MA 02139





reply via email to

[Prev in Thread] Current Thread [Next in Thread]