Version 8 of stdin

Updated 2006-11-07 17:49:37

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.


Category Glossary