bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] possible documentation and code bug


From: Aharon Robbins
Subject: Re: [bug-gawk] possible documentation and code bug
Date: Tue, 23 Sep 2014 19:56:59 +0300
User-agent: Heirloom mailx 12.5 6/20/10

Greetings. Re this:

> From: Aharon Robbins <address@hidden>
> Date: Sun, 21 Sep 2014 22:15:00 +0300
> To: address@hidden
> Subject: [bug-gawk] possible documentation and code bug
>
> Hi Gawk Maintainers!

Hi.

> The node "Special Caveats" in the manual says that gawk does not
> process /dev/{stdin,stdout,stderr} itself when in compatibility mode,
> and the code bears this out.
>
> But given that current BWK awk does process these files, gawk appears
> to be behaving incorrectly.  It should probably treat these 3 files
> as special in all cases except with --posix, but for /dev/fd/N not
> process the files if --traditional.
>
> The doc should then be updated as well.
>
> Talk about a subtlety, sheesh.

You're quite right. Thanks for the clear and concise bug report.

> A happy gawk user.

We're glad you're a happy user!

The code and doc patch is below. It will be pushed out to the repo shortly.

Thanks,

Arnold
-------------------------------------------
diff --git a/doc/gawktexi.in b/doc/gawktexi.in
index ecd6a97..1acd1a9 100644
--- a/doc/gawktexi.in
+++ b/doc/gawktexi.in
@@ -560,8 +560,8 @@ particular records in a file and perform operations upon 
them.
 * Regexp Field Splitting::              Using regexps as the field separator.
 * Single Character Fields::             Making each character a separate
                                         field.
-* Command Line Field Separator::        Setting @code{FS} from the
-                                        command line.
+* Command Line Field Separator::        Setting @code{FS} from the command
+                                        line.
 * Full Line Fields::                    Making the full line be a single
                                         field.
 * Field Splitting Summary::             Some final points and a summary table.
@@ -605,10 +605,12 @@ particular records in a file and perform operations upon 
them.
 * Printf Examples::                     Several examples.
 * Redirection::                         How to redirect output to multiple
                                         files and pipes.
+* Special FD::                          Special files for I/O.
 * Special Files::                       File name interpretation in
                                         @command{gawk}. @command{gawk} allows
                                         access to inherited file descriptors.
-* Special FD::                          Special files for I/O.
+* Other Inherited Files::               Accessing other open files with
+                                        @command{gawk}.
 * Special Network::                     Special files for network
                                         communications.
 * Special Caveats::                     Things to watch out for.
@@ -8405,6 +8407,7 @@ and discusses the @code{close()} built-in function.
 * Printf::                      The @code{printf} statement.
 * Redirection::                 How to redirect output to multiple files and
                                 pipes.
+* Special FD::                  Special files for I/O.
 * Special Files::               File name interpretation in @command{gawk}.
                                 @command{gawk} allows access to inherited file
                                 descriptors.
@@ -9390,23 +9393,8 @@ It then sends the list to the shell for execution.
 @c ENDOFRANGE outre
 @c ENDOFRANGE reout
 
address@hidden Special Files
address@hidden Special @value{FFN}s in @command{gawk}
address@hidden STARTOFRANGE gfn
address@hidden @command{gawk}, file names in
-
address@hidden provides a number of special @value{FN}s that it interprets
-internally.  These @value{FN}s provide access to standard pre-opened files
-and TCP/IP networking.
-
address@hidden
-* Special FD::                  Special files for I/O.
-* Special Network::             Special files for network communications.
-* Special Caveats::             Things to watch out for.
address@hidden menu
-
 @node Special FD
address@hidden Special Files for Standard Pre-Opened Files
address@hidden Special Files for Standard Pre-Opened Data Streams
 @cindex standard input
 @cindex input, standard
 @cindex standard output
@@ -9417,9 +9405,12 @@ and TCP/IP networking.
 @cindex files, descriptors, See file descriptors
 
 Running programs conventionally have three input and output streams
-already available to them for reading and writing.  These are known as
-the @dfn{standard input}, @dfn{standard output}, and @dfn{standard error
-output}.  These streams are, by default, connected to your keyboard and 
screen, but
+already available to them for reading and writing.  These are known
+as the @dfn{standard input}, @dfn{standard output}, and @dfn{standard
+error output}.  These open streams (and any other open file or pipe)
+are often referred to by the technical term @dfn{file descriptors}.
+
+These streams are, by default, connected to your keyboard and screen, but
 they are often redirected with the shell, via the @samp{<}, @samp{<<},
 @samp{>}, @samp{>>}, @samp{>&}, and @samp{|} operators.  Standard error
 is typically used for writing error messages; the reason there are two separate
@@ -9428,7 +9419,7 @@ redirected separately.
 
 @cindex differences in @command{awk} and @command{gawk}, error messages
 @cindex error handling
-In other implementations of @command{awk}, the only way to write an error
+In traditional implementations of @command{awk}, the only way to write an error
 message to standard error in an @command{awk} program is as follows:
 
 @example
@@ -9461,15 +9452,12 @@ that happens, writing to the screen is not correct.  In 
fact, if
 terminal at all.
 Then opening @file{/dev/tty} fails.
 
address@hidden provides special @value{FN}s for accessing the three standard
-streams. @value{COMMONEXT} It also provides syntax for accessing
-any other inherited open files.
-These open files are often referred to by the technical term
address@hidden descriptor}.
-If the @value{FN} matches
-one of these special names when @command{gawk} redirects input or output,
-then it directly uses the descriptor that the @value{FN} stands for.
-These special @value{FN}s work for all operating systems that @command{gawk}
address@hidden, BWK @command{awk} and @command{mawk} provide
+special @value{FN}s for accessing the three standard streams.
+If the @value{FN} matches one of these special names when @command{gawk}
+(or one of the others) redirects input or output, then it directly uses
+the descriptor that the @value{FN} stands for.  These special
address@hidden work for all operating systems that @command{gawk}
 has been ported to, not just those that are POSIX-compliant:
 
 @cindex common extensions, @code{/dev/stdin} special file
@@ -9491,19 +9479,10 @@ The standard output (file descriptor 1).
 
 @item /dev/stderr
 The standard error output (file descriptor 2).
-
address@hidden /dev/fd/@var{N}
-The file associated with file descriptor @var{N}.  Such a file must
-be opened by the program initiating the @command{awk} execution (typically
-the shell).  Unless special pains are taken in the shell from which
address@hidden is invoked, only descriptors 0, 1, and 2 are available.
 @end table
 
-The @value{FN}s @file{/dev/stdin}, @file{/dev/stdout}, and @file{/dev/stderr}
-are aliases for @file{/dev/fd/0}, @file{/dev/fd/1}, and @file{/dev/fd/2},
-respectively. However, they are more self-explanatory.
-The proper way to write an error message in a @command{gawk} program
-is to use @file{/dev/stderr}, like this:
+With these facilities,
+the proper way to write an error message then becomes:
 
 @example
 print "Serious error detected!" > "/dev/stderr"
@@ -9515,14 +9494,51 @@ Like any other redirection, the value must be a string.
 It is a common error to omit the quotes, which leads
 to confusing results.
 
