Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close() on pipe command does not return exit status #203

Closed
juster opened this issue Aug 25, 2023 · 2 comments
Closed

close() on pipe command does not return exit status #203

juster opened this issue Aug 25, 2023 · 2 comments

Comments

@juster
Copy link
Contributor

juster commented Aug 25, 2023

An undocumented feature of other awks is to have the close() builtin function return the exit status of the process which was opened by the pipe. The close() builtin for goawk always returns -1 for output pipes and always returns 0 for input pipes.

Example

$ goawk 'BEGIN { print|"exit 1"; print close("exit 1") }'
-1
$ goawk 'BEGIN { "exit 1"|getline; print close("exit 1") }'
0

Portability?

Different awks also have various implementations. The same example above was run for ...

  1. the "one-true" Lucent copyright awk
  2. GNU gawk
  3. plan9 from userspace awk
  4. Mac OS awk
Variant Input/Output Pipe Behavior
one-true / mac os both Value is 256 (1 << 8)
gawk both Value is 1
plan9 both Use of the value of close() is illegal

edit: Removed duplicate rows. I also remembered how the wait() result shifts the exit code.

juster added a commit to juster/goawk that referenced this issue Aug 25, 2023
Creates the iostream.go file. Use I/O stream interfaces for all file redirects
and command pipes.

Fixes benhoyt#203
Fixes benhoyt#204
@benhoyt
Copy link
Owner

benhoyt commented Aug 25, 2023

Interesting! Linking the GAWK docs on this for reference. For comparison, POSIX just says "If the close was successful, the function shall return zero; otherwise, it shall return non-zero."

@juster
Copy link
Contributor Author

juster commented Aug 25, 2023

Ah nice! Those linked docs explain plan9 awk's behavior:

In many older versions of Unix awk, the close() function is actually a statement. (d.c.) It is a syntax error to try and use the return value from close():

It complains about an illegal statement.

But yeah this is definitely an undocumented easter egg!

juster added a commit to juster/goawk that referenced this issue Aug 26, 2023
Creates the iostream.go file. Use I/O stream interfaces for all file redirects
and command pipes.

Fixes benhoyt#203
Fixes benhoyt#204
juster added a commit to juster/goawk that referenced this issue Aug 26, 2023
Creates the iostream.go file. Use I/O stream interfaces for all file redirects
and command pipes.

Fixes benhoyt#203
Fixes benhoyt#204
juster added a commit to juster/goawk that referenced this issue Aug 27, 2023
Creates the iostream.go file. Use I/O stream interfaces for all file redirects
and command pipes.

Mimicks gawk's behavior for the result of the `close' builtin function when the
argument to close() is a pipe:

1. the value passed to exit() when process exits normally
2. 256 + signal code when process exits due to signal
3. 512 + signal code when process exits due to signal with a core dump

Fixes benhoyt#203
Fixes benhoyt#204
juster added a commit to juster/goawk that referenced this issue Aug 27, 2023
Creates the iostream.go file. Use I/O stream interfaces for all file redirects
and command pipes.

Mimicks gawk's behavior for the result of the `close' builtin function when the
argument to close() is a pipe:

1. the value passed to exit() when process exits normally
2. 256 + signal code when process exits due to signal
3. 512 + signal code when process exits due to signal with a core dump

Fixes benhoyt#203
Fixes benhoyt#204
juster added a commit to juster/goawk that referenced this issue Sep 1, 2023
Creates the iostream.go file and uses I/O stream interfaces for all
file redirects and command pipes. Defines inputStream and outputStream
interfaces which satisfy the io.ReadCloser and io.WriteCloser
interfaces, respectively. Adds an additional .ExitCode() accessor
to return the exit code for goawk's close() builtin function.

When the argument to close() is a pipe, this mimicks gawk's behavior:

1. the value passed to exit() when process exits normally
2. 256 + signal code when process exits due to signal
3. 512 + signal code when process exits due to signal with a core dump

Fixes benhoyt#203
Fixes benhoyt#204
juster added a commit to juster/goawk that referenced this issue Sep 1, 2023
Creates the iostream.go file and uses I/O stream interfaces for all
file redirects and command pipes. Defines inputStream and outputStream
interfaces which satisfy the io.ReadCloser and io.WriteCloser
interfaces, respectively. Adds an additional .ExitCode() accessor
to return the exit code for goawk's close() builtin function.

When the argument to close() is a pipe, this mimicks gawk's behavior:

1. the value passed to exit() when process exits normally
2. 256 + signal code when process exits due to signal
3. 512 + signal code when process exits due to signal with a core dump

Fixes benhoyt#203
Fixes benhoyt#204
juster added a commit to juster/goawk that referenced this issue Sep 10, 2023
Creates the iostream.go file and uses I/O stream interfaces for all
file redirects and command pipes. Defines inputStream and outputStream
interfaces which satisfy the io.ReadCloser and io.WriteCloser
interfaces, respectively. Adds an additional .ExitCode() accessor
to return the exit code for goawk's close() builtin function.

When the argument to close() is a pipe, this mimicks gawk's behavior:

1. The value passed to exit() when process exits normally
2. 256 + signal code when process exits due to signal
3. 512 + signal code when process exits due to signal with a core dump

Fixes benhoyt#203
Fixes benhoyt#204
juster added a commit to juster/goawk that referenced this issue Sep 17, 2023
Creates the iostream.go file and uses I/O stream interfaces for all
file redirects and command pipes. Defines inputStream and outputStream
interfaces which satisfy the io.ReadCloser and io.WriteCloser
interfaces, respectively. Adds an additional .ExitCode() accessor
to return the exit code for goawk's close() builtin function.

When the argument to close() is a pipe, this mimicks gawk's behavior:

1. The value passed to exit() when process exits normally
2. 256 + signal code when process exits due to signal
3. 512 + signal code when process exits due to signal with a core dump

Fixes benhoyt#203
Fixes benhoyt#204
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants