summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-02-12 16:15:50 +0100
committerAndreas Rumpf <rumpf_a@web.de>2015-02-12 16:15:50 +0100
commit1d65cd277e210e4b3227919f32357ac6277a3db9 (patch)
treee9fa48e5e10c7cd07c89f59eb612ec0904f33a3f /lib
parentf9b3f7f98037065ae8fbb8bda562136b2dfde95a (diff)
parentd6d152e451cf54bf4e20885bdc786ee1187e5cfb (diff)
downloadNim-1d65cd277e210e4b3227919f32357ac6277a3db9.tar.gz
Merge pull request #2095 from def-/rdstdin-winfix
Fix readPasswordFromStdin for Windows
Diffstat (limited to 'lib')
-rw-r--r--lib/impure/rdstdin.nim18
1 files changed, 12 insertions, 6 deletions
diff --git a/lib/impure/rdstdin.nim b/lib/impure/rdstdin.nim
index b188ead1f..aaf2ed1ca 100644
--- a/lib/impure/rdstdin.nim
+++ b/lib/impure/rdstdin.nim
@@ -33,14 +33,16 @@ when defined(Windows):
     stdout.write(prompt)
     result = readLine(stdin, line)
 
-  proc readPasswordFromStdin*(prompt: string, password: var TaintedString) =
+  proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
+                              bool {.tags: [ReadIOEffect, WriteIOEffect].} =
     ## Reads a `password` from stdin without printing it. `password` must not
-    ## be ``nil``!
+    ## be ``nil``! Returns ``false`` if the end of the file has been reached,
+    ## ``true`` otherwise.
     proc getch(): cint {.header: "<conio.h>", importc: "_getch".}
 
     password.setLen(0)
     var c: char
-    echo prompt
+    stdout.write(prompt)
     while true:
       c = getch().char
       case c
@@ -50,6 +52,8 @@ when defined(Windows):
         password.setLen(password.len - 1)
       else:
         password.add(c)
+    stdout.write "\n"
+    # TODO: How to detect EOF on Windows?
 
 else:
   import readline, history, termios, unsigned
@@ -80,7 +84,8 @@ else:
 
   discard readline.bind_key('\t'.ord, doNothing)
 
-  proc readPasswordFromStdin*(prompt: string, password: var TaintedString) =
+  proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
+                              bool {.tags: [ReadIOEffect, WriteIOEffect].} =
     password.setLen(0)
     let fd = stdin.getFileHandle()
     var cur, old: Termios
@@ -89,10 +94,11 @@ else:
     cur.lflag = cur.lflag and not Tcflag(ECHO)
     discard fd.tcsetattr(TCSADRAIN, cur.addr)
     stdout.write prompt
-    discard stdin.readLine(password)
+    result = stdin.readLine(password)
+    stdout.write "\n"
     discard fd.tcsetattr(TCSADRAIN, old.addr)
 
 proc readPasswordFromStdin*(prompt: string): TaintedString =
   ## Reads a password from stdin without printing it.
   result = TaintedString("")
-  readPasswordFromStdin(prompt, result)
+  discard readPasswordFromStdin(prompt, result)