Repository: benhoyt/goawk (go)

Issue:
Add support for `getline < "-"` to read from stdin
gawk, mawk and the "one true awk" have support for this, and it seems reasonable considering that POSIX requires interpretation of `-` as stdin in basically all other areas.

Only issue is that read prompts in general are a bit awkward with #47. For example:
```
$ goawk 'BEGIN { ARGV[ARGC++] = "-"; print "Enter input:"; getline; print "You entered: " $0 }'
this is my input
Enter input:
You entered: this is my input
```

Pull request description:
Add support for getline < "-"
Resolves #62
Disclaimer: I don't have much experience in Go

Patch:
diff --git a/interp/io.go b/interp/io.go
index a218f62..31624c4 100644
--- a/interp/io.go
+++ b/interp/io.go
@@ -69,6 +69,10 @@ func (p *interp) getOutputStream(redirect Token, dest Expr) (io.Writer, error) {
 
 	switch redirect {
 	case GREATER, APPEND:
+		if name == "-" {
+			// filename of "-" means write to stdout, eg: print "x" >"-"
+			return p.output, nil
+		}
 		// Write or append to file
 		if p.noFileWrites {
 			return nil, newError("can't write to file due to NoFileWrites")
@@ -123,6 +127,15 @@ func (p *interp) getInputScannerFile(name string) (*bufio.Scanner, error) {
 	if _, ok := p.inputStreams[name]; ok {
 		return p.scanners[name], nil
 	}
+	if name == "-" {
+		// filename of "-" means read from stdin, eg: getline <"-"
+		if scanner, ok := p.scanners["-"]; ok {
+			return scanner, nil
+		}
+		scanner := p.newScanner(p.stdin)
+		p.scanners[name] = scanner
+		return scanner, nil
+	}
 	if p.noFileReads {
 		return nil, newError("can't read from file due to NoFileReads")
 	}


Test patch:
diff --git a/interp/interp_test.go b/interp/interp_test.go
index 53ce1f6..fff6447 100644
--- a/interp/interp_test.go
+++ b/interp/interp_test.go
@@ -591,6 +591,11 @@ BEGIN { foo(5); bar(10) }
 	// {`BEGIN { ">&2 echo error" | getline; print }`, "", "error\n\n", "", ""},
 	{`BEGIN { print getline x < "/no/such/file" }  # !fuzz`, "", "-1\n", "", ""},
 
+	// Redirecting to or from a filename of "-" means write to stdout or read from stdin
+	{`BEGIN { print getline x < "-"; print x }`, "a\nb\n", "1\na\n", "", ""},
+	{`{ print $0; print getline x <"-"; print x }`, "one\ntwo\n", "one\n0\n\ntwo\n0\n\n", "", ""},
+	{`BEGIN { print "x" >"-"; print "y" >"-" }`, "", "x\ny\n", "", ""},
+
 	// fflush() function - tests parsing and some edge cases, but not
 	// actual flushing behavior (that's partially tested in TestFlushes).
 	{`BEGIN { print fflush(); print fflush("") }`, "", "0\n0\n", "", ""},