all: import golang.org/x/crypto/ssh/terminal

This merges all code from x/crypto/ssh/terminal at commit
0c6587e931a935f573efb6988c8470cd8c0a58de with no refactoring,
simply mapping util_* files to term_*.

The x/crypto history for the merge was rewritten with

    git filter-repo --path ssh/terminal/ --path-rename ssh/terminal/:

and then stitched together with "git replace --graft".

The merge preserves git blame visibility.

Updates #31044

Change-Id: Ic51fc8ec20b3a384cdd8868fde06fe69b6317695
diff --git a/AUTHORS b/AUTHORS
new file mode 100644
index 0000000..15167cd
--- /dev/null
+++ b/AUTHORS
@@ -0,0 +1,3 @@
+# This source code refers to The Go Authors for copyright purposes.
+# The master list of authors is in the main Go distribution,
+# visible at http://tip.golang.org/AUTHORS.
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..d0485e8
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,26 @@
+# Contributing to Go
+
+Go is an open source project.
+
+It is the work of hundreds of contributors. We appreciate your help!
+
+## Filing issues
+
+When [filing an issue](https://golang.org/issue/new), make sure to answer these five questions:
+
+1.  What version of Go are you using (`go version`)?
+2.  What operating system and processor architecture are you using?
+3.  What did you do?
+4.  What did you expect to see?
+5.  What did you see instead?
+
+General questions should go to the [golang-nuts mailing list](https://groups.google.com/group/golang-nuts) instead of the issue tracker.
+The gophers there will answer or ask you to file an issue if you've tripped over a bug.
+
+## Contributing code
+
+Please read the [Contribution Guidelines](https://golang.org/doc/contribute.html)
+before sending patches.
+
+Unless otherwise noted, the Go source files are distributed under
+the BSD-style license found in the LICENSE file.
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
new file mode 100644
index 0000000..1c4577e
--- /dev/null
+++ b/CONTRIBUTORS
@@ -0,0 +1,3 @@
+# This source code was written by the Go contributors.
+# The master list of contributors is in the main Go distribution,
+# visible at http://tip.golang.org/CONTRIBUTORS.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..6a66aea
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) 2009 The Go Authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+   * Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+   * Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+   * Neither the name of Google Inc. nor the names of its
+contributors may be used to endorse or promote products derived from
+this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/PATENTS b/PATENTS
new file mode 100644
index 0000000..7330990
--- /dev/null
+++ b/PATENTS
@@ -0,0 +1,22 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Google as part of the Go project.
+
+Google hereby grants to You a perpetual, worldwide, non-exclusive,
+no-charge, royalty-free, irrevocable (except as stated in this section)
+patent license to make, have made, use, offer to sell, sell, import,
+transfer and otherwise run, modify and propagate the contents of this
+implementation of Go, where such license applies only to those patent
+claims, both currently owned or controlled by Google and acquired in
+the future, licensable by Google that are necessarily infringed by this
+implementation of Go.  This grant does not include claims that would be
+infringed only as a consequence of further modification of this
+implementation.  If you or your agent or exclusive licensee institute or
+order or agree to the institution of patent litigation against any
+entity (including a cross-claim or counterclaim in a lawsuit) alleging
+that this implementation of Go or any code incorporated within this
+implementation of Go constitutes direct or contributory patent
+infringement, or inducement of patent infringement, then any patent
+rights granted to you under this License for this implementation of Go
+shall terminate as of the date such litigation is filed.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e0f390c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+# Go terminal/console support
+
+This repository provides Go terminal and console support packages.
+
+## Download/Install
+
+The easiest way to install is to run `go get -u golang.org/x/term`. You can
+also manually git clone the repository to `$GOPATH/src/golang.org/x/term`.
+
+## Report Issues / Send Patches
+
+This repository uses Gerrit for code changes. To learn how to submit changes to
+this repository, see https://golang.org/doc/contribute.html.
+
+The main issue tracker for the term repository is located at
+https://github.com/golang/go/issues. Prefix your issue with "x/term:" in the
+subject line, so it is easy to find.
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..e522191
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module golang.org/x/term
+
+go 1.11
+
+require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..5e0752b
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
+golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
diff --git a/term.go b/term.go
new file mode 100644
index 0000000..3e8e8dc
--- /dev/null
+++ b/term.go
@@ -0,0 +1,20 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package term provides support functions for dealing with terminals, as
+// commonly found on UNIX systems.
+//
+// Putting a terminal into raw mode is the most common requirement:
+//
+// 	oldState, err := terminal.MakeRaw(0)
+// 	if err != nil {
+// 	        panic(err)
+// 	}
+// 	defer terminal.Restore(0, oldState)
+package term
+
+// IsTerminal returns whether the given file descriptor is a terminal.
+func IsTerminal(fd int) bool {
+	return isTerminal(fd)
+}
diff --git a/util_zos.go b/term_aix.go
similarity index 74%
copy from util_zos.go
copy to term_aix.go
index 8314a2d..2d5efd2 100644
--- a/util_zos.go
+++ b/term_aix.go
@@ -1,8 +1,8 @@
-// Copyright 2020 The Go Authors. All rights reserved.
+// Copyright 2019 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package terminal
+package term
 
 import "golang.org/x/sys/unix"
 
diff --git a/util_bsd.go b/term_bsd.go
similarity index 94%
rename from util_bsd.go
rename to term_bsd.go
index cb23a59..3342be0 100644
--- a/util_bsd.go
+++ b/term_bsd.go
@@ -4,7 +4,7 @@
 
 // +build darwin dragonfly freebsd netbsd openbsd
 
-package terminal
+package term
 
 import "golang.org/x/sys/unix"
 
diff --git a/util_zos.go b/term_linux.go
similarity index 74%
copy from util_zos.go
copy to term_linux.go
index 8314a2d..2d5efd2 100644
--- a/util_zos.go
+++ b/term_linux.go
@@ -1,8 +1,8 @@
-// Copyright 2020 The Go Authors. All rights reserved.
+// Copyright 2019 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package terminal
+package term
 
 import "golang.org/x/sys/unix"
 
diff --git a/term_linux_test.go b/term_linux_test.go
new file mode 100644
index 0000000..3931b8d
--- /dev/null
+++ b/term_linux_test.go
@@ -0,0 +1,24 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package term_test
+
+import (
+	"os"
+	"testing"
+
+	"golang.org/x/term"
+)
+
+func TestIsTerminalTerm(t *testing.T) {
+	file, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer file.Close()
+
+	if !term.IsTerminal(int(file.Fd())) {
+		t.Fatalf("IsTerminal unexpectedly returned false for terminal file %s", file.Name())
+	}
+}
diff --git a/util_plan9.go b/term_plan9.go
similarity index 75%
rename from util_plan9.go
rename to term_plan9.go
index 9317ac7..7582be5 100644
--- a/util_plan9.go
+++ b/term_plan9.go
@@ -1,29 +1,24 @@
-// Copyright 2016 The Go Authors. All rights reserved.
+// Copyright 2019 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Package terminal provides support functions for dealing with terminals, as
-// commonly found on UNIX systems.
-//
-// Putting a terminal into raw mode is the most common requirement:
-//
-// 	oldState, err := terminal.MakeRaw(0)
-// 	if err != nil {
-// 	        panic(err)
-// 	}
-// 	defer terminal.Restore(0, oldState)
-package terminal
+package term
 
 import (
 	"fmt"
 	"runtime"
+
+	"golang.org/x/sys/plan9"
 )
 
 type State struct{}
 
-// IsTerminal returns whether the given file descriptor is a terminal.
-func IsTerminal(fd int) bool {
-	return false
+func isTerminal(fd int) bool {
+	path, err := plan9.Fd2path(fd)
+	if err != nil {
+		return false
+	}
+	return path == "/dev/cons" || path == "/mnt/term/dev/cons"
 }
 
 // MakeRaw put the terminal connected to the given file descriptor into raw
diff --git a/util_solaris.go b/term_solaris.go
similarity index 92%
rename from util_solaris.go
rename to term_solaris.go
index 3d5f06a..052e722 100644
--- a/util_solaris.go
+++ b/term_solaris.go
@@ -1,15 +1,14 @@
-// Copyright 2015 The Go Authors. All rights reserved.
+// Copyright 2019 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build solaris
-
-package terminal // import "golang.org/x/crypto/ssh/terminal"
+package term
 
 import (
-	"golang.org/x/sys/unix"
 	"io"
 	"syscall"
+
+	"golang.org/x/sys/unix"
 )
 
 // State contains the state of a terminal.
@@ -17,8 +16,7 @@
 	termios unix.Termios
 }
 
-// IsTerminal returns whether the given file descriptor is a terminal.
-func IsTerminal(fd int) bool {
+func isTerminal(fd int) bool {
 	_, err := unix.IoctlGetTermio(fd, unix.TCGETA)
 	return err == nil
 }
diff --git a/term_test.go b/term_test.go
new file mode 100644
index 0000000..aef6b51
--- /dev/null
+++ b/term_test.go
@@ -0,0 +1,26 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package term_test
+
+import (
+	"io/ioutil"
+	"os"
+	"testing"
+
+	"golang.org/x/term"
+)
+
+func TestIsTerminalTempFile(t *testing.T) {
+	file, err := ioutil.TempFile("", "TestIsTerminalTempFile")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer os.Remove(file.Name())
+	defer file.Close()
+
+	if term.IsTerminal(int(file.Fd())) {
+		t.Fatalf("IsTerminal unexpectedly returned true for temporary file %s", file.Name())
+	}
+}
diff --git a/util.go b/term_unix.go
similarity index 84%
rename from util.go
rename to term_unix.go
index 73b370a..e0c2edb 100644
--- a/util.go
+++ b/term_unix.go
@@ -1,20 +1,10 @@
-// Copyright 2011 The Go Authors. All rights reserved.
+// Copyright 2019 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
 // +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd zos
 
-// Package terminal provides support functions for dealing with terminals, as
-// commonly found on UNIX systems.
-//
-// Putting a terminal into raw mode is the most common requirement:
-//
-// 	oldState, err := terminal.MakeRaw(0)
-// 	if err != nil {
-// 	        panic(err)
-// 	}
-// 	defer terminal.Restore(0, oldState)
-package terminal // import "golang.org/x/crypto/ssh/terminal"
+package term
 
 import (
 	"golang.org/x/sys/unix"
@@ -25,8 +15,7 @@
 	termios unix.Termios
 }
 
-// IsTerminal returns whether the given file descriptor is a terminal.
-func IsTerminal(fd int) bool {
+func isTerminal(fd int) bool {
 	_, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
 	return err == nil
 }
diff --git a/term_unsupported.go b/term_unsupported.go
new file mode 100644
index 0000000..76ca931
--- /dev/null
+++ b/term_unsupported.go
@@ -0,0 +1,11 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build js,wasm nacl
+
+package term
+
+func isTerminal(fd int) bool {
+	return false
+}
diff --git a/util_windows.go b/term_windows.go
similarity index 84%
rename from util_windows.go
rename to term_windows.go
index f614e9c..17f25ad 100644
--- a/util_windows.go
+++ b/term_windows.go
@@ -1,20 +1,8 @@
-// Copyright 2011 The Go Authors. All rights reserved.
+// Copyright 2019 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build windows
-
-// Package terminal provides support functions for dealing with terminals, as
-// commonly found on UNIX systems.
-//
-// Putting a terminal into raw mode is the most common requirement:
-//
-// 	oldState, err := terminal.MakeRaw(0)
-// 	if err != nil {
-// 	        panic(err)
-// 	}
-// 	defer terminal.Restore(0, oldState)
-package terminal
+package term
 
 import (
 	"os"
@@ -26,8 +14,7 @@
 	mode uint32
 }
 
-// IsTerminal returns whether the given file descriptor is a terminal.
-func IsTerminal(fd int) bool {
+func isTerminal(fd int) bool {
 	var st uint32
 	err := windows.GetConsoleMode(windows.Handle(fd), &st)
 	return err == nil
diff --git a/util_zos.go b/term_zos.go
similarity index 93%
rename from util_zos.go
rename to term_zos.go
index 8314a2d..b85ab89 100644
--- a/util_zos.go
+++ b/term_zos.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package terminal
+package term
 
 import "golang.org/x/sys/unix"
 
diff --git a/terminal.go b/terminal.go
index 2ffb97b..535ab82 100644
--- a/terminal.go
+++ b/terminal.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package terminal
+package term
 
 import (
 	"bytes"
diff --git a/terminal_test.go b/terminal_test.go
index c99638d..a04ed52 100644
--- a/terminal_test.go
+++ b/terminal_test.go
@@ -4,7 +4,7 @@
 
 // +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd windows plan9 solaris
 
-package terminal
+package term
 
 import (
 	"bytes"
diff --git a/util_aix.go b/util_aix.go
deleted file mode 100644
index dfcd627..0000000
--- a/util_aix.go
+++ /dev/null
@@ -1,12 +0,0 @@
-// Copyright 2018 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// +build aix
-
-package terminal
-
-import "golang.org/x/sys/unix"
-
-const ioctlReadTermios = unix.TCGETS
-const ioctlWriteTermios = unix.TCSETS
diff --git a/util_linux.go b/util_linux.go
deleted file mode 100644
index 5fadfe8..0000000
--- a/util_linux.go
+++ /dev/null
@@ -1,10 +0,0 @@
-// Copyright 2013 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package terminal
-
-import "golang.org/x/sys/unix"
-
-const ioctlReadTermios = unix.TCGETS
-const ioctlWriteTermios = unix.TCSETS