Version 10 of stdin

Updated 2006-11-07 18:53:04

a part of stdio, this file is opened by default for each application making use of the stdio package of code.

This input file may correspond to a disk file, pipe, terminal device, or other construct.

To refer to the stdin filehandle in Tcl, use the string stdin when using gets .

A quite distinct use of the same word refers to the stdin package Stephen Uhler includes with tclhttpd.


An example of reassigning stdin and providing a prompt and capturing user input for tclsh.

 #!/usr/bin/tclsh
 # by Mike Tuxford
 #
 # the handler for stdin
 proc localHandler {} {
   # get the data from stdin
   gets stdin data
   # client will exit if you type "exit". How original!
   if {$data == "exit"} {
     exit
   } else {
     # process your data here, or pass it to another proc
     # or pass it to a local/remote server

     # return a new pseudo-prompt
     puts -nonewline stdout \
       "[clock format [clock seconds] -format "%H:%M:%S"] Tcl> "
     flush stdout
   }
   return
 }
 # assign our event handler for stdin
 fileevent stdin readable localHandler
 # send a startup message and initial prompt
 puts -nonewline stdout \
   "Tcl pseudo-prompt activated... \
   \n[clock format [clock seconds] -format "%H:%M:%S"] Tcl> "
 flush stdout
 # enter the tcl event loop
 vwait __forever__

Mike Tuxford


You can even use it with inetd for an instant TCP/IP server:

in your services file:

 test 10000/tcp

in your /etc/inetd.conf:

 test stream tcp /home/user/develop/program.tcl program.tcl

Then in your program just read stdin like above and that's it!

JC


SB: Turn off buffering on stdout to use repetitive -nonewline in interactive input with tclsh:

 fconfigure stdout -buffering none
 puts -nonewline "Give me your input: "
 gets stdin first
 puts -nonewline "Give me another input: "
 gets stdin second
 puts "First you told me $first and then $second"

MC: Using "gets stdin myVar" doesn't WAIT for the user to enter stuff...

it simply reads from the stdin stream. Any idea how to make it wait? I tried:

  while {[[gets stdin myVar] < 0} {}

This works...but of course takes 100% CPU.. .ew.


Well, gets will wait until some newline terminated text is available on the stdin file descriptor. However, if the user typed ahead, or the input is coming from a file, then of course, no waiting is necessary.

If what you really want to do is "gather input from the user's terminal after prompting, disgarding anything previously typed', then you probably want to mess with stty on Unix like systems. I don't know what the equivalent would be on Windows.


MC: Solution

It turns out that by default?, expect 8.4 was causing the issue. (As running expect 8.3 or tclsh did not procude this problem) Issuing a command: CODE fconfigure stdin -blocking 1 /CODE Enabled blocking once again on the standard input, and then of course waiting occured!


Category Glossary