caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Chet Murthy <murthy.chet@gmail.com>
To: Matthew Ryan <matthew@o1labs.org>
Cc: Helmut Brandl <helmut.brandl@gmx.net>, caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Turn echoing off on standard input to read e.g. passwords
Date: Thu, 28 Mar 2019 20:05:41 -0700	[thread overview]
Message-ID: <CA++P_gcEW+_69-17sajueteW3eMwOxLmhY-+ztH-uUZ+E07XhA@mail.gmail.com> (raw)
In-Reply-To: <CAHsMTAENQt=0aQS5hUnm=sgbK0NZgUUOCpfQihWTCbw0-jC4xQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2661 bytes --]

[Argh, resending, b/c got the recipient-list wrong]

Perhaps this has changed in the N years since last I worked with
sgttyb/termios.  But back in the day, the answer was "there is no portable
way; the usual way is to manipulate terminal (actually, serial-port)
attributes".

This is less difficult than it seems: there should be support in ncurses
for it, and there should be a portable API between glibc (on Unixes) and
cygwin (on Winders).

I *do* think it would be worth looking at (for instance) the code of sudo,
to see what libraries it calls.

OK, I'm done, lemme adjust this onion on my belt,
--chet--

P.S. i re-reading the above (for resend) I'm being unclear.  So I'll
restate: the standard way that I've seen this done (many times over the
years) is to manipulate the state of the serial-port and the
"line-discipline" (that is, the equivalent of using the "stty" command).
This is not connected with issuing terminal escape-sequences, because the
former changes what is actually sent on the wire, whereas the latter
changes only what is displayed in the virtual or real terminal.  E.g. "stty
-echo" disables the echoing of characters in the line-discipline --
whatever you type into a TTY, the kernel-level terminal-driver will not
echo those chars back.  Whereas telling the terminal emulator (or real
terminal) to not display chars, doesn't change that the chars are actually
sent.

It's possible that this is a difference without relevance at this point in
the evolution of UNIX software.  But for sure, it was manipulation of
line-discipline characteristics, that was the means of disabling echo,
"back in the day".

On Thu, Mar 28, 2019 at 1:33 PM Matthew Ryan <matthew@o1labs.org> wrote:

> Hi Helmut,
>
> The usual way to do this (for any language) is using ANSI escape
> sequences. Code 8 sets the terminal to conceal characters and code 0 resets
> the attributes, making them visible again.
>
> For example, in a unix shell you can test this with echo and read:
>
> echo -e '\x1b[8m'; read varname; echo -e '\x1b[0m'
>
> To do the same from OCaml, you can output "\x1b[8m", read the password,
> and then output "\x1b[0m" afterwards to switch printing back on.
>
> I believe that this will work on Windows 10, but earlier versions may not
> have the necessary ANSI support.
>
> Hope this helps,
> Matthew
>
> On Thu, 28 Mar 2019, 20:04 Helmut Brandl, <helmut.brandl@gmx.net> wrote:
>
>> Hello list,
>>
>> Is there a portable way in ocaml to turn echoing off on standard input
>> from the terminal to read e.g. passwords? By portable I mean that it works
>> for Windows, Unix and Mac.
>>
>> Thanks for any hint.
>>
>> Regards
>> Helmut
>
>

[-- Attachment #2: Type: text/html, Size: 3832 bytes --]

  parent reply	other threads:[~2019-03-29  3:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-28 20:03 Helmut Brandl
2019-03-28 20:33 ` Matthew Ryan
2019-03-28 20:40   ` Helmut Brandl
2019-03-28 21:12   ` Daniel Bünzli
2019-03-29  3:05   ` Chet Murthy [this message]
2019-03-29 23:13     ` Chet Murthy
2019-04-01  9:05 ` Jeremie Dimino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA++P_gcEW+_69-17sajueteW3eMwOxLmhY-+ztH-uUZ+E07XhA@mail.gmail.com \
    --to=murthy.chet@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=helmut.brandl@gmx.net \
    --cc=matthew@o1labs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).