''' Copyright (C) 2009 Don Peterson This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ''' import sys, getopt from os import environ out = sys.stdout.write nl = "\n" decimal = False octal = False column_width = 9 number_of_columns = 8 manual = '''Usage: %(name)s [options] Prints ASCII character set. Options: -d Print in decimal (default is hex) -l Print the lower 128 characters -o Print octal characters -u Print the upper 128 characters ''' def ListInColumns(list, col_width=0, num_columns=0, space_betw=0, truncate=0): '''Returns a list of strings with the elements of list (must be strings) printed in columnar format. Elements of list that won't fit in a column either generate an exception if truncate is 0 or get truncated if truncate is nonzero. The number of spaces between columns is space_betw. If col_width and num_columns are 0, then the program will set them by reading the COLUMNS environment variable. If COLUMNS doesn't exist, col_width will default to 80. num_columns will be chosen by finding the length of the largest element so that it is not truncated. Caveat: if there are a small number of elements in the list, you may not get what you expect. For example, try a list size of 1 to 10 with num_columns equal to 4: for lists of 1, 2, 3, 5, 6, and 9, you'll get fewer than four columns. ''' # Make all integers col_width = int(col_width) num_columns = int(num_columns) space_betw = int(space_betw) truncate = int(truncate) lines = [] N = len(list) # Get the length of the longest line in the list maxlen = 0 for line in list: if len(line) > maxlen: maxlen = len(line) if maxlen == 0: return [""] if col_width == 0: if environ.has_key("COLUMNS"): columns = int(environ["COLUMNS"]) else: columns = 80 col_width = maxlen if num_columns == 0: try: num_columns = int(columns/maxlen) except: return [""] if num_columns < 1: raise Exception("A line is too long to display") space_betw = 1 if col_width < 1 or num_columns < 1 or space_betw < 0: raise Exception("Error: invalid parameters") if 0: print "col_width =", col_width print "num_columns =", num_columns print "space_betw =", space_betw print "truncate =", truncate print "maxlen =", maxlen if N == 0: return [""] num_rows = int(N//num_columns + (N % num_columns != 0)) for row in xrange(num_rows): st = "" for column in xrange(num_columns): ix = int(num_rows*column + row) if 0 <= ix <= (N-1): if len(list[ix]) > col_width: if truncate: st = st + list[ix][:col_width] + " "*space_betw else: raise Exception("Error: element %d too long" % ix) else: st = st + list[ix] + " " * (col_width - len(list[ix])) \ + " " * space_betw lines.append(st) assert(len(lines) == num_rows) return lines def Usage(): name = sys.argv[0] out(manual % locals()) exit(1) def ParseCommandLine(): try: optlist, args = getopt.getopt(sys.argv[1:], "dhlou") except getopt.GetoptError, st: msg, option = st out(msg + nl) sys.exit(1) lower, upper = 0, 256 for opt in optlist: if opt[0] == "-d": global decimal decimal = True if opt[0] == "-h": Usage() if opt[0] == "-l": lower, upper = 0, 128 if opt[0] == "-o": global octal octal = True if opt[0] == "-u": lower, upper = 128, 256 return lower, upper def PrintTable(format, lower, upper): ctrl = ("nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", "bs", "ht", "nl", "vt", "np", "cr", "so", "si", "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", "can", "em", "sub", "esc", "fs", "gs", "rs", "us", "sp",) s = [] for i in xrange(lower, upper): if i <= ord(" "): s.append(format % (i, ctrl[i])) else: s.append(format % (i, chr(i))) for i in ListInColumns(s, col_width=column_width, \ num_columns=number_of_columns): out(i + nl) def main(): lower, upper = ParseCommandLine() if decimal: PrintTable("%03d %-3s", lower, upper) elif octal: PrintTable("%03o %-3s", lower, upper) else: PrintTable("%02x %-3s", lower, upper) main()