#!/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 regular expressions.           #
#-----------------------------------------------------------------------------#


import pipe
import os
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 )
print 'Starting "ls" process....'
LS.OpenLog( "ls.log", "w" )
LS.Open( "ls -al" )

User = os.environ["USER"]
w = "[.a-zA-Z0-9_-]+"
W = "[^.a-zA-Z0-9_-]+"
d = "[0-9]+"
s = "[ \t]+"
S = "[^ \t]+"
Link = pipe.RegexPattern( "l........." + s + d + s + User + s + \
			    w + s + "\(" + d + "\)" + s + w + \
			    s + d + s + S + s + "\(" + w + "\)" )
EOF = pipe.EOF()
Error = pipe.ERROR()
while 1:
    LS.MatchLine( Link, EOF, Error )
    if Link.Matched():
	print "Link --> %s\t\tSize --> %s" % Link.group( 2, 1 )
    elif EOF.Matched():
	print "Done!"
	break
    elif Error.Matched():
	print "Error --> %s" % (Error.ErrorMsg())
	break
print LS.ErrorMsg()


