Hi,

A while ago I decided to try to make a contool with wish.  The two files in
this distribution are how I did it on a Sun running SunOs 4.1.3.  I had to
make a new wish :-( to do it.  I hear that Tclx might have the necessary
commands but I haven't explored it.  Anyhow, the hack to wish is small and
easy to install.  What it allows you to do is open the console from a 
wish script and have a Tcl routine called everytime the console gets input.

How to Install:

Put this line close to the top of the main.c:

#include <sys/ioctl.h>

Put this line in your main.c next to the other Tcl_CreateCommands

    Tcl_CreateCommand(interp, "opencons", OpenConsole, (ClientData) w,
        (void (*)()) NULL);

Then append the file main_append.c to your main.c and recompile.
If everything works you should have the new wish command:

	opencons pty_file tty_file

opencons will create a handler for input to the tty and perform
the ioctl call to grab console input.  When input is received Tcl will
call the routine 'console' with the number of bytes that are available on
the console. 

The file getcons is an example of a wish script that calls opencons.

-steve
osborn@ae.sps.mot.com

------------------------ cut ---------------------------
#!/bin/sh
# This is a shell archive (shar 3.32)
# made 04/09/1993 16:21 UTC by osborn@ae
# Source directory /usr1/ae/osborn/src/tcl/con/dist
#
# existing files WILL be overwritten
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   1183 -rw-rw-r-- README
#   1139 -rwxrwxr-x getcons
#    900 -rw-rw-r-- main_append.c
#
if touch 2>&1 | fgrep 'amc' > /dev/null
 then TOUCH=touch
 else TOUCH=true
fi
# ============= README ==============
echo "x - extracting README (Text)"
sed 's/^X//' << 'SHAR_EOF' > README &&
XHi,
X
XA while ago I decided to try to make a contool with wish.  The two files in
Xthis distribution are how I did it on a Sun running SunOs 4.1.3.  I had to
Xmake a new wish :-( to do it.  I hear that Tclx might have the necessary
Xcommands but I haven't explored it.  Anyhow, the hack to wish is small and
Xeasy to install.  What it allows you to do is open the console from a 
Xwish script and have a Tcl routine called everytime the console gets input.
X
XHow to Install:
X
XPut this line close to the top of the main.c:
X
X#include <sys/ioctl.h>
X
XPut this line in your main.c next to the other Tcl_CreateCommands
X
X    Tcl_CreateCommand(interp, "opencons", OpenConsole, (ClientData) w,
X        (void (*)()) NULL);
X
XThen append the file main_append.c to your main.c and recompile.
XIf everything works you should have the new wish command:
X
X	opencons pty_file tty_file
X
Xopencons will create a handler for input to the tty and perform
Xthe ioctl call to grab console input.  When input is received Tcl will
Xcall the routine 'console' with the number of bytes that are available on
Xthe console. 
X
XThe file getcons is an example of a wish script that calls opencons.
X
X-steve
Xosborn@ae.sps.mot.com
SHAR_EOF
$TOUCH -am 0409112193 README &&
chmod 0664 README ||
echo "restore of README failed"
set `wc -c README`;Wc_c=$1
if test "$Wc_c" != "1183"; then
	echo original size 1183, current size $Wc_c
fi
# ============= getcons ==============
echo "x - extracting getcons (Text)"
sed 's/^X//' << 'SHAR_EOF' > getcons &&
X#! /usr/local/bin/wish -f
X
X# opencons causes a procedure named console to be called with the number
X# of bytes to read from the console
Xset previous ""
Xproc console {s} {
X	global pty
X	global previous
X
X	set in [read $pty $s]
X	regsub -all "\015" $in "" in
X	set lines [split "$previous$in" \n]
X	set nlines [expr [llength $lines]-1]
X	set previous [lindex $lines $nlines]
X	if {[catch {
X			foreach line [lrange $lines 0 [expr $nlines-1]] {
X				.text insert end [xlateline $line]\n 
X			}
X		} msg]} {.text insert end ${msg}\n}
X	.text yview -pickplace end
X}
X
X# this routine is used to filter console messages.
Xproc xlateline {l} {
X
X# call the newmail routine (not included)
X	if {[string match "You have new mail." $l]} {return [newmail]}
X
X# default just return the line
X	return $l
X}
X
Xwm geometry . 80x2+1+860
Xwm iconname . "Console"
Xwm title . "Console"
Xwm minsize . 2 2
X
Xtext .text -height 2 -setgrid 1 -wrap word
Xpack append . .text {fill expand}
X
X# open a pair of ttys -- note this should be expanded so it could pick any 
X# pair of ttys that isn't busy.
X
X set pty [open "/dev/ptyqe" "r+"]
X set tty [open "/dev/ttyqe" "r+"]
X opencons $pty $tty
X
SHAR_EOF
$TOUCH -am 0409111493 getcons &&
chmod 0775 getcons ||
echo "restore of getcons failed"
set `wc -c getcons`;Wc_c=$1
if test "$Wc_c" != "1139"; then
	echo original size 1139, current size $Wc_c
fi
# ============= main_append.c ==============
echo "x - extracting main_append.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > main_append.c &&
Xstatic int
XOpenConsole(dummy, interp, argc, argv)
X    ClientData dummy;			/* Not used. */
X    Tcl_Interp *interp;			/* Current interpreter. */
X    int argc;				/* Number of arguments. */
X    char **argv;			/* Argument strings. */
X{
X  int ptyfd, ttyfd; /* files to do console stuff with */
X	if (argc != 3) {
X	  Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
X		" ptyfile ttyfile \"", (char *) NULL);
X	  return TCL_ERROR;
X    }
X	ptyfd=(argv[1][4] - '0');
X	ttyfd=(argv[2][4] - '0');
X
X	ioctl(ttyfd,TIOCCONS, 0);
X	Tk_CreateFileHandler(ptyfd,TK_READABLE,ConsInput,(ClientData) ptyfd);
X
X	return TCL_OK;
X}
X
X    /* ARGSUSED */
Xstatic void
XConsInput(ptyfd, mask)
X    ClientData ptyfd;
X    int mask;				/* Not used. */
X{
X  char weestring[50];
X  long numtoread;
X  ioctl(ptyfd,FIONREAD,&numtoread);
X  sprintf(weestring,"console %ld",numtoread);
X  Tcl_Eval(interp, weestring, 0, (char **) NULL);
X}
SHAR_EOF
$TOUCH -am 0409110393 main_append.c &&
chmod 0664 main_append.c ||
echo "restore of main_append.c failed"
set `wc -c main_append.c`;Wc_c=$1
if test "$Wc_c" != "900"; then
	echo original size 900, current size $Wc_c
fi
exit 0
