#!/bin/env python
# -*- PYTHON -*-

#-----------------------------------------------------------------------------#
#		       Copyright (c) John F. Croix, 1997
#
#			      All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any non-commercial or internal-use-only purpose and without
# fee is hereby granted, provided that the above copyright notice appears in
# all copies and that both the copyright notice and this permission notice
# appear in supporting documentation, and all changes or alterations to the
# software and documentation are clearly documented.  Resale of any part of
# this software or documentation is prohibited without a license.  Licenses may
# be obtained by contacting the author, John F. Croix, at John.Croix@amd.com or
# journeyman@mail.utexas.edu.
#
# THE AUTHOR, JOHN F. CROIX, DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OF PERFORMANCE
# OF THIS SOFTWARE.
#-----------------------------------------------------------------------------#

#-----------------------------------------------------------------------------#
# Test the ability of the module to handle the Pipe "FULL_BUFFER" condition #
#-----------------------------------------------------------------------------#


import pipe
import sys
import termios
import TERMIOS


class GlobalTimeout(pipe.TIMEOUT):
    def __init__(Self):
	pipe.TIMEOUT.__init__(Self)

    def Apply(Self, PipeProcess, BeforeMatch, MatchingText):
	pipe.TIMEOUT.Apply( Self, PipeProcess, BeforeMatch, MatchingText )
	print "\nProcess has timed out!  Exiting."
	raise 1


#
# Start an interactive process with the user.
#

print "\n\n\n"
print "***********************************************************************"
print "This program will mangle your input by shifting each typed letter by 5."
print "The program will automatically end after 10 seconds of inactivity."
print "You can also hit ^C."
print "***********************************************************************"
print "\n\n\n"

User = pipe.Pipe()
User.MatchAfter( GlobalTimeout() )
User.SetTimeout( 10 )
User.Open( sys.stdin )

#
# Turn off echo.  We need to get two independent copies of the term data
# since we'll be modifying the copy.  We don't want to end up with OldTC and
# NewTC to point to the same thing
#

print "Turning ECHO off\n\n\n"
OldTC = termios.tcgetattr( sys.stdin.fileno() )
NewTC = termios.tcgetattr( sys.stdin.fileno() )
NewTC[ 3 ] = NewTC[ 3 ] & ~TERMIOS.ECHO
termios.tcsetattr( sys.stdin.fileno(), TERMIOS.TCSANOW, NewTC )

try:
    AnyPattern = pipe.RegexPattern( "\(.\)" )
    while 1:
	User.Match( AnyPattern )
	Ch = AnyPattern.group( 1 )[ 0 ][ 0 ]
	if ((Ch >= ' ') and (Ch <= '~')):
	    sys.stdout.write( chr( ord( Ch ) + 5 ) )
	    sys.stdout.flush()
except:
    print "\nTurning echo back on."
    termios.tcsetattr( sys.stdin.fileno(), TERMIOS.TCSANOW, OldTC )


