Version 1 of gets workaround

Updated 2004-01-25 23:08:32

if 0 {Richard Suchenwirth 2004-01-25 - It is very standard I/O to read a line from standard input with gets - but older wishes on Windows, or the Keuchel port on Windows/CE don't allow this: either stdin reports EOF, or the channel name is not even known. Here is a workaround that creates a toplevel window with an entry, which is returned to the caller (and the window destroyed) once <Return> is hit. It solved the gets inability both on my W95 box and my PocketPC.

First we have to save the original gets, as it may still be needed for file I/O, by renaming it into the tcl namespace:}

 if {[info command tcl::gets] eq ""} {rename gets tcl::gets}

# This is the substitute (workaround) which discriminates use cases:

 proc gets {chan {varN ""}} {
    if {$varN ne ""} {upvar 1 $varN var}
    if {$chan ne "stdin"} {
        if {$varN ne ""} {
            tcl::gets $chan $var ;# return character count
        } else {
            tcl::gets $chan ;# return the string
        }
    } else {
        set var [gets_window] ;# always return string
    }
 }

# And now for the minimal dialog window:

 proc gets_window {{echo 1}} {
    set w [toplevel .[clock clicks]]
    wm title $w gets:
    pack [entry $w.e -textvar gets_w -width 42]
    set ::gets_w ""
    raise $w
    focus $w.e
    bind $w <Return> {set getsw_done 1}
    vwait ::getsw_done
    destroy $w
    if $echo {puts $::gets_w}
    set ::gets_w
 }