#!/usr/bin/python2 -tt # vim:set fileencoding=utf-8 from subprocess import * import sys import os, select special = { # define new unicode nethack/tradewars glyphs here } if len(sys.argv) < 2: print "usage: ibmfilter [command]" print "Runs command in a subshell and translates its output from ibm473 codepage to UTF-8." sys.exit(0) handle = Popen(sys.argv[1:], stdout=PIPE, bufsize=1) buf = handle.stdout.read(1) while buf != '': if buf in special: out = special[buf] else: out = buf.decode('ibm437') writeable = select.select([], [sys.stdout], [], 5)[1] if not writeable: os.kill(handle.pid) os.system('reset') raise Exception("Timed out while waiting for stdout to be writeable...") sys.stdout.write(out.encode("UTF-8")) buf = handle.stdout.read(1) handle.wait()