-Finally, using the @code{close()} function on a @value{FN} of the
address@hidden does not treat these @value{FN}s as special when
+in POSIX compatibility mode. However, since BWK @command{awk}
+supports them, @command{gawk} does support them even when
+invoked with the @option{--traditional} option (@pxref{Options}).
+
address@hidden Special Files
address@hidden Special @value{FFN}s in @command{gawk}
address@hidden STARTOFRANGE gfn
address@hidden @command{gawk}, file names in
+
+Besides access to standard input, stanard output, and standard error,
address@hidden provides access to any open file descriptor.
+Additionally, there are special @value{FN}s reserved for
+TCP/IP networking.
+
address@hidden
+* Other Inherited Files::       Accessing other open files with
+                                @command{gawk}.
+* Special Network::             Special files for network communications.
+* Special Caveats::             Things to watch out for.
address@hidden menu
+
address@hidden Other Inherited Files
address@hidden Accessing Other Open Files With @command{gawk}
+
+Besides the @code{/dev/stdin}, @code{/dev/stdout}, and @code{/dev/stderr}
+special @value{FN}s mentioned earlier, @command{gawk} provides syntax
+for accessing any other inherited open file:
+
address@hidden @file
address@hidden /dev/fd/@var{N}
+The file associated with file descriptor @var{N}.  Such a file must
+be opened by the program initiating the @command{awk} execution (typically
+the shell).  Unless special pains are taken in the shell from which
address@hidden is invoked, only descriptors 0, 1, and 2 are available.
address@hidden table
+
+The @value{FN}s @file{/dev/stdin}, @file{/dev/stdout}, and @file{/dev/stderr}
+are essentially aliases for @file{/dev/fd/0}, @file{/dev/fd/1}, and
address@hidden/dev/fd/2}, respectively. However, those names are more 
self-explanatory.
+
+Note that using @code{close()} on a @value{FN} of the
 form @code{"/dev/fd/@var{N}"}, for file descriptor numbers
 above two, does actually close the given file descriptor.
 
-The @file{/dev/stdin}, @file{/dev/stdout}, and @file{/dev/stderr}
-special files are also recognized internally by several other
-versions of @command{awk}.
-
 @node Special Network
 @subsection Special Files for Network Communications
 @cindex networks, support for
@@ -9558,8 +9574,13 @@ special @value{FN}s that @command{gawk} provides:
 @cindex compatibility mode (@command{gawk}), file names
 @cindex file names, in compatibility mode
 @item
-Recognition of these special @value{FN}s is disabled if @command{gawk} is in
-compatibility mode (@pxref{Options}).
+Recognition of the @value{FN}s for the three standard pre-opened
+files is disabled only in POSIX mode.
+
address@hidden
+Recognition of the other special @value{FN}s is disabled if @command{gawk} is 
in
+compatibility mode (either @option{--traditional} or @option{--posix};
address@hidden).
 
 @item
 @command{gawk} @emph{always}
diff --git a/io.c b/io.c
index 7930904..7154a71 100644
--- a/io.c
+++ b/io.c
@@ -1550,6 +1550,17 @@ nextrres:
  * change the string.
  */
 
+/*
+ * 9/2014: Flow here is a little messy.
+ *
+ * For do_posix, we don't allow any of the special filenames.
+ *
+ * For do_traditional, we allow /dev/{stdin,stdout,stderr} since BWK awk
+ * (and mawk) support them.  But we don't allow /dev/fd/N or /inet.
+ *
+ * Note that for POSIX systems os_devopen() is a no-op.
+ */
+
 int
 devopen(const char *name, const char *mode)
 {
@@ -1565,7 +1576,7 @@ devopen(const char *name, const char *mode)
        flag = str2mode(mode);
        openfd = INVALID_HANDLE;
 
-       if (do_traditional)
+       if (do_posix)
                goto strictopen;
 
        if ((openfd = os_devopen(name, flag)) != INVALID_HANDLE) {
@@ -1582,6 +1593,8 @@ devopen(const char *name, const char *mode)
                        openfd = fileno(stdout);
                else if (strcmp(cp, "stderr") == 0 && (flag & O_ACCMODE) == 
O_WRONLY)
                        openfd = fileno(stderr);
+               else if (do_traditional)
+                       goto strictopen;
                else if (strncmp(cp, "fd/", 3) == 0) {
                        struct stat sbuf;
 
@@ -1594,6 +1607,8 @@ devopen(const char *name, const char *mode)
                /* do not set close-on-exec for inherited fd's */
                if (openfd != INVALID_HANDLE)
                        return openfd;
+       } else if (do_traditional) {
+               goto strictopen;
        } else if (inetfile(name, & isi)) {
 #ifdef HAVE_SOCKETS
                cp = (char *) name;



reply via email to

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