diff options
Diffstat (limited to 'docs/examples/password_input.lua.html')
-rw-r--r-- | docs/examples/password_input.lua.html | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/docs/examples/password_input.lua.html b/docs/examples/password_input.lua.html new file mode 100644 index 0000000..4234fbb --- /dev/null +++ b/docs/examples/password_input.lua.html @@ -0,0 +1,139 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +<head> + <title>Lua-System docs</title> + <link rel="stylesheet" href="../ldoc.css" type="text/css" /> +</head> +<body> + +<div id="container"> + +<div id="product"> + <div id="product_logo"></div> + <div id="product_name"><big><b></b></big></div> + <div id="product_description"></div> +</div> <!-- id="product" --> + + +<div id="main"> + + +<!-- Menu --> + +<div id="navigation"> +<br/> +<h1>Lua-System</h1> + + +<ul> + <li><a href="../index.html">Index</a></li> +</ul> + + + +<h2>Examples</h2> +<ul class="nowrap"> + <li><a href="../examples/compat.lua.html">compat.lua</a></li> + <li><a href="../examples/flag_debugging.lua.html">flag_debugging.lua</a></li> + <li><strong>password_input.lua</strong></li> + <li><a href="../examples/read.lua.html">read.lua</a></li> + <li><a href="../examples/readline.lua.html">readline.lua</a></li> + <li><a href="../examples/spinner.lua.html">spinner.lua</a></li> + <li><a href="../examples/spiral_snake.lua.html">spiral_snake.lua</a></li> + <li><a href="../examples/terminalsize.lua.html">terminalsize.lua</a></li> +</ul> +<h2>Modules</h2> +<ul class="nowrap"> + <li><a href="../modules/system.html">system</a></li> +</ul> +<h2>Classes</h2> +<ul class="nowrap"> + <li><a href="../classes/bitflags.html">bitflags</a></li> +</ul> +<h2>Topics</h2> +<ul class=""> + <li><a href="../topics/01-introduction.md.html">1. Introduction</a></li> + <li><a href="../topics/02-development.md.html">2. Development</a></li> + <li><a href="../topics/03-terminal.md.html">3. Terminal functionality</a></li> + <li><a href="../topics/CHANGELOG.md.html">CHANGELOG</a></li> + <li><a href="../topics/LICENSE.md.html">MIT License</a></li> +</ul> + +</div> + +<div id="content"> + + <h2>password_input.lua</h2> +<pre> +<span class="keyword">local</span> sys = <span class="global">require</span> <span class="string">"system"</span> + +<span class="global">print</span> <span class="string">[[ + +This example shows how to disable the "echo" of characters read to the console, +useful for reading secrets from the user. + +]]</span> + +<span class="comment">--- Function to read from stdin without echoing the input (for secrets etc). +</span><span class="comment">-- It will (in a platform agnostic way) disable echo on the terminal, read the +</span><span class="comment">-- input, and then re-enable echo. +</span><span class="comment">-- @param ... Arguments to pass to <code>io.stdin:read()</code> +</span><span class="comment">-- @return the results of <code>io.stdin:read(...)</code> +</span><span class="keyword">local</span> <span class="keyword">function</span> <span class="function-name">read_secret</span>(...) + <span class="keyword">local</span> w_oldflags, p_oldflags + + <span class="keyword">if</span> sys.<span class="function-name">isatty</span>(<span class="global">io</span>.stdin) <span class="keyword">then</span> + <span class="comment">-- backup settings, configure echo flags +</span> w_oldflags = sys.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdin) + p_oldflags = sys.<span class="function-name">tcgetattr</span>(<span class="global">io</span>.stdin) + <span class="comment">-- set echo off to not show password on screen +</span> <span class="global">assert</span>(sys.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdin, w_oldflags - sys.CIF_ECHO_INPUT)) + <span class="global">assert</span>(sys.<span class="function-name">tcsetattr</span>(<span class="global">io</span>.stdin, sys.TCSANOW, { lflag = p_oldflags.lflag - sys.L_ECHO })) + <span class="keyword">end</span> + + <span class="keyword">local</span> secret, err = <span class="global">io</span>.stdin:<span class="function-name">read</span>(...) + + <span class="comment">-- restore settings +</span> <span class="keyword">if</span> sys.<span class="function-name">isatty</span>(<span class="global">io</span>.stdin) <span class="keyword">then</span> + <span class="global">io</span>.stdout:<span class="function-name">write</span>(<span class="string">"\n"</span>) <span class="comment">-- Add newline after reading the password +</span> sys.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdin, w_oldflags) + sys.<span class="function-name">tcsetattr</span>(<span class="global">io</span>.stdin, sys.TCSANOW, p_oldflags) + <span class="keyword">end</span> + + <span class="keyword">return</span> secret, err +<span class="keyword">end</span> + + + +<span class="comment">-- Get username +</span><span class="global">io</span>.<span class="function-name">write</span>(<span class="string">"Username: "</span>) +<span class="keyword">local</span> username = <span class="global">io</span>.stdin:<span class="function-name">read</span>(<span class="string">"*l"</span>) + +<span class="comment">-- Get the secret +</span><span class="global">io</span>.<span class="function-name">write</span>(<span class="string">"Password: "</span>) +<span class="keyword">local</span> password = <span class="function-name">read_secret</span>(<span class="string">"*l"</span>) + +<span class="comment">-- Get domainname +</span><span class="global">io</span>.<span class="function-name">write</span>(<span class="string">"Domain : "</span>) +<span class="keyword">local</span> domain = <span class="global">io</span>.stdin:<span class="function-name">read</span>(<span class="string">"*l"</span>) + + +<span class="comment">-- Print the results +</span><span class="global">print</span>(<span class="string">""</span>) +<span class="global">print</span>(<span class="string">"Here's what we got:"</span>) +<span class="global">print</span>(<span class="string">" username: "</span> .. username) +<span class="global">print</span>(<span class="string">" password: "</span> .. password) +<span class="global">print</span>(<span class="string">" domain : "</span> .. domain)</pre> + + +</div> <!-- id="content" --> +</div> <!-- id="main" --> +<div id="about"> +<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i> +<i style="float:right;">Last updated 2024-06-20 23:11:37 </i> +</div> <!-- id="about" --> +</div> <!-- id="container" --> +</body> +</html> |