Version 24 of stdin

Updated 2009-05-10 14:13:06 by JAJC

A part of stdio, this file handle is opened by default for each application making use of the stdio package of code. It is also the name of the standard input channel in Tcl (though Tcl doesn't use stdio). 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 as the channel name 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


The entry in inetd.conf, at least for Linux 2.4.20+, should be

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

TK


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"

Question:

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.

Answer:

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.

Solution:

It turns out that by default?, expect 8.4 was causing the behavior, (as running expect 8.3 or tclsh did not produce this problem).

Issuing a command:

 fconfigure stdin -blocking 1

Enabled blocking once again on the standard input, and then of course waiting occured!


See also read both a file and stdin,


LV Could someone add info (or a pointer) regarding the techniques a Tcl developer must use to be able to interact with stdin when running on Windows using a wish based tcl interpreter?


rpremuz How to check if stdin corresponds to a terminal or a file/pipe?

DKF: If stdin is a terminal (or serial line; they're the same at the device driver layer on Unix) or a network client socket, Tcl automatically detects it and makes the channel be of the right type, enabling extra options for fconfigure. We don't do anything for pipes at the moment; Tcl treats them the same as files and any differences you see are due to the OS.


Question: I'm using a package (DpTcl) that takes the handle of a socket as an argument. My program is invoked through telnet so stdin is a (pty in front of a) socket, yet if I pass stdin as the socket I think I'll get <<channel "stdin" wasn't opened for writing>> when DpTcl goes to write to it. How can I merge stdin and stdout back to be one socket?