#!/usr/bin/python # $Id: catv,v 1.2 2019/02/22 21:53:01 friedman Exp $ # This was written to work in Python 2.5 (and possibly earlier) for ESXi 4.x, # as well as Python 3.x for later versions of ESXi. Thus, some contortions. import sys import os import re # The `bytes' type is not defined in python < 2.6 at all, # and in 2.6 <= python < 3.0, it is equivalent to str. try: bytes except NameError: bytes = str def to_bytes( x, encoding='iso8859-1' ): # any 8-bit encoding would do if bytes is not str: return bytes( x, encoding=encoding ) return x def dispchar( c ): n = ord( c ) if n < 31: return to_bytes( '^' + chr( n + 64 ) ) if n == 0x7f: return to_bytes( '^?' ) if n > 127: return to_bytes( '\\{:x}'.format( n ) ) else: return c pat = re.compile( to_bytes( '([\x00-\x08\x0b-\x1f\x7f-\xff])' )) def catv( fd ): stdout = sys.stdout.fileno() while True: buf = os.read( fd, 4096 ) if len( buf ) == 0: return buf = pat.sub( lambda c: dispchar( c.group( 1 )), buf ) os.write( stdout, buf ) if __name__ == '__main__': if len( sys.argv ) < 2: sys.argv.insert( 1, '-' ) for filename in sys.argv[ 1: ]: if filename == '-': fd = os.dup( sys.stdin.fileno() ) else: try: fd = os.open( filename, os.O_RDONLY ) except (OSError, IOError): e = sys.exc_info()[ 1 ] msg = str.join( ': ', (e.filename, e.strerror) ) + '\n' os.write( sys.stderr.fileno(), to_bytes( msg )) continue catv( fd ) os.close( fd ) # eof