Command line client for Write.as https://write.as/apps/cli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License, Version 1.0 only
  6. * (the "License"). You may not use this file except in compliance
  7. * with the License.
  8. *
  9. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10. * or http://www.opensolaris.org/os/licensing.
  11. * See the License for the specific language governing permissions
  12. * and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL HEADER in each
  15. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16. * If applicable, add the following below this CDDL HEADER, with the
  17. * fields enclosed by brackets "[]" replaced with your own identifying
  18. * information: Portions Copyright [yyyy] [name of copyright owner]
  19. *
  20. * CDDL HEADER END
  21. */
  22. // Below is derived from Solaris source, so CDDL license is included.
  23. package gopass
  24. import (
  25. "syscall"
  26. "golang.org/x/sys/unix"
  27. )
  28. type terminalState struct {
  29. state *unix.Termios
  30. }
  31. // isTerminal returns true if there is a terminal attached to the given
  32. // file descriptor.
  33. // Source: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libbc/libc/gen/common/isatty.c
  34. func isTerminal(fd uintptr) bool {
  35. var termio unix.Termio
  36. err := unix.IoctlSetTermio(int(fd), unix.TCGETA, &termio)
  37. return err == nil
  38. }
  39. // makeRaw puts the terminal connected to the given file descriptor into raw
  40. // mode and returns the previous state of the terminal so that it can be
  41. // restored.
  42. // Source: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libast/common/uwin/getpass.c
  43. func makeRaw(fd uintptr) (*terminalState, error) {
  44. oldTermiosPtr, err := unix.IoctlGetTermios(int(fd), unix.TCGETS)
  45. if err != nil {
  46. return nil, err
  47. }
  48. oldTermios := *oldTermiosPtr
  49. newTermios := oldTermios
  50. newTermios.Lflag &^= syscall.ECHO | syscall.ECHOE | syscall.ECHOK | syscall.ECHONL
  51. if err := unix.IoctlSetTermios(int(fd), unix.TCSETS, &newTermios); err != nil {
  52. return nil, err
  53. }
  54. return &terminalState{
  55. state: oldTermiosPtr,
  56. }, nil
  57. }
  58. func restore(fd uintptr, oldState *terminalState) error {
  59. return unix.IoctlSetTermios(int(fd), unix.TCSETS, oldState.state)
  60. }