From 28adbb58148e9d9b9608f64f44e364da712b3e1a Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Tue, 6 Feb 2018 19:16:01 +0100 Subject: [PATCH] Utils: add `getpass` --- src/lib_stdlib_unix/lwt_utils_unix.ml | 16 ++++++++++++++++ src/lib_stdlib_unix/lwt_utils_unix.mli | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/src/lib_stdlib_unix/lwt_utils_unix.ml b/src/lib_stdlib_unix/lwt_utils_unix.ml index 7aa184272..4d4112699 100644 --- a/src/lib_stdlib_unix/lwt_utils_unix.ml +++ b/src/lib_stdlib_unix/lwt_utils_unix.ml @@ -136,6 +136,22 @@ let getaddrinfo ~passive ~node ~service = addr in Lwt.return points +let getpass () = + let open Unix in + (* Turn echoing off and fail if we can't. *) + let tio = tcgetattr stdin in + let old_echo = tio.c_echo in + let old_echonl = tio.c_echonl in + tio.c_echo <- false ; + tio.c_echonl <- true ; + tcsetattr stdin TCSAFLUSH tio ; + (* Read the passwd. *) + let passwd = read_line () in + (* Restore terminal. *) + tio.c_echo <- old_echo ; + tio.c_echonl <- old_echonl ; + tcsetattr stdin TCSAFLUSH tio ; + passwd module Json = struct diff --git a/src/lib_stdlib_unix/lwt_utils_unix.mli b/src/lib_stdlib_unix/lwt_utils_unix.mli index 9d43446c2..146a601d7 100644 --- a/src/lib_stdlib_unix/lwt_utils_unix.mli +++ b/src/lib_stdlib_unix/lwt_utils_unix.mli @@ -34,6 +34,10 @@ val getaddrinfo: node:string -> service:string -> (Ipaddr.V6.t * int) list Lwt.t +(** [getpass ()] reads a password from stdio while setting-up the + terminal to not display the password being typed. *) +val getpass : unit -> string + module Json : sig (** Loads a JSON file in memory *)