#!/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


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 "Error --> Process has timed out!  Exiting."
	sys.exit( 1 )


#
# Start DC Shell.  Wait for it to initialize.
#


LS = pipe.Pipe()
LS.MatchAfter( GlobalTimeout() )
LS.LogUser( 1 )
LS.SetBufferSize( 30 )
print 'Starting "ls" process....'
LS.Open( "ls -al" )

FullBuffer = pipe.FULL_BUFFER()
NoSuchPattern = pipe.RegexPattern( "There should be no file with this name" )
for I in range(2):
    LS.Match( NoSuchPattern, FullBuffer )
    if FullBuffer.Matched():
	print "\n\n\nDiscovered a full buffer."
	print "\tThere were %d characters read" % \
	      (len( FullBuffer.MatchingText ))
	print "\tContents:"
	print "---------------------------------------------------------------"
	print "%s" % (FullBuffer.MatchingText)
	print "---------------------------------------------------------------"
	print "\n\n"